File indexing completed on 2025-01-18 10:07:41
0001
0002
0003
0004 #ifndef QVARIANT_H
0005 #define QVARIANT_H
0006
0007 #include <QtCore/qatomic.h>
0008 #include <QtCore/qcontainerfwd.h>
0009 #include <QtCore/qmetatype.h>
0010 #ifndef QT_NO_DEBUG_STREAM
0011 #include <QtCore/qdebug.h>
0012 #endif
0013
0014 #include <memory>
0015 #include <QtCore/q20type_traits.h>
0016 #include <QtCore/q23utility.h>
0017 #include <variant>
0018
0019 #if !defined(QT_LEAN_HEADERS) || QT_LEAN_HEADERS < 1
0020 # include <QtCore/qlist.h>
0021 # include <QtCore/qstringlist.h>
0022 # include <QtCore/qbytearraylist.h>
0023 # include <QtCore/qhash.h>
0024 # include <QtCore/qmap.h>
0025 # include <QtCore/qobject.h>
0026 #endif
0027
0028 QT_BEGIN_NAMESPACE
0029
0030 QT_ENABLE_P0846_SEMANTICS_FOR(get_if)
0031 QT_ENABLE_P0846_SEMANTICS_FOR(get)
0032
0033 class QBitArray;
0034 class QDataStream;
0035 class QDate;
0036 class QDateTime;
0037 class QEasingCurve;
0038 class QLine;
0039 class QLineF;
0040 class QLocale;
0041 class QModelIndex;
0042 class QPersistentModelIndex;
0043 class QPoint;
0044 class QPointF;
0045 class QRect;
0046 class QRectF;
0047 class QRegularExpression;
0048 class QSize;
0049 class QSizeF;
0050 class QTextFormat;
0051 class QTextLength;
0052 class QTime;
0053 class QTransform;
0054 class QUrl;
0055 class QVariant;
0056
0057 template<typename T>
0058 inline T qvariant_cast(const QVariant &);
0059
0060 namespace QtPrivate {
0061 template<> constexpr inline bool qIsRelocatable<QVariant> = true;
0062 }
0063 class Q_CORE_EXPORT QVariant
0064 {
0065 template <typename T, typename... Args>
0066 using if_constructible = std::enable_if_t<
0067 std::conjunction_v<
0068 std::is_copy_constructible<q20::remove_cvref_t<T>>,
0069 std::is_destructible<q20::remove_cvref_t<T>>,
0070 std::is_constructible<q20::remove_cvref_t<T>, Args...>
0071 >,
0072 bool>;
0073
0074 template <typename T>
0075 using if_rvalue = std::enable_if_t<!std::is_reference_v<T>, bool>;
0076
0077 struct CborValueStandIn { qint64 n; void *c; int t; };
0078 public:
0079 struct PrivateShared
0080 {
0081 private:
0082 inline PrivateShared() : ref(1) { }
0083 public:
0084 static int computeOffset(PrivateShared *ps, size_t align);
0085 static size_t computeAllocationSize(size_t size, size_t align);
0086 static PrivateShared *create(size_t size, size_t align);
0087 static void free(PrivateShared *p);
0088
0089 alignas(8) QAtomicInt ref;
0090 int offset;
0091
0092 const void *data() const { return reinterpret_cast<const uchar *>(this) + offset; }
0093 void *data() { return reinterpret_cast<uchar *>(this) + offset; }
0094 };
0095
0096 struct Private
0097 {
0098 static constexpr size_t MaxInternalSize = 3 * sizeof(void *);
0099 template <size_t S> static constexpr bool FitsInInternalSize = S <= MaxInternalSize;
0100 template<typename T> static constexpr bool CanUseInternalSpace =
0101 (QTypeInfo<T>::isRelocatable && FitsInInternalSize<sizeof(T)> && alignof(T) <= alignof(double));
0102 static constexpr bool canUseInternalSpace(const QtPrivate::QMetaTypeInterface *type)
0103 {
0104 Q_ASSERT(type);
0105 return QMetaType::TypeFlags(type->flags) & QMetaType::RelocatableType &&
0106 size_t(type->size) <= MaxInternalSize && size_t(type->alignment) <= alignof(double);
0107 }
0108
0109 union
0110 {
0111 uchar data[MaxInternalSize] = {};
0112 PrivateShared *shared;
0113 double _forAlignment;
0114 } data;
0115 quintptr is_shared : 1;
0116 quintptr is_null : 1;
0117 quintptr packedType : sizeof(QMetaType) * 8 - 2;
0118
0119 constexpr Private() noexcept : is_shared(false), is_null(true), packedType(0) {}
0120 explicit Private(const QtPrivate::QMetaTypeInterface *iface) noexcept;
0121 template <typename T> explicit Private(std::piecewise_construct_t, const T &t);
0122
0123 const void *storage() const
0124 { return is_shared ? data.shared->data() : &data.data; }
0125
0126
0127 template<typename T> const T &get() const
0128 { return *static_cast<const T *>(CanUseInternalSpace<T> ? &data.data : data.shared->data()); }
0129
0130 inline const QtPrivate::QMetaTypeInterface *typeInterface() const
0131 {
0132 return reinterpret_cast<const QtPrivate::QMetaTypeInterface *>(packedType << 2);
0133 }
0134
0135 inline QMetaType type() const
0136 {
0137 return QMetaType(typeInterface());
0138 }
0139 };
0140
0141 #if QT_DEPRECATED_SINCE(6, 0)
0142 enum QT_DEPRECATED_VERSION_X_6_0("Use QMetaType::Type instead.") Type
0143 {
0144 Invalid = QMetaType::UnknownType,
0145 Bool = QMetaType::Bool,
0146 Int = QMetaType::Int,
0147 UInt = QMetaType::UInt,
0148 LongLong = QMetaType::LongLong,
0149 ULongLong = QMetaType::ULongLong,
0150 Double = QMetaType::Double,
0151 Char = QMetaType::QChar,
0152 Map = QMetaType::QVariantMap,
0153 List = QMetaType::QVariantList,
0154 String = QMetaType::QString,
0155 StringList = QMetaType::QStringList,
0156 ByteArray = QMetaType::QByteArray,
0157 BitArray = QMetaType::QBitArray,
0158 Date = QMetaType::QDate,
0159 Time = QMetaType::QTime,
0160 DateTime = QMetaType::QDateTime,
0161 Url = QMetaType::QUrl,
0162 Locale = QMetaType::QLocale,
0163 Rect = QMetaType::QRect,
0164 RectF = QMetaType::QRectF,
0165 Size = QMetaType::QSize,
0166 SizeF = QMetaType::QSizeF,
0167 Line = QMetaType::QLine,
0168 LineF = QMetaType::QLineF,
0169 Point = QMetaType::QPoint,
0170 PointF = QMetaType::QPointF,
0171 #if QT_CONFIG(regularexpression)
0172 RegularExpression = QMetaType::QRegularExpression,
0173 #endif
0174 Hash = QMetaType::QVariantHash,
0175 #if QT_CONFIG(easingcurve)
0176 EasingCurve = QMetaType::QEasingCurve,
0177 #endif
0178 Uuid = QMetaType::QUuid,
0179 #if QT_CONFIG(itemmodel)
0180 ModelIndex = QMetaType::QModelIndex,
0181 PersistentModelIndex = QMetaType::QPersistentModelIndex,
0182 #endif
0183 LastCoreType = QMetaType::LastCoreType,
0184
0185 Font = QMetaType::QFont,
0186 Pixmap = QMetaType::QPixmap,
0187 Brush = QMetaType::QBrush,
0188 Color = QMetaType::QColor,
0189 Palette = QMetaType::QPalette,
0190 Image = QMetaType::QImage,
0191 Polygon = QMetaType::QPolygon,
0192 Region = QMetaType::QRegion,
0193 Bitmap = QMetaType::QBitmap,
0194 Cursor = QMetaType::QCursor,
0195 #if QT_CONFIG(shortcut)
0196 KeySequence = QMetaType::QKeySequence,
0197 #endif
0198 Pen = QMetaType::QPen,
0199 TextLength = QMetaType::QTextLength,
0200 TextFormat = QMetaType::QTextFormat,
0201 Transform = QMetaType::QTransform,
0202 Matrix4x4 = QMetaType::QMatrix4x4,
0203 Vector2D = QMetaType::QVector2D,
0204 Vector3D = QMetaType::QVector3D,
0205 Vector4D = QMetaType::QVector4D,
0206 Quaternion = QMetaType::QQuaternion,
0207 PolygonF = QMetaType::QPolygonF,
0208 Icon = QMetaType::QIcon,
0209 LastGuiType = QMetaType::LastGuiType,
0210
0211 SizePolicy = QMetaType::QSizePolicy,
0212
0213 UserType = QMetaType::User,
0214 LastType = 0xffffffff
0215 };
0216 #endif
0217 QVariant() noexcept : d() {}
0218 ~QVariant();
0219 explicit QVariant(QMetaType type, const void *copy = nullptr);
0220 QVariant(const QVariant &other);
0221
0222 private:
0223 template <typename T, typename ...Args>
0224 using is_noexcept_constructible = std::conjunction<
0225 std::bool_constant<Private::CanUseInternalSpace<T>>,
0226 std::is_nothrow_constructible<T, Args...>
0227 >;
0228
0229 public:
0230 template <typename T, typename... Args,
0231 if_constructible<T, Args...> = true>
0232 explicit QVariant(std::in_place_type_t<T>, Args&&... args)
0233 noexcept(is_noexcept_constructible<q20::remove_cvref_t<T>, Args...>::value)
0234 : QVariant(std::in_place, QMetaType::fromType<q20::remove_cvref_t<T>>() )
0235 {
0236 void *data = const_cast<void *>(constData());
0237 new (data) T(std::forward<Args>(args)...);
0238 }
0239
0240 template <typename T, typename U, typename... Args,
0241 if_constructible<T, std::initializer_list<U> &, Args...> = true>
0242 explicit QVariant(std::in_place_type_t<T>, std::initializer_list<U> il, Args&&... args)
0243 noexcept(is_noexcept_constructible<q20::remove_cvref_t<T>,
0244 std::initializer_list<U> &,
0245 Args...
0246 >::value)
0247 : QVariant(std::in_place, QMetaType::fromType<q20::remove_cvref_t<T>>())
0248 {
0249 char *data = static_cast<char *>(const_cast<void *>(constData()));
0250 new (data) T(il, std::forward<Args>(args)...);
0251 }
0252
0253
0254 QVariant(int i) noexcept;
0255 QVariant(uint ui) noexcept;
0256 QVariant(qlonglong ll) noexcept;
0257 QVariant(qulonglong ull) noexcept;
0258 QVariant(bool b) noexcept;
0259 QVariant(double d) noexcept;
0260 QVariant(float f) noexcept;
0261
0262
0263 QVariant(QChar qchar) noexcept;
0264 QVariant(QDate date) noexcept;
0265 QVariant(QTime time) noexcept;
0266 #ifndef QT_BOOTSTRAPPED
0267 QVariant(const QBitArray &bitarray) noexcept;
0268 #endif
0269 QVariant(const QByteArray &bytearray) noexcept;
0270 QVariant(const QDateTime &datetime) noexcept;
0271 QVariant(const QHash<QString, QVariant> &hash) noexcept;
0272 QVariant(const QJsonArray &jsonArray) noexcept;
0273 QVariant(const QJsonObject &jsonObject) noexcept;
0274 QVariant(const QList<QVariant> &list) noexcept;
0275 QVariant(const QLocale &locale) noexcept;
0276 QVariant(const QMap<QString, QVariant> &map) noexcept;
0277 QVariant(const QRegularExpression &re) noexcept;
0278 QVariant(const QString &string) noexcept;
0279 QVariant(const QStringList &stringlist) noexcept;
0280 QVariant(const QUrl &url) noexcept;
0281
0282
0283
0284 QVariant(const QJsonValue &jsonValue) noexcept(Private::FitsInInternalSize<sizeof(CborValueStandIn)>);
0285 QVariant(const QModelIndex &modelIndex) noexcept(Private::FitsInInternalSize<8 + 2 * sizeof(quintptr)>);
0286 QVariant(QUuid uuid) noexcept(Private::FitsInInternalSize<16>);
0287 #ifndef QT_NO_GEOM_VARIANT
0288 QVariant(QSize size) noexcept;
0289 QVariant(QSizeF size) noexcept(Private::FitsInInternalSize<sizeof(qreal) * 2>);
0290 QVariant(QPoint pt) noexcept;
0291 QVariant(QPointF pt) noexcept(Private::FitsInInternalSize<sizeof(qreal) * 2>);
0292 QVariant(QLine line) noexcept(Private::FitsInInternalSize<sizeof(int) * 4>);
0293 QVariant(QLineF line) noexcept(Private::FitsInInternalSize<sizeof(qreal) * 4>);
0294 QVariant(QRect rect) noexcept(Private::FitsInInternalSize<sizeof(int) * 4>);
0295 QVariant(QRectF rect) noexcept(Private::FitsInInternalSize<sizeof(qreal) * 4>);
0296 #endif
0297
0298
0299 QVariant(const QEasingCurve &easing) noexcept(false);
0300 QVariant(const QJsonDocument &jsonDocument) noexcept(false);
0301 QVariant(const QPersistentModelIndex &modelIndex) noexcept(false);
0302
0303 #ifndef QT_NO_CAST_FROM_ASCII
0304 QT_ASCII_CAST_WARN QVariant(const char *str) noexcept(false)
0305 : QVariant(QString::fromUtf8(str))
0306 {}
0307 #endif
0308 QVariant(QLatin1StringView string) noexcept(false);
0309
0310 #if !defined(Q_CC_GHS)
0311
0312 template <typename T,
0313 std::enable_if_t<std::disjunction_v<std::is_pointer<T>, std::is_member_pointer<T>>, bool> = false>
0314 QVariant(T) = delete;
0315 #else
0316 QVariant(const volatile void *) = delete;
0317 #endif
0318
0319 #if QT_CORE_REMOVED_SINCE(6, 5)
0320 QVariant(const QSize &size);
0321 QVariant(const QSizeF &size);
0322 QVariant(const QPoint &pt);
0323 QVariant(const QPointF &pt);
0324 QVariant(const QLine &line);
0325 QVariant(const QLineF &line);
0326 QVariant(const QRect &rect);
0327 QVariant(const QRectF &rect);
0328 QVariant(const QUuid &uuid);
0329 #endif
0330
0331 QVariant& operator=(const QVariant &other);
0332 inline QVariant(QVariant &&other) noexcept : d(other.d)
0333 { other.d = Private(); }
0334 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QVariant)
0335
0336 inline void swap(QVariant &other) noexcept { std::swap(d, other.d); }
0337
0338 int userType() const { return typeId(); }
0339 int typeId() const { return metaType().id(); }
0340
0341 const char *typeName() const;
0342 QMetaType metaType() const;
0343
0344 bool canConvert(QMetaType targetType) const
0345 { return QMetaType::canConvert(d.type(), targetType); }
0346 bool convert(QMetaType type);
0347
0348 bool canView(QMetaType targetType) const
0349 { return QMetaType::canView(d.type(), targetType); }
0350
0351 #if QT_DEPRECATED_SINCE(6, 0)
0352 QT_DEPRECATED_VERSION_6_0
0353 bool canConvert(int targetTypeId) const
0354 { return QMetaType::canConvert(d.type(), QMetaType(targetTypeId)); }
0355 QT_DEPRECATED_VERSION_6_0
0356 bool convert(int targetTypeId)
0357 { return convert(QMetaType(targetTypeId)); }
0358 #endif
0359
0360 inline bool isValid() const;
0361 bool isNull() const;
0362
0363 void clear();
0364
0365 void detach();
0366 inline bool isDetached() const;
0367
0368 int toInt(bool *ok = nullptr) const;
0369 uint toUInt(bool *ok = nullptr) const;
0370 qlonglong toLongLong(bool *ok = nullptr) const;
0371 qulonglong toULongLong(bool *ok = nullptr) const;
0372 bool toBool() const;
0373 double toDouble(bool *ok = nullptr) const;
0374 float toFloat(bool *ok = nullptr) const;
0375 qreal toReal(bool *ok = nullptr) const;
0376 QByteArray toByteArray() const;
0377 #ifndef QT_BOOTSTRAPPED
0378 QBitArray toBitArray() const;
0379 #endif
0380 QString toString() const;
0381 QStringList toStringList() const;
0382 QChar toChar() const;
0383 QDate toDate() const;
0384 QTime toTime() const;
0385 QDateTime toDateTime() const;
0386 QList<QVariant> toList() const;
0387 QMap<QString, QVariant> toMap() const;
0388 QHash<QString, QVariant> toHash() const;
0389
0390 #ifndef QT_NO_GEOM_VARIANT
0391 QPoint toPoint() const;
0392 QPointF toPointF() const;
0393 QRect toRect() const;
0394 QSize toSize() const;
0395 QSizeF toSizeF() const;
0396 QLine toLine() const;
0397 QLineF toLineF() const;
0398 QRectF toRectF() const;
0399 #endif
0400 QLocale toLocale() const;
0401 #if QT_CONFIG(regularexpression)
0402 QRegularExpression toRegularExpression() const;
0403 #endif
0404 #if QT_CONFIG(easingcurve)
0405 QEasingCurve toEasingCurve() const;
0406 #endif
0407 QUuid toUuid() const;
0408 #ifndef QT_BOOTSTRAPPED
0409 QUrl toUrl() const;
0410 QJsonValue toJsonValue() const;
0411 QJsonObject toJsonObject() const;
0412 QJsonArray toJsonArray() const;
0413 QJsonDocument toJsonDocument() const;
0414 #endif
0415 #if QT_CONFIG(itemmodel)
0416 QModelIndex toModelIndex() const;
0417 QPersistentModelIndex toPersistentModelIndex() const;
0418 #endif
0419
0420 #ifndef QT_NO_DATASTREAM
0421 void load(QDataStream &ds);
0422 void save(QDataStream &ds) const;
0423 #endif
0424 #if QT_DEPRECATED_SINCE(6, 0)
0425 QT_WARNING_PUSH
0426 QT_WARNING_DISABLE_DEPRECATED
0427 QT_DEPRECATED_VERSION_X_6_0("Use the constructor taking a QMetaType instead.")
0428 explicit QVariant(Type type)
0429 : QVariant(QMetaType(int(type)))
0430 {}
0431 QT_DEPRECATED_VERSION_X_6_0("Use typeId() or metaType().")
0432 Type type() const
0433 {
0434 int type = d.type().id();
0435 return type >= QMetaType::User ? UserType : static_cast<Type>(type);
0436 }
0437 QT_DEPRECATED_VERSION_6_0
0438 static const char *typeToName(int typeId)
0439 { return QMetaType(typeId).name(); }
0440 QT_DEPRECATED_VERSION_6_0
0441 static Type nameToType(const char *name)
0442 {
0443 int metaType = QMetaType::fromName(name).id();
0444 return metaType <= int(UserType) ? QVariant::Type(metaType) : UserType;
0445 }
0446 QT_WARNING_POP
0447 #endif
0448
0449 void *data();
0450 const void *constData() const
0451 { return d.storage(); }
0452 inline const void *data() const { return constData(); }
0453
0454 private:
0455 template <typename T>
0456 void verifySuitableForEmplace()
0457 {
0458 static_assert(!std::is_reference_v<T>,
0459 "QVariant does not support reference types");
0460 static_assert(!std::is_const_v<T>,
0461 "QVariant does not support const types");
0462 static_assert(std::is_copy_constructible_v<T>,
0463 "QVariant requires that the type is copyable");
0464 static_assert(std::is_destructible_v<T>,
0465 "QVariant requires that the type is destructible");
0466 }
0467
0468 template <typename T, typename... Args>
0469 T &emplaceImpl(Args&&... args)
0470 {
0471 verifySuitableForEmplace<T>();
0472 auto data = static_cast<T *>(prepareForEmplace(QMetaType::fromType<T>()));
0473 return *q20::construct_at(data, std::forward<Args>(args)...);
0474 }
0475
0476 public:
0477 template <typename T, typename... Args,
0478 if_constructible<T, Args...> = true>
0479 T &emplace(Args&&... args)
0480 {
0481 return emplaceImpl<T>(std::forward<Args>(args)...);
0482 }
0483
0484 template <typename T, typename U, typename... Args,
0485 if_constructible<T, std::initializer_list<U> &, Args...> = true>
0486 T &emplace(std::initializer_list<U> list, Args&&... args)
0487 {
0488 return emplaceImpl<T>(list, std::forward<Args>(args)...);
0489 }
0490
0491 template<typename T, typename = std::enable_if_t<!std::is_same_v<std::decay_t<T>, QVariant>>>
0492 void setValue(T &&avalue)
0493 {
0494 using VT = std::decay_t<T>;
0495 QMetaType metaType = QMetaType::fromType<VT>();
0496
0497 if (isDetached() && d.type() == metaType) {
0498 *reinterpret_cast<VT *>(const_cast<void *>(constData())) = std::forward<T>(avalue);
0499 d.is_null = false;
0500 } else {
0501 *this = QVariant::fromValue<VT>(std::forward<T>(avalue));
0502 }
0503 }
0504
0505 void setValue(const QVariant &avalue)
0506 {
0507 *this = avalue;
0508 }
0509
0510 void setValue(QVariant &&avalue)
0511 {
0512 *this = std::move(avalue);
0513 }
0514
0515 template<typename T>
0516 inline T value() const &
0517 { return qvariant_cast<T>(*this); }
0518
0519 template<typename T>
0520 inline T view()
0521 {
0522 T t{};
0523 QMetaType::view(metaType(), data(), QMetaType::fromType<T>(), &t);
0524 return t;
0525 }
0526
0527 template<typename T>
0528 inline T value() &&
0529 { return qvariant_cast<T>(std::move(*this)); }
0530
0531 template<typename T, if_rvalue<T> = true>
0532 #ifndef Q_QDOC
0533
0534
0535
0536 static inline auto fromValue(T &&value)
0537 noexcept(std::is_nothrow_copy_constructible_v<T> && Private::CanUseInternalSpace<T>)
0538 -> std::enable_if_t<std::conjunction_v<std::is_copy_constructible<T>,
0539 std::is_destructible<T>>, QVariant>
0540 #else
0541 static inline QVariant fromValue(T &&value)
0542 #endif
0543 {
0544
0545 using Type = std::remove_cv_t<T>;
0546 if constexpr (std::is_null_pointer_v<Type>)
0547 return QVariant::fromMetaType(QMetaType::fromType<std::nullptr_t>());
0548 else if constexpr (std::is_same_v<Type, QVariant>)
0549 return std::forward<T>(value);
0550 else if constexpr (std::is_same_v<Type, std::monostate>)
0551 return QVariant();
0552 QMetaType mt = QMetaType::fromType<Type>();
0553 mt.registerType();
0554
0555
0556
0557
0558 if constexpr (std::conjunction_v<std::is_move_constructible<Type>, std::negation<std::is_const<T>>>)
0559 return moveConstruct(QMetaType::fromType<Type>(), std::addressof(value));
0560 else
0561 return copyConstruct(mt, std::addressof(value));
0562 }
0563
0564 template<typename T>
0565 #ifndef Q_QDOC
0566 static inline auto fromValue(const T &value)
0567 noexcept(std::is_nothrow_copy_constructible_v<T> && Private::CanUseInternalSpace<T>)
0568 -> std::enable_if_t<std::is_copy_constructible_v<T> && std::is_destructible_v<T>, QVariant>
0569 #else
0570 static inline QVariant fromValue(const T &value)
0571 #endif
0572 {
0573 if constexpr (std::is_null_pointer_v<T>)
0574 return QVariant(QMetaType::fromType<std::nullptr_t>());
0575 else if constexpr (std::is_same_v<T, QVariant>)
0576 return value;
0577 else if constexpr (std::is_same_v<T, std::monostate>)
0578 return QVariant();
0579 return QVariant(QMetaType::fromType<T>(), std::addressof(value));
0580 }
0581
0582 template<typename... Types>
0583 static inline QVariant fromStdVariant(const std::variant<Types...> &value)
0584 {
0585 return fromStdVariantImpl(value);
0586 }
0587
0588 template<typename... Types>
0589 static QVariant fromStdVariant(std::variant<Types...> &&value)
0590 {
0591 return fromStdVariantImpl(std::move(value));
0592 }
0593
0594 static QVariant fromMetaType(QMetaType type, const void *copy = nullptr);
0595
0596 template<typename T>
0597 bool canConvert() const
0598 { return canConvert(QMetaType::fromType<T>()); }
0599
0600 template<typename T>
0601 bool canView() const
0602 { return canView(QMetaType::fromType<T>()); }
0603
0604 static QPartialOrdering compare(const QVariant &lhs, const QVariant &rhs);
0605
0606 private:
0607 template <typename StdVariant>
0608 static QVariant fromStdVariantImpl(StdVariant &&v)
0609 {
0610 if (Q_UNLIKELY(v.valueless_by_exception()))
0611 return QVariant();
0612 auto visitor = [](auto &&arg) {
0613 return QVariant::fromValue(q23::forward_like<StdVariant>(arg));
0614 };
0615 return std::visit(visitor, std::forward<StdVariant>(v));
0616 }
0617
0618 friend inline bool operator==(const QVariant &a, const QVariant &b)
0619 { return a.equals(b); }
0620 friend inline bool operator!=(const QVariant &a, const QVariant &b)
0621 { return !a.equals(b); }
0622 #ifndef QT_NO_DEBUG_STREAM
0623 template <typename T>
0624 friend auto operator<<(const QDebug &debug, const T &variant) -> std::enable_if_t<std::is_same_v<T, QVariant>, QDebug> {
0625 return variant.qdebugHelper(debug);
0626 }
0627 QDebug qdebugHelper(QDebug) const;
0628 #endif
0629
0630 template <typename T>
0631 friend T *get_if(QVariant *v) noexcept
0632 {
0633
0634 if (!v || v->d.type() != QMetaType::fromType<T>())
0635 return nullptr;
0636 return static_cast<T*>(v->data());
0637 }
0638 template <typename T>
0639 friend const T *get_if(const QVariant *v) noexcept
0640 {
0641
0642 if (!v || v->d.is_null || v->d.type() != QMetaType::fromType<T>())
0643 return nullptr;
0644 return static_cast<const T*>(v->data());
0645 }
0646
0647 #define Q_MK_GET(cvref) \
0648 template <typename T> \
0649 friend T cvref get(QVariant cvref v) \
0650 { \
0651 if constexpr (std::is_const_v<T cvref>) \
0652 Q_ASSERT(!v.d.is_null); \
0653 Q_ASSERT(v.d.type() == QMetaType::fromType<q20::remove_cvref_t<T>>()); \
0654 return static_cast<T cvref>(*get_if<T>(&v)); \
0655 } \
0656
0657 Q_MK_GET(&)
0658 Q_MK_GET(const &)
0659 Q_MK_GET(&&)
0660 Q_MK_GET(const &&)
0661 #undef Q_MK_GET
0662
0663 static QVariant moveConstruct(QMetaType type, void *data);
0664 static QVariant copyConstruct(QMetaType type, const void *data);
0665
0666 template<typename T>
0667 friend inline T qvariant_cast(const QVariant &);
0668 template<typename T>
0669 friend inline T qvariant_cast(QVariant &&);
0670
0671 protected:
0672 Private d;
0673 void create(int type, const void *copy);
0674 void create(QMetaType type, const void *copy);
0675 bool equals(const QVariant &other) const;
0676 bool convert(int type, void *ptr) const;
0677 bool view(int type, void *ptr);
0678
0679 private:
0680
0681 inline QVariant(void *) = delete;
0682
0683
0684
0685
0686
0687
0688 QVariant(QMetaType::Type) = delete;
0689
0690
0691 QVariant(std::in_place_t, QMetaType type);
0692
0693 void *prepareForEmplace(QMetaType type);
0694
0695
0696
0697
0698
0699
0700 QVariant(Qt::GlobalColor) = delete;
0701 QVariant(Qt::BrushStyle) = delete;
0702 QVariant(Qt::PenStyle) = delete;
0703 QVariant(Qt::CursorShape) = delete;
0704 #ifdef QT_NO_CAST_FROM_ASCII
0705
0706 inline QVariant(const char *) = delete;
0707 #endif
0708 public:
0709 typedef Private DataPtr;
0710 inline DataPtr &data_ptr() { return d; }
0711 inline const DataPtr &data_ptr() const { return d; }
0712 };
0713
0714 inline bool QVariant::isValid() const
0715 {
0716 return d.type().isValid();
0717 }
0718
0719 #ifndef QT_NO_DATASTREAM
0720 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &s, QVariant &p);
0721 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &s, const QVariant &p);
0722
0723 #if QT_DEPRECATED_SINCE(6, 0)
0724 QT_WARNING_PUSH
0725 QT_WARNING_DISABLE_DEPRECATED
0726 QT_DEPRECATED_VERSION_6_0
0727 inline QDataStream &operator>>(QDataStream &s, QVariant::Type &p)
0728 {
0729 quint32 u;
0730 s >> u;
0731 p = static_cast<QVariant::Type>(u);
0732 return s;
0733 }
0734 QT_DEPRECATED_VERSION_6_0
0735 inline QDataStream &operator<<(QDataStream &s, const QVariant::Type p)
0736 {
0737 s << static_cast<quint32>(p);
0738 return s;
0739 }
0740 QT_WARNING_POP
0741 #endif
0742
0743 #endif
0744
0745 inline bool QVariant::isDetached() const
0746 { return !d.is_shared || d.data.shared->ref.loadRelaxed() == 1; }
0747
0748 inline void swap(QVariant &value1, QVariant &value2) noexcept
0749 { value1.swap(value2); }
0750
0751 #ifndef QT_MOC
0752
0753 template<typename T> inline T qvariant_cast(const QVariant &v)
0754 {
0755 QMetaType targetType = QMetaType::fromType<T>();
0756 if (v.d.type() == targetType)
0757 return v.d.get<T>();
0758 if constexpr (std::is_same_v<T,std::remove_const_t<std::remove_pointer_t<T>> const *>) {
0759 using nonConstT = std::remove_const_t<std::remove_pointer_t<T>> *;
0760 QMetaType nonConstTargetType = QMetaType::fromType<nonConstT>();
0761 if (v.d.type() == nonConstTargetType)
0762 return v.d.get<nonConstT>();
0763 }
0764
0765 T t{};
0766 QMetaType::convert(v.metaType(), v.constData(), targetType, &t);
0767 return t;
0768 }
0769
0770 template<typename T> inline T qvariant_cast(QVariant &&v)
0771 {
0772 QMetaType targetType = QMetaType::fromType<T>();
0773 if (v.d.type() == targetType) {
0774 if constexpr (QVariant::Private::CanUseInternalSpace<T>) {
0775 return std::move(*reinterpret_cast<T *>(v.d.data.data));
0776 } else {
0777 if (v.d.data.shared->ref.loadRelaxed() == 1)
0778 return std::move(*reinterpret_cast<T *>(v.d.data.shared->data()));
0779 else
0780 return v.d.get<T>();
0781 }
0782 }
0783 if constexpr (std::is_same_v<T, QVariant>) {
0784
0785 return v;
0786 } if constexpr (std::is_same_v<T,std::remove_const_t<std::remove_pointer_t<T>> const *>) {
0787
0788 using nonConstT = std::remove_const_t<std::remove_pointer_t<T>> *;
0789 QMetaType nonConstTargetType = QMetaType::fromType<nonConstT>();
0790 if (v.d.type() == nonConstTargetType)
0791 return v.d.get<nonConstT>();
0792 }
0793
0794 T t{};
0795 QMetaType::convert(v.metaType(), v.constData(), targetType, &t);
0796 return t;
0797 }
0798
0799 template<> inline QVariant qvariant_cast<QVariant>(const QVariant &v)
0800 {
0801 if (v.metaType().id() == QMetaType::QVariant)
0802 return *reinterpret_cast<const QVariant *>(v.constData());
0803 return v;
0804 }
0805
0806 #endif
0807
0808 #ifndef QT_NO_DEBUG_STREAM
0809 #if QT_DEPRECATED_SINCE(6, 0)
0810 QT_WARNING_PUSH
0811 QT_WARNING_DISABLE_DEPRECATED
0812 QT_DEPRECATED_VERSION_6_0
0813 Q_CORE_EXPORT QDebug operator<<(QDebug, const QVariant::Type);
0814 QT_WARNING_POP
0815 #endif
0816 #endif
0817
0818 namespace QtPrivate {
0819 class Q_CORE_EXPORT QVariantTypeCoercer
0820 {
0821 public:
0822
0823 const void *convert(const QVariant &value, const QMetaType &type);
0824 const void *coerce(const QVariant &value, const QMetaType &type);
0825
0826 private:
0827 QVariant converted;
0828 };
0829 }
0830
0831 template<typename Pointer>
0832 class QVariantRef
0833 {
0834 private:
0835 const Pointer *m_pointer = nullptr;
0836
0837 public:
0838 explicit QVariantRef(const Pointer *reference) : m_pointer(reference) {}
0839 QVariantRef(const QVariantRef &) = default;
0840 QVariantRef(QVariantRef &&) = default;
0841 ~QVariantRef() = default;
0842
0843 operator QVariant() const;
0844 QVariantRef &operator=(const QVariant &value);
0845 QVariantRef &operator=(const QVariantRef &value) { return operator=(QVariant(value)); }
0846 QVariantRef &operator=(QVariantRef &&value) { return operator=(QVariant(value)); }
0847
0848 friend void swap(QVariantRef a, QVariantRef b)
0849 {
0850 QVariant tmp = a;
0851 a = b;
0852 b = std::move(tmp);
0853 }
0854 };
0855
0856 class Q_CORE_EXPORT QVariantConstPointer
0857 {
0858 private:
0859 QVariant m_variant;
0860
0861 public:
0862 explicit QVariantConstPointer(QVariant variant);
0863
0864 QVariant operator*() const;
0865 const QVariant *operator->() const;
0866 };
0867
0868 template<typename Pointer>
0869 class QVariantPointer
0870 {
0871 private:
0872 const Pointer *m_pointer = nullptr;
0873
0874 public:
0875 explicit QVariantPointer(const Pointer *pointer) : m_pointer(pointer) {}
0876 QVariantRef<Pointer> operator*() const { return QVariantRef<Pointer>(m_pointer); }
0877 Pointer operator->() const { return *m_pointer; }
0878 };
0879
0880 QT_END_NAMESPACE
0881
0882 #endif