GammaRay  2.2.0
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
metaobject.h
1 /*
2  metaobject.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_METAOBJECT_H
25 #define GAMMARAY_METAOBJECT_H
26 
27 #include "gammaray_core_export.h"
28 
29 #include "metaproperty.h"
30 
31 #include <QVector>
32 
33 namespace GammaRay {
34 
36 class GAMMARAY_CORE_EXPORT MetaObject
37 {
38  public:
39  MetaObject();
40  virtual ~MetaObject();
41 
45  int propertyCount() const;
46 
50  MetaProperty *propertyAt(int index) const;
51 
53  void addBaseClass(MetaObject *baseClass);
54 
56  void addProperty(MetaProperty *property);
57 
59  QString className() const;
60 
65  void *castForPropertyAt(void *object, int index) const;
66 
67  void setClassName(const QString &className);
68 
69  MetaObject *superClass(int index = 0) const;
70  bool inherits(const QString &className) const;
71 
72  protected:
76  virtual void *castToBaseClass(void *object, int baseClassIndex) const = 0;
77 
78  protected:
79  QVector<MetaObject*> m_baseClasses;
80 
81  private:
82  QVector<MetaProperty*> m_properties;
83  QString m_className;
84 };
85 
87 template <typename T, typename Base1 = void, typename Base2 = void, typename Base3 = void>
88 class MetaObjectImpl : public MetaObject
89 {
90  public:
91  void *castToBaseClass(void *object, int baseClassIndex) const
92  {
93  Q_ASSERT(baseClassIndex >= 0 && baseClassIndex < m_baseClasses.size());
94  switch (baseClassIndex) {
95  case 0:
96  return static_cast<Base1*>(static_cast<T*>(object));
97  case 1:
98  return static_cast<Base2*>(static_cast<T*>(object));
99  case 2:
100  return static_cast<Base3*>(static_cast<T*>(object));
101  }
102  Q_ASSERT(!"WTF!?");
103  return 0;
104  }
105 };
106 
107 }
108 
109 #endif // GAMMARAY_METAOBJECT_H

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/