forked from KDAB/GammaRay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metaproperty.h
116 lines (89 loc) · 3.11 KB
/
metaproperty.h
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
/*
metaproperty.h
This file is part of GammaRay, the Qt application inspection and
manipulation tool.
Copyright (C) 2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Author: Volker Krause <volker.krause@kdab.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GAMMARAY_METAPROPERTY_H
#define GAMMARAY_METAPROPERTY_H
#include <QString>
#include <QVariant>
namespace GammaRay {
class MetaObject;
/** Introspectable adaptor to non-QObject properties. */
class MetaProperty
{
public:
MetaProperty();
virtual ~MetaProperty();
/// User-readable name of that property
virtual QString name() const = 0;
/// Current value of the property for object @p object.
virtual QVariant value(void *object) const = 0;
/// Returns @c true if this property is read-only.
virtual bool isReadOnly() const = 0;
/// Allows changing the property value, assuming it's not read-only, for the instance @p object.
virtual void setValue(void *object, const QVariant &value) = 0;
/// Returns the name of the data type of this property.
virtual QString typeName() const = 0;
/// Returns the class this property belongs to.
MetaObject* metaObject() const;
private:
friend class MetaObject;
void setMetaObject( MetaObject* om );
MetaObject* m_class;
};
/** Template-ed implementation of MetaProperty. */
template <typename Class, typename ValueType, typename SetterArgType = ValueType>
class MetaPropertyImpl : public MetaProperty
{
public:
inline MetaPropertyImpl( const QString &name, ValueType (Class::*getter)() const, void (Class::*setter)(SetterArgType) = 0 ) :
m_name( name ),
m_getter( getter ),
m_setter( setter )
{
}
inline QString name() const { return m_name; }
inline bool isReadOnly() const { return m_setter == 0 ; }
inline QVariant value(void *object) const
{
Q_ASSERT(object);
return value(static_cast<Class*>(object));
}
inline void setValue(void *object, const QVariant &value) { setValue(static_cast<Class*>(object), value); }
private:
inline QVariant value(Class *object) const
{
Q_ASSERT(object);
const ValueType v = (object->*(m_getter))();
return QVariant::fromValue(v);
}
inline void setValue(Class *object, const QVariant &value)
{
if (isReadOnly())
return;
(object->*(m_setter))(value.value<ValueType>());
}
inline QString typeName() const
{
return QMetaType::typeName( qMetaTypeId<ValueType>() );
}
private:
QString m_name;
ValueType (Class::*m_getter)() const;
void (Class::*m_setter)(SetterArgType);
};
}
#endif