Grantlee 5.3.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
50 class GRANTLEE_TEMPLATES_EXPORT MetaType
51{
52 public:
56 typedef QVariant (*LookupFunction)(const QVariant &, const QString &);
57
61 static void registerLookUpOperator(int id, LookupFunction f);
62
66 static void internalLock();
67
71 static void internalUnlock();
72
76 static QVariant lookup(const QVariant &object, const QString &property);
77
81 static bool lookupAlreadyRegistered(int id);
82
83 private:
84 MetaType();
85};
86#endif
87
88 namespace
89{
90
91/*
92 This is a helper to select an appropriate overload of indexAccess
93 */
94 template <typename RealType, typename HandleAs> struct LookupTrait {
95 static QVariant doLookUp(const QVariant &object, const QString &property)
96 {
97 typedef typename Grantlee::TypeAccessor<RealType> Accessor;
98 return Accessor::lookUp(object.value<RealType>(), property);
99 }
100};
101
102 template <typename RealType, typename HandleAs>
103 struct LookupTrait<RealType &, HandleAs &> {
104 static QVariant doLookUp(const QVariant &object, const QString &property)
105 {
106 typedef typename Grantlee::TypeAccessor<HandleAs &> Accessor;
107 return Accessor::lookUp(object.value<HandleAs>(), property);
108 }
109};
110
111 template <typename RealType, typename HandleAs> static int doRegister(int id)
112{
113 if (MetaType::lookupAlreadyRegistered(id))
114 return id;
115
116 QVariant (*lf)(const QVariant &, const QString &)
117 = LookupTrait<RealType, HandleAs>::doLookUp;
118
119 MetaType::registerLookUpOperator(
120 id, reinterpret_cast<MetaType::LookupFunction>(lf));
121
122 return id;
123}
124
125/*
126 Register a type so grantlee knows how to handle it.
127 */
128 template <typename RealType, typename HandleAs> struct InternalRegisterType {
129 static int doReg()
130 {
131 const int id = qMetaTypeId<RealType>();
132 return doRegister<RealType &, HandleAs &>(id);
133 }
134};
135
136 template <typename RealType, typename HandleAs>
137 struct InternalRegisterType<RealType *, HandleAs *> {
138 static int doReg()
139 {
140 const int id = qMetaTypeId<RealType *>();
141 return doRegister<RealType *, HandleAs *>(id);
142 }
143};
144}
145
182 template <typename RealType, typename HandleAs> int registerMetaType()
183{
184 MetaType::internalLock();
185
186 const int id = InternalRegisterType<RealType, HandleAs>::doReg();
187
188 MetaType::internalUnlock();
189
190 return id;
191}
192
193#ifndef Q_QDOC
200 template <typename Type> int registerMetaType()
201{
202 return registerMetaType<Type, Type>();
203}
204
205#endif
206} // namespace Grantlee
207
213#define GRANTLEE_BEGIN_LOOKUP(Type) \
214 namespace Grantlee \
215 { \
216 template <> \
217 inline QVariant TypeAccessor<Type &>::lookUp(const Type &object, \
218 const QString &property) \
219 {
220
226#define GRANTLEE_BEGIN_LOOKUP_PTR(Type) \
227 namespace Grantlee \
228 { \
229 template <> \
230 inline QVariant TypeAccessor<Type *>::lookUp(const Type *const object, \
231 const QString &property) \
232 {
233
239#define GRANTLEE_END_LOOKUP \
240 return QVariant(); \
241 } \
242 }
243
244#endif // #define GRANTLEE_METATYPE_H
Grantlee
The Grantlee namespace holds all public Grantlee API.
Definition: Mainpage.dox:8
Grantlee::registerMetaType
int registerMetaType()
Registers the type RealType with the metatype system.
Definition: metatype.h:182
typeaccessor.h