PageRenderTime 49ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/qt/qtbase/src/corelib/kernel/qmetatype.cpp

https://gitlab.com/x33n/phantomjs
C++ | 1537 lines | 842 code | 103 blank | 592 comment | 85 complexity | 028db8c6e7f3bbc199b189fc5894d596 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the QtCore 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. #include "qmetatype.h"
  42. #include "qmetatype_p.h"
  43. #include "qobjectdefs.h"
  44. #include "qdatetime.h"
  45. #include "qbytearray.h"
  46. #include "qreadwritelock.h"
  47. #include "qstring.h"
  48. #include "qstringlist.h"
  49. #include "qvector.h"
  50. #include "qlocale.h"
  51. #include "qeasingcurve.h"
  52. #include "quuid.h"
  53. #include "qvariant.h"
  54. #include "qmetatypeswitcher_p.h"
  55. #ifndef QT_BOOTSTRAPPED
  56. # include "qbitarray.h"
  57. # include "qurl.h"
  58. # include "qvariant.h"
  59. # include "qabstractitemmodel.h"
  60. # include "qregularexpression.h"
  61. # include "qjsonvalue.h"
  62. # include "qjsonobject.h"
  63. # include "qjsonarray.h"
  64. # include "qjsondocument.h"
  65. #endif
  66. #ifndef QT_NO_GEOM_VARIANT
  67. # include "qsize.h"
  68. # include "qpoint.h"
  69. # include "qrect.h"
  70. # include "qline.h"
  71. #endif
  72. QT_BEGIN_NAMESPACE
  73. #define NS(x) QT_PREPEND_NAMESPACE(x)
  74. namespace {
  75. struct DefinedTypesFilter {
  76. template<typename T>
  77. struct Acceptor {
  78. static const bool IsAccepted = QtMetaTypePrivate::TypeDefinition<T>::IsAvailable && QModulesPrivate::QTypeModuleInfo<T>::IsCore;
  79. };
  80. };
  81. } // namespace
  82. /*!
  83. \macro Q_DECLARE_OPAQUE_POINTER(PointerType)
  84. \relates QMetaType
  85. \since 5.0
  86. This macro enables pointers to forward-declared types (\a PointerType)
  87. to be registered with QMetaType using either Q_DECLARE_METATYPE()
  88. or qRegisterMetaType().
  89. \sa Q_DECLARE_METATYPE(), qRegisterMetaType()
  90. */
  91. /*!
  92. \macro Q_DECLARE_METATYPE(Type)
  93. \relates QMetaType
  94. This macro makes the type \a Type known to QMetaType as long as it
  95. provides a public default constructor, a public copy constructor and
  96. a public destructor.
  97. It is needed to use the type \a Type as a custom type in QVariant.
  98. This macro requires that \a Type is a fully defined type at the point where
  99. it is used. For pointer types, it also requires that the pointed to type is
  100. fully defined. Use in conjunction with Q_DECLARE_OPAQUE_POINTER() to
  101. register pointers to forward declared types.
  102. Ideally, this macro should be placed below the declaration of
  103. the class or struct. If that is not possible, it can be put in
  104. a private header file which has to be included every time that
  105. type is used in a QVariant.
  106. Adding a Q_DECLARE_METATYPE() makes the type known to all template
  107. based functions, including QVariant. Note that if you intend to
  108. use the type in \e queued signal and slot connections or in
  109. QObject's property system, you also have to call
  110. qRegisterMetaType() since the names are resolved at runtime.
  111. This example shows a typical use case of Q_DECLARE_METATYPE():
  112. \snippet code/src_corelib_kernel_qmetatype.cpp 0
  113. If \c MyStruct is in a namespace, the Q_DECLARE_METATYPE() macro
  114. has to be outside the namespace:
  115. \snippet code/src_corelib_kernel_qmetatype.cpp 1
  116. Since \c{MyStruct} is now known to QMetaType, it can be used in QVariant:
  117. \snippet code/src_corelib_kernel_qmetatype.cpp 2
  118. \sa qRegisterMetaType()
  119. */
  120. /*!
  121. \macro Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(Container)
  122. \relates QMetaType
  123. This macro makes the container \a Container known to QMetaType as a sequential
  124. container. This makes it possible to put an instance of Container<T> into
  125. a QVariant, if T itself is known to QMetaType.
  126. Note that all of the Qt sequential containers already have built-in
  127. support, and it is not necessary to use this macro with them. The
  128. std::vector and std::list containers also have built-in support.
  129. This example shows a typical use of Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE():
  130. \snippet code/src_corelib_kernel_qmetatype.cpp 10
  131. */
  132. /*!
  133. \macro Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(Container)
  134. \relates QMetaType
  135. This macro makes the container \a Container known to QMetaType as an associative
  136. container. This makes it possible to put an instance of Container<T, U> into
  137. a QVariant, if T and U are themselves known to QMetaType.
  138. Note that all of the Qt associative containers already have built-in
  139. support, and it is not necessary to use this macro with them. The
  140. std::map container also has built-in support.
  141. This example shows a typical use of Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE():
  142. \snippet code/src_corelib_kernel_qmetatype.cpp 11
  143. */
  144. /*!
  145. \macro Q_DECLARE_SMART_POINTER_METATYPE(SmartPointer)
  146. \relates QMetaType
  147. This macro makes the smart pointer \a SmartPointer known to QMetaType as a
  148. smart pointer. This makes it possible to put an instance of SmartPointer<T> into
  149. a QVariant, if T is a type which inherits QObject.
  150. Note that the QWeakPointer, QSharedPointer and QPointer already have built-in
  151. support, and it is not necessary to use this macro with them.
  152. This example shows a typical use of Q_DECLARE_SMART_POINTER_METATYPE():
  153. \snippet code/src_corelib_kernel_qmetatype.cpp 13
  154. */
  155. /*!
  156. \enum QMetaType::Type
  157. These are the built-in types supported by QMetaType:
  158. \value Void \c void
  159. \value Bool \c bool
  160. \value Int \c int
  161. \value UInt \c{unsigned int}
  162. \value Double \c double
  163. \value QChar QChar
  164. \value QString QString
  165. \value QByteArray QByteArray
  166. \value VoidStar \c{void *}
  167. \value Long \c{long}
  168. \value LongLong LongLong
  169. \value Short \c{short}
  170. \value Char \c{char}
  171. \value ULong \c{unsigned long}
  172. \value ULongLong ULongLong
  173. \value UShort \c{unsigned short}
  174. \value SChar \c{signed char}
  175. \value UChar \c{unsigned char}
  176. \value Float \c float
  177. \value QObjectStar QObject *
  178. \value QVariant QVariant
  179. \value QCursor QCursor
  180. \value QDate QDate
  181. \value QSize QSize
  182. \value QTime QTime
  183. \value QVariantList QVariantList
  184. \value QPolygon QPolygon
  185. \value QPolygonF QPolygonF
  186. \value QColor QColor
  187. \value QSizeF QSizeF
  188. \value QRectF QRectF
  189. \value QLine QLine
  190. \value QTextLength QTextLength
  191. \value QStringList QStringList
  192. \value QVariantMap QVariantMap
  193. \value QVariantHash QVariantHash
  194. \value QIcon QIcon
  195. \value QPen QPen
  196. \value QLineF QLineF
  197. \value QTextFormat QTextFormat
  198. \value QRect QRect
  199. \value QPoint QPoint
  200. \value QUrl QUrl
  201. \value QRegExp QRegExp
  202. \value QRegularExpression QRegularExpression
  203. \value QDateTime QDateTime
  204. \value QPointF QPointF
  205. \value QPalette QPalette
  206. \value QFont QFont
  207. \value QBrush QBrush
  208. \value QRegion QRegion
  209. \value QBitArray QBitArray
  210. \value QImage QImage
  211. \value QKeySequence QKeySequence
  212. \value QSizePolicy QSizePolicy
  213. \value QPixmap QPixmap
  214. \value QLocale QLocale
  215. \value QBitmap QBitmap
  216. \value QMatrix QMatrix
  217. \value QTransform QTransform
  218. \value QMatrix4x4 QMatrix4x4
  219. \value QVector2D QVector2D
  220. \value QVector3D QVector3D
  221. \value QVector4D QVector4D
  222. \value QQuaternion QQuaternion
  223. \value QEasingCurve QEasingCurve
  224. \value QJsonValue QJsonValue
  225. \value QJsonObject QJsonObject
  226. \value QJsonArray QJsonArray
  227. \value QJsonDocument QJsonDocument
  228. \value QModelIndex QModelIndex
  229. \value QUuid QUuid
  230. \value User Base value for user types
  231. \value UnknownType This is an invalid type id. It is returned from QMetaType for types that are not registered
  232. Additional types can be registered using Q_DECLARE_METATYPE().
  233. \sa type(), typeName()
  234. */
  235. /*!
  236. \enum QMetaType::TypeFlag
  237. The enum describes attributes of a type supported by QMetaType.
  238. \value NeedsConstruction This type has non-trivial constructors. If the flag is not set instances can be safely initialized with memset to 0.
  239. \value NeedsDestruction This type has a non-trivial destructor. If the flag is not set calls to the destructor are not necessary before discarding objects.
  240. \value MovableType An instance of a type having this attribute can be safely moved by memcpy.
  241. \omitvalue SharedPointerToQObject
  242. \omitvalue IsEnumeration
  243. \omitvalue PointerToQObject
  244. \omitvalue WeakPointerToQObject
  245. \omitvalue TrackingPointerToQObject
  246. \omitvalue WasDeclaredAsMetaType
  247. */
  248. /*!
  249. \class QMetaType
  250. \inmodule QtCore
  251. \brief The QMetaType class manages named types in the meta-object system.
  252. \ingroup objectmodel
  253. \threadsafe
  254. The class is used as a helper to marshall types in QVariant and
  255. in queued signals and slots connections. It associates a type
  256. name to a type so that it can be created and destructed
  257. dynamically at run-time. Declare new types with Q_DECLARE_METATYPE()
  258. to make them available to QVariant and other template-based functions.
  259. Call qRegisterMetaType() to make type available to non-template based
  260. functions, such as the queued signal and slot connections.
  261. Any class or struct that has a public default
  262. constructor, a public copy constructor, and a public destructor
  263. can be registered.
  264. The following code allocates and destructs an instance of
  265. \c{MyClass}:
  266. \snippet code/src_corelib_kernel_qmetatype.cpp 3
  267. If we want the stream operators \c operator<<() and \c
  268. operator>>() to work on QVariant objects that store custom types,
  269. the custom type must provide \c operator<<() and \c operator>>()
  270. operators.
  271. \sa Q_DECLARE_METATYPE(), QVariant::setValue(), QVariant::value(), QVariant::fromValue()
  272. */
  273. /*!
  274. \fn bool QMetaType::isValid() const
  275. \since 5.0
  276. Returns \c true if this QMetaType object contains valid
  277. information about a type, false otherwise.
  278. */
  279. /*!
  280. \fn bool QMetaType::isRegistered() const
  281. \since 5.0
  282. Returns \c true if this QMetaType object contains valid
  283. information about a type, false otherwise.
  284. */
  285. /*!
  286. \fn bool QMetaType::sizeOf() const
  287. \since 5.0
  288. Returns the size of the type in bytes (i.e. sizeof(T),
  289. where T is the actual type for which this QMetaType instance
  290. was constructed for).
  291. This function is typically used together with construct()
  292. to perform low-level management of the memory used by a type.
  293. \sa QMetaType::construct(), QMetaType::sizeOf()
  294. */
  295. /*!
  296. \fn TypeFlags QMetaType::flags() const
  297. \since 5.0
  298. Returns flags of the type for which this QMetaType instance was constructed.
  299. \sa QMetaType::TypeFlags, QMetaType::typeFlags()
  300. */
  301. /*!
  302. \fn const QMetaObject *QMetaType::metaObject() const
  303. \since 5.0
  304. \internal
  305. */
  306. /*!
  307. \fn void *QMetaType::create(const void *copy = 0) const
  308. \since 5.0
  309. Returns a copy of \a copy, assuming it is of the type that this
  310. QMetaType instance was created for. If \a copy is null, creates
  311. a default constructed instance.
  312. \sa QMetaType::destroy()
  313. */
  314. /*!
  315. \fn void QMetaType::destroy(void *data) const
  316. \since 5.0
  317. Destroys the \a data, assuming it is of the type that this
  318. QMetaType instance was created for.
  319. \sa QMetaType::create()
  320. */
  321. /*!
  322. \fn void *QMetaType::construct(void *where, const void *copy = 0) const
  323. \since 5.0
  324. Constructs a value of the type that this QMetaType instance
  325. was constructed for in the existing memory addressed by \a where,
  326. that is a copy of \a copy, and returns \a where. If \a copy is
  327. zero, the value is default constructed.
  328. This is a low-level function for explicitly managing the memory
  329. used to store the type. Consider calling create() if you don't
  330. need this level of control (that is, use "new" rather than
  331. "placement new").
  332. You must ensure that \a where points to a location where the new
  333. value can be stored and that \a where is suitably aligned.
  334. The type's size can be queried by calling sizeOf().
  335. The rule of thumb for alignment is that a type is aligned to its
  336. natural boundary, which is the smallest power of 2 that is bigger
  337. than the type, unless that alignment is larger than the maximum
  338. useful alignment for the platform. For practical purposes,
  339. alignment larger than 2 * sizeof(void*) is only necessary for
  340. special hardware instructions (e.g., aligned SSE loads and stores
  341. on x86).
  342. */
  343. /*!
  344. \fn void QMetaType::destruct(void *data) const
  345. \since 5.0
  346. Destructs the value, located at \a data, assuming that it is
  347. of the type for which this QMetaType instance was constructed for.
  348. Unlike destroy(), this function only invokes the type's
  349. destructor, it doesn't invoke the delete operator.
  350. \sa QMetaType::construct()
  351. */
  352. /*!
  353. \fn QMetaType::~QMetaType()
  354. Destructs this object.
  355. */
  356. #define QT_ADD_STATIC_METATYPE(MetaTypeName, MetaTypeId, RealName) \
  357. { #RealName, sizeof(#RealName) - 1, MetaTypeId },
  358. #define QT_ADD_STATIC_METATYPE_ALIASES_ITER(MetaTypeName, MetaTypeId, AliasingName, RealNameStr) \
  359. { RealNameStr, sizeof(RealNameStr) - 1, QMetaType::MetaTypeName },
  360. #define QT_ADD_STATIC_METATYPE_HACKS_ITER(MetaTypeName, TypeId, Name) \
  361. QT_ADD_STATIC_METATYPE(MetaTypeName, MetaTypeName, Name)
  362. static const struct { const char * typeName; int typeNameLength; int type; } types[] = {
  363. QT_FOR_EACH_STATIC_TYPE(QT_ADD_STATIC_METATYPE)
  364. QT_FOR_EACH_STATIC_ALIAS_TYPE(QT_ADD_STATIC_METATYPE_ALIASES_ITER)
  365. QT_FOR_EACH_STATIC_HACKS_TYPE(QT_ADD_STATIC_METATYPE_HACKS_ITER)
  366. {0, 0, QMetaType::UnknownType}
  367. };
  368. Q_CORE_EXPORT const QMetaTypeInterface *qMetaTypeGuiHelper = 0;
  369. Q_CORE_EXPORT const QMetaTypeInterface *qMetaTypeWidgetsHelper = 0;
  370. Q_CORE_EXPORT const QMetaObject *qMetaObjectWidgetsHelper = 0;
  371. class QCustomTypeInfo : public QMetaTypeInterface
  372. {
  373. public:
  374. QCustomTypeInfo()
  375. : alias(-1)
  376. {
  377. QMetaTypeInterface empty = QT_METATYPE_INTERFACE_INIT(void);
  378. *static_cast<QMetaTypeInterface*>(this) = empty;
  379. }
  380. QByteArray typeName;
  381. int alias;
  382. };
  383. template<typename T, typename Key>
  384. class QMetaTypeFunctionRegistry
  385. {
  386. public:
  387. ~QMetaTypeFunctionRegistry()
  388. {
  389. const QWriteLocker locker(&lock);
  390. map.clear();
  391. }
  392. bool contains(Key k) const
  393. {
  394. const QReadLocker locker(&lock);
  395. return map.contains(k);
  396. }
  397. bool insertIfNotContains(Key k, const T *f)
  398. {
  399. const QWriteLocker locker(&lock);
  400. const T* &fun = map[k];
  401. if (fun != 0)
  402. return false;
  403. fun = f;
  404. return true;
  405. }
  406. const T *function(Key k) const
  407. {
  408. const QReadLocker locker(&lock);
  409. return map.value(k, 0);
  410. }
  411. void remove(int from, int to)
  412. {
  413. const Key k(from, to);
  414. const QWriteLocker locker(&lock);
  415. map.remove(k);
  416. }
  417. private:
  418. mutable QReadWriteLock lock;
  419. QHash<Key, const T *> map;
  420. };
  421. typedef QMetaTypeFunctionRegistry<QtPrivate::AbstractConverterFunction,QPair<int,int> >
  422. QMetaTypeConverterRegistry;
  423. typedef QMetaTypeFunctionRegistry<QtPrivate::AbstractComparatorFunction,int>
  424. QMetaTypeComparatorRegistry;
  425. typedef QMetaTypeFunctionRegistry<QtPrivate::AbstractDebugStreamFunction,int>
  426. QMetaTypeDebugStreamRegistry;
  427. namespace
  428. {
  429. union CheckThatItIsPod
  430. { // This should break if QMetaTypeInterface is not a POD type
  431. QMetaTypeInterface iface;
  432. };
  433. }
  434. Q_DECLARE_TYPEINFO(QCustomTypeInfo, Q_MOVABLE_TYPE);
  435. Q_GLOBAL_STATIC(QVector<QCustomTypeInfo>, customTypes)
  436. Q_GLOBAL_STATIC(QReadWriteLock, customTypesLock)
  437. Q_GLOBAL_STATIC(QMetaTypeConverterRegistry, customTypesConversionRegistry)
  438. Q_GLOBAL_STATIC(QMetaTypeComparatorRegistry, customTypesComparatorRegistry)
  439. Q_GLOBAL_STATIC(QMetaTypeDebugStreamRegistry, customTypesDebugStreamRegistry)
  440. /*!
  441. \fn bool QMetaType::registerConverter()
  442. \since 5.2
  443. Registers the possibility of an implicit conversion from type From to type To in the meta
  444. type system. Returns \c true if the registration succeeded, otherwise false.
  445. */
  446. /*!
  447. \fn bool QMetaType::registerConverter(MemberFunction function)
  448. \since 5.2
  449. \overload
  450. Registers a method \a function like To From::function() const as converter from type From
  451. to type To in the meta type system. Returns \c true if the registration succeeded, otherwise false.
  452. */
  453. /*!
  454. \fn bool QMetaType::registerConverter(MemberFunctionOk function)
  455. \since 5.2
  456. \overload
  457. Registers a method \a function like To From::function(bool *ok) const as converter from type From
  458. to type To in the meta type system. Returns \c true if the registration succeeded, otherwise false.
  459. */
  460. /*!
  461. \fn bool QMetaType::registerConverter(UnaryFunction function)
  462. \since 5.2
  463. \overload
  464. Registers a unary function object \a function as converter from type From
  465. to type To in the meta type system. Returns \c true if the registration succeeded, otherwise false.
  466. */
  467. /*!
  468. \fn bool QMetaType::registerComparators()
  469. \since 5.2
  470. Registers comparison operetarors for the user-registered type T. This requires T to have
  471. both an operator== and an operator<.
  472. Returns \c true if the registration succeeded, otherwise false.
  473. */
  474. #ifndef QT_NO_DEBUG_STREAM
  475. /*!
  476. \fn bool QMetaType::registerDebugStreamOperator()
  477. Registers the debug stream operator for the user-registered type T. This requires T to have
  478. an operator<<(QDebug dbg, T).
  479. Returns \c true if the registration succeeded, otherwise false.
  480. */
  481. #endif
  482. /*!
  483. Registers function \a f as converter function from type id \a from to \a to.
  484. If there's already a conversion registered, this does nothing but deleting \a f.
  485. Returns \c true if the registration succeeded, otherwise false.
  486. \since 5.2
  487. \internal
  488. */
  489. bool QMetaType::registerConverterFunction(const QtPrivate::AbstractConverterFunction *f, int from, int to)
  490. {
  491. if (!customTypesConversionRegistry()->insertIfNotContains(qMakePair(from, to), f)) {
  492. qWarning("Type conversion already registered from type %s to type %s",
  493. QMetaType::typeName(from), QMetaType::typeName(to));
  494. return false;
  495. }
  496. return true;
  497. }
  498. /*!
  499. \internal
  500. Invoked automatically when a converter function object is destroyed.
  501. */
  502. void QMetaType::unregisterConverterFunction(int from, int to)
  503. {
  504. if (customTypesConversionRegistry.isDestroyed())
  505. return;
  506. customTypesConversionRegistry()->remove(from, to);
  507. }
  508. bool QMetaType::registerComparatorFunction(const QtPrivate::AbstractComparatorFunction *f, int type)
  509. {
  510. if (!customTypesComparatorRegistry()->insertIfNotContains(type, f)) {
  511. qWarning("Comparators already registered for type %s", QMetaType::typeName(type));
  512. return false;
  513. }
  514. return true;
  515. }
  516. /*!
  517. \fn bool QMetaType::hasRegisteredComparators()
  518. Returns \c true, if the meta type system has registered comparators for type T.
  519. \since 5.2
  520. */
  521. /*!
  522. Returns \c true, if the meta type system has registered comparators for type id \a typeId.
  523. \since 5.2
  524. */
  525. bool QMetaType::hasRegisteredComparators(int typeId)
  526. {
  527. return customTypesComparatorRegistry()->contains(typeId);
  528. }
  529. #ifndef QT_NO_DEBUG_STREAM
  530. bool QMetaType::registerDebugStreamOperatorFunction(const QtPrivate::AbstractDebugStreamFunction *f,
  531. int type)
  532. {
  533. if (!customTypesDebugStreamRegistry()->insertIfNotContains(type, f)) {
  534. qWarning("Debug stream operator already registered for type %s", QMetaType::typeName(type));
  535. return false;
  536. }
  537. return true;
  538. }
  539. /*!
  540. \fn bool QMetaType::hasRegisteredDebugStreamOperator()
  541. Returns \c true, if the meta type system has a registered debug stream operator for type T.
  542. \since 5.2
  543. */
  544. /*!
  545. Returns \c true, if the meta type system has a registered debug stream operator for type
  546. id \a typeId.
  547. \since 5.2
  548. */
  549. bool QMetaType::hasRegisteredDebugStreamOperator(int typeId)
  550. {
  551. return customTypesDebugStreamRegistry()->contains(typeId);
  552. }
  553. #endif
  554. /*!
  555. Converts the object at \a from from \a fromTypeId to the preallocated space at \a to
  556. typed \a toTypeId. Returns \c true, if the conversion succeeded, otherwise false.
  557. \since 5.2
  558. */
  559. bool QMetaType::convert(const void *from, int fromTypeId, void *to, int toTypeId)
  560. {
  561. const QtPrivate::AbstractConverterFunction * const f =
  562. customTypesConversionRegistry()->function(qMakePair(fromTypeId, toTypeId));
  563. return f && f->convert(f, from, to);
  564. }
  565. /*!
  566. Compares the objects at \a lhs and \a rhs. Both objects need to be of type \a typeId.
  567. \a result is set to less than, equal to or greater than zero, if \a lhs is less than, equal to
  568. or greater than \a rhs. Returns \c true, if the comparison succeeded, otherwiess false.
  569. \since 5.2
  570. */
  571. bool QMetaType::compare(const void *lhs, const void *rhs, int typeId, int* result)
  572. {
  573. const QtPrivate::AbstractComparatorFunction * const f =
  574. customTypesComparatorRegistry()->function(typeId);
  575. if (!f)
  576. return false;
  577. if (f->equals(f, lhs, rhs))
  578. *result = 0;
  579. else
  580. *result = f->lessThan(f, lhs, rhs) ? -1 : 1;
  581. return true;
  582. }
  583. /*!
  584. Streams the object at \a rhs of type \a typeId to the debug stream \a dbg. Returns \c true
  585. on success, otherwise false.
  586. \since 5.2
  587. */
  588. bool QMetaType::debugStream(QDebug& dbg, const void *rhs, int typeId)
  589. {
  590. const QtPrivate::AbstractDebugStreamFunction * const f = customTypesDebugStreamRegistry()->function(typeId);
  591. if (!f)
  592. return false;
  593. f->stream(f, dbg, rhs);
  594. return true;
  595. }
  596. /*!
  597. \fn bool QMetaType::hasRegisteredConverterFunction()
  598. Returns \c true, if the meta type system has a registered conversion from type From to type To.
  599. \since 5.2
  600. \overload
  601. */
  602. /*!
  603. Returns \c true, if the meta type system has a registered conversion from meta type id \a fromTypeId
  604. to \a toTypeId
  605. \since 5.2
  606. */
  607. bool QMetaType::hasRegisteredConverterFunction(int fromTypeId, int toTypeId)
  608. {
  609. return customTypesConversionRegistry()->contains(qMakePair(fromTypeId, toTypeId));
  610. }
  611. #ifndef QT_NO_DATASTREAM
  612. /*!
  613. \internal
  614. */
  615. void QMetaType::registerStreamOperators(const char *typeName, SaveOperator saveOp,
  616. LoadOperator loadOp)
  617. {
  618. registerStreamOperators(type(typeName), saveOp, loadOp);
  619. }
  620. /*!
  621. \internal
  622. */
  623. void QMetaType::registerStreamOperators(int idx, SaveOperator saveOp,
  624. LoadOperator loadOp)
  625. {
  626. if (idx < User)
  627. return; //builtin types should not be registered;
  628. QVector<QCustomTypeInfo> *ct = customTypes();
  629. if (!ct)
  630. return;
  631. QWriteLocker locker(customTypesLock());
  632. QCustomTypeInfo &inf = (*ct)[idx - User];
  633. inf.saveOp = saveOp;
  634. inf.loadOp = loadOp;
  635. }
  636. #endif // QT_NO_DATASTREAM
  637. /*!
  638. Returns the type name associated with the given \a typeId, or 0 if no
  639. matching type was found. The returned pointer must not be deleted.
  640. \sa type(), isRegistered(), Type
  641. */
  642. const char *QMetaType::typeName(int typeId)
  643. {
  644. const uint type = typeId;
  645. // In theory it can be filled during compilation time, but for some reason template code
  646. // that is able to do it causes GCC 4.6 to generate additional 3K of executable code. Probably
  647. // it is not worth of it.
  648. static const char *namesCache[QMetaType::HighestInternalId + 1];
  649. const char *result;
  650. if (type <= QMetaType::HighestInternalId && ((result = namesCache[type])))
  651. return result;
  652. #define QT_METATYPE_TYPEID_TYPENAME_CONVERTER(MetaTypeName, TypeId, RealName) \
  653. case QMetaType::MetaTypeName: result = #RealName; break;
  654. switch (QMetaType::Type(type)) {
  655. QT_FOR_EACH_STATIC_TYPE(QT_METATYPE_TYPEID_TYPENAME_CONVERTER)
  656. default: {
  657. if (Q_UNLIKELY(type < QMetaType::User)) {
  658. return 0; // It can happen when someone cast int to QVariant::Type, we should not crash...
  659. } else {
  660. const QVector<QCustomTypeInfo> * const ct = customTypes();
  661. QReadLocker locker(customTypesLock());
  662. return ct && uint(ct->count()) > type - QMetaType::User && !ct->at(type - QMetaType::User).typeName.isEmpty()
  663. ? ct->at(type - QMetaType::User).typeName.constData()
  664. : 0;
  665. }
  666. }
  667. }
  668. #undef QT_METATYPE_TYPEID_TYPENAME_CONVERTER
  669. Q_ASSERT(type <= QMetaType::HighestInternalId);
  670. namesCache[type] = result;
  671. return result;
  672. }
  673. /*!
  674. \internal
  675. Similar to QMetaType::type(), but only looks in the static set of types.
  676. */
  677. static inline int qMetaTypeStaticType(const char *typeName, int length)
  678. {
  679. int i = 0;
  680. while (types[i].typeName && ((length != types[i].typeNameLength)
  681. || strcmp(typeName, types[i].typeName))) {
  682. ++i;
  683. }
  684. return types[i].type;
  685. }
  686. /*!
  687. \internal
  688. Similar to QMetaType::type(), but only looks in the custom set of
  689. types, and doesn't lock the mutex.
  690. */
  691. static int qMetaTypeCustomType_unlocked(const char *typeName, int length)
  692. {
  693. const QVector<QCustomTypeInfo> * const ct = customTypes();
  694. if (!ct)
  695. return QMetaType::UnknownType;
  696. for (int v = 0; v < ct->count(); ++v) {
  697. const QCustomTypeInfo &customInfo = ct->at(v);
  698. if ((length == customInfo.typeName.size())
  699. && !strcmp(typeName, customInfo.typeName.constData())) {
  700. if (customInfo.alias >= 0)
  701. return customInfo.alias;
  702. return v + QMetaType::User;
  703. }
  704. }
  705. return QMetaType::UnknownType;
  706. }
  707. /*!
  708. \internal
  709. This function is needed until existing code outside of qtbase
  710. has been changed to call the new version of registerType().
  711. */
  712. int QMetaType::registerType(const char *typeName, Deleter deleter,
  713. Creator creator)
  714. {
  715. return registerType(typeName, deleter, creator,
  716. QtMetaTypePrivate::QMetaTypeFunctionHelper<void>::Destruct,
  717. QtMetaTypePrivate::QMetaTypeFunctionHelper<void>::Construct, 0, TypeFlags(), 0);
  718. }
  719. /*!
  720. \internal
  721. \since 5.0
  722. Registers a user type for marshalling, with \a typeName, a \a
  723. deleter, a \a creator, a \a destructor, a \a constructor, and
  724. a \a size. Returns the type's handle, or -1 if the type could
  725. not be registered.
  726. */
  727. int QMetaType::registerType(const char *typeName, Deleter deleter,
  728. Creator creator,
  729. Destructor destructor,
  730. Constructor constructor,
  731. int size, TypeFlags flags, const QMetaObject *metaObject)
  732. {
  733. #ifdef QT_NO_QOBJECT
  734. NS(QByteArray) normalizedTypeName = typeName;
  735. #else
  736. NS(QByteArray) normalizedTypeName = QMetaObject::normalizedType(typeName);
  737. #endif
  738. return registerNormalizedType(normalizedTypeName, deleter, creator, destructor, constructor, size, flags, metaObject);
  739. }
  740. /*!
  741. \internal
  742. \since 5.0
  743. Registers a user type for marshalling, with \a normalizedTypeName, a \a
  744. deleter, a \a creator, a \a destructor, a \a constructor, and
  745. a \a size. Returns the type's handle, or -1 if the type could
  746. not be registered. Note that normalizedTypeName is not checked for
  747. conformance with Qt's normalized format, so it must already
  748. conform.
  749. */
  750. int QMetaType::registerNormalizedType(const NS(QByteArray) &normalizedTypeName, Deleter deleter,
  751. Creator creator,
  752. Destructor destructor,
  753. Constructor constructor,
  754. int size, TypeFlags flags, const QMetaObject *metaObject)
  755. {
  756. QVector<QCustomTypeInfo> *ct = customTypes();
  757. if (!ct || normalizedTypeName.isEmpty() || !deleter || !creator || !destructor || !constructor)
  758. return -1;
  759. int idx = qMetaTypeStaticType(normalizedTypeName.constData(),
  760. normalizedTypeName.size());
  761. int previousSize = 0;
  762. int previousFlags = 0;
  763. if (idx == UnknownType) {
  764. QWriteLocker locker(customTypesLock());
  765. idx = qMetaTypeCustomType_unlocked(normalizedTypeName.constData(),
  766. normalizedTypeName.size());
  767. if (idx == UnknownType) {
  768. QCustomTypeInfo inf;
  769. inf.typeName = normalizedTypeName;
  770. inf.creator = creator;
  771. inf.deleter = deleter;
  772. #ifndef QT_NO_DATASTREAM
  773. inf.loadOp = 0;
  774. inf.saveOp = 0;
  775. #endif
  776. inf.alias = -1;
  777. inf.constructor = constructor;
  778. inf.destructor = destructor;
  779. inf.size = size;
  780. inf.flags = flags;
  781. inf.metaObject = metaObject;
  782. idx = ct->size() + User;
  783. ct->append(inf);
  784. return idx;
  785. }
  786. if (idx >= User) {
  787. previousSize = ct->at(idx - User).size;
  788. previousFlags = ct->at(idx - User).flags;
  789. }
  790. }
  791. if (idx < User) {
  792. previousSize = QMetaType::sizeOf(idx);
  793. previousFlags = QMetaType::typeFlags(idx);
  794. }
  795. if (previousSize != size) {
  796. qFatal("QMetaType::registerType: Binary compatibility break "
  797. "-- Size mismatch for type '%s' [%i]. Previously registered "
  798. "size %i, now registering size %i.",
  799. normalizedTypeName.constData(), idx, previousSize, size);
  800. }
  801. // Ignore WasDeclaredAsMetaType inconsitency, to many users were hitting the problem
  802. previousFlags |= WasDeclaredAsMetaType;
  803. flags |= WasDeclaredAsMetaType;
  804. if (previousFlags != flags) {
  805. const int maskForTypeInfo = NeedsConstruction | NeedsDestruction | MovableType;
  806. const char *msg = "QMetaType::registerType: Binary compatibility break. "
  807. "\nType flags for type '%s' [%i] don't match. Previously "
  808. "registered TypeFlags(0x%x), now registering TypeFlags(0x%x). "
  809. "This is an ODR break, which means that your application depends on a C++ undefined behavior."
  810. "\nHint: %s";
  811. QT_PREPEND_NAMESPACE(QByteArray) hint;
  812. if ((previousFlags & maskForTypeInfo) != (flags & maskForTypeInfo)) {
  813. hint += "\nIt seems that the type was registered at least twice in a different translation units, "
  814. "but Q_DECLARE_TYPEINFO is not visible from all the translations unit or different flags were used."
  815. "Remember that Q_DECLARE_TYPEINFO should be declared before QMetaType registration, "
  816. "preferably it should be placed just after the type declaration and before Q_DECLARE_METATYPE";
  817. }
  818. qFatal(msg, normalizedTypeName.constData(), idx, previousFlags, int(flags), hint.constData());
  819. }
  820. return idx;
  821. }
  822. /*!
  823. \internal
  824. \since 4.7
  825. Registers a user type for marshalling, as an alias of another type (typedef)
  826. */
  827. int QMetaType::registerTypedef(const char* typeName, int aliasId)
  828. {
  829. #ifdef QT_NO_QOBJECT
  830. NS(QByteArray) normalizedTypeName = typeName;
  831. #else
  832. NS(QByteArray) normalizedTypeName = QMetaObject::normalizedType(typeName);
  833. #endif
  834. return registerNormalizedTypedef(normalizedTypeName, aliasId);
  835. }
  836. /*!
  837. \internal
  838. \since 5.0
  839. Registers a user type for marshalling, as an alias of another type (typedef).
  840. Note that normalizedTypeName is not checked for conformance with Qt's normalized format,
  841. so it must already conform.
  842. */
  843. int QMetaType::registerNormalizedTypedef(const NS(QByteArray) &normalizedTypeName, int aliasId)
  844. {
  845. QVector<QCustomTypeInfo> *ct = customTypes();
  846. if (!ct || normalizedTypeName.isEmpty())
  847. return -1;
  848. int idx = qMetaTypeStaticType(normalizedTypeName.constData(),
  849. normalizedTypeName.size());
  850. if (idx == UnknownType) {
  851. QWriteLocker locker(customTypesLock());
  852. idx = qMetaTypeCustomType_unlocked(normalizedTypeName.constData(),
  853. normalizedTypeName.size());
  854. if (idx == UnknownType) {
  855. QCustomTypeInfo inf;
  856. inf.typeName = normalizedTypeName;
  857. inf.alias = aliasId;
  858. inf.creator = 0;
  859. inf.deleter = 0;
  860. ct->append(inf);
  861. return aliasId;
  862. }
  863. }
  864. if (idx != aliasId) {
  865. qWarning("QMetaType::registerTypedef: "
  866. "-- Type name '%s' previously registered as typedef of '%s' [%i], "
  867. "now registering as typedef of '%s' [%i].",
  868. normalizedTypeName.constData(), QMetaType::typeName(idx), idx,
  869. QMetaType::typeName(aliasId), aliasId);
  870. }
  871. return idx;
  872. }
  873. /*!
  874. Returns \c true if the datatype with ID \a type is registered;
  875. otherwise returns \c false.
  876. \sa type(), typeName(), Type
  877. */
  878. bool QMetaType::isRegistered(int type)
  879. {
  880. // predefined type
  881. if ((type >= FirstCoreType && type <= LastCoreType)
  882. || (type >= FirstGuiType && type <= LastGuiType)
  883. || (type >= FirstWidgetsType && type <= LastWidgetsType)) {
  884. return true;
  885. }
  886. QReadLocker locker(customTypesLock());
  887. const QVector<QCustomTypeInfo> * const ct = customTypes();
  888. return ((type >= User) && (ct && ct->count() > type - User) && !ct->at(type - User).typeName.isEmpty());
  889. }
  890. /*!
  891. \fn int qMetaTypeTypeImpl(const char *typeName)
  892. \internal
  893. Implementation of QMetaType::type().
  894. */
  895. template <bool tryNormalizedType>
  896. static inline int qMetaTypeTypeImpl(const char *typeName)
  897. {
  898. int length = qstrlen(typeName);
  899. if (!length)
  900. return QMetaType::UnknownType;
  901. int type = qMetaTypeStaticType(typeName, length);
  902. if (type == QMetaType::UnknownType) {
  903. QReadLocker locker(customTypesLock());
  904. type = qMetaTypeCustomType_unlocked(typeName, length);
  905. #ifndef QT_NO_QOBJECT
  906. if ((type == QMetaType::UnknownType) && tryNormalizedType) {
  907. const NS(QByteArray) normalizedTypeName = QMetaObject::normalizedType(typeName);
  908. type = qMetaTypeStaticType(normalizedTypeName.constData(),
  909. normalizedTypeName.size());
  910. if (type == QMetaType::UnknownType) {
  911. type = qMetaTypeCustomType_unlocked(normalizedTypeName.constData(),
  912. normalizedTypeName.size());
  913. }
  914. }
  915. #endif
  916. }
  917. return type;
  918. }
  919. /*!
  920. Returns a handle to the type called \a typeName, or QMetaType::UnknownType if there is
  921. no such type.
  922. \sa isRegistered(), typeName(), Type
  923. */
  924. int QMetaType::type(const char *typeName)
  925. {
  926. return qMetaTypeTypeImpl</*tryNormalizedType=*/true>(typeName);
  927. }
  928. /*!
  929. \a internal
  930. Similar to QMetaType::type(); the only difference is that this function
  931. doesn't attempt to normalize the type name (i.e., the lookup will fail
  932. for type names in non-normalized form).
  933. */
  934. int qMetaTypeTypeInternal(const char *typeName)
  935. {
  936. return qMetaTypeTypeImpl</*tryNormalizedType=*/false>(typeName);
  937. }
  938. #ifndef QT_NO_DATASTREAM
  939. /*!
  940. Writes the object pointed to by \a data with the ID \a type to
  941. the given \a stream. Returns \c true if the object is saved
  942. successfully; otherwise returns \c false.
  943. The type must have been registered with qRegisterMetaType() and
  944. qRegisterMetaTypeStreamOperators() beforehand.
  945. Normally, you should not need to call this function directly.
  946. Instead, use QVariant's \c operator<<(), which relies on save()
  947. to stream custom types.
  948. \sa load(), qRegisterMetaTypeStreamOperators()
  949. */
  950. bool QMetaType::save(QDataStream &stream, int type, const void *data)
  951. {
  952. if (!data || !isRegistered(type))
  953. return false;
  954. switch(type) {
  955. case QMetaType::UnknownType:
  956. case QMetaType::Void:
  957. case QMetaType::VoidStar:
  958. case QMetaType::QObjectStar:
  959. case QMetaType::QModelIndex:
  960. case QMetaType::QJsonValue:
  961. case QMetaType::QJsonObject:
  962. case QMetaType::QJsonArray:
  963. case QMetaType::QJsonDocument:
  964. return false;
  965. case QMetaType::Long:
  966. stream << qlonglong(*static_cast<const long *>(data));
  967. break;
  968. case QMetaType::Int:
  969. stream << *static_cast<const int *>(data);
  970. break;
  971. case QMetaType::Short:
  972. stream << *static_cast<const short *>(data);
  973. break;
  974. case QMetaType::Char:
  975. // force a char to be signed
  976. stream << *static_cast<const signed char *>(data);
  977. break;
  978. case QMetaType::ULong:
  979. stream << qulonglong(*static_cast<const ulong *>(data));
  980. break;
  981. case QMetaType::UInt:
  982. stream << *static_cast<const uint *>(data);
  983. break;
  984. case QMetaType::LongLong:
  985. stream << *static_cast<const qlonglong *>(data);
  986. break;
  987. case QMetaType::ULongLong:
  988. stream << *static_cast<const qulonglong *>(data);
  989. break;
  990. case QMetaType::UShort:
  991. stream << *static_cast<const ushort *>(data);
  992. break;
  993. case QMetaType::SChar:
  994. stream << *static_cast<const signed char *>(data);
  995. break;
  996. case QMetaType::UChar:
  997. stream << *static_cast<const uchar *>(data);
  998. break;
  999. case QMetaType::Bool:
  1000. stream << qint8(*static_cast<const bool *>(data));
  1001. break;
  1002. case QMetaType::Float:
  1003. stream << *static_cast<const float *>(data);
  1004. break;
  1005. case QMetaType::Double:
  1006. stream << *static_cast<const double *>(data);
  1007. break;
  1008. case QMetaType::QChar:
  1009. stream << *static_cast<const NS(QChar) *>(data);
  1010. break;
  1011. #ifndef QT_BOOTSTRAPPED
  1012. case QMetaType::QVariantMap:
  1013. stream << *static_cast<const NS(QVariantMap)*>(data);
  1014. break;
  1015. case QMetaType::QVariantHash:
  1016. stream << *static_cast<const NS(QVariantHash)*>(data);
  1017. break;
  1018. case QMetaType::QVariantList:
  1019. stream << *static_cast<const NS(QVariantList)*>(data);
  1020. break;
  1021. case QMetaType::QVariant:
  1022. stream << *static_cast<const NS(QVariant)*>(data);
  1023. break;
  1024. #endif
  1025. case QMetaType::QByteArray:
  1026. stream << *static_cast<const NS(QByteArray)*>(data);
  1027. break;
  1028. case QMetaType::QString:
  1029. stream << *static_cast<const NS(QString)*>(data);
  1030. break;
  1031. case QMetaType::QStringList:
  1032. stream << *static_cast<const NS(QStringList)*>(data);
  1033. break;
  1034. #ifndef QT_BOOTSTRAPPED
  1035. case QMetaType::QBitArray:
  1036. stream << *static_cast<const NS(QBitArray)*>(data);
  1037. break;
  1038. #endif
  1039. case QMetaType::QDate:
  1040. stream << *static_cast<const NS(QDate)*>(data);
  1041. break;
  1042. case QMetaType::QTime:
  1043. stream << *static_cast<const NS(QTime)*>(data);
  1044. break;
  1045. case QMetaType::QDateTime:
  1046. stream << *static_cast<const NS(QDateTime)*>(data);
  1047. break;
  1048. #ifndef QT_BOOTSTRAPPED
  1049. case QMetaType::QUrl:
  1050. stream << *static_cast<const NS(QUrl)*>(data);
  1051. break;
  1052. #endif
  1053. case QMetaType::QLocale:
  1054. stream << *static_cast<const NS(QLocale)*>(data);
  1055. break;
  1056. #ifndef QT_NO_GEOM_VARIANT
  1057. case QMetaType::QRect:
  1058. stream << *static_cast<const NS(QRect)*>(data);
  1059. break;
  1060. case QMetaType::QRectF:
  1061. stream << *static_cast<const NS(QRectF)*>(data);
  1062. break;
  1063. case QMetaType::QSize:
  1064. stream << *static_cast<const NS(QSize)*>(data);
  1065. break;
  1066. case QMetaType::QSizeF:
  1067. stream << *static_cast<const NS(QSizeF)*>(data);
  1068. break;
  1069. case QMetaType::QLine:
  1070. stream << *static_cast<const NS(QLine)*>(data);
  1071. break;
  1072. case QMetaType::QLineF:
  1073. stream << *static_cast<const NS(QLineF)*>(data);
  1074. break;
  1075. case QMetaType::QPoint:
  1076. stream << *static_cast<const NS(QPoint)*>(data);
  1077. break;
  1078. case QMetaType::QPointF:
  1079. stream << *static_cast<const NS(QPointF)*>(data);
  1080. break;
  1081. #endif
  1082. #ifndef QT_NO_REGEXP
  1083. case QMetaType::QRegExp:
  1084. stream << *static_cast<const NS(QRegExp)*>(data);
  1085. break;
  1086. #endif
  1087. #ifndef QT_BOOTSTRAPPED
  1088. #ifndef QT_NO_REGULAREXPRESSION
  1089. case QMetaType::QRegularExpression:
  1090. stream << *static_cast<const NS(QRegularExpression)*>(data);
  1091. break;
  1092. #endif // QT_NO_REGULAREXPRESSION
  1093. case QMetaType::QEasingCurve:
  1094. stream << *static_cast<const NS(QEasingCurve)*>(data);
  1095. break;
  1096. #endif // QT_BOOTSTRAPPED
  1097. case QMetaType::QFont:
  1098. case QMetaType::QPixmap:
  1099. case QMetaType::QBrush:
  1100. case QMetaType::QColor:
  1101. case QMetaType::QPalette:
  1102. case QMetaType::QImage:
  1103. case QMetaType::QPolygon:
  1104. case QMetaType::QPolygonF:
  1105. case QMetaType::QRegion:
  1106. case QMetaType::QBitmap:
  1107. case QMetaType::QCursor:
  1108. case QMetaType::QKeySequence:
  1109. case QMetaType::QPen:
  1110. case QMetaType::QTextLength:
  1111. case QMetaType::QTextFormat:
  1112. case QMetaType::QMatrix:
  1113. case QMetaType::QTransform:
  1114. case QMetaType::QMatrix4x4:
  1115. case QMetaType::QVector2D:
  1116. case QMetaType::QVector3D:
  1117. case QMetaType::QVector4D:
  1118. case QMetaType::QQuaternion:
  1119. case QMetaType::QIcon:
  1120. if (!qMetaTypeGuiHelper)
  1121. return false;
  1122. qMetaTypeGuiHelper[type - FirstGuiType].saveOp(stream, data);
  1123. break;
  1124. case QMetaType::QSizePolicy:
  1125. if (!qMetaTypeWidgetsHelper)
  1126. return false;
  1127. qMetaTypeWidgetsHelper[type - FirstWidgetsType].saveOp(stream, data);
  1128. break;
  1129. case QMetaType::QUuid:
  1130. stream << *static_cast<const NS(QUuid)*>(data);
  1131. break;
  1132. default: {
  1133. const QVector<QCustomTypeInfo> * const ct = customTypes();
  1134. if (!ct)
  1135. return false;
  1136. SaveOperator saveOp = 0;
  1137. {
  1138. QReadLocker locker(customTypesLock());
  1139. saveOp = ct->at(type - User).saveOp;
  1140. }
  1141. if (!saveOp)
  1142. return false;
  1143. saveOp(stream, data);
  1144. break; }
  1145. }
  1146. return true;
  1147. }
  1148. /*!
  1149. Reads the object of the specified \a type from the given \a
  1150. stream into \a data. Returns \c true if the object is loaded
  1151. successfully; otherwise returns \c false.
  1152. The type must have been registered with qRegisterMetaType() and
  1153. qRegisterMetaTypeStreamOperators() beforehand.
  1154. Normally, you should not need to call this function directly.
  1155. Instead, use QVariant's \c operator>>(), which relies on load()
  1156. to stream custom types.
  1157. \sa save(), qRegisterMetaTypeStreamOperators()
  1158. */
  1159. bool QMetaType::load(QDataStream &stream, int type, void *data)
  1160. {
  1161. if (!data || !isRegistered(type))
  1162. return false;
  1163. switch(type) {
  1164. case QMetaType::UnknownType:
  1165. case QMetaType::Void:
  1166. case QMetaType::VoidStar:
  1167. case QMetaType::QObjectStar:
  1168. case QMetaType::QModelIndex:
  1169. case QMetaType::QJsonValue:
  1170. case QMetaType::QJsonObject:
  1171. case QMetaType::QJsonArray:
  1172. case QMetaType::QJsonDocument:
  1173. return false;
  1174. case QMetaType::Long: {
  1175. qlonglong l;
  1176. stream >> l;
  1177. *static_cast<long *>(data) = long(l);
  1178. break; }
  1179. case QMetaType::Int:
  1180. stream >> *static_cast<int *>(data);
  1181. break;
  1182. case QMetaType::Short:
  1183. stream >> *static_cast<short *>(data);
  1184. break;
  1185. case QMetaType::Char:
  1186. // force a char to be signed
  1187. stream >> *static_cast<signed char *>(data);
  1188. break;
  1189. case QMetaType::ULong: {
  1190. qulonglong ul;
  1191. stream >> ul;
  1192. *static_cast<ulong *>(data) = ulong(ul);
  1193. break; }
  1194. case QMetaType::UInt:
  1195. stream >> *static_cast<uint *>(data);
  1196. break;
  1197. case QMetaType::LongLong:
  1198. stream >> *static_cast<qlonglong *>(data);
  1199. break;
  1200. case QMetaType::ULongLong:
  1201. stream >> *static_cast<qulonglong *>(data);
  1202. break;
  1203. case QMetaType::UShort:
  1204. stream >> *static_cast<ushort *>(data);
  1205. break;
  1206. case QMetaType::SChar:
  1207. stream >> *static_cast<signed char *>(data);
  1208. break;
  1209. case QMetaType::UChar:
  1210. stream >> *static_cast<uchar *>(data);
  1211. break;
  1212. case QMetaType::Bool: {
  1213. qint8 b;
  1214. stream >> b;
  1215. *static_cast<bool *>(data) = b;
  1216. break; }
  1217. case QMetaType::Float:
  1218. stream >> *static_cast<float *>(data);
  1219. break;
  1220. case QMetaType::Double:
  1221. stream >> *static_cast<double *>(data);
  1222. break;
  1223. case QMetaType::QChar:
  1224. stream >> *static_cast< NS(QChar)*>(data);
  1225. break;
  1226. #ifndef QT_BOOTSTRAPPED
  1227. case QMetaType::QVariantMap:
  1228. stream >> *static_cast< NS(QVariantMap)*>(data);
  1229. break;
  1230. case QMetaType::QVariantHash:
  1231. stream >> *static_cast< NS(QVariantHash)*>(data);
  1232. break;
  1233. case QMetaType::QVariantList:
  1234. stream >> *static_cast< NS(QVariantList)*>(data);
  1235. break;
  1236. case QMetaType::QVariant:
  1237. stream >> *static_cast< NS(QVariant)*>(data);
  1238. break;
  1239. #endif
  1240. case QMetaType::QByteArray:
  1241. stream >> *static_cast< NS(QByteArray)*>(data);
  1242. break;
  1243. case QMetaType::QString:
  1244. stream >> *static_cast< NS(QString)*>(data);
  1245. break;
  1246. case QMetaType::QStringList:
  1247. stream >> *static_cast< NS(QStringList)*>(data);
  1248. break;
  1249. #ifndef QT_BOOTSTRAPPED
  1250. case QMetaType::QBitArray:
  1251. stream >> *static_cast< NS(QBitArray)*>(data);
  1252. break;
  1253. #endif
  1254. case QMetaType::QDate:
  1255. stream >> *static_cast< NS(QDate)*>(data);
  1256. break;
  1257. case QMetaType::QTime:
  1258. stream >> *static_cast< NS(QTime)*>(data);
  1259. break;
  1260. case QMetaType::QDateTime:
  1261. stream >> *static_cast< NS(QDateTime)*>(data);
  1262. break;
  1263. #ifndef QT_BOOTSTRAPPED
  1264. case QMetaType::QUrl:
  1265. stream >> *static_cast< NS(QUrl)*>(data);
  1266. break;
  1267. #endif
  1268. case QMetaType::QLocale:
  1269. stream >> *static_cast< NS(QLocale)*>(data);
  1270. break;
  1271. #ifndef QT_NO_GEOM_VARIANT
  1272. case QMetaType::QRect:
  1273. stream >> *static_cast< NS(QRect)*>(data);
  1274. break;
  1275. case QMetaType::QRectF:
  1276. stream >> *static_cast< NS(QRectF)*>(data);
  1277. break;
  1278. case QMetaType::QSize:
  1279. stream >> *static_cast< NS(QSize)*>(data);
  1280. break;
  1281. case QMetaType::QSizeF:
  1282. stream >> *static_cast< NS(QSizeF)*>(data);
  1283. break;
  1284. case QMetaType::QLine:
  1285. stream >> *static_cast< NS(QLine)*>(data);
  1286. break;
  1287. case QMetaType::QLineF:
  1288. stream >> *static_cast< NS(QLineF)*>(data);
  1289. break;
  1290. case QMetaType::QPoint:
  1291. stream >> *static_cast< NS(QPoint)*>(data);
  1292. break;
  1293. case QMetaType::QPointF:
  1294. stream >> *static_cast< NS(QPointF)*>(data);
  1295. break;
  1296. #endif
  1297. #ifndef QT_NO_REGEXP
  1298. case QMetaType::QRegExp:
  1299. stream >> *static_cast< NS(QRegExp)*>(data);
  1300. break;
  1301. #endif
  1302. #ifndef QT_BOOTSTRAPPED
  1303. #ifndef QT_NO_REGULAREXPRESSION
  1304. case QMetaType::QRegularExpression:
  1305. stream >> *static_cast< NS(QRegularExpression)*>(data);
  1306. break;
  1307. #endif // QT_NO_REGULAREXPRESSION
  1308. case QMetaType::QEasingCurve:
  1309. stream >> *static_cast< NS(QEasingCurve)*>(data);
  1310. break;
  1311. #endif // QT_BOOTSTRAPPED
  1312. case QMetaType::QFont:
  1313. case QMetaType::QPixmap:
  1314. case QMetaType::QBrush:
  1315. case QMetaType::QColor:
  1316. case QMetaType::QPalette:
  1317. case QMetaType::QImage:
  1318. case QMetaType::QPolygon:
  1319. case QMetaType::QPolygonF:
  1320. case QMetaType::QRegion:
  1321. case QMetaType::QBitmap:
  1322. case QMetaType::QCursor:
  1323. case QMetaType::QKeySequence:
  1324. case QMetaType::QPen:
  1325. case QMetaType::QTextLength:
  1326. case QMetaType::QTextFormat:
  1327. case QMetaType::QMatrix:
  1328. case QMetaType::QTransform:
  1329. case QMetaType::QMatrix4x4:
  1330. case QMetaType::QVector2D:
  1331. case QMetaType::QVector3D:
  1332. case QMetaType::QVector4D:
  1333. case QMetaType::QQuaternion:
  1334. case QMetaType::QIcon:
  1335. if (!qMetaTypeGuiHelper)
  1336. return false;
  1337. qMetaTypeGuiHelper[type - FirstGuiType].loadOp(stream, data);
  1338. break;
  1339. case QMetaType::QSizePolicy:
  1340. if (!qMetaTypeWidgetsHelper)
  1341. return false;
  1342. qMetaTypeWidgetsHelper[type - FirstWidgetsType].loadOp(stream, data);
  1343. break;
  1344. case QMetaType::QUuid:
  1345. stream >> *static_cast< NS(QUuid)*>(data);
  1346. break;
  1347. default: {
  1348. const QVector<QCustomTypeInfo> * const ct = customTypes();
  1349. if (!ct)
  1350. return false;
  1351. LoadOperator loadOp = 0;
  1352. {
  1353. QReadLocker locker(customTypesLock());
  1354. loadOp = ct->at(type - User).loadOp;
  1355. }
  1356. if (!loadOp)
  1357. return false;
  1358. loadOp(stream