File indexing completed on 2024-04-28 03:52:08

0001 /*
0002  * BluezQt - Asynchronous BlueZ wrapper library
0003  *
0004  * SPDX-FileCopyrightText: 2019 Manuel Weichselbaumer <mincequi@web.de>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #include <QRegularExpression>
0010 
0011 #include "Method.h"
0012 
0013 Method::Method()
0014 {
0015 }
0016 
0017 bool Method::finalize()
0018 {
0019     for (const auto &tag : m_stringTags) {
0020         m_tags.isOptional |= tag.contains(QLatin1String("optional"), Qt::CaseInsensitive);
0021         m_tags.isDeprecated |= tag.contains(QLatin1String("deprecated"), Qt::CaseInsensitive);
0022         m_tags.isExperimental |= tag.contains(QLatin1String("experimental"), Qt::CaseInsensitive);
0023     }
0024 
0025     bool success = true;
0026     success &= m_comment.finalize();
0027 
0028     for (const auto &inParam : m_inParameterStrings) {
0029         m_inParameters.push_back(Parameter::fromString(inParam));
0030     }
0031 
0032     m_outParameterStrings.removeOne(QStringLiteral("void"));
0033     if (m_outParameterStrings.isEmpty()) {
0034         m_outParameter = Parameter::fromString(QStringLiteral("void unnamend"));
0035         return true;
0036     }
0037 
0038     // Guess out parameter name from method name
0039     QString paramName = guessOutParameterName();
0040     if (!paramName.isEmpty()) {
0041         m_outParameterStrings.front() += QLatin1Char(' ') + paramName;
0042         m_outParameter = Parameter::fromString(m_outParameterStrings.front());
0043     } else {
0044         for (int i = 0; i < m_outParameterStrings.size(); ++i) {
0045             m_outParameterStrings[i] += QLatin1String(" value") + QString::number(i);
0046         }
0047     }
0048 
0049     for (const auto &outParam : m_outParameterStrings) {
0050         m_outParameters.push_back(Parameter::fromString(outParam));
0051     }
0052 
0053     return success;
0054 }
0055 
0056 QString Method::name() const
0057 {
0058     return m_name;
0059 }
0060 
0061 QList<Parameter> Method::inParameters() const
0062 {
0063     return m_inParameters;
0064 }
0065 
0066 QList<Parameter> Method::outParameters() const
0067 {
0068     return m_outParameters;
0069 }
0070 
0071 Parameter Method::outParameter() const
0072 {
0073     return m_outParameter;
0074 }
0075 
0076 Method::Tags Method::tags() const
0077 {
0078     return m_tags;
0079 }
0080 
0081 QStringList Method::comment() const
0082 {
0083     return m_comment;
0084 }
0085 
0086 QString Method::guessOutParameterName() const
0087 {
0088     if (m_outParameterStrings.size() != 1) {
0089         return QString();
0090     }
0091 
0092     const QRegularExpression rx(QStringLiteral("([A-Z][a-z0-9]+)+"));
0093     QRegularExpressionMatch match = rx.match(m_name, 1);
0094     if (!match.hasMatch()) {
0095         return QStringLiteral("value");
0096     }
0097 
0098     return match.captured(1).toLower();
0099 }