KTextTemplate

metatype.h
Go to the documentation of this file.
1/*
2 This file is part of the KTextTemplate library
3
4 SPDX-FileCopyrightText: 2010 Michael Jansen <kde@michael-jansen.biz>
5 SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
6
7 SPDX-License-Identifier: LGPL-2.1-or-later
8
9*/
10
11#ifndef KTEXTTEMPLATE_METATYPE_H
12#define KTEXTTEMPLATE_METATYPE_H
13
14#include "ktexttemplate_export.h"
15
16#include "typeaccessor.h"
17
18#include <QVariant>
19
20/// @file
21
22namespace KTextTemplate
23{
24
25/// @headerfile metatype.h <KTextTemplate/MetaType>
26
27#ifndef K_DOXYGEN
28/**
29 @brief The **%MetaType** is the interface to the KTextTemplate introspection
30 system.
31
32 The **%MetaType** class is used as part of the type registration system of
33 %KTextTemplate.
34
35 @see @ref generic_types_and_templates
36 @author Michael Jansen <kde@michael-jansen.biz>
37 @author Stephen Kelly <steveire@gmail.com>
38*/
39class KTEXTTEMPLATE_EXPORT MetaType
40{
41public:
42 /**
43 @internal The signature for a property lookup method
44 */
45 typedef QVariant (*LookupFunction)(const QVariant &, const QString &);
46
47 /**
48 @internal Registers a property lookup method
49 */
50 static void registerLookUpOperator(int id, LookupFunction f);
51
52 /**
53 @internal
54 */
55 static void internalLock();
56
57 /**
58 @internal
59 */
60 static void internalUnlock();
61
62 /**
63 @internal
64 */
65 static QVariant lookup(const QVariant &object, const QString &property);
66
67 /**
68 @internal
69 */
70 static bool lookupAlreadyRegistered(int id);
71
72private:
73 MetaType();
74};
75#endif
76
77namespace
78{
79
80/*
81 This is a helper to select an appropriate overload of indexAccess
82 */
83template<typename RealType, typename HandleAs>
84struct LookupTrait {
85 static QVariant doLookUp(const QVariant &object, const QString &property)
86 {
87 typedef typename KTextTemplate::TypeAccessor<RealType> Accessor;
88 return Accessor::lookUp(object.value<RealType>(), property);
89 }
90};
91
92template<typename RealType, typename HandleAs>
93struct LookupTrait<RealType &, HandleAs &> {
94 static QVariant doLookUp(const QVariant &object, const QString &property)
95 {
96 typedef typename KTextTemplate::TypeAccessor<HandleAs &> Accessor;
97 return Accessor::lookUp(object.value<HandleAs>(), property);
98 }
99};
100
101template<typename RealType, typename HandleAs>
102static int doRegister(int id)
103{
104 if (MetaType::lookupAlreadyRegistered(id))
105 return id;
106
107 QVariant (*lf)(const QVariant &, const QString &) = LookupTrait<RealType, HandleAs>::doLookUp;
108
109 MetaType::registerLookUpOperator(id, reinterpret_cast<MetaType::LookupFunction>(lf));
110
111 return id;
112}
113
114/*
115 Register a type so KTextTemplate knows how to handle it.
116 */
117template<typename RealType, typename HandleAs>
118struct InternalRegisterType {
119 static int doReg()
120 {
121 const int id = qMetaTypeId<RealType>();
122 return doRegister<RealType &, HandleAs &>(id);
123 }
124};
125
126template<typename RealType, typename HandleAs>
127struct InternalRegisterType<RealType *, HandleAs *> {
128 static int doReg()
129 {
130 const int id = qMetaTypeId<RealType *>();
131 return doRegister<RealType *, HandleAs *>(id);
132 }
133};
134}
135
136/**
137 @brief Registers the type RealType with the metatype system.
138
139 This method can take a second template parameter to specify a cast
140 that should be invoked during registration. This is useful if a base type is
141 already supported.
142
143 @code
144 class SomeType
145 {
146 public:
147 QString someProp() const;
148 };
149
150 // define some introspectable API for SomeType
151
152 KTEXTTEMPLATE_BEGIN_LOOKUP(SomeType)
153 if (property == "someProp")
154 return object.someProp();
155 KTEXTTEMPLATE_END_LOOKUP
156
157
158 class OtherType : public SomeType
159 {
160 // ...
161 };
162
163 registerMetaType<SomeType>();
164
165 // Only the introspectable API from SomeType is needed, so we can reuse that
166 registration.
167 registerMetaType<OtherType, SomeType>();
168 @endcode
169
170 @see @ref generic_types_and_templates
171 */
172template<typename RealType, typename HandleAs>
174{
175 MetaType::internalLock();
176
177 const int id = InternalRegisterType<RealType, HandleAs>::doReg();
178
179 MetaType::internalUnlock();
180
181 return id;
182}
183
184#ifndef K_DOXYGEN
185/**
186 @internal
187 Register a type so %KTextTemplate knows how to handle it.
188
189 This is a convenience method.
190 */
191template<typename Type>
193{
195}
196
197#endif
198} // namespace KTextTemplate
199
200/**
201 Top boundary of a lookup function for Type.
202
203 @see @ref generic_types
204 */
205#define KTEXTTEMPLATE_BEGIN_LOOKUP(Type) \
206 namespace KTextTemplate \
207 { \
208 template<> \
209 inline QVariant TypeAccessor<Type &>::lookUp(const Type &object, const QString &property) \
210 {
211/**
212 Top boundary of a lookup function for Type*.
213
214 @see @ref generic_types
215 */
216#define KTEXTTEMPLATE_BEGIN_LOOKUP_PTR(Type) \
217 namespace KTextTemplate \
218 { \
219 template<> \
220 inline QVariant TypeAccessor<Type *>::lookUp(const Type *const object, const QString &property) \
221 {
222/**
223 Bottom boundary of a lookup function for Type.
224
225 @see @ref generic_types
226 */
227#define KTEXTTEMPLATE_END_LOOKUP \
228 return QVariant(); \
229 } \
230 }
231
232#endif // #define KTEXTTEMPLATE_METATYPE_H
The KTextTemplate namespace holds all public KTextTemplate API.
Definition Mainpage.dox:8
int registerMetaType()
Registers the type RealType with the metatype system.
Definition metatype.h:173
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:17 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.