aboutsummaryrefslogtreecommitdiffstats
path: root/src/webchannel/variantargument_p.h
diff options
context:
space:
mode:
authorMilian Wolff <milian.wolff@kdab.com>2014-01-10 11:38:55 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-01-10 14:46:14 +0100
commitc1f4f8e626f3d5c2a2d6112e727ae0d15248cf19 (patch)
treec55947e57e2aedd808420cab8e5676b98baca5b2 /src/webchannel/variantargument_p.h
parent625c881cc74f2552e211b9ba81ac3b0ed30e9947 (diff)
Properly convert JSON data to target type of function parameter.
This used to work but broke in one of the last few commits apparently. Now we add a proper unit test to ensure it stays working. The issue was that JSON only knows e.g. numeric types stored as double. When we then try to call a method taking an int with the VariantArgument that tries to convert the double to int, we failed and produced an invalid QVariant which then converts to 0. Now we use the appropriate API to convert the JSON data to the correct target type before calling the method. Furthermore, it became clear that we can greatly cleanup the VariantArgument thanks to that. It now is reduced to just a QVariant wrapper class with an implicit cast operator to QGenericArgument. Change-Id: Ieaf60f548ea9584e7d760f9cd935da455787f376 Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'src/webchannel/variantargument_p.h')
-rw-r--r--src/webchannel/variantargument_p.h47
1 files changed, 4 insertions, 43 deletions
diff --git a/src/webchannel/variantargument_p.h b/src/webchannel/variantargument_p.h
index ee625fc..5f17953 100644
--- a/src/webchannel/variantargument_p.h
+++ b/src/webchannel/variantargument_p.h
@@ -44,60 +44,21 @@
#define VARIANTARGUMENT_H
#include <QVariant>
-#include <QMetaType>
/**
* RAII QVariant to Q[Generic]Argument conversion
*/
-class VariantArgument
+struct VariantArgument
{
-public:
- explicit VariantArgument()
- : m_data(0)
- , m_paramType(0)
- {
- }
-
- /// TODO: test with C++ methods that don't take a QVariant as arg
- /// also test conversions
- void setValue(const QVariant &value, int paramType)
- {
- if (m_data) {
- QMetaType::destroy(m_paramType, m_data);
- m_name.clear();
- m_data = 0;
- }
-
- m_paramType = paramType;
-
- if (value.isValid()) {
- m_name = value.typeName();
- m_data = QMetaType::create(m_paramType, value.constData());
- }
- }
-
- ~VariantArgument()
- {
- if (m_data) {
- QMetaType::destroy(m_paramType, m_data);
- m_data = 0;
- }
- }
-
operator QGenericArgument() const
{
- if (!m_data) {
+ if (!value.isValid()) {
return QGenericArgument();
}
- return QGenericArgument(m_name.constData(), m_data);
+ return QGenericArgument(value.typeName(), value.constData());
}
-private:
- Q_DISABLE_COPY(VariantArgument)
-
- QByteArray m_name;
- void* m_data;
- int m_paramType;
+ QVariant value;
};
#endif // VARIANTARGUMENT_H