Grantlee: templates/lib/metatype.h Source File

Grantlee 5.1.0

metatype.h
Go to the documentation of this file.
1 /*
2 This file is part of the Grantlee template system.
3
4 Copyright (c) 2010 Michael Jansen <kde@michael-jansen.biz>
5 Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either version
10 2.1 of the Licence, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library. If not, see <http://www.gnu.org/licenses/>.
19
20 */
21
22 #ifndef GRANTLEE_METATYPE_H
23 #define GRANTLEE_METATYPE_H
24
25 #include "grantlee_templates_export.h"
26
27 #include "typeaccessor.h"
28
29 #include <QtCore/QVariant>
30
32
33 namespace Grantlee
34 {
35
37
38 #ifndef Q_QDOC
39
52 class GRANTLEE_TEMPLATES_EXPORT MetaType
53 {
54 public:
58 typedef QVariant (*LookupFunction)(const QVariant &, const QString &);
59
63 static void registerLookUpOperator(int id, LookupFunction f);
64
68 static void internalLock();
69
73 static void internalUnlock();
74
78 static QVariant lookup(const QVariant &object, const QString &property);
79
83 static bool lookupAlreadyRegistered(int id);
84
85 private:
86 MetaType();
87 };
88 #endif
89
90 namespace
91 {
92
93 /*
94 * This is a helper to select an appropriate overload of indexAccess
95 */
96 template <typename RealType, typename HandleAs> struct LookupTrait {
97 static QVariant doLookUp(const QVariant &object, const QString &property)
98 {
99 typedef typename Grantlee::TypeAccessor<RealType> Accessor;
100 return Accessor::lookUp(object.value<RealType>(), property);
101 }
102 };
103
104 template <typename RealType, typename HandleAs>
105 struct LookupTrait<RealType &, HandleAs &> {
106 static QVariant doLookUp(const QVariant &object, const QString &property)
107 {
108 typedef typename Grantlee::TypeAccessor<HandleAs &> Accessor;
109 return Accessor::lookUp(object.value<HandleAs>(), property);
110 }
111 };
112
113 template <typename RealType, typename HandleAs> static int doRegister(int id)
114 {
115 if (MetaType::lookupAlreadyRegistered(id))
116 return id;
117
118 QVariant (*lf)(const QVariant &, const QString &)
119 = LookupTrait<RealType, HandleAs>::doLookUp;
120
121 MetaType::registerLookUpOperator(
122 id, reinterpret_cast<MetaType::LookupFunction>(lf));
123
124 return id;
125 }
126
127 /*
128 * Register a type so grantlee knows how to handle it.
129 */
130 template <typename RealType, typename HandleAs> struct InternalRegisterType {
131 static int doReg()
132 {
133 const int id = qMetaTypeId<RealType>();
134 return doRegister<RealType &, HandleAs &>(id);
135 }
136 };
137
138 template <typename RealType, typename HandleAs>
139 struct InternalRegisterType<RealType *, HandleAs *> {
140 static int doReg()
141 {
142 const int id = qMetaTypeId<RealType *>();
143 return doRegister<RealType *, HandleAs *>(id);
144 }
145 };
146 }
147
184 template <typename RealType, typename HandleAs> int registerMetaType()
185 {
186 MetaType::internalLock();
187
188 const int id = InternalRegisterType<RealType, HandleAs>::doReg();
189
190 MetaType::internalUnlock();
191
192 return id;
193 }
194
195 #ifndef Q_QDOC
196
202 template <typename Type> int registerMetaType()
203 {
204 return registerMetaType<Type, Type>();
205 }
206
207 #endif
208 } // namespace Grantlee
209
215 #define GRANTLEE_BEGIN_LOOKUP(Type) \
216 namespace Grantlee \
217 { \
218 template <> \
219 inline QVariant TypeAccessor<Type &>::lookUp(const Type &object, \
220 const QString &property) \
221 {
222
228 #define GRANTLEE_BEGIN_LOOKUP_PTR(Type) \
229 namespace Grantlee \
230 { \
231 template <> \
232 inline QVariant TypeAccessor<Type *>::lookUp(const Type *const object, \
233 const QString &property) \
234 {
235
241 #define GRANTLEE_END_LOOKUP \
242 return QVariant(); \
243 } \
244 }
245
246 #endif // #define GRANTLEE_METATYPE_H
Grantlee::registerMetaType
int registerMetaType()
Registers the type RealType with the metatype system.
Definition: metatype.h:184
typeaccessor.h
Grantlee
The Grantlee namespace holds all public Grantlee API.
Definition: Mainpage.dox:7