| 1 |
/* |
| 2 |
* BluezQt - Asynchronous BlueZ wrapper library |
| 3 |
* |
| 4 |
* SPDX-FileCopyrightText: 2019 Manuel Weichselbaumer <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <QRegularExpression> |
| 10 |
|
| 11 |
#include "Method.h" |
| 12 |
|
| 13 |
Method::Method() |
| 14 |
{ |
| 15 |
} |
| 16 |
|
| 17 |
bool Method::finalize() |
| 18 |
{ |
| 19 |
for (const auto &tag : m_stringTags) { |
| 20 |
m_tags.isOptional |= tag.contains(QLatin1String("optional"), Qt::CaseInsensitive); |
| 21 |
m_tags.isDeprecated |= tag.contains(QLatin1String("deprecated"), Qt::CaseInsensitive); |
| 22 |
m_tags.isExperimental |= tag.contains(QLatin1String("experimental"), Qt::CaseInsensitive); |
| 23 |
} |
| 24 |
|
| 25 |
bool success = true; |
| 26 |
success &= m_comment.finalize(); |
| 27 |
|
| 28 |
for (const auto &inParam : m_inParameterStrings) { |
| 29 |
m_inParameters.push_back(Parameter::fromString(inParam)); |
| 30 |
} |
| 31 |
|
| 32 |
m_outParameterStrings.removeOne(QStringLiteral("void")); |
| 33 |
if (m_outParameterStrings.isEmpty()) { |
| 34 |
m_outParameter = Parameter::fromString(QStringLiteral("void unnamend")); |
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
// Guess out parameter name from method name |
| 39 |
QString paramName = guessOutParameterName(); |
| 40 |
if (!paramName.isEmpty()) { |
| 41 |
m_outParameterStrings.front() += QLatin1Char(' ') + paramName; |
| 42 |
m_outParameter = Parameter::fromString(m_outParameterStrings.front()); |
| 43 |
} else { |
| 44 |
for (int i = 0; i < m_outParameterStrings.size(); ++i) { |
| 45 |
m_outParameterStrings[i] += QLatin1String(" value") + QString::number(i); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
for (const auto &outParam : m_outParameterStrings) { |
| 50 |
m_outParameters.push_back(Parameter::fromString(outParam)); |
| 51 |
} |
| 52 |
|
| 53 |
return success; |
| 54 |
} |
| 55 |
|
| 56 |
QString Method::name() const |
| 57 |
{ |
| 58 |
return m_name; |
| 59 |
} |
| 60 |
|
| 61 |
QList<Parameter> Method::inParameters() const |
| 62 |
{ |
| 63 |
return m_inParameters; |
| 64 |
} |
| 65 |
|
| 66 |
QList<Parameter> Method::outParameters() const |
| 67 |
{ |
| 68 |
return m_outParameters; |
| 69 |
} |
| 70 |
|
| 71 |
Parameter Method::outParameter() const |
| 72 |
{ |
| 73 |
return m_outParameter; |
| 74 |
} |
| 75 |
|
| 76 |
Method::Tags Method::tags() const |
| 77 |
{ |
| 78 |
return m_tags; |
| 79 |
} |
| 80 |
|
| 81 |
QStringList Method::comment() const |
| 82 |
{ |
| 83 |
return m_comment; |
| 84 |
} |
| 85 |
|
| 86 |
QString Method::guessOutParameterName() const |
| 87 |
{ |
| 88 |
if (m_outParameterStrings.size() != 1) { |
| 89 |
return QString(); |
| 90 |
} |
| 91 |
|
| 92 |
const QRegularExpression rx(QStringLiteral("([A-Z][a-z0-9]+)+")); |
| 93 |
QRegularExpressionMatch match = rx.match(m_name, 1); |
| 94 |
if (!match.hasMatch()) { |
| 95 |
return QStringLiteral("value"); |
| 96 |
} |
| 97 |
|
| 98 |
return match.captured(1).toLower(); |
| 99 |
} |