PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/x33n/phantomjs
C++ | 1762 lines | 1071 code | 130 blank | 561 comment | 108 complexity | b48aeef7ef209246510f83273404c1f6 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Copyright (C) 2013 Olivier Goffart <ogoffart@woboq.com>
  5. ** Contact: http://www.qt-project.org/legal
  6. **
  7. ** This file is part of the QtCore module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** Commercial License Usage
  11. ** Licensees holding valid commercial Qt licenses may use this file in
  12. ** accordance with the commercial license agreement provided with the
  13. ** Software or, alternatively, in accordance with the terms contained in
  14. ** a written agreement between you and Digia. For licensing terms and
  15. ** conditions see http://qt.digia.com/licensing. For further information
  16. ** use the contact form at http://qt.digia.com/contact-us.
  17. **
  18. ** GNU Lesser General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU Lesser
  20. ** General Public License version 2.1 as published by the Free Software
  21. ** Foundation and appearing in the file LICENSE.LGPL included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU Lesser General Public License version 2.1 requirements
  24. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  25. **
  26. ** In addition, as a special exception, Digia gives you certain additional
  27. ** rights. These rights are described in the Digia Qt LGPL Exception
  28. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  29. **
  30. ** GNU General Public License Usage
  31. ** Alternatively, this file may be used under the terms of the GNU
  32. ** General Public License version 3.0 as published by the Free Software
  33. ** Foundation and appearing in the file LICENSE.GPL included in the
  34. ** packaging of this file. Please review the following information to
  35. ** ensure the GNU General Public License version 3.0 requirements will be
  36. ** met: http://www.gnu.org/copyleft/gpl.html.
  37. **
  38. **
  39. ** $QT_END_LICENSE$
  40. **
  41. ****************************************************************************/
  42. #include "qvariant.h"
  43. #include "qbitarray.h"
  44. #include "qbytearray.h"
  45. #include "qdatastream.h"
  46. #include "qdebug.h"
  47. #include "qmap.h"
  48. #include "qdatetime.h"
  49. #include "qeasingcurve.h"
  50. #include "qlist.h"
  51. #include "qregularexpression.h"
  52. #include "qstring.h"
  53. #include "qstringlist.h"
  54. #include "qurl.h"
  55. #include "qlocale.h"
  56. #include "quuid.h"
  57. #ifndef QT_BOOTSTRAPPED
  58. #include "qabstractitemmodel.h"
  59. #include "qjsonvalue.h"
  60. #include "qjsonobject.h"
  61. #include "qjsonarray.h"
  62. #include "qjsondocument.h"
  63. #endif
  64. #include "private/qvariant_p.h"
  65. #include "qmetatype_p.h"
  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. #include <float.h>
  73. QT_BEGIN_NAMESPACE
  74. #ifndef DBL_DIG
  75. # define DBL_DIG 10
  76. #endif
  77. #ifndef FLT_DIG
  78. # define FLT_DIG 6
  79. #endif
  80. namespace {
  81. class HandlersManager
  82. {
  83. static const QVariant::Handler *Handlers[QModulesPrivate::ModulesCount];
  84. public:
  85. const QVariant::Handler *operator[] (const uint typeId) const
  86. {
  87. return Handlers[QModulesPrivate::moduleForType(typeId)];
  88. }
  89. void registerHandler(const QModulesPrivate::Names name, const QVariant::Handler *handler)
  90. {
  91. Handlers[name] = handler;
  92. }
  93. };
  94. } // namespace
  95. namespace {
  96. struct CoreTypesFilter {
  97. template<typename T>
  98. struct Acceptor {
  99. static const bool IsAccepted = QModulesPrivate::QTypeModuleInfo<T>::IsCore && QtMetaTypePrivate::TypeDefinition<T>::IsAvailable;
  100. };
  101. };
  102. } // annonymous
  103. namespace { // annonymous used to hide QVariant handlers
  104. static void construct(QVariant::Private *x, const void *copy)
  105. {
  106. QVariantConstructor<CoreTypesFilter> constructor(x, copy);
  107. QMetaTypeSwitcher::switcher<void>(constructor, x->type, 0);
  108. }
  109. static void clear(QVariant::Private *d)
  110. {
  111. QVariantDestructor<CoreTypesFilter> cleaner(d);
  112. QMetaTypeSwitcher::switcher<void>(cleaner, d->type, 0);
  113. }
  114. static bool isNull(const QVariant::Private *d)
  115. {
  116. QVariantIsNull<CoreTypesFilter> isNull(d);
  117. return QMetaTypeSwitcher::switcher<bool>(isNull, d->type, 0);
  118. }
  119. /*!
  120. \internal
  121. Compares \a a to \a b. The caller guarantees that \a a and \a b
  122. are of the same type.
  123. */
  124. static bool compare(const QVariant::Private *a, const QVariant::Private *b)
  125. {
  126. QVariantComparator<CoreTypesFilter> comparator(a, b);
  127. return QMetaTypeSwitcher::switcher<bool>(comparator, a->type, 0);
  128. }
  129. /*!
  130. \internal
  131. */
  132. static qlonglong qMetaTypeNumber(const QVariant::Private *d)
  133. {
  134. switch (d->type) {
  135. case QMetaType::Int:
  136. return d->data.i;
  137. case QMetaType::LongLong:
  138. return d->data.ll;
  139. case QMetaType::Char:
  140. return qlonglong(d->data.c);
  141. case QMetaType::SChar:
  142. return qlonglong(d->data.sc);
  143. case QMetaType::Short:
  144. return qlonglong(d->data.s);
  145. case QMetaType::Long:
  146. return qlonglong(d->data.l);
  147. case QMetaType::Float:
  148. return qRound64(d->data.f);
  149. case QVariant::Double:
  150. return qRound64(d->data.d);
  151. #ifndef QT_BOOTSTRAPPED
  152. case QMetaType::QJsonValue:
  153. return v_cast<QJsonValue>(d)->toDouble();
  154. #endif
  155. }
  156. Q_ASSERT(false);
  157. return 0;
  158. }
  159. static qulonglong qMetaTypeUNumber(const QVariant::Private *d)
  160. {
  161. switch (d->type) {
  162. case QVariant::UInt:
  163. return d->data.u;
  164. case QVariant::ULongLong:
  165. return d->data.ull;
  166. case QMetaType::UChar:
  167. return d->data.uc;
  168. case QMetaType::UShort:
  169. return d->data.us;
  170. case QMetaType::ULong:
  171. return d->data.ul;
  172. }
  173. Q_ASSERT(false);
  174. return 0;
  175. }
  176. static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok)
  177. {
  178. *ok = true;
  179. switch (uint(d->type)) {
  180. case QVariant::String:
  181. return v_cast<QString>(d)->toLongLong(ok);
  182. case QVariant::Char:
  183. return v_cast<QChar>(d)->unicode();
  184. case QVariant::ByteArray:
  185. return v_cast<QByteArray>(d)->toLongLong(ok);
  186. case QVariant::Bool:
  187. return qlonglong(d->data.b);
  188. case QVariant::Double:
  189. case QVariant::Int:
  190. case QMetaType::Char:
  191. case QMetaType::SChar:
  192. case QMetaType::Short:
  193. case QMetaType::Long:
  194. case QMetaType::Float:
  195. case QMetaType::LongLong:
  196. case QMetaType::QJsonValue:
  197. return qMetaTypeNumber(d);
  198. case QVariant::ULongLong:
  199. case QVariant::UInt:
  200. case QMetaType::UChar:
  201. case QMetaType::UShort:
  202. case QMetaType::ULong:
  203. return qlonglong(qMetaTypeUNumber(d));
  204. }
  205. if (QMetaType::typeFlags(d->type) & QMetaType::IsEnumeration) {
  206. switch (QMetaType::sizeOf(d->type)) {
  207. case 1:
  208. return d->is_shared ? *reinterpret_cast<signed char *>(d->data.shared->ptr) : d->data.sc;
  209. case 2:
  210. return d->is_shared ? *reinterpret_cast<qint16 *>(d->data.shared->ptr) : d->data.s;
  211. case 4:
  212. return d->is_shared ? *reinterpret_cast<qint32 *>(d->data.shared->ptr) : d->data.i;
  213. case 8:
  214. return d->is_shared ? *reinterpret_cast<qint64 *>(d->data.shared->ptr) : d->data.ll;
  215. }
  216. }
  217. *ok = false;
  218. return Q_INT64_C(0);
  219. }
  220. static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok)
  221. {
  222. *ok = true;
  223. switch (uint(d->type)) {
  224. case QVariant::String:
  225. return v_cast<QString>(d)->toULongLong(ok);
  226. case QVariant::Char:
  227. return v_cast<QChar>(d)->unicode();
  228. case QVariant::ByteArray:
  229. return v_cast<QByteArray>(d)->toULongLong(ok);
  230. case QVariant::Bool:
  231. return qulonglong(d->data.b);
  232. case QVariant::Double:
  233. case QVariant::Int:
  234. case QMetaType::Char:
  235. case QMetaType::SChar:
  236. case QMetaType::Short:
  237. case QMetaType::Long:
  238. case QMetaType::Float:
  239. case QMetaType::LongLong:
  240. case QMetaType::QJsonValue:
  241. return qulonglong(qMetaTypeNumber(d));
  242. case QVariant::ULongLong:
  243. case QVariant::UInt:
  244. case QMetaType::UChar:
  245. case QMetaType::UShort:
  246. case QMetaType::ULong:
  247. return qMetaTypeUNumber(d);
  248. }
  249. if (QMetaType::typeFlags(d->type) & QMetaType::IsEnumeration) {
  250. switch (QMetaType::sizeOf(d->type)) {
  251. case 1:
  252. return d->is_shared ? *reinterpret_cast<uchar *>(d->data.shared->ptr) : d->data.uc;
  253. case 2:
  254. return d->is_shared ? *reinterpret_cast<quint16 *>(d->data.shared->ptr) : d->data.us;
  255. case 4:
  256. return d->is_shared ? *reinterpret_cast<quint32 *>(d->data.shared->ptr) : d->data.u;
  257. case 8:
  258. return d->is_shared ? *reinterpret_cast<qint64 *>(d->data.shared->ptr) : d->data.ull;
  259. }
  260. }
  261. *ok = false;
  262. return Q_UINT64_C(0);
  263. }
  264. template<typename TInput, typename LiteralWrapper>
  265. inline bool qt_convertToBool(const QVariant::Private *const d)
  266. {
  267. TInput str = v_cast<TInput>(d)->toLower();
  268. return !(str == LiteralWrapper("0") || str == LiteralWrapper("false") || str.isEmpty());
  269. }
  270. /*!
  271. \internal
  272. Returns the internal data pointer from \a d.
  273. */
  274. static const void *constData(const QVariant::Private &d)
  275. {
  276. return d.is_shared ? d.data.shared->ptr : reinterpret_cast<const void *>(&d.data.c);
  277. }
  278. /*!
  279. \internal
  280. Converts \a d to type \a t, which is placed in \a result.
  281. */
  282. static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
  283. {
  284. Q_ASSERT(d->type != uint(t));
  285. Q_ASSERT(result);
  286. if (d->type >= QMetaType::User || t >= QMetaType::User) {
  287. const bool isOk = QMetaType::convert(constData(*d), d->type, result, t);
  288. if (ok)
  289. *ok = isOk;
  290. if (isOk)
  291. return true;
  292. }
  293. bool dummy;
  294. if (!ok)
  295. ok = &dummy;
  296. switch (uint(t)) {
  297. #ifndef QT_BOOTSTRAPPED
  298. case QVariant::Url:
  299. switch (d->type) {
  300. case QVariant::String:
  301. *static_cast<QUrl *>(result) = QUrl(*v_cast<QString>(d));
  302. break;
  303. default:
  304. return false;
  305. }
  306. break;
  307. #endif
  308. case QVariant::String: {
  309. QString *str = static_cast<QString *>(result);
  310. switch (d->type) {
  311. case QVariant::Char:
  312. *str = QString(*v_cast<QChar>(d));
  313. break;
  314. case QMetaType::Char:
  315. case QMetaType::SChar:
  316. case QMetaType::UChar:
  317. *str = QChar::fromLatin1(d->data.c);
  318. break;
  319. case QMetaType::Short:
  320. case QMetaType::Long:
  321. case QVariant::Int:
  322. case QVariant::LongLong:
  323. *str = QString::number(qMetaTypeNumber(d));
  324. break;
  325. case QVariant::UInt:
  326. case QVariant::ULongLong:
  327. case QMetaType::UShort:
  328. case QMetaType::ULong:
  329. *str = QString::number(qMetaTypeUNumber(d));
  330. break;
  331. case QMetaType::Float:
  332. *str = QString::number(d->data.f, 'g', FLT_DIG);
  333. break;
  334. case QVariant::Double:
  335. *str = QString::number(d->data.d, 'g', DBL_DIG);
  336. break;
  337. #if !defined(QT_NO_DATESTRING)
  338. case QVariant::Date:
  339. *str = v_cast<QDate>(d)->toString(Qt::ISODate);
  340. break;
  341. case QVariant::Time:
  342. *str = v_cast<QTime>(d)->toString(Qt::ISODate);
  343. break;
  344. case QVariant::DateTime:
  345. *str = v_cast<QDateTime>(d)->toString(Qt::ISODate);
  346. break;
  347. #endif
  348. case QVariant::Bool:
  349. *str = QLatin1String(d->data.b ? "true" : "false");
  350. break;
  351. case QVariant::ByteArray:
  352. *str = QString::fromUtf8(v_cast<QByteArray>(d)->constData());
  353. break;
  354. case QVariant::StringList:
  355. if (v_cast<QStringList>(d)->count() == 1)
  356. *str = v_cast<QStringList>(d)->at(0);
  357. break;
  358. #ifndef QT_BOOTSTRAPPED
  359. case QVariant::Url:
  360. *str = v_cast<QUrl>(d)->toString();
  361. break;
  362. case QMetaType::QJsonValue:
  363. *str = v_cast<QJsonValue>(d)->toString();
  364. break;
  365. #endif
  366. case QVariant::Uuid:
  367. *str = v_cast<QUuid>(d)->toString();
  368. break;
  369. default:
  370. return false;
  371. }
  372. break;
  373. }
  374. case QVariant::Char: {
  375. QChar *c = static_cast<QChar *>(result);
  376. switch (d->type) {
  377. case QVariant::Int:
  378. case QVariant::LongLong:
  379. case QMetaType::Char:
  380. case QMetaType::SChar:
  381. case QMetaType::Short:
  382. case QMetaType::Long:
  383. case QMetaType::Float:
  384. *c = QChar(ushort(qMetaTypeNumber(d)));
  385. break;
  386. case QVariant::UInt:
  387. case QVariant::ULongLong:
  388. case QMetaType::UChar:
  389. case QMetaType::UShort:
  390. case QMetaType::ULong:
  391. *c = QChar(ushort(qMetaTypeUNumber(d)));
  392. break;
  393. default:
  394. return false;
  395. }
  396. break;
  397. }
  398. #ifndef QT_NO_GEOM_VARIANT
  399. case QVariant::Size: {
  400. QSize *s = static_cast<QSize *>(result);
  401. switch (d->type) {
  402. case QVariant::SizeF:
  403. *s = v_cast<QSizeF>(d)->toSize();
  404. break;
  405. default:
  406. return false;
  407. }
  408. break;
  409. }
  410. case QVariant::SizeF: {
  411. QSizeF *s = static_cast<QSizeF *>(result);
  412. switch (d->type) {
  413. case QVariant::Size:
  414. *s = QSizeF(*(v_cast<QSize>(d)));
  415. break;
  416. default:
  417. return false;
  418. }
  419. break;
  420. }
  421. case QVariant::Line: {
  422. QLine *s = static_cast<QLine *>(result);
  423. switch (d->type) {
  424. case QVariant::LineF:
  425. *s = v_cast<QLineF>(d)->toLine();
  426. break;
  427. default:
  428. return false;
  429. }
  430. break;
  431. }
  432. case QVariant::LineF: {
  433. QLineF *s = static_cast<QLineF *>(result);
  434. switch (d->type) {
  435. case QVariant::Line:
  436. *s = QLineF(*(v_cast<QLine>(d)));
  437. break;
  438. default:
  439. return false;
  440. }
  441. break;
  442. }
  443. #endif
  444. case QVariant::StringList:
  445. if (d->type == QVariant::List) {
  446. QStringList *slst = static_cast<QStringList *>(result);
  447. const QVariantList *list = v_cast<QVariantList >(d);
  448. for (int i = 0; i < list->size(); ++i)
  449. slst->append(list->at(i).toString());
  450. } else if (d->type == QVariant::String) {
  451. QStringList *slst = static_cast<QStringList *>(result);
  452. *slst = QStringList(*v_cast<QString>(d));
  453. } else {
  454. return false;
  455. }
  456. break;
  457. case QVariant::Date: {
  458. QDate *dt = static_cast<QDate *>(result);
  459. if (d->type == QVariant::DateTime)
  460. *dt = v_cast<QDateTime>(d)->date();
  461. #ifndef QT_NO_DATESTRING
  462. else if (d->type == QVariant::String)
  463. *dt = QDate::fromString(*v_cast<QString>(d), Qt::ISODate);
  464. #endif
  465. else
  466. return false;
  467. return dt->isValid();
  468. }
  469. case QVariant::Time: {
  470. QTime *t = static_cast<QTime *>(result);
  471. switch (d->type) {
  472. case QVariant::DateTime:
  473. *t = v_cast<QDateTime>(d)->time();
  474. break;
  475. #ifndef QT_NO_DATESTRING
  476. case QVariant::String:
  477. *t = QTime::fromString(*v_cast<QString>(d), Qt::ISODate);
  478. break;
  479. #endif
  480. default:
  481. return false;
  482. }
  483. return t->isValid();
  484. }
  485. case QVariant::DateTime: {
  486. QDateTime *dt = static_cast<QDateTime *>(result);
  487. switch (d->type) {
  488. #ifndef QT_NO_DATESTRING
  489. case QVariant::String:
  490. *dt = QDateTime::fromString(*v_cast<QString>(d), Qt::ISODate);
  491. break;
  492. #endif
  493. case QVariant::Date:
  494. *dt = QDateTime(*v_cast<QDate>(d));
  495. break;
  496. default:
  497. return false;
  498. }
  499. return dt->isValid();
  500. }
  501. case QVariant::ByteArray: {
  502. QByteArray *ba = static_cast<QByteArray *>(result);
  503. switch (d->type) {
  504. case QVariant::String:
  505. *ba = v_cast<QString>(d)->toUtf8();
  506. break;
  507. case QVariant::Double:
  508. *ba = QByteArray::number(d->data.d, 'g', DBL_DIG);
  509. break;
  510. case QMetaType::Float:
  511. *ba = QByteArray::number(d->data.f, 'g', FLT_DIG);
  512. break;
  513. case QMetaType::Char:
  514. case QMetaType::SChar:
  515. case QMetaType::UChar:
  516. *ba = QByteArray(1, d->data.c);
  517. break;
  518. case QVariant::Int:
  519. case QVariant::LongLong:
  520. case QMetaType::Short:
  521. case QMetaType::Long:
  522. *ba = QByteArray::number(qMetaTypeNumber(d));
  523. break;
  524. case QVariant::UInt:
  525. case QVariant::ULongLong:
  526. case QMetaType::UShort:
  527. case QMetaType::ULong:
  528. *ba = QByteArray::number(qMetaTypeUNumber(d));
  529. break;
  530. case QVariant::Bool:
  531. *ba = QByteArray(d->data.b ? "true" : "false");
  532. break;
  533. default:
  534. return false;
  535. }
  536. }
  537. break;
  538. case QMetaType::Short:
  539. *static_cast<short *>(result) = short(qConvertToNumber(d, ok));
  540. return *ok;
  541. case QMetaType::Long:
  542. *static_cast<long *>(result) = long(qConvertToNumber(d, ok));
  543. return *ok;
  544. case QMetaType::UShort:
  545. *static_cast<ushort *>(result) = ushort(qConvertToUnsignedNumber(d, ok));
  546. return *ok;
  547. case QMetaType::ULong:
  548. *static_cast<ulong *>(result) = ulong(qConvertToUnsignedNumber(d, ok));
  549. return *ok;
  550. case QVariant::Int:
  551. *static_cast<int *>(result) = int(qConvertToNumber(d, ok));
  552. return *ok;
  553. case QVariant::UInt:
  554. *static_cast<uint *>(result) = uint(qConvertToUnsignedNumber(d, ok));
  555. return *ok;
  556. case QVariant::LongLong:
  557. *static_cast<qlonglong *>(result) = qConvertToNumber(d, ok);
  558. return *ok;
  559. case QVariant::ULongLong: {
  560. *static_cast<qulonglong *>(result) = qConvertToUnsignedNumber(d, ok);
  561. return *ok;
  562. }
  563. case QMetaType::SChar: {
  564. signed char s = qConvertToNumber(d, ok);
  565. *static_cast<signed char*>(result) = s;
  566. return *ok;
  567. }
  568. case QMetaType::UChar: {
  569. *static_cast<uchar *>(result) = qConvertToUnsignedNumber(d, ok);
  570. return *ok;
  571. }
  572. case QVariant::Bool: {
  573. bool *b = static_cast<bool *>(result);
  574. switch(d->type) {
  575. case QVariant::ByteArray:
  576. *b = qt_convertToBool<QByteArray, QByteArray>(d);
  577. break;
  578. case QVariant::String:
  579. *b = qt_convertToBool<QString, QLatin1String>(d);
  580. break;
  581. case QVariant::Char:
  582. *b = !v_cast<QChar>(d)->isNull();
  583. break;
  584. case QVariant::Double:
  585. case QVariant::Int:
  586. case QVariant::LongLong:
  587. case QMetaType::Char:
  588. case QMetaType::SChar:
  589. case QMetaType::Short:
  590. case QMetaType::Long:
  591. case QMetaType::Float:
  592. *b = qMetaTypeNumber(d) != Q_INT64_C(0);
  593. break;
  594. case QVariant::UInt:
  595. case QVariant::ULongLong:
  596. case QMetaType::UChar:
  597. case QMetaType::UShort:
  598. case QMetaType::ULong:
  599. *b = qMetaTypeUNumber(d) != Q_UINT64_C(0);
  600. break;
  601. #ifndef QT_BOOTSTRAPPED
  602. case QMetaType::QJsonValue:
  603. *b = v_cast<QJsonValue>(d)->toBool();
  604. break;
  605. #endif
  606. default:
  607. *b = false;
  608. return false;
  609. }
  610. break;
  611. }
  612. case QVariant::Double: {
  613. double *f = static_cast<double *>(result);
  614. switch (d->type) {
  615. case QVariant::String:
  616. *f = v_cast<QString>(d)->toDouble(ok);
  617. break;
  618. case QVariant::ByteArray:
  619. *f = v_cast<QByteArray>(d)->toDouble(ok);
  620. break;
  621. case QVariant::Bool:
  622. *f = double(d->data.b);
  623. break;
  624. case QMetaType::Float:
  625. *f = double(d->data.f);
  626. break;
  627. case QVariant::LongLong:
  628. case QVariant::Int:
  629. case QMetaType::Char:
  630. case QMetaType::SChar:
  631. case QMetaType::Short:
  632. case QMetaType::Long:
  633. *f = double(qMetaTypeNumber(d));
  634. break;
  635. case QVariant::UInt:
  636. case QVariant::ULongLong:
  637. case QMetaType::UChar:
  638. case QMetaType::UShort:
  639. case QMetaType::ULong:
  640. *f = double(qMetaTypeUNumber(d));
  641. break;
  642. #ifndef QT_BOOTSTRAPPED
  643. case QMetaType::QJsonValue:
  644. *f = v_cast<QJsonValue>(d)->toDouble();
  645. break;
  646. #endif
  647. default:
  648. *f = 0.0;
  649. return false;
  650. }
  651. break;
  652. }
  653. case QMetaType::Float: {
  654. float *f = static_cast<float *>(result);
  655. switch (d->type) {
  656. case QVariant::String:
  657. *f = v_cast<QString>(d)->toFloat(ok);
  658. break;
  659. case QVariant::ByteArray:
  660. *f = v_cast<QByteArray>(d)->toFloat(ok);
  661. break;
  662. case QVariant::Bool:
  663. *f = float(d->data.b);
  664. break;
  665. case QVariant::Double:
  666. *f = float(d->data.d);
  667. break;
  668. case QVariant::LongLong:
  669. case QVariant::Int:
  670. case QMetaType::Char:
  671. case QMetaType::SChar:
  672. case QMetaType::Short:
  673. case QMetaType::Long:
  674. *f = float(qMetaTypeNumber(d));
  675. break;
  676. case QVariant::UInt:
  677. case QVariant::ULongLong:
  678. case QMetaType::UChar:
  679. case QMetaType::UShort:
  680. case QMetaType::ULong:
  681. *f = float(qMetaTypeUNumber(d));
  682. break;
  683. #ifndef QT_BOOTSTRAPPED
  684. case QMetaType::QJsonValue:
  685. *f = v_cast<QJsonValue>(d)->toDouble();
  686. break;
  687. #endif
  688. default:
  689. *f = 0.0f;
  690. return false;
  691. }
  692. break;
  693. }
  694. case QVariant::List:
  695. if (d->type == QVariant::StringList) {
  696. QVariantList *lst = static_cast<QVariantList *>(result);
  697. const QStringList *slist = v_cast<QStringList>(d);
  698. for (int i = 0; i < slist->size(); ++i)
  699. lst->append(QVariant(slist->at(i)));
  700. } else if (qstrcmp(QMetaType::typeName(d->type), "QList<QVariant>") == 0) {
  701. *static_cast<QVariantList *>(result) =
  702. *static_cast<QList<QVariant> *>(d->data.shared->ptr);
  703. } else {
  704. return false;
  705. }
  706. break;
  707. case QVariant::Map:
  708. if (qstrcmp(QMetaType::typeName(d->type), "QMap<QString, QVariant>") == 0) {
  709. *static_cast<QVariantMap *>(result) =
  710. *static_cast<QMap<QString, QVariant> *>(d->data.shared->ptr);
  711. } else {
  712. return false;
  713. }
  714. break;
  715. case QVariant::Hash:
  716. if (qstrcmp(QMetaType::typeName(d->type), "QHash<QString, QVariant>") == 0) {
  717. *static_cast<QVariantHash *>(result) =
  718. *static_cast<QHash<QString, QVariant> *>(d->data.shared->ptr);
  719. } else {
  720. return false;
  721. }
  722. break;
  723. #ifndef QT_NO_GEOM_VARIANT
  724. case QVariant::Rect:
  725. if (d->type == QVariant::RectF)
  726. *static_cast<QRect *>(result) = (v_cast<QRectF>(d))->toRect();
  727. else
  728. return false;
  729. break;
  730. case QVariant::RectF:
  731. if (d->type == QVariant::Rect)
  732. *static_cast<QRectF *>(result) = *v_cast<QRect>(d);
  733. else
  734. return false;
  735. break;
  736. case QVariant::PointF:
  737. if (d->type == QVariant::Point)
  738. *static_cast<QPointF *>(result) = *v_cast<QPoint>(d);
  739. else
  740. return false;
  741. break;
  742. case QVariant::Point:
  743. if (d->type == QVariant::PointF)
  744. *static_cast<QPoint *>(result) = (v_cast<QPointF>(d))->toPoint();
  745. else
  746. return false;
  747. break;
  748. case QMetaType::Char:
  749. {
  750. *static_cast<qint8 *>(result) = qint8(qConvertToNumber(d, ok));
  751. return *ok;
  752. }
  753. #endif
  754. case QVariant::Uuid:
  755. switch (d->type) {
  756. case QVariant::String:
  757. *static_cast<QUuid *>(result) = QUuid(*v_cast<QString>(d));
  758. break;
  759. default:
  760. return false;
  761. }
  762. break;
  763. default:
  764. return false;
  765. }
  766. return true;
  767. }
  768. #if !defined(QT_NO_DEBUG_STREAM)
  769. static void streamDebug(QDebug dbg, const QVariant &v)
  770. {
  771. QVariant::Private *d = const_cast<QVariant::Private *>(&v.data_ptr());
  772. QVariantDebugStream<CoreTypesFilter> stream(dbg, d);
  773. QMetaTypeSwitcher::switcher<void>(stream, d->type, 0);
  774. }
  775. #endif
  776. const QVariant::Handler qt_kernel_variant_handler = {
  777. construct,
  778. clear,
  779. isNull,
  780. #ifndef QT_NO_DATASTREAM
  781. 0,
  782. 0,
  783. #endif
  784. compare,
  785. convert,
  786. 0,
  787. #if !defined(QT_NO_DEBUG_STREAM)
  788. streamDebug
  789. #else
  790. 0
  791. #endif
  792. };
  793. static void dummyConstruct(QVariant::Private *, const void *) { Q_ASSERT_X(false, "QVariant", "Trying to construct an unknown type"); }
  794. static void dummyClear(QVariant::Private *) { Q_ASSERT_X(false, "QVariant", "Trying to clear an unknown type"); }
  795. static bool dummyIsNull(const QVariant::Private *d) { Q_ASSERT_X(false, "QVariant::isNull", "Trying to call isNull on an unknown type"); return d->is_null; }
  796. static bool dummyCompare(const QVariant::Private *, const QVariant::Private *) { Q_ASSERT_X(false, "QVariant", "Trying to compare an unknown types"); return false; }
  797. static bool dummyConvert(const QVariant::Private *, int, void *, bool *) { Q_ASSERT_X(false, "QVariant", "Trying to convert an unknown type"); return false; }
  798. #if !defined(QT_NO_DEBUG_STREAM)
  799. static void dummyStreamDebug(QDebug, const QVariant &) { Q_ASSERT_X(false, "QVariant", "Trying to convert an unknown type"); }
  800. #endif
  801. const QVariant::Handler qt_dummy_variant_handler = {
  802. dummyConstruct,
  803. dummyClear,
  804. dummyIsNull,
  805. #ifndef QT_NO_DATASTREAM
  806. 0,
  807. 0,
  808. #endif
  809. dummyCompare,
  810. dummyConvert,
  811. 0,
  812. #if !defined(QT_NO_DEBUG_STREAM)
  813. dummyStreamDebug
  814. #else
  815. 0
  816. #endif
  817. };
  818. static void customConstruct(QVariant::Private *d, const void *copy)
  819. {
  820. const QMetaType type(d->type);
  821. const uint size = type.sizeOf();
  822. if (!size) {
  823. qWarning("Trying to construct an instance of an invalid type, type id: %i", d->type);
  824. d->type = QVariant::Invalid;
  825. return;
  826. }
  827. // this logic should match with QVariantIntegrator::CanUseInternalSpace
  828. if (size <= sizeof(QVariant::Private::Data)
  829. && (type.flags() & (QMetaType::MovableType | QMetaType::IsEnumeration))) {
  830. type.construct(&d->data.ptr, copy);
  831. d->is_shared = false;
  832. } else {
  833. void *ptr = type.create(copy);
  834. d->is_shared = true;
  835. d->data.shared = new QVariant::PrivateShared(ptr);
  836. }
  837. }
  838. static void customClear(QVariant::Private *d)
  839. {
  840. if (!d->is_shared) {
  841. QMetaType::destruct(d->type, &d->data.ptr);
  842. } else {
  843. QMetaType::destroy(d->type, d->data.shared->ptr);
  844. delete d->data.shared;
  845. }
  846. }
  847. static bool customIsNull(const QVariant::Private *d)
  848. {
  849. return d->is_null;
  850. }
  851. static bool customCompare(const QVariant::Private *a, const QVariant::Private *b)
  852. {
  853. const char *const typeName = QMetaType::typeName(a->type);
  854. if (Q_UNLIKELY(!typeName) && Q_LIKELY(!QMetaType::isRegistered(a->type)))
  855. qFatal("QVariant::compare: type %d unknown to QVariant.", a->type);
  856. const void *a_ptr = a->is_shared ? a->data.shared->ptr : &(a->data.ptr);
  857. const void *b_ptr = b->is_shared ? b->data.shared->ptr : &(b->data.ptr);
  858. uint typeNameLen = qstrlen(typeName);
  859. if (typeNameLen > 0 && typeName[typeNameLen - 1] == '*')
  860. return *static_cast<void *const *>(a_ptr) == *static_cast<void *const *>(b_ptr);
  861. if (a->is_null && b->is_null)
  862. return true;
  863. return !memcmp(a_ptr, b_ptr, QMetaType::sizeOf(a->type));
  864. }
  865. static bool customConvert(const QVariant::Private *d, int t, void *result, bool *ok)
  866. {
  867. if (d->type >= QMetaType::User || t >= QMetaType::User) {
  868. if (QMetaType::convert(constData(*d), d->type, result, t)) {
  869. if (ok)
  870. *ok = true;
  871. return true;
  872. }
  873. }
  874. return convert(d, t, result, ok);
  875. }
  876. #if !defined(QT_NO_DEBUG_STREAM)
  877. static void customStreamDebug(QDebug dbg, const QVariant &variant) {
  878. #ifndef QT_BOOTSTRAPPED
  879. QMetaType::TypeFlags flags = QMetaType::typeFlags(variant.userType());
  880. if (flags & QMetaType::PointerToQObject)
  881. dbg.nospace() << variant.value<QObject*>();
  882. #else
  883. Q_UNUSED(dbg);
  884. Q_UNUSED(variant);
  885. #endif
  886. }
  887. #endif
  888. const QVariant::Handler qt_custom_variant_handler = {
  889. customConstruct,
  890. customClear,
  891. customIsNull,
  892. #ifndef QT_NO_DATASTREAM
  893. 0,
  894. 0,
  895. #endif
  896. customCompare,
  897. customConvert,
  898. 0,
  899. #if !defined(QT_NO_DEBUG_STREAM)
  900. customStreamDebug
  901. #else
  902. 0
  903. #endif
  904. };
  905. } // annonymous used to hide QVariant handlers
  906. static HandlersManager handlerManager;
  907. Q_STATIC_ASSERT_X(!QModulesPrivate::Core, "Initialization assumes that ModulesNames::Core is 0");
  908. const QVariant::Handler *HandlersManager::Handlers[QModulesPrivate::ModulesCount]
  909. = { &qt_kernel_variant_handler, &qt_dummy_variant_handler,
  910. &qt_dummy_variant_handler, &qt_custom_variant_handler };
  911. Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler()
  912. {
  913. return &qt_kernel_variant_handler;
  914. }
  915. Q_CORE_EXPORT void QVariantPrivate::registerHandler(const int /* Modules::Names */name, const QVariant::Handler *handler)
  916. {
  917. handlerManager.registerHandler(static_cast<QModulesPrivate::Names>(name), handler);
  918. }
  919. /*!
  920. \class QVariant
  921. \inmodule QtCore
  922. \brief The QVariant class acts like a union for the most common Qt data types.
  923. \ingroup objectmodel
  924. \ingroup shared
  925. Because C++ forbids unions from including types that have
  926. non-default constructors or destructors, most interesting Qt
  927. classes cannot be used in unions. Without QVariant, this would be
  928. a problem for QObject::property() and for database work, etc.
  929. A QVariant object holds a single value of a single type() at a
  930. time. (Some type()s are multi-valued, for example a string list.)
  931. You can find out what type, T, the variant holds, convert it to a
  932. different type using convert(), get its value using one of the
  933. toT() functions (e.g., toSize()) and check whether the type can
  934. be converted to a particular type using canConvert().
  935. The methods named toT() (e.g., toInt(), toString()) are const. If
  936. you ask for the stored type, they return a copy of the stored
  937. object. If you ask for a type that can be generated from the
  938. stored type, toT() copies and converts and leaves the object
  939. itself unchanged. If you ask for a type that cannot be generated
  940. from the stored type, the result depends on the type; see the
  941. function documentation for details.
  942. Here is some example code to demonstrate the use of QVariant:
  943. \snippet code/src_corelib_kernel_qvariant.cpp 0
  944. You can even store QList<QVariant> and QMap<QString, QVariant>
  945. values in a variant, so you can easily construct arbitrarily
  946. complex data structures of arbitrary types. This is very powerful
  947. and versatile, but may prove less memory and speed efficient than
  948. storing specific types in standard data structures.
  949. QVariant also supports the notion of null values, where you can
  950. have a defined type with no value set. However, note that QVariant
  951. types can only be cast when they have had a value set.
  952. \snippet code/src_corelib_kernel_qvariant.cpp 1
  953. QVariant can be extended to support other types than those
  954. mentioned in the \l Type enum. See the \l QMetaType documentation
  955. for details.
  956. \section1 A Note on GUI Types
  957. Because QVariant is part of the Qt Core module, it cannot provide
  958. conversion functions to data types defined in Qt GUI, such as
  959. QColor, QImage, and QPixmap. In other words, there is no \c
  960. toColor() function. Instead, you can use the QVariant::value() or
  961. the qvariant_cast() template function. For example:
  962. \snippet code/src_corelib_kernel_qvariant.cpp 2
  963. The inverse conversion (e.g., from QColor to QVariant) is
  964. automatic for all data types supported by QVariant, including
  965. GUI-related types:
  966. \snippet code/src_corelib_kernel_qvariant.cpp 3
  967. \section1 Using canConvert() and convert() Consecutively
  968. When using canConvert() and convert() consecutively, it is possible for
  969. canConvert() to return true, but convert() to return false. This
  970. is typically because canConvert() only reports the general ability of
  971. QVariant to convert between types given suitable data; it is still
  972. possible to supply data which cannot actually be converted.
  973. For example, canConvert(Int) would return true when called on a variant
  974. containing a string because, in principle, QVariant is able to convert
  975. strings of numbers to integers.
  976. However, if the string contains non-numeric characters, it cannot be
  977. converted to an integer, and any attempt to convert it will fail.
  978. Hence, it is important to have both functions return true for a
  979. successful conversion.
  980. \sa QMetaType
  981. */
  982. /*!
  983. \obsolete Use QMetaType::Type instead
  984. \enum QVariant::Type
  985. This enum type defines the types of variable that a QVariant can
  986. contain.
  987. \value Invalid no type
  988. \value BitArray a QBitArray
  989. \value Bitmap a QBitmap
  990. \value Bool a bool
  991. \value Brush a QBrush
  992. \value ByteArray a QByteArray
  993. \value Char a QChar
  994. \value Color a QColor
  995. \value Cursor a QCursor
  996. \value Date a QDate
  997. \value DateTime a QDateTime
  998. \value Double a double
  999. \value EasingCurve a QEasingCurve
  1000. \value Uuid a QUuid
  1001. \value ModelIndex a QModelIndex
  1002. \value Font a QFont
  1003. \value Hash a QVariantHash
  1004. \value Icon a QIcon
  1005. \value Image a QImage
  1006. \value Int an int
  1007. \value KeySequence a QKeySequence
  1008. \value Line a QLine
  1009. \value LineF a QLineF
  1010. \value List a QVariantList
  1011. \value Locale a QLocale
  1012. \value LongLong a \l qlonglong
  1013. \value Map a QVariantMap
  1014. \value Matrix a QMatrix
  1015. \value Transform a QTransform
  1016. \value Matrix4x4 a QMatrix4x4
  1017. \value Palette a QPalette
  1018. \value Pen a QPen
  1019. \value Pixmap a QPixmap
  1020. \value Point a QPoint
  1021. \value PointF a QPointF
  1022. \value Polygon a QPolygon
  1023. \value PolygonF a QPolygonF
  1024. \value Quaternion a QQuaternion
  1025. \value Rect a QRect
  1026. \value RectF a QRectF
  1027. \value RegExp a QRegExp
  1028. \value RegularExpression a QRegularExpression
  1029. \value Region a QRegion
  1030. \value Size a QSize
  1031. \value SizeF a QSizeF
  1032. \value SizePolicy a QSizePolicy
  1033. \value String a QString
  1034. \value StringList a QStringList
  1035. \value TextFormat a QTextFormat
  1036. \value TextLength a QTextLength
  1037. \value Time a QTime
  1038. \value UInt a \l uint
  1039. \value ULongLong a \l qulonglong
  1040. \value Url a QUrl
  1041. \value Vector2D a QVector2D
  1042. \value Vector3D a QVector3D
  1043. \value Vector4D a QVector4D
  1044. \value UserType Base value for user-defined types.
  1045. \omitvalue LastGuiType
  1046. \omitvalue LastCoreType
  1047. \omitvalue LastType
  1048. */
  1049. /*!
  1050. \fn QVariant::QVariant(QVariant &&other)
  1051. Move-constructs a QVariant instance, making it point at the same
  1052. object that \a other was pointing to.
  1053. \since 5.2
  1054. */
  1055. /*!
  1056. \fn QVariant &QVariant::operator=(QVariant &&other)
  1057. Move-assigns \a other to this QVariant instance.
  1058. \since 5.2
  1059. */
  1060. /*!
  1061. \fn QVariant::QVariant()
  1062. Constructs an invalid variant.
  1063. */
  1064. /*!
  1065. \fn QVariant::QVariant(int typeId, const void *copy)
  1066. Constructs variant of type \a typeId, and initializes with
  1067. \a copy if \a copy is not 0.
  1068. Note that you have to pass the address of the variable you want stored.
  1069. Usually, you never have to use this constructor, use QVariant::fromValue()
  1070. instead to construct variants from the pointer types represented by
  1071. \c QMetaType::VoidStar, and \c QMetaType::QObjectStar.
  1072. \sa QVariant::fromValue(), QMetaType::Type
  1073. */
  1074. /*!
  1075. \fn QVariant::QVariant(Type type)
  1076. Constructs a null variant of type \a type.
  1077. */
  1078. /*!
  1079. \fn QVariant::create(int type, const void *copy)
  1080. \internal
  1081. Constructs a variant private of type \a type, and initializes with \a copy if
  1082. \a copy is not 0.
  1083. */
  1084. void QVariant::create(int type, const void *copy)
  1085. {
  1086. d.type = type;
  1087. handlerManager[type]->construct(&d, copy);
  1088. }
  1089. /*!
  1090. \fn QVariant::~QVariant()
  1091. Destroys the QVariant and the contained object.
  1092. Note that subclasses that reimplement clear() should reimplement
  1093. the destructor to call clear(). This destructor calls clear(), but
  1094. because it is the destructor, QVariant::clear() is called rather
  1095. than a subclass's clear().
  1096. */
  1097. QVariant::~QVariant()
  1098. {
  1099. if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type > Char))
  1100. handlerManager[d.type]->clear(&d);
  1101. }
  1102. /*!
  1103. \fn QVariant::QVariant(const QVariant &p)
  1104. Constructs a copy of the variant, \a p, passed as the argument to
  1105. this constructor.
  1106. */
  1107. QVariant::QVariant(const QVariant &p)
  1108. : d(p.d)
  1109. {
  1110. if (d.is_shared) {
  1111. d.data.shared->ref.ref();
  1112. } else if (p.d.type > Char) {
  1113. handlerManager[d.type]->construct(&d, p.constData());
  1114. d.is_null = p.d.is_null;
  1115. }
  1116. }
  1117. #ifndef QT_NO_DATASTREAM
  1118. /*!
  1119. Reads the variant from the data stream, \a s.
  1120. */
  1121. QVariant::QVariant(QDataStream &s)
  1122. {
  1123. d.is_null = true;
  1124. s >> *this;
  1125. }
  1126. #endif //QT_NO_DATASTREAM
  1127. /*!
  1128. \fn QVariant::QVariant(const QString &val)
  1129. Constructs a new variant with a string value, \a val.
  1130. */
  1131. /*!
  1132. \fn QVariant::QVariant(QLatin1String val)
  1133. Constructs a new variant with a string value, \a val.
  1134. */
  1135. /*!
  1136. \fn QVariant::QVariant(const char *val)
  1137. Constructs a new variant with a string value of \a val.
  1138. The variant creates a deep copy of \a val into a QString assuming
  1139. UTF-8 encoding on the input \a val.
  1140. Note that \a val is converted to a QString for storing in the
  1141. variant and QVariant::userType() will return QMetaType::QString for
  1142. the variant.
  1143. You can disable this operator by defining \c
  1144. QT_NO_CAST_FROM_ASCII when you compile your applications.
  1145. */
  1146. #ifndef QT_NO_CAST_FROM_ASCII
  1147. QVariant::QVariant(const char *val)
  1148. {
  1149. QString s = QString::fromUtf8(val);
  1150. create(String, &s);
  1151. }
  1152. #endif
  1153. /*!
  1154. \fn QVariant::QVariant(const QStringList &val)
  1155. Constructs a new variant with a string list value, \a val.
  1156. */
  1157. /*!
  1158. \fn QVariant::QVariant(const QMap<QString, QVariant> &val)
  1159. Constructs a new variant with a map of QVariants, \a val.
  1160. */
  1161. /*!
  1162. \fn QVariant::QVariant(const QHash<QString, QVariant> &val)
  1163. Constructs a new variant with a hash of QVariants, \a val.
  1164. */
  1165. /*!
  1166. \fn QVariant::QVariant(const QDate &val)
  1167. Constructs a new variant with a date value, \a val.
  1168. */
  1169. /*!
  1170. \fn QVariant::QVariant(const QTime &val)
  1171. Constructs a new variant with a time value, \a val.
  1172. */
  1173. /*!
  1174. \fn QVariant::QVariant(const QDateTime &val)
  1175. Constructs a new variant with a date/time value, \a val.
  1176. */
  1177. /*!
  1178. \since 4.7
  1179. \fn QVariant::QVariant(const QEasingCurve &val)
  1180. Constructs a new variant with an easing curve value, \a val.
  1181. */
  1182. /*!
  1183. \since 5.0
  1184. \fn QVariant::QVariant(const QUuid &val)
  1185. Constructs a new variant with an uuid value, \a val.
  1186. */
  1187. /*!
  1188. \since 5.0
  1189. \fn QVariant::QVariant(const QModelIndex &val)
  1190. Constructs a new variant with an modelIndex value, \a val.
  1191. */
  1192. /*!
  1193. \since 5.0
  1194. \fn QVariant::QVariant(const QJsonValue &val)
  1195. Constructs a new variant with a json value, \a val.
  1196. */
  1197. /*!
  1198. \since 5.0
  1199. \fn QVariant::QVariant(const QJsonObject &val)
  1200. Constructs a new variant with a json object value, \a val.
  1201. */
  1202. /*!
  1203. \since 5.0
  1204. \fn QVariant::QVariant(const QJsonArray &val)
  1205. Constructs a new variant with a json array value, \a val.
  1206. */
  1207. /*!
  1208. \since 5.0
  1209. \fn QVariant::QVariant(const QJsonDocument &val)
  1210. Constructs a new variant with a json document value, \a val.
  1211. */
  1212. /*!
  1213. \fn QVariant::QVariant(const QByteArray &val)
  1214. Constructs a new variant with a bytearray value, \a val.
  1215. */
  1216. /*!
  1217. \fn QVariant::QVariant(const QBitArray &val)
  1218. Constructs a new variant with a bitarray value, \a val.
  1219. */
  1220. /*!
  1221. \fn QVariant::QVariant(const QPoint &val)
  1222. Constructs a new variant with a point value of \a val.
  1223. */
  1224. /*!
  1225. \fn QVariant::QVariant(const QPointF &val)
  1226. Constructs a new variant with a point value of \a val.
  1227. */
  1228. /*!
  1229. \fn QVariant::QVariant(const QRectF &val)
  1230. Constructs a new variant with a rect value of \a val.
  1231. */
  1232. /*!
  1233. \fn QVariant::QVariant(const QLineF &val)
  1234. Constructs a new variant with a line value of \a val.
  1235. */
  1236. /*!
  1237. \fn QVariant::QVariant(const QLine &val)
  1238. Constructs a new variant with a line value of \a val.
  1239. */
  1240. /*!
  1241. \fn QVariant::QVariant(const QRect &val)
  1242. Constructs a new variant with a rect value of \a val.
  1243. */
  1244. /*!
  1245. \fn QVariant::QVariant(const QSize &val)
  1246. Constructs a new variant with a size value of \a val.
  1247. */
  1248. /*!
  1249. \fn QVariant::QVariant(const QSizeF &val)
  1250. Constructs a new variant with a size value of \a val.
  1251. */
  1252. /*!
  1253. \fn QVariant::QVariant(const QUrl &val)
  1254. Constructs a new variant with a url value of \a val.
  1255. */
  1256. /*!
  1257. \fn QVariant::QVariant(int val)
  1258. Constructs a new variant with an integer value, \a val.
  1259. */
  1260. /*!
  1261. \fn QVariant::QVariant(uint val)
  1262. Constructs a new variant with an unsigned integer value, \a val.
  1263. */
  1264. /*!
  1265. \fn QVariant::QVariant(qlonglong val)
  1266. Constructs a new variant with a long long integer value, \a val.
  1267. */
  1268. /*!
  1269. \fn QVariant::QVariant(qulonglong val)
  1270. Constructs a new variant with an unsigned long long integer value, \a val.
  1271. */
  1272. /*!
  1273. \fn QVariant::QVariant(bool val)
  1274. Constructs a new variant with a boolean value, \a val.
  1275. */
  1276. /*!
  1277. \fn QVariant::QVariant(double val)
  1278. Constructs a new variant with a floating point value, \a val.
  1279. */
  1280. /*!
  1281. \fn QVariant::QVariant(float val)
  1282. Constructs a new variant with a floating point value, \a val.
  1283. \since 4.6
  1284. */
  1285. /*!
  1286. \fn QVariant::QVariant(const QList<QVariant> &val)
  1287. Constructs a new variant with a list value, \a val.
  1288. */
  1289. /*!
  1290. \fn QVariant::QVariant(QChar c)
  1291. Constructs a new variant with a char value, \a c.
  1292. */
  1293. /*!
  1294. \fn QVariant::QVariant(const QLocale &l)
  1295. Constructs a new variant with a locale value, \a l.
  1296. */
  1297. /*!
  1298. \fn QVariant::QVariant(const QRegExp &regExp)
  1299. Constructs a new variant with the regexp value \a regExp.
  1300. */
  1301. /*!
  1302. \fn QVariant::QVariant(const QRegularExpression &re)
  1303. \since 5.0
  1304. Constructs a new variant with the regular expression value \a re.
  1305. */
  1306. QVariant::QVariant(Type type)
  1307. { create(type, 0); }
  1308. QVariant::QVariant(int typeId, const void *copy)
  1309. { create(typeId, copy); d.is_null = false; }
  1310. /*!
  1311. \internal
  1312. flags is true if it is a pointer type
  1313. */
  1314. QVariant::QVariant(int typeId, const void *copy, uint flags)
  1315. {
  1316. if (flags) { //type is a pointer type
  1317. d.type = typeId;
  1318. d.data.ptr = *reinterpret_cast<void *const*>(copy);
  1319. } else {
  1320. create(typeId, copy);
  1321. }
  1322. d.is_null = false;
  1323. }
  1324. QVariant::QVariant(int val)
  1325. : d(Int)
  1326. { d.data.i = val; }
  1327. QVariant::QVariant(uint val)
  1328. : d(UInt)
  1329. { d.data.u = val; }
  1330. QVariant::QVariant(qlonglong val)
  1331. : d(LongLong)
  1332. { d.data.ll = val; }
  1333. QVariant::QVariant(qulonglong val)
  1334. : d(ULongLong)
  1335. { d.data.ull = val; }
  1336. QVariant::QVariant(bool val)
  1337. : d(Bool)
  1338. { d.data.b = val; }
  1339. QVariant::QVariant(double val)
  1340. : d(Double)
  1341. { d.data.d = val; }
  1342. QVariant::QVariant(float val)
  1343. : d(QMetaType::Float)
  1344. { d.data.f = val; }
  1345. QVariant::QVariant(const QByteArray &val)
  1346. : d(ByteArray)
  1347. { v_construct<QByteArray>(&d, val); }
  1348. QVariant::QVariant(const QBitArray &val)
  1349. : d(BitArray)
  1350. { v_construct<QBitArray>(&d, val); }
  1351. QVariant::QVariant(const QString &val)
  1352. : d(String)
  1353. { v_construct<QString>(&d, val); }
  1354. QVariant::QVariant(QChar val)
  1355. : d(Char)
  1356. { v_construct<QChar>(&d, val); }
  1357. QVariant::QVariant(QLatin1String val)
  1358. : d(String)
  1359. { v_construct<QString>(&d, val); }
  1360. QVariant::QVariant(const QStringList &val)
  1361. : d(StringList)
  1362. { v_construct<QStringList>(&d, val); }
  1363. QVariant::QVariant(const QDate &val)
  1364. : d(Date)
  1365. { v_construct<QDate>(&d, val); }
  1366. QVariant::QVariant(const QTime &val)
  1367. : d(Time)
  1368. { v_construct<QTime>(&d, val); }
  1369. QVariant::QVariant(const QDateTime &val)
  1370. : d(DateTime)
  1371. { v_construct<QDateTime>(&d, val); }
  1372. #ifndef QT_BOOTSTRAPPED
  1373. QVariant::QVariant(const QEasingCurve &val)
  1374. : d(EasingCurve)
  1375. { v_construct<QEasingCurve>(&d, val); }
  1376. #endif
  1377. QVariant::QVariant(const QList<QVariant> &list)
  1378. : d(List)
  1379. { v_construct<QVariantList>(&d, list); }
  1380. QVariant::QVariant(const QMap<QString, QVariant> &map)
  1381. : d(Map)
  1382. { v_construct<QVariantMap>(&d, map); }
  1383. QVariant::QVariant(const QHash<QString, QVariant> &hash)
  1384. : d(Hash)
  1385. { v_construct<QVariantHash>(&d, hash); }
  1386. #ifndef QT_NO_GEOM_VARIANT
  1387. QVariant::QVariant(const QPoint &pt)
  1388. : d(Point)
  1389. { v_construct<QPoint>(&d, pt); }
  1390. QVariant::QVariant(const QPointF &pt)
  1391. : d(PointF)
  1392. { v_construct<QPointF>(&d, pt); }
  1393. QVariant::QVariant(const QRectF &r)
  1394. : d(RectF)
  1395. { v_construct<QRectF>(&d, r); }
  1396. QVariant::QVariant(const QLineF &l)
  1397. : d(LineF)
  1398. { v_construct<QLineF>(&d, l); }
  1399. QVariant::QVariant(const QLine &l)
  1400. : d(Line)
  1401. { v_construct<QLine>(&d, l); }
  1402. QVariant::QVariant(const QRect &r)
  1403. : d(Rect)
  1404. { v_construct<QRect>(&d, r); }
  1405. QVariant::QVariant(const QSize &s)
  1406. : d(Size)
  1407. { v_construct<QSize>(&d, s); }
  1408. QVariant::QVariant(const QSizeF &s)
  1409. : d(SizeF)
  1410. { v_construct<QSizeF>(&d, s); }
  1411. #endif
  1412. #ifndef QT_BOOTSTRAPPED
  1413. QVariant::QVariant(const QUrl &u)
  1414. : d(Url)
  1415. { v_construct<QUrl>(&d, u); }
  1416. #endif
  1417. QVariant::QVariant(const QLocale &l)
  1418. : d(Locale)
  1419. { v_construct<QLocale>(&d, l); }
  1420. #ifndef QT_NO_REGEXP
  1421. QVariant::QVariant(const QRegExp &regExp)
  1422. : d(RegExp)
  1423. { v_construct<QRegExp>(&d, regExp); }
  1424. #endif // QT_NO_REGEXP
  1425. #ifndef QT_BOOTSTRAPPED
  1426. #ifndef QT_NO_REGULAREXPRESSION
  1427. QVariant::QVariant(const QRegularExpression &re)
  1428. : d(RegularExpression)
  1429. { v_construct<QRegularExpression>(&d, re); }
  1430. #endif
  1431. QVariant::QVariant(const QUuid &uuid)
  1432. : d(Uuid)
  1433. { v_construct<QUuid>(&d, uuid); }
  1434. QVariant::QVariant(const QModelIndex &modelIndex)
  1435. : d(ModelIndex)
  1436. { v_construct<QModelIndex>(&d, modelIndex); }
  1437. QVariant::QVariant(const QJsonValue &jsonValue)
  1438. : d(QMetaType::QJsonValue)
  1439. { v_construct<QJsonValue>(&d, jsonValue); }
  1440. QVariant::QVariant(const QJsonObject &jsonObject)
  1441. : d(QMetaType::QJsonObject)
  1442. { v_construct<QJsonObject>(&d, jsonObject); }
  1443. QVariant::QVariant(const QJsonArray &jsonArray)
  1444. : d(QMetaType::QJsonArray)
  1445. { v_construct<QJsonArray>(&d, jsonArray); }
  1446. QVariant::QVariant(const QJsonDocument &jsonDocument)
  1447. : d(QMetaType::QJsonDocument)
  1448. { v_construct<QJsonDocument>(&d, jsonDocument); }
  1449. #endif // QT_BOOTSTRAPPED
  1450. /*!
  1451. Returns the storage type of the value stored in the variant.
  1452. Although this function is declared as returning QVariant::Type,
  1453. the return value should be interpreted as QMetaType::Type. In
  1454. particular, QVariant::UserType is returned here only if the value
  1455. is equal or greater than QMetaType::User.
  1456. Note that return values in the ranges QVariant::Char through
  1457. QVariant::RegExp and QVariant::Font through QVariant::Transform
  1458. correspond to the values in the ranges QMetaType::QChar through
  1459. QMetaType::QRegExp and QMetaType::QFont through QMetaType::QQuaternion.
  1460. Pay particular attention when working with char and QChar
  1461. variants. Note that there is no QVariant constructor specifically
  1462. for type char, but there is one for QChar. For a variant of type
  1463. QChar, this function returns QVariant::Char, which is the same as
  1464. QMetaType::QChar, but for a variant of type \c char, this function
  1465. returns QMetaType::Char, which is \e not the same as
  1466. QVariant::Char.
  1467. Also note that the types \c void*, \c long, \c short, \c unsigned
  1468. \c long, \c unsigned \c short, \c unsigned \c char, \c float, \c
  1469. QObject*, and \c QWidget* are represented in QMetaType::Type but
  1470. not in QVariant::Type, and they can be returned by this function.
  1471. However, they are considered to be user defined types when tested
  1472. against QVariant::Type.
  1473. To test whether an instance of QVariant contains a data type that
  1474. is compatible with the data type you are interested in, use
  1475. canConvert().
  1476. */
  1477. QVariant::Type QVariant::type() const
  1478. {
  1479. return d.type >= QMetaType::User ? UserType : static_cast<Type>(d.type);
  1480. }
  1481. /*!
  1482. Returns the storage type of the value stored in the variant. For
  1483. non-user types, this is the same as type().
  1484. \sa type()
  1485. */
  1486. int QVariant::userType() const
  1487. {
  1488. return d.type;
  1489. }
  1490. /*!
  1491. Assigns the value of the variant \a variant to this variant.
  1492. */
  1493. QVariant& QVariant::operator=(const QVariant &variant)
  1494. {
  1495. if (this == &variant)
  1496. return *this;
  1497. clear();
  1498. if (variant.d.is_shared) {
  1499. variant.d.data.shared->ref.ref();
  1500. d = variant.d;
  1501. } else if (variant.d.type > Char) {
  1502. d.type = variant.d.type;
  1503. handlerManager[d.type]->construct(&d, variant.constData());
  1504. d.is_null = variant.d.is_null;
  1505. } else {
  1506. d = variant.d;
  1507. }
  1508. return *this;
  1509. }
  1510. /*!
  1511. \fn void QVariant::swap(QVariant &other)
  1512. \since 4.8
  1513. Swaps variant \a other with this variant. This operation is very
  1514. fast and never fails.
  1515. */
  1516. /*!
  1517. \fn void QVariant::detach()
  1518. \internal
  1519. */
  1520. void QVariant::detach()
  1521. {
  1522. if (!d.is_shared || d.data.shared->ref.load() == 1)
  1523. return;
  1524. Private dd;
  1525. dd.type = d.type;
  1526. handlerManager[d.type]->construct(&dd, constData());
  1527. if (!d.data.shared->ref.deref())
  1528. handlerManager[d.type]->clear(&d);
  1529. d.data.shared = dd.data.shared;
  1530. }
  1531. /*!
  1532. \fn bool QVariant::isDetached() const
  1533. \internal
  1534. */
  1535. /*!
  1536. Returns the name of the type stored in the variant. The returned
  1537. strings describe the C++ datatype used to store the data: for
  1538. example, "QFont", "QString", or "QVariantList". An Invalid
  1539. variant returns 0.
  1540. */
  1541. const char