qqmlprivate.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  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 The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QQMLPRIVATE_H
  40. #define QQMLPRIVATE_H
  41. //
  42. // W A R N I N G
  43. // -------------
  44. //
  45. // This file is not part of the Qt API. It exists purely as an
  46. // implementation detail. This header file may change from version to
  47. // version without notice, or even be removed.
  48. //
  49. // We mean it.
  50. //
  51. #include <QtQml/qtqmlglobal.h>
  52. #include <QtCore/qglobal.h>
  53. #include <QtCore/qvariant.h>
  54. #include <QtCore/qurl.h>
  55. QT_BEGIN_NAMESPACE
  56. namespace QQmlPrivate {
  57. struct CachedQmlUnit;
  58. }
  59. namespace QV4 {
  60. struct ExecutionEngine;
  61. namespace CompiledData {
  62. struct Unit;
  63. struct CompilationUnit;
  64. }
  65. typedef CompiledData::CompilationUnit *(*CompilationUnitFactoryFunction)();
  66. }
  67. namespace QmlIR {
  68. struct Document;
  69. typedef void (*IRLoaderFunction)(Document *, const QQmlPrivate::CachedQmlUnit *);
  70. }
  71. typedef QObject *(*QQmlAttachedPropertiesFunc)(QObject *);
  72. template <typename TYPE>
  73. class QQmlTypeInfo
  74. {
  75. public:
  76. enum {
  77. hasAttachedProperties = 0
  78. };
  79. };
  80. class QJSValue;
  81. class QJSEngine;
  82. class QQmlEngine;
  83. class QQmlCustomParser;
  84. namespace QQmlPrivate
  85. {
  86. void Q_QML_EXPORT qdeclarativeelement_destructor(QObject *);
  87. template<typename T>
  88. class QQmlElement : public T
  89. {
  90. public:
  91. virtual ~QQmlElement() {
  92. QQmlPrivate::qdeclarativeelement_destructor(this);
  93. }
  94. };
  95. template<typename T>
  96. void createInto(void *memory) { new (memory) QQmlElement<T>; }
  97. template<typename T>
  98. QObject *createParent(QObject *p) { return new T(p); }
  99. template<class From, class To, int N>
  100. struct StaticCastSelectorClass
  101. {
  102. static inline int cast() { return -1; }
  103. };
  104. template<class From, class To>
  105. struct StaticCastSelectorClass<From, To, sizeof(int)>
  106. {
  107. static inline int cast() { return int(reinterpret_cast<quintptr>(static_cast<To *>(reinterpret_cast<From *>(0x10000000)))) - 0x10000000; }
  108. };
  109. template<class From, class To>
  110. struct StaticCastSelector
  111. {
  112. typedef int yes_type;
  113. typedef char no_type;
  114. static yes_type checkType(To *);
  115. static no_type checkType(...);
  116. static inline int cast()
  117. {
  118. return StaticCastSelectorClass<From, To, sizeof(checkType(reinterpret_cast<From *>(0)))>::cast();
  119. }
  120. };
  121. template <typename T>
  122. struct has_attachedPropertiesMember
  123. {
  124. static bool const value = QQmlTypeInfo<T>::hasAttachedProperties;
  125. };
  126. template <typename T, bool hasMember>
  127. class has_attachedPropertiesMethod
  128. {
  129. public:
  130. typedef int yes_type;
  131. typedef char no_type;
  132. template<typename ReturnType>
  133. static yes_type checkType(ReturnType *(*)(QObject *));
  134. static no_type checkType(...);
  135. static bool const value = sizeof(checkType(&T::qmlAttachedProperties)) == sizeof(yes_type);
  136. };
  137. template <typename T>
  138. class has_attachedPropertiesMethod<T, false>
  139. {
  140. public:
  141. static bool const value = false;
  142. };
  143. template<typename T, int N>
  144. class AttachedPropertySelector
  145. {
  146. public:
  147. static inline QQmlAttachedPropertiesFunc func() { return 0; }
  148. static inline const QMetaObject *metaObject() { return 0; }
  149. };
  150. template<typename T>
  151. class AttachedPropertySelector<T, 1>
  152. {
  153. static inline QObject *attachedProperties(QObject *obj) {
  154. return T::qmlAttachedProperties(obj);
  155. }
  156. template<typename ReturnType>
  157. static inline const QMetaObject *attachedPropertiesMetaObject(ReturnType *(*)(QObject *)) {
  158. return &ReturnType::staticMetaObject;
  159. }
  160. public:
  161. static inline QQmlAttachedPropertiesFunc func() {
  162. return &attachedProperties;
  163. }
  164. static inline const QMetaObject *metaObject() {
  165. return attachedPropertiesMetaObject(&T::qmlAttachedProperties);
  166. }
  167. };
  168. template<typename T>
  169. inline QQmlAttachedPropertiesFunc attachedPropertiesFunc()
  170. {
  171. return AttachedPropertySelector<T, has_attachedPropertiesMethod<T, has_attachedPropertiesMember<T>::value>::value>::func();
  172. }
  173. template<typename T>
  174. inline const QMetaObject *attachedPropertiesMetaObject()
  175. {
  176. return AttachedPropertySelector<T, has_attachedPropertiesMethod<T, has_attachedPropertiesMember<T>::value>::value>::metaObject();
  177. }
  178. enum AutoParentResult { Parented, IncompatibleObject, IncompatibleParent };
  179. typedef AutoParentResult (*AutoParentFunction)(QObject *object, QObject *parent);
  180. struct RegisterType {
  181. int version;
  182. int typeId;
  183. int listId;
  184. int objectSize;
  185. void (*create)(void *);
  186. QString noCreationReason;
  187. const char *uri;
  188. int versionMajor;
  189. int versionMinor;
  190. const char *elementName;
  191. const QMetaObject *metaObject;
  192. QQmlAttachedPropertiesFunc attachedPropertiesFunction;
  193. const QMetaObject *attachedPropertiesMetaObject;
  194. int parserStatusCast;
  195. int valueSourceCast;
  196. int valueInterceptorCast;
  197. QObject *(*extensionObjectCreate)(QObject *);
  198. const QMetaObject *extensionMetaObject;
  199. QQmlCustomParser *customParser;
  200. int revision;
  201. // If this is extended ensure "version" is bumped!!!
  202. };
  203. struct RegisterInterface {
  204. int version;
  205. int typeId;
  206. int listId;
  207. const char *iid;
  208. };
  209. struct RegisterAutoParent {
  210. int version;
  211. AutoParentFunction function;
  212. };
  213. struct RegisterSingletonType {
  214. int version;
  215. const char *uri;
  216. int versionMajor;
  217. int versionMinor;
  218. const char *typeName;
  219. QJSValue (*scriptApi)(QQmlEngine *, QJSEngine *);
  220. QObject *(*qobjectApi)(QQmlEngine *, QJSEngine *);
  221. const QMetaObject *instanceMetaObject; // new in version 1
  222. int typeId; // new in version 2
  223. int revision; // new in version 2
  224. // If this is extended ensure "version" is bumped!!!
  225. };
  226. struct RegisterCompositeType {
  227. QUrl url;
  228. const char *uri;
  229. int versionMajor;
  230. int versionMinor;
  231. const char *typeName;
  232. };
  233. struct RegisterCompositeSingletonType {
  234. QUrl url;
  235. const char *uri;
  236. int versionMajor;
  237. int versionMinor;
  238. const char *typeName;
  239. };
  240. struct CachedQmlUnit {
  241. const QV4::CompiledData::Unit *qmlData;
  242. QV4::CompilationUnitFactoryFunction createCompilationUnit;
  243. QmlIR::IRLoaderFunction loadIR;
  244. };
  245. typedef const CachedQmlUnit *(*QmlUnitCacheLookupFunction)(const QUrl &url);
  246. struct RegisterQmlUnitCacheHook {
  247. int version;
  248. QmlUnitCacheLookupFunction lookupCachedQmlUnit;
  249. };
  250. enum RegistrationType {
  251. TypeRegistration = 0,
  252. InterfaceRegistration = 1,
  253. AutoParentRegistration = 2,
  254. SingletonRegistration = 3,
  255. CompositeRegistration = 4,
  256. CompositeSingletonRegistration = 5,
  257. QmlUnitCacheHookRegistration = 6
  258. };
  259. int Q_QML_EXPORT qmlregister(RegistrationType, void *);
  260. }
  261. QT_END_NAMESPACE
  262. #endif // QQMLPRIVATE_H