GammaRay  2.3.0
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-2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8  Author: Volker Krause <volker.krause@kdab.com>
9 
10  Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11  accordance with GammaRay Commercial License Agreement provided with the Software.
12 
13  Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 
15  This program is free software; you can redistribute it and/or modify
16  it under the terms of the GNU General Public License as published by
17  the Free Software Foundation, either version 2 of the License, or
18  (at your option) any later version.
19 
20  This program is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  GNU General Public License for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with this program. If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #ifndef GAMMARAY_METAPROPERTY_H
30 #define GAMMARAY_METAPROPERTY_H
31 
32 #include "gammaray_core_export.h"
33 
34 #include <QString>
35 #include <QVariant>
36 
37 namespace GammaRay {
38 
39 class MetaObject;
40 
42 class GAMMARAY_CORE_EXPORT MetaProperty
43 {
44  public:
45  explicit MetaProperty(const QString &name);
46  virtual ~MetaProperty();
47 
49  QString name() const;
50 
52  virtual QVariant value(void *object) const = 0;
53 
55  virtual bool isReadOnly() const = 0;
56 
58  virtual void setValue(void *object, const QVariant &value);
59 
61  virtual QString typeName() const = 0;
62 
64  MetaObject *metaObject() const;
65 
66  private:
67  friend class MetaObject;
68  void setMetaObject(MetaObject *om);
69 
70  MetaObject *m_class;
71  QString m_name;
72 };
73 
75 namespace detail {
76 
77 template <typename T>
78 struct strip_const_ref { typedef T type; };
79 
80 template <typename T>
81 struct strip_const_ref<const T&> { typedef T type; };
82 
83 }
85 
87 template <typename Class, typename GetterReturnType, typename SetterArgType = GetterReturnType>
89 {
90  private:
91  typedef typename detail::strip_const_ref<GetterReturnType>::type ValueType;
92 
93  public:
94  inline MetaPropertyImpl(
95  const QString &name,
96  GetterReturnType (Class::*getter)() const, void (Class::*setter)(SetterArgType) = 0)
97  : MetaProperty(name), m_getter(getter), m_setter(setter)
98  {
99  }
100 
101  inline bool isReadOnly() const
102  {
103  return m_setter == 0 ;
104  }
105 
106  inline QVariant value(void *object) const
107  {
108  Q_ASSERT(object);
109  Q_ASSERT(m_getter);
110  const ValueType v = (static_cast<Class*>(object)->*(m_getter))();
111  return QVariant::fromValue(v);
112  }
113 
114  inline void setValue(void *object, const QVariant &value)
115  {
116  if (isReadOnly())
117  return;
118  Q_ASSERT(object);
119  Q_ASSERT(m_setter);
120  (static_cast<Class*>(object)->*(m_setter))(value.value<ValueType>());
121  }
122 
123  inline QString typeName() const
124  {
125  return QMetaType::typeName(qMetaTypeId<ValueType>()) ;
126  }
127 
128  private:
129  GetterReturnType (Class::*m_getter)() const;
130  void (Class::*m_setter)(SetterArgType);
131 };
132 
133 
135 template <typename Class, typename GetterReturnType>
137 {
138  private:
139  typedef typename detail::strip_const_ref<GetterReturnType>::type ValueType;
140 
141  public:
142  inline MetaStaticPropertyImpl(const QString &name, GetterReturnType (*getter)())
143  : MetaProperty(name), m_getter(getter)
144  {
145  }
146 
147  inline bool isReadOnly() const
148  {
149  return true;
150  }
151 
152  inline QVariant value(void *object) const
153  {
154  Q_UNUSED(object);
155  Q_ASSERT(m_getter);
156  const ValueType v = m_getter();
157  return QVariant::fromValue(v);
158  }
159 
160  inline QString typeName() const
161  {
162  return QMetaType::typeName(qMetaTypeId<ValueType>()) ;
163  }
164 
165  private:
166  GetterReturnType (*m_getter)();
167 };
168 
169 }
170 
171 #endif
QString typeName() const
Returns the name of the data type of this property.
Definition: metaproperty.h:123
QVariant value(void *object) const
Current value of the property for object object.
Definition: metaproperty.h:106
Compile-time introspection adaptor for non-QObject classes.
Definition: metaobject.h:41
QString typeName() const
Returns the name of the data type of this property.
Definition: metaproperty.h:160
QVariant value(void *object) const
Current value of the property for object object.
Definition: metaproperty.h:152
Introspectable adaptor to non-QObject properties.
Definition: metaproperty.h:42
bool isReadOnly() const
Returns true if this property is read-only.
Definition: metaproperty.h:147
bool isReadOnly() const
Returns true if this property is read-only.
Definition: metaproperty.h:101
QString name() const
User-readable name of that property.
Definition: endpoint.h:41
void setValue(void *object, const QVariant &value)
Allows changing the property value, assuming it's not read-only, for the instance object...
Definition: metaproperty.h:114
QMetaObject instance only.
Definition: enums.h:41
Template-ed implementation of MetaProperty for member properties.
Definition: metaproperty.h:88
Template-ed implementation of MetaProperty for static properties.
Definition: metaproperty.h:136

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/