Imported Upstream version 1.1.0
[gammaray-debian.git] / metaobject.h
blobdd5016089cd81daa4bca4eed4ec191b4bf296657
1 /*
2 metaobject.h
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
7 Copyright (C) 2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Volker Krause <volker.krause@kdab.com>
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.
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.
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/>.
24 #ifndef GAMMARAY_METAOBJECT_H
25 #define GAMMARAY_METAOBJECT_H
27 #include "metaproperty.h"
29 #include <QVector>
31 namespace GammaRay {
33 /** Compile-time introspection adaptor for non-QObject classes. */
34 class MetaObject
36 public:
37 MetaObject();
38 virtual ~MetaObject();
40 /**
41 * Returns the amount of properties available in this class (including base classes).
43 int propertyCount() const;
45 /**
46 * Returns the property adaptor for index @p index.
48 MetaProperty *propertyAt(int index) const;
50 /** Add a base class meta object. */
51 void addBaseClass(MetaObject *baseClass);
53 /** Add a property for this class. This transfers ownership. */
54 void addProperty(MetaProperty *property);
56 /// Returns the name of the class represented by this object.
57 QString className() const;
59 /** Casts a void pointer for an instance of this type to one appropriate
60 * for use with the property at index @p index.
61 * Make sure to use this when dealing with multi-inheritance.
63 void *castForPropertyAt(void *object, int index) const;
65 protected:
66 /** Casts down to base class @p baseClassIndex.
67 * This is important when traversing multi-inheritance trees.
69 virtual void *castToBaseClass(void *object, int baseClassIndex) const = 0;
71 protected:
72 QVector<MetaObject*> m_baseClasses;
74 private:
75 friend class MetaObjectRepository;
76 void setClassName(const QString &className);
78 private:
79 QVector<MetaProperty*> m_properties;
80 QString m_className;
83 /** Template implementation of MetaObject. */
84 template <typename T, typename Base1 = void, typename Base2 = void, typename Base3 = void>
85 class MetaObjectImpl : public MetaObject
87 public:
88 void *castToBaseClass(void *object, int baseClassIndex) const
90 Q_ASSERT(baseClassIndex >= 0 && baseClassIndex < m_baseClasses.size());
91 switch (baseClassIndex) {
92 case 0:
93 return static_cast<Base1*>(static_cast<T*>(object));
94 case 1:
95 return static_cast<Base2*>(static_cast<T*>(object));
96 case 2:
97 return static_cast<Base3*>(static_cast<T*>(object));
99 Q_ASSERT(!"WTF!?");
100 return 0;
106 #endif // GAMMARAY_METAOBJECT_H