4 This file is part of GammaRay, the Qt application inspection and
7 Copyright (C) 2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Volker Krause <volker.krause@kdab.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #ifndef GAMMARAY_METAPROPERTY_H
25 #define GAMMARAY_METAPROPERTY_H
34 /** Introspectable adaptor to non-QObject properties. */
39 virtual ~MetaProperty();
41 /// User-readable name of that property
42 virtual QString
name() const = 0;
44 /// Current value of the property for object @p object.
45 virtual QVariant
value(void *object
) const = 0;
47 /// Returns @c true if this property is read-only.
48 virtual bool isReadOnly() const = 0;
50 /// Allows changing the property value, assuming it's not read-only, for the instance @p object.
51 virtual void setValue(void *object
, const QVariant
&value
) = 0;
53 /// Returns the name of the data type of this property.
54 virtual QString
typeName() const = 0;
56 /// Returns the class this property belongs to.
57 MetaObject
*metaObject() const;
60 friend class MetaObject
;
61 void setMetaObject(MetaObject
*om
);
66 /** Template-ed implementation of MetaProperty. */
67 template <typename Class
, typename ValueType
, typename SetterArgType
= ValueType
>
68 class MetaPropertyImpl
: public MetaProperty
71 inline MetaPropertyImpl(
73 ValueType (Class::*getter
)() const, void (Class::*setter
)(SetterArgType
) = 0)
74 : m_name(name
), m_getter(getter
), m_setter(setter
)
78 inline QString
name() const
83 inline bool isReadOnly() const
85 return m_setter
== 0 ;
88 inline QVariant
value(void *object
) const
91 return value(static_cast<Class
*>(object
));
94 inline void setValue(void *object
, const QVariant
&value
)
96 setValue(static_cast<Class
*>(object
), value
);
100 inline QVariant
value(Class
*object
) const
103 const ValueType v
= (object
->*(m_getter
))();
104 return QVariant::fromValue(v
);
107 inline void setValue(Class
*object
, const QVariant
&value
)
112 (object
->*(m_setter
))(value
.value
<ValueType
>());
115 inline QString
typeName() const
117 return QMetaType::typeName(qMetaTypeId
<ValueType
>()) ;
122 ValueType (Class::*m_getter
)() const;
123 void (Class::*m_setter
)(SetterArgType
);