GammaRay  2.4.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 <QVariant>
35 
36 namespace GammaRay {
37 
38 class MetaObject;
39 
41 class GAMMARAY_CORE_EXPORT MetaProperty
42 {
43  public:
44  explicit MetaProperty(const char *name);
45  virtual ~MetaProperty();
46 
48  const char* name() const;
49 
51  virtual QVariant value(void *object) const = 0;
52 
54  virtual bool isReadOnly() const = 0;
55 
57  virtual void setValue(void *object, const QVariant &value);
58 
60  virtual const char* typeName() const = 0;
61 
63  MetaObject *metaObject() const;
64 
65  private:
66  friend class MetaObject;
67  void setMetaObject(MetaObject *om);
68 
69  MetaObject *m_class;
70  const char *m_name;
71 };
72 
74 namespace detail {
75 
76 template <typename T>
77 struct strip_const_ref { typedef T type; };
78 
79 template <typename T>
80 struct strip_const_ref<const T&> { typedef T type; };
81 
82 }
84 
86 template <typename Class, typename GetterReturnType, typename SetterArgType = GetterReturnType>
88 {
89  private:
90  typedef typename detail::strip_const_ref<GetterReturnType>::type ValueType;
91 
92  public:
93  inline MetaPropertyImpl(
94  const char *name,
95  GetterReturnType (Class::*getter)() const, void (Class::*setter)(SetterArgType) = 0)
96  : MetaProperty(name), m_getter(getter), m_setter(setter)
97  {
98  }
99 
100  bool isReadOnly() const Q_DECL_OVERRIDE
101  {
102  return m_setter == 0 ;
103  }
104 
105  QVariant value(void *object) const Q_DECL_OVERRIDE
106  {
107  Q_ASSERT(object);
108  Q_ASSERT(m_getter);
109  const ValueType v = (static_cast<Class*>(object)->*(m_getter))();
110  return QVariant::fromValue(v);
111  }
112 
113  void setValue(void *object, const QVariant &value) Q_DECL_OVERRIDE
114  {
115  if (isReadOnly())
116  return;
117  Q_ASSERT(object);
118  Q_ASSERT(m_setter);
119  (static_cast<Class*>(object)->*(m_setter))(value.value<ValueType>());
120  }
121 
122  const char* typeName() const Q_DECL_OVERRIDE
123  {
124  return QMetaType::typeName(qMetaTypeId<ValueType>()) ;
125  }
126 
127  private:
128  GetterReturnType (Class::*m_getter)() const;
129  void (Class::*m_setter)(SetterArgType);
130 };
131 
132 
134 template <typename Class, typename GetterReturnType>
136 {
137  private:
138  typedef typename detail::strip_const_ref<GetterReturnType>::type ValueType;
139 
140  public:
141  inline MetaStaticPropertyImpl(const char *name, GetterReturnType (*getter)())
142  : MetaProperty(name), m_getter(getter)
143  {
144  }
145 
146  bool isReadOnly() const Q_DECL_OVERRIDE
147  {
148  return true;
149  }
150 
151  QVariant value(void *object) const Q_DECL_OVERRIDE
152  {
153  Q_UNUSED(object);
154  Q_ASSERT(m_getter);
155  const ValueType v = m_getter();
156  return QVariant::fromValue(v);
157  }
158 
159  const char* typeName() const Q_DECL_OVERRIDE
160  {
161  return QMetaType::typeName(qMetaTypeId<ValueType>()) ;
162  }
163 
164  private:
165  GetterReturnType (*m_getter)();
166 };
167 
168 }
169 
170 #endif
const char * name() const
User-readable name of that property.
Compile-time introspection adaptor for non-QObject classes.
Definition: metaobject.h:41
QVariant value(void *object) const Q_DECL_OVERRIDE
Current value of the property for object object.
Definition: metaproperty.h:105
Introspectable adaptor to non-QObject properties.
Definition: metaproperty.h:41
void setValue(void *object, const QVariant &value) Q_DECL_OVERRIDE
Allows changing the property value, assuming it's not read-only, for the instance object...
Definition: metaproperty.h:113
bool isReadOnly() const Q_DECL_OVERRIDE
Returns true if this property is read-only.
Definition: metaproperty.h:100
Definition: endpoint.h:42
bool isReadOnly() const Q_DECL_OVERRIDE
Returns true if this property is read-only.
Definition: metaproperty.h:146
QMetaObject instance only.
Definition: enums.h:41
Template-ed implementation of MetaProperty for member properties.
Definition: metaproperty.h:87
Template-ed implementation of MetaProperty for static properties.
Definition: metaproperty.h:135
QVariant value(void *object) const Q_DECL_OVERRIDE
Current value of the property for object object.
Definition: metaproperty.h:151
const char * typeName() const Q_DECL_OVERRIDE
Returns the name of the data type of this property.
Definition: metaproperty.h:122
const char * typeName() const Q_DECL_OVERRIDE
Returns the name of the data type of this property.
Definition: metaproperty.h:159

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/