22 #ifndef GRANTLEE_METATYPE_H 23 #define GRANTLEE_METATYPE_H 25 #include "grantlee_core_export.h" 29 #include <QtCore/QVariant> 30 #include <QtCore/QStringList> 31 #include <QtCore/QStack> 32 #include <QtCore/QQueue> 33 #include <QtCore/QDateTime> 59 class GRANTLEE_CORE_EXPORT MetaType
65 typedef QVariant ( *LookupFunction )(
const QVariant &,
const QString & );
70 typedef QVariantList ( *ToVariantListFunction )(
const QVariant & );
75 static void registerLookUpOperator(
int id, LookupFunction f );
80 static void registerToVariantListOperator(
int id, ToVariantListFunction f );
85 static void internalLock();
90 static void internalUnlock();
95 static QVariant lookup(
const QVariant &
object,
const QString &property );
100 static QVariantList toVariantList(
const QVariant &obj );
105 static bool lookupAlreadyRegistered(
int id );
110 static bool toListAlreadyRegistered(
int id );
115 static inline int init();
120 static int initBuiltins() {
return init(); }
133 template<
typename RealType,
typename HandleAs>
136 static QVariant doLookUp(
const QVariant &
object,
const QString &property );
142 enum { Yes =
false };
146 struct IsQObjectStar<T*>
148 typedef int yes_type;
149 typedef char no_type;
151 static yes_type check(QObject*);
152 static no_type check(...);
153 enum { Yes =
sizeof(check(static_cast<T*>(0))) ==
sizeof(yes_type) };
156 template<
typename T,
bool>
159 static QVariant doLookUp(
const QVariant &
object,
const QString &property )
161 typedef typename Grantlee::TypeAccessor<T> Accessor;
162 return Accessor::lookUp(
object.value<T>(), property );
167 struct LookupPointer<T, true>
169 static QVariant doLookUp(
const QVariant &
object,
const QString &property )
171 typedef typename Grantlee::TypeAccessor<QObject*> Accessor;
172 return Accessor::lookUp(
object.value<T>(), property );
176 template<
typename RealType>
177 struct LookupTrait<RealType*, RealType*>
179 static QVariant doLookUp(
const QVariant &
object,
const QString &property )
181 return LookupPointer<RealType*, IsQObjectStar<RealType*>::Yes>::doLookUp(
object, property);
185 template<
typename RealType,
typename HandleAs>
186 struct LookupTrait<RealType&, HandleAs&>
188 static QVariant doLookUp(
const QVariant &
object,
const QString &property )
190 typedef typename Grantlee::TypeAccessor<HandleAs&> Accessor;
191 return Accessor::lookUp( static_cast<HandleAs>(
object.value<RealType>() ), property );
195 template<
typename RealType,
typename HandleAs>
196 static int doRegister(
int id )
198 if ( MetaType::lookupAlreadyRegistered(
id ) )
201 QVariant ( *lf )(
const QVariant&,
const QString& ) = LookupTrait<RealType, HandleAs>::doLookUp;
203 MetaType::registerLookUpOperator(
id, reinterpret_cast<MetaType::LookupFunction>( lf ) );
211 template<
typename RealType,
typename HandleAs>
212 struct InternalRegisterType
215 const int id = qMetaTypeId<RealType>();
216 return doRegister<RealType&, HandleAs&>( id );
220 template<
typename RealType,
typename HandleAs>
221 struct InternalRegisterType<RealType*, HandleAs*>
224 const int id = qMetaTypeId<RealType*>();
225 return doRegister<RealType*, HandleAs*>( id );
229 template<
typename Container,
typename HandleAs>
230 int registerSequentialContainer()
232 const int id = InternalRegisterType<Container, HandleAs>::doReg();
234 if ( MetaType::toListAlreadyRegistered(
id ) )
237 QVariantList ( *tlf )(
const QVariant& ) = SequentialContainerAccessor<Container>::doToList;
238 MetaType::registerToVariantListOperator(
id, reinterpret_cast<MetaType::ToVariantListFunction>( tlf ) );
242 template<
typename Container>
243 int registerSequentialContainer()
245 return registerSequentialContainer<Container, Container>();
248 template<
typename Container,
typename HandleAs>
249 int registerAssociativeContainer()
251 const int id = InternalRegisterType<Container, HandleAs>::doReg();
253 if ( MetaType::toListAlreadyRegistered(
id ) )
256 QVariantList ( *tlf )(
const QVariant& ) = AssociativeContainerAccessor<Container>::doToList;
257 MetaType::registerToVariantListOperator(
id, reinterpret_cast<MetaType::ToVariantListFunction>( tlf ) );
261 template<
typename Container>
262 int registerAssociativeContainer()
264 return registerAssociativeContainer<Container, Container>();
276 template<
typename RealType,
int n>
277 struct RegisterTypeContainer
285 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) 287 #define GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_IF(Container, Type) 288 #define GRANTLEE_REGISTER_ASSOCIATIVE_CONTAINER_KEY_IF(Container, Key, Type) 297 #define GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_IF(Container, Type) \ 298 Grantlee::RegisterTypeContainer<Container<Type>, QMetaTypeId2<Container<Type> >::Defined>::reg(); \ 304 #define GRANTLEE_REGISTER_ASSOCIATIVE_CONTAINER_KEY_IF(Container, Key, Type) \ 305 Grantlee::RegisterTypeContainer<Container<Key, Type>, QMetaTypeId2<Container<Key, Type> >::Defined>::reg(); \ 326 #define GRANTLEE_REGISTER_ASSOCIATIVE_CONTAINER_IF(Container, Type) \ 327 GRANTLEE_REGISTER_ASSOCIATIVE_CONTAINER_KEY_IF(Container, QString, Type) \ 328 GRANTLEE_REGISTER_ASSOCIATIVE_CONTAINER_KEY_IF(Container, qint16, Type) \ 329 GRANTLEE_REGISTER_ASSOCIATIVE_CONTAINER_KEY_IF(Container, qint32, Type) \ 330 GRANTLEE_REGISTER_ASSOCIATIVE_CONTAINER_KEY_IF(Container, qint64, Type) \ 331 GRANTLEE_REGISTER_ASSOCIATIVE_CONTAINER_KEY_IF(Container, quint16, Type) \ 332 GRANTLEE_REGISTER_ASSOCIATIVE_CONTAINER_KEY_IF(Container, quint32, Type) \ 333 GRANTLEE_REGISTER_ASSOCIATIVE_CONTAINER_KEY_IF(Container, quint64, Type) \ 339 void registerContainers()
341 GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_IF( QList, T )
342 GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_IF( QQueue, T )
343 GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_IF( QVector, T )
344 GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_IF( QStack, T )
345 GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_IF( QSet, T )
346 GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_IF( QLinkedList, T )
351 GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_IF( std::deque, T )
352 GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_IF( std::vector, T )
353 GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_IF( std::list, T )
357 struct BuiltinRegister
359 void registerBuiltinContainers()
const 361 Grantlee::MetaType::internalLock();
363 registerContainers< bool >();
364 registerContainers< qint16 >();
365 registerContainers< qint32 >();
366 registerContainers< qint64 >();
367 registerContainers< quint16 >();
368 registerContainers< quint32 >();
369 registerContainers< quint64 >();
370 registerContainers< float >();
371 registerContainers< double >();
372 registerContainers< QString >();
373 registerContainers< QVariant >();
374 registerContainers< QDateTime >();
375 registerContainers< QObject* >();
377 registerSequentialContainer<QStringList, QList<QString> >();
378 Grantlee::MetaType::internalUnlock();
382 Q_GLOBAL_STATIC( BuiltinRegister, builtinRegister )
387 struct MetaTypeInitializer {
388 static inline int initialize()
390 static const BuiltinRegister *br = builtinRegister();
391 br->registerBuiltinContainers();
402 #define GRANTLEE_METATYPE_INITIALIZE static const int i = Grantlee::MetaTypeInitializer::initialize(); Q_UNUSED(i) 405 inline int MetaType::init()
447 template<
typename RealType,
typename HandleAs>
454 MetaType::internalLock();
456 const int id = InternalRegisterType<RealType, HandleAs>::doReg();
458 registerContainers<RealType>();
460 MetaType::internalUnlock();
472 template<
typename Type>
475 return registerMetaType<Type, Type>();
492 #define GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER(Container) \ 493 namespace Grantlee { \ 494 template<typename T> \ 495 struct RegisterTypeContainer<Container<T>, MoreMagic> \ 499 const int id = registerSequentialContainer<Container<T> >(); \ 500 registerContainers<Container<T> >(); \ 511 #define GRANTLEE_REGISTER_ASSOCIATIVE_CONTAINER(Container) \ 512 namespace Grantlee { \ 513 template<typename T, typename U> \ 514 struct RegisterTypeContainer<Container<T, U>, MoreMagic> \ 518 const int id = registerAssociativeContainer<Container<T, U> >(); \ 519 registerContainers<Container<T, U> >(); \ 529 #define GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_AS(Container, As) \ 530 namespace Grantlee { \ 531 template<typename T> \ 532 struct RegisterTypeContainer<Container<T>, MoreMagic> \ 536 return registerSequentialContainer<Container<T>, As<T> >(); \ 548 #define GRANTLEE_BEGIN_LOOKUP(Type) \ 552 inline QVariant TypeAccessor<Type&>::lookUp( const Type &object, const QString &property ) \ 560 #define GRANTLEE_BEGIN_LOOKUP_PTR(Type) \ 564 inline QVariant TypeAccessor<Type*>::lookUp( const Type * const object, const QString &property ) \ 572 #define GRANTLEE_END_LOOKUP \ 579 GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_AS (QQueue, QList)
581 GRANTLEE_REGISTER_SEQUENTIAL_CONTAINER_AS (QStack, QVector)
593 #endif // #define GRANTLEE_METATYPE_H
int registerMetaType()
Registers the type RealType with the metatype system.
The Grantlee namespace holds all public Grantlee API.