GammaRay  2.2.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 namespace detail {
70 
71 template <typename T>
72 struct strip_const_ref { typedef T type; };
73 
74 template <typename T>
75 struct strip_const_ref<const T&> { typedef T type; };
76 
77 }
79 
81 template <typename Class, typename GetterReturnType, typename SetterArgType = GetterReturnType>
83 {
84  private:
85  typedef typename detail::strip_const_ref<GetterReturnType>::type ValueType;
86 
87  public:
88  inline MetaPropertyImpl(
89  const QString &name,
90  GetterReturnType (Class::*getter)() const, void (Class::*setter)(SetterArgType) = 0)
91  : m_name(name), m_getter(getter), m_setter(setter)
92  {
93  }
94 
95  inline QString name() const
96  {
97  return m_name;
98  }
99 
100  inline bool isReadOnly() const
101  {
102  return m_setter == 0 ;
103  }
104 
105  inline QVariant value(void *object) const
106  {
107  Q_ASSERT(object);
108  return value(static_cast<Class*>(object));
109  }
110 
111  inline void setValue(void *object, const QVariant &value)
112  {
113  setValue(static_cast<Class*>(object), value);
114  }
115 
116  private:
117  inline QVariant value(Class *object) const
118  {
119  Q_ASSERT(object);
120  const ValueType v = (object->*(m_getter))();
121  return QVariant::fromValue(v);
122  }
123 
124  inline void setValue(Class *object, const QVariant &value)
125  {
126  if (isReadOnly()) {
127  return;
128  }
129  (object->*(m_setter))(value.value<ValueType>());
130  }
131 
132  inline QString typeName() const
133  {
134  return QMetaType::typeName(qMetaTypeId<ValueType>()) ;
135  }
136 
137  private:
138  QString m_name;
139  GetterReturnType (Class::*m_getter)() const;
140  void (Class::*m_setter)(SetterArgType);
141 };
142 
143 }
144 
145 #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/