File: objectinstance.cpp

package info (click to toggle)
gammaray 3.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,612 kB
  • sloc: cpp: 94,643; ansic: 2,227; sh: 336; python: 164; yacc: 90; lex: 82; xml: 61; makefile: 26
file content (212 lines) | stat: -rw-r--r-- 5,545 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
  objectinstance.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Volker Krause <volker.krause@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include "objectinstance.h"
#include "metaobjectrepository.h"
#include "metaobject.h"
#include "util.h"

using namespace GammaRay;

ObjectInstance::ObjectInstance(QObject *obj)
    : m_qtObj(obj)
    , m_type(QtObject)
{
    m_metaObj = obj ? obj->metaObject() : nullptr;
}

ObjectInstance::ObjectInstance(void *obj, const QMetaObject *metaObj)
    : m_obj(obj)
    , m_metaObj(metaObj)
    , m_type(obj ? QtGadgetPointer : QtMetaObject)
{
}

ObjectInstance::ObjectInstance(void *obj, const char *typeName)
    : m_obj(obj)
    , m_typeName(typeName)
    , m_type(Object)
{
}

ObjectInstance::ObjectInstance(const QVariant &value)
    : m_type(QtVariant)
{
    m_variant = value;
    if (value.canConvert<QObject *>()) {
        m_qtObj = value.value<QObject *>();
        if (m_qtObj) {
            m_metaObj = m_qtObj->metaObject();
            m_type = QtObject;
        }
    } else {
        auto mt = QMetaType(value.userType());
        if (mt.flags() & QMetaType::IsGadget) {
            m_metaObj = mt.metaObject();
            if (m_metaObj)
                m_type = QtGadgetValue;
        } else {
            unpackVariant();
        }
    }
}

ObjectInstance::ObjectInstance(const ObjectInstance &other)
{
    copy(other);
}

ObjectInstance &ObjectInstance::operator=(const ObjectInstance &other)
{
    copy(other);
    return *this;
}

bool ObjectInstance::operator==(const ObjectInstance &rhs) const
{
    if (type() != rhs.type())
        return false;
    switch (type()) {
    case Invalid:
        return false;
    case QtObject:
    case QtGadgetPointer:
    case Object:
        return object() == rhs.object();
    case QtMetaObject:
        return metaObject() == rhs.metaObject();
    case Value:
    case QtVariant:
    case QtGadgetValue:
        return variant() == rhs.variant();
    }

    Q_ASSERT(false);
    return false;
}

ObjectInstance::Type ObjectInstance::type() const
{
    return m_type;
}

bool ObjectInstance::isValueType() const
{
    return m_type == Value || m_type == QtGadgetValue; // ### || m_type == QtVariant; ??
}

QObject *ObjectInstance::qtObject() const
{
    return m_qtObj;
}

void *ObjectInstance::object() const
{
    Q_ASSERT(m_type == QtObject || m_type == QtGadgetPointer || m_type == QtGadgetValue || m_type == Object || m_type == Value);
    switch (m_type) {
    case QtObject:
        return m_qtObj;
    case QtGadgetPointer:
    case QtGadgetValue:
        return m_obj ? m_obj : const_cast<void *>(m_variant.constData());
    default:
        return m_obj;
    }
    Q_ASSERT(false);
    return nullptr;
}

const QVariant &ObjectInstance::variant() const
{
    Q_ASSERT(m_type == QtVariant || isValueType());
    return m_variant;
}

const QMetaObject *ObjectInstance::metaObject() const
{
    return m_metaObj;
}

QByteArray ObjectInstance::typeName() const
{
    if (m_metaObj)
        return m_metaObj->className();
    if (m_variant.isValid() && m_typeName.isEmpty())
        return m_variant.typeName();
    return m_typeName;
}

bool ObjectInstance::isValid() const
{
    switch (m_type) {
    case Invalid:
        return false;
    case QtObject:
        return m_qtObj;
    case QtMetaObject:
        return m_metaObj;
    default:
        break;
    }
    return true;
}

void ObjectInstance::copy(const ObjectInstance &other)
{
    m_obj = other.m_obj;
    m_qtObj = other.m_qtObj.data();
    m_variant = other.m_variant;
    m_metaObj = other.m_metaObj;
    m_typeName = other.m_typeName;
    m_type = other.m_type;

    if (m_type == Value || m_type == QtGadgetPointer)
        unpackVariant(); // pointer changes when copying the variant
}

void ObjectInstance::unpackVariant()
{
    const auto mo = MetaObjectRepository::instance()->metaObject(m_variant.typeName());
    if (mo && strstr(m_variant.typeName(), "*") != nullptr) { // pointer types
        QMetaType(m_variant.userType()).construct(&m_obj, m_variant.constData());
        if (!Util::isNullish(m_obj)) {
            m_type = Object;
            m_typeName = m_variant.typeName();
        }
    } else if (mo) { // value types
        m_obj = const_cast<void *>(m_variant.constData());
        m_type = Value;
        m_typeName = m_variant.typeName();
    }

    if (!m_variant.isNull() && strstr(m_variant.typeName(), "*") != nullptr) { // pointer to gadget
        QByteArray normalizedTypeName = m_variant.typeName();
        // krazy:cond=doublequote_chars
        normalizedTypeName.replace('*', "");
        normalizedTypeName.replace('&', "");
        normalizedTypeName.replace("const ", "");
        normalizedTypeName.replace(" const", "");
        normalizedTypeName.replace(' ', "");
        // krazy:endcond=doublequote_chars

        const auto typeId = QMetaType::fromName(normalizedTypeName).id();
        if (typeId != QMetaType::UnknownType && (QMetaType(typeId).flags() & QMetaType::IsGadget)) {
            QMetaType(m_variant.userType()).construct(&m_obj, m_variant.constData());
            m_metaObj = QMetaType(typeId).metaObject();
            if (m_obj && m_metaObj) {
                m_type = QtGadgetPointer;
                m_typeName = m_variant.typeName();
            }
        }
    }
}