d/control: Recommend gdb
[gammaray-debian.git] / metaproperty.h
blobb62dae64f999d8bbf71d8f9f4de28c0c07bada8b
1 /*
2 metaproperty.h
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
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
27 #include <QString>
28 #include <QVariant>
30 namespace GammaRay {
32 class MetaObject;
34 /** Introspectable adaptor to non-QObject properties. */
35 class MetaProperty
37 public:
38 MetaProperty();
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;
59 private:
60 friend class MetaObject;
61 void setMetaObject(MetaObject *om);
63 MetaObject *m_class;
66 /** Template-ed implementation of MetaProperty. */
67 template <typename Class, typename ValueType, typename SetterArgType = ValueType>
68 class MetaPropertyImpl : public MetaProperty
70 public:
71 inline MetaPropertyImpl(
72 const QString &name,
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
80 return m_name;
83 inline bool isReadOnly() const
85 return m_setter == 0 ;
88 inline QVariant value(void *object) const
90 Q_ASSERT(object);
91 return value(static_cast<Class*>(object));
94 inline void setValue(void *object, const QVariant &value)
96 setValue(static_cast<Class*>(object), value);
99 private:
100 inline QVariant value(Class *object) const
102 Q_ASSERT(object);
103 const ValueType v = (object->*(m_getter))();
104 return QVariant::fromValue(v);
107 inline void setValue(Class *object, const QVariant &value)
109 if (isReadOnly()) {
110 return;
112 (object->*(m_setter))(value.value<ValueType>());
115 inline QString typeName() const
117 return QMetaType::typeName(qMetaTypeId<ValueType>()) ;
120 private:
121 QString m_name;
122 ValueType (Class::*m_getter)() const;
123 void (Class::*m_setter)(SetterArgType);
128 #endif