1 /*
2   methodargument.cpp
3 
4   This file is part of GammaRay, the Qt application inspection and
5   manipulation tool.
6 
7   Copyright (C) 2013-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8   Author: Volker Krause <volker.krause@kdab.com>
9 
10   Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11   accordance with GammaRay Commercial License Agreement provided with the Software.
12 
13   Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 
15   This program is free software; you can redistribute it and/or modify
16   it under the terms of the GNU General Public License as published by
17   the Free Software Foundation, either version 2 of the License, or
18   (at your option) any later version.
19 
20   This program is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   GNU General Public License for more details.
24 
25   You should have received a copy of the GNU General Public License
26   along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #include "methodargument.h"
30 #include "variantwrapper.h"
31 
32 #include <QSharedData>
33 #include <QMetaType>
34 
35 using namespace GammaRay;
36 
37 class GammaRay::MethodArgumentPrivate : public QSharedData
38 {
39 public:
40     MethodArgumentPrivate() = default;
41 
MethodArgumentPrivate(const MethodArgumentPrivate & other)42     MethodArgumentPrivate(const MethodArgumentPrivate &other)
43         : QSharedData(other)
44     {
45         value = other.value;
46         name = other.name;
47         data = nullptr;
48         unwrapVariant = other.unwrapVariant;
49     }
50 
~MethodArgumentPrivate()51     ~MethodArgumentPrivate()
52     {
53         if (data)
54             QMetaType::destroy(value.userType(), data);
55     }
56 
57     QVariant value;
58     QByteArray name;
59     void *data = nullptr;
60     bool unwrapVariant = true;
61 };
62 
MethodArgument()63 MethodArgument::MethodArgument()
64     : d(new MethodArgumentPrivate)
65 {
66 }
67 
MethodArgument(const QVariant & v)68 MethodArgument::MethodArgument(const QVariant &v)
69     : d(new MethodArgumentPrivate)
70 {
71     if (v.userType() == qMetaTypeId<VariantWrapper>()) {
72         d->value = v.value<VariantWrapper>().variant();
73         d->unwrapVariant = false;
74         d->name = "QVariant";
75     } else {
76         d->value = v;
77         d->unwrapVariant = true;
78         d->name = v.typeName();
79     }
80 }
81 
82 MethodArgument::MethodArgument(const MethodArgument &) = default;
83 
84 MethodArgument::~MethodArgument() = default;
85 
86 MethodArgument &MethodArgument::operator=(const MethodArgument &) = default;
87 
operator QGenericArgument() const88 MethodArgument::operator QGenericArgument() const
89 {
90     if (!d->unwrapVariant)
91         return QGenericArgument(d->name.constData(), &d->value);
92 
93     if (d->value.isValid()) {
94         d->data = QMetaType::create(d->value.userType(), d->value.constData());
95         Q_ASSERT(d->data);
96         return QGenericArgument(d->name.constData(), d->data);
97     }
98 
99     return {};
100 }
101