Improve QJSValueIterator implementation.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativeprivate.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEPRIVATE_H
43 #define QDECLARATIVEPRIVATE_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include <QtCore/qglobal.h>
57 #include <QtCore/qvariant.h>
58
59 QT_BEGIN_HEADER
60
61 QT_BEGIN_NAMESPACE
62
63 QT_MODULE(Declarative)
64
65 typedef QObject *(*QDeclarativeAttachedPropertiesFunc)(QObject *);
66
67 template <typename TYPE>
68 class QDeclarativeTypeInfo
69 {
70 public:
71     enum {
72         hasAttachedProperties = 0
73     };
74 };
75
76
77 class QJSValue;
78 class QJSEngine;
79 class QDeclarativeEngine;
80 class QDeclarativeCustomParser;
81 namespace QDeclarativePrivate
82 {
83     void Q_DECLARATIVE_EXPORT qdeclarativeelement_destructor(QObject *);
84     template<typename T>
85     class QDeclarativeElement : public T
86     {
87     public:
88         virtual ~QDeclarativeElement() { 
89             QDeclarativePrivate::qdeclarativeelement_destructor(this); 
90         }
91     };
92
93     template<typename T>
94     void createInto(void *memory) { new (memory) QDeclarativeElement<T>; }
95
96     template<typename T>
97     QObject *createParent(QObject *p) { return new T(p); }
98
99     template<class From, class To, int N>
100     struct StaticCastSelectorClass
101     {
102         static inline int cast() { return -1; }
103     };
104
105     template<class From, class To>
106     struct StaticCastSelectorClass<From, To, sizeof(int)>
107     {
108         static inline int cast() { return int(reinterpret_cast<quintptr>(static_cast<To *>(reinterpret_cast<From *>(0x10000000)))) - 0x10000000; }
109     };
110
111     template<class From, class To>
112     struct StaticCastSelector
113     {
114         typedef int yes_type;
115         typedef char no_type;
116
117         static yes_type check(To *);
118         static no_type check(...);
119
120         static inline int cast()
121         {
122             return StaticCastSelectorClass<From, To, sizeof(check(reinterpret_cast<From *>(0)))>::cast();
123         }
124     };
125
126     template <typename T>
127     struct has_attachedPropertiesMember
128     {
129         static bool const value = QDeclarativeTypeInfo<T>::hasAttachedProperties;
130     };
131
132     template <typename T, bool hasMember>
133     class has_attachedPropertiesMethod 
134     {
135     public:
136         typedef int yes_type;
137         typedef char no_type;
138
139         template<typename ReturnType>
140         static yes_type check(ReturnType *(*)(QObject *));
141         static no_type check(...);
142
143         static bool const value = sizeof(check(&T::qmlAttachedProperties)) == sizeof(yes_type);
144     }; 
145
146     template <typename T>
147     class has_attachedPropertiesMethod<T, false>
148     {
149     public:
150         static bool const value = false;
151     };
152
153     template<typename T, int N>
154     class AttachedPropertySelector
155     {
156     public:
157         static inline QDeclarativeAttachedPropertiesFunc func() { return 0; }
158         static inline const QMetaObject *metaObject() { return 0; }
159     };
160     template<typename T>
161     class AttachedPropertySelector<T, 1>
162     {
163         static inline QObject *attachedProperties(QObject *obj) {
164             return T::qmlAttachedProperties(obj);
165         }
166         template<typename ReturnType>
167         static inline const QMetaObject *attachedPropertiesMetaObject(ReturnType *(*)(QObject *)) {
168             return &ReturnType::staticMetaObject;
169         }
170     public:
171         static inline QDeclarativeAttachedPropertiesFunc func() {
172             return &attachedProperties;
173         }
174         static inline const QMetaObject *metaObject() {
175             return attachedPropertiesMetaObject(&T::qmlAttachedProperties);
176         }
177     };
178
179     template<typename T>
180     inline QDeclarativeAttachedPropertiesFunc attachedPropertiesFunc()
181     {
182         return AttachedPropertySelector<T, has_attachedPropertiesMethod<T, has_attachedPropertiesMember<T>::value>::value>::func();
183     }
184
185     template<typename T>
186     inline const QMetaObject *attachedPropertiesMetaObject()
187     {
188         return AttachedPropertySelector<T, has_attachedPropertiesMethod<T, has_attachedPropertiesMember<T>::value>::value>::metaObject();
189     }
190
191     enum AutoParentResult { Parented, IncompatibleObject, IncompatibleParent };
192     typedef AutoParentResult (*AutoParentFunction)(QObject *object, QObject *parent);
193
194     struct RegisterType {
195         int version;
196
197         int typeId;
198         int listId;
199         int objectSize;
200         void (*create)(void *);
201         QString noCreationReason;
202
203         const char *uri;
204         int versionMajor;
205         int versionMinor;
206         const char *elementName;
207         const QMetaObject *metaObject;
208
209         QDeclarativeAttachedPropertiesFunc attachedPropertiesFunction;
210         const QMetaObject *attachedPropertiesMetaObject;
211
212         int parserStatusCast;
213         int valueSourceCast;
214         int valueInterceptorCast;
215
216         QObject *(*extensionObjectCreate)(QObject *);
217         const QMetaObject *extensionMetaObject;
218
219         QDeclarativeCustomParser *customParser;
220         int revision;
221         // If this is extended ensure "version" is bumped!!!
222     };
223
224     struct RegisterInterface {
225         int version;
226
227         int typeId;
228         int listId;
229
230         const char *iid;
231     };
232
233     struct RegisterAutoParent {
234         int version;
235
236         AutoParentFunction function;
237     };
238
239     struct RegisterModuleApi {
240         int version;
241
242         const char *uri;
243         int versionMajor;
244         int versionMinor;
245
246         QJSValue (*scriptApi)(QDeclarativeEngine *, QJSEngine *);
247         QObject *(*qobjectApi)(QDeclarativeEngine *, QJSEngine *);
248     };
249
250     enum RegistrationType {
251         TypeRegistration       = 0, 
252         InterfaceRegistration  = 1,
253         AutoParentRegistration = 2,
254         ModuleApiRegistration  = 3,
255     };
256
257     int Q_DECLARATIVE_EXPORT qmlregister(RegistrationType, void *);
258 }
259
260 QT_END_NAMESPACE
261
262 QT_END_HEADER
263
264 #endif // QDECLARATIVEPRIVATE_H