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