GammaRay  2.1.0
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
metaproperty.h
1 /*
2  metaproperty.h
3 
4  This file is part of GammaRay, the Qt application inspection and
5  manipulation tool.
6 
7  Copyright (C) 2011-2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8  Author: Volker Krause <volker.krause@kdab.com>
9 
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.
14 
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.
19 
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/>.
22 */
23 
24 #ifndef GAMMARAY_METAPROPERTY_H
25 #define GAMMARAY_METAPROPERTY_H
26 
27 #include "gammaray_core_export.h"
28 
29 #include <QString>
30 #include <QVariant>
31 
32 namespace GammaRay {
33 
34 class MetaObject;
35 
37 class GAMMARAY_CORE_EXPORT MetaProperty
38 {
39  public:
40  MetaProperty();
41  virtual ~MetaProperty();
42 
44  virtual QString name() const = 0;
45 
47  virtual QVariant value(void *object) const = 0;
48 
50  virtual bool isReadOnly() const = 0;
51 
53  virtual void setValue(void *object, const QVariant &value) = 0;
54 
56  virtual QString typeName() const = 0;
57 
59  MetaObject *metaObject() const;
60 
61  private:
62  friend class MetaObject;
63  void setMetaObject(MetaObject *om);
64 
65  MetaObject *m_class;
66 };
67 
69 template <typename Class, typename ValueType, typename SetterArgType = ValueType>
71 {
72  public:
73  inline MetaPropertyImpl(
74  const QString &name,
75  ValueType (Class::*getter)() const, void (Class::*setter)(SetterArgType) = 0)
76  : m_name(name), m_getter(getter), m_setter(setter)
77  {
78  }
79 
80  inline QString name() const
81  {
82  return m_name;
83  }
84 
85  inline bool isReadOnly() const
86  {
87  return m_setter == 0 ;
88  }
89 
90  inline QVariant value(void *object) const
91  {
92  Q_ASSERT(object);
93  return value(static_cast<Class*>(object));
94  }
95 
96  inline void setValue(void *object, const QVariant &value)
97  {
98  setValue(static_cast<Class*>(object), value);
99  }
100 
101  private:
102  inline QVariant value(Class *object) const
103  {
104  Q_ASSERT(object);
105  const ValueType v = (object->*(m_getter))();
106  return QVariant::fromValue(v);
107  }
108 
109  inline void setValue(Class *object, const QVariant &value)
110  {
111  if (isReadOnly()) {
112  return;
113  }
114  (object->*(m_setter))(value.value<ValueType>());
115  }
116 
117  inline QString typeName() const
118  {
119  return QMetaType::typeName(qMetaTypeId<ValueType>()) ;
120  }
121 
122  private:
123  QString m_name;
124  ValueType (Class::*m_getter)() const;
125  void (Class::*m_setter)(SetterArgType);
126 };
127 
128 }
129 
130 #endif

Klarälvdalens Datakonsult AB (KDAB)
"The Qt Experts"
http://www.kdab.com/
GammaRay
Qt-application inspection and manipulation tool
http://www.kdab.com/kdab-products/gammaray/