Skip to content

Commit fcab513

Browse files
authored
[C++][Qt]Replace usage of QVariant with a more intuitive optional wrapper (#8960)
* Replace usage of QVariant with a more intuitive wrapper which also supports non standard types/classes * Fix crash due to usage of object instead of reference
1 parent 9d8494a commit fcab513

File tree

11 files changed

+231
-216
lines changed

11 files changed

+231
-216
lines changed

modules/openapi-generator/src/main/resources/cpp-qt5-client/api-body.mustache

+58-64
Large diffs are not rendered by default.

modules/openapi-generator/src/main/resources/cpp-qt5-client/api-header.mustache

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef {{prefix}}_{{classname}}_H
33
#define {{prefix}}_{{classname}}_H
44

5+
#include "{{prefix}}Helpers.h"
56
#include "{{prefix}}HttpRequest.h"
67
#include "{{prefix}}ServerConfiguration.h"
78

@@ -13,7 +14,6 @@
1314
#include <QStringList>
1415
#include <QList>
1516
#include <QNetworkAccessManager>
16-
#include <QVariant>
1717

1818
{{#cppNamespaceDeclarations}}
1919
namespace {{this}} {
@@ -57,7 +57,7 @@ public:
5757
{{/required}}
5858
{{/allParams}}
5959
*/{{/hasParams}}
60-
{{#isDeprecated}}Q_DECL_DEPRECATED {{/isDeprecated}}void {{nickname}}({{#allParams}}{{#required}}const {{{dataType}}} &{{/required}}{{^required}}const QVariant &{{/required}}{{paramName}}{{^required}} = QVariant(){{/required}}{{^-last}}, {{/-last}}{{/allParams}});
60+
{{#isDeprecated}}Q_DECL_DEPRECATED {{/isDeprecated}}void {{nickname}}({{#allParams}}{{#required}}const {{{dataType}}} &{{/required}}{{^required}}const ::{{cppNamespace}}::OptionalParam<{{{dataType}}}> &{{/required}}{{paramName}}{{^required}} = ::{{cppNamespace}}::OptionalParam<{{{dataType}}}>(){{/required}}{{^-last}}, {{/-last}}{{/allParams}});
6161
{{/operation}}{{/operations}}
6262

6363
private:
@@ -86,7 +86,7 @@ signals:
8686
{{#operations}}{{#operation}}
8787
void {{nickname}}SignalEFull({{prefix}}HttpRequestWorker *worker, QNetworkReply::NetworkError error_type, QString error_str);{{/operation}}{{/operations}}
8888

89-
void abortRequestsSignal();
89+
void abortRequestsSignal();
9090
void allPendingRequestsCompleted();
9191
};
9292

modules/openapi-generator/src/main/resources/cpp-qt5-client/helpers-header.mustache

+21-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <QList>
1212
#include <QMap>
1313
#include <QSet>
14-
#include <QVariant>
1514

1615
#include "{{prefix}}Enum.h"
1716
#include "{{prefix}}HttpFileElement.h"
@@ -21,6 +20,27 @@
2120
namespace {{this}} {
2221
{{/cppNamespaceDeclarations}}
2322

23+
template <typename T>
24+
class OptionalParam {
25+
public:
26+
T m_Value;
27+
bool m_hasValue;
28+
public:
29+
OptionalParam(){
30+
m_hasValue = false;
31+
}
32+
OptionalParam(const T &val){
33+
m_hasValue = true;
34+
m_Value = val;
35+
}
36+
bool hasValue() const {
37+
return m_hasValue;
38+
}
39+
T value() const{
40+
return m_Value;
41+
}
42+
};
43+
2444
bool setDateTimeFormat(const QString&);
2545

2646
template <typename T>

samples/client/petstore/cpp-qt5/PetStore/PetApiTests.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ void PetApiTests::updatePetWithFormTest() {
217217
// fetch it
218218
bool petUpdated2 = false;
219219
connect(&api, &PFXPetApi::getPetByIdSignal, [&](PFXPet pet) {
220+
Q_UNUSED(pet);
220221
petUpdated2 = true;
221222
// QVERIFY(pet.getName().compare(QString("gorilla")) == 0);
222223
QTimer::singleShot(0, &loop, &QEventLoop::quit);

samples/client/petstore/cpp-qt5/client/PFXHelpers.h

+21-1
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,34 @@
2121
#include <QList>
2222
#include <QMap>
2323
#include <QSet>
24-
#include <QVariant>
2524

2625
#include "PFXEnum.h"
2726
#include "PFXHttpFileElement.h"
2827
#include "PFXObject.h"
2928

3029
namespace test_namespace {
3130

31+
template <typename T>
32+
class OptionalParam {
33+
public:
34+
T m_Value;
35+
bool m_hasValue;
36+
public:
37+
OptionalParam(){
38+
m_hasValue = false;
39+
}
40+
OptionalParam(const T &val){
41+
m_hasValue = true;
42+
m_Value = val;
43+
}
44+
bool hasValue() const {
45+
return m_hasValue;
46+
}
47+
T value() const{
48+
return m_Value;
49+
}
50+
};
51+
3252
bool setDateTimeFormat(const QString&);
3353

3454
template <typename T>

0 commit comments

Comments
 (0)