Grantlee  5.0.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 
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>
95 struct LookupTrait
96 {
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 {
107  static QVariant doLookUp( const QVariant &object, const QString &property )
108  {
109  typedef typename Grantlee::TypeAccessor<HandleAs&> Accessor;
110  return Accessor::lookUp( object.value<HandleAs>(), property );
111  }
112 };
113 
114 template<typename RealType, typename HandleAs>
115 static int doRegister( int id )
116 {
117  if ( MetaType::lookupAlreadyRegistered( id ) )
118  return id;
119 
120  QVariant ( *lf )( const QVariant&, const QString& ) = LookupTrait<RealType, HandleAs>::doLookUp;
121 
122  MetaType::registerLookUpOperator( 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>
131 struct InternalRegisterType
132 {
133  static int doReg() {
134  const int id = qMetaTypeId<RealType>();
135  return doRegister<RealType&, HandleAs&>( id );
136  }
137 };
138 
139 template<typename RealType, typename HandleAs>
140 struct InternalRegisterType<RealType*, HandleAs*>
141 {
142  static int doReg() {
143  const int id = qMetaTypeId<RealType*>();
144  return doRegister<RealType*, HandleAs*>( id );
145  }
146 };
147 
148 }
149 
185 template<typename RealType, typename HandleAs>
187 {
188  MetaType::internalLock();
189 
190  const int id = InternalRegisterType<RealType, HandleAs>::doReg();
191 
192  MetaType::internalUnlock();
193 
194  return id;
195 }
196 
197 #ifndef Q_QDOC
198 
204 template<typename Type>
205 int registerMetaType()
206 {
207  return registerMetaType<Type, Type>();
208 }
209 
210 #endif
211 } // namespace Grantlee
212 
218 #define GRANTLEE_BEGIN_LOOKUP(Type) \
219 namespace Grantlee \
220 { \
221 template<> \
222 inline QVariant TypeAccessor<Type&>::lookUp( const Type &object, const QString &property ) \
223 { \
224 
225 
230 #define GRANTLEE_BEGIN_LOOKUP_PTR(Type) \
231 namespace Grantlee \
232 { \
233 template<> \
234 inline QVariant TypeAccessor<Type*>::lookUp( const Type * const object, const QString &property ) \
235 { \
236 
237 
242 #define GRANTLEE_END_LOOKUP \
243  return QVariant(); \
244 } \
245 } \
246 
247 #endif // #define GRANTLEE_METATYPE_H
int registerMetaType()
Registers the type RealType with the metatype system.
Definition: metatype.h:186
The Grantlee namespace holds all public Grantlee API.
Definition: Mainpage.dox:7