Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-23 09:20:07

0001 // Copyright (C) 2020 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QVARIANT_H
0005 #define QVARIANT_H
0006 
0007 #include <QtCore/qatomic.h>
0008 #include <QtCore/qcompare.h>
0009 #include <QtCore/qcontainerfwd.h>
0010 #include <QtCore/qmetatype.h>
0011 #ifndef QT_NO_DEBUG_STREAM
0012 #include <QtCore/qdebug.h>
0013 #endif
0014 
0015 #include <memory>
0016 #include <QtCore/q20type_traits.h>
0017 #include <QtCore/q23utility.h>
0018 #include <variant>
0019 
0020 #if !defined(QT_LEAN_HEADERS) || QT_LEAN_HEADERS < 1
0021 #  include <QtCore/qlist.h>
0022 #  include <QtCore/qstringlist.h>
0023 #  include <QtCore/qbytearraylist.h>
0024 #  include <QtCore/qhash.h>
0025 #  include <QtCore/qmap.h>
0026 #  include <QtCore/qobject.h>
0027 #endif
0028 
0029 QT_BEGIN_NAMESPACE
0030 
0031 QT_ENABLE_P0846_SEMANTICS_FOR(get_if)
0032 QT_ENABLE_P0846_SEMANTICS_FOR(get)
0033 
0034 class QBitArray;
0035 class QDataStream;
0036 class QDate;
0037 class QDateTime;
0038 class QEasingCurve;
0039 class QLine;
0040 class QLineF;
0041 class QLocale;
0042 class QModelIndex;
0043 class QPersistentModelIndex;
0044 class QPoint;
0045 class QPointF;
0046 class QRect;
0047 class QRectF;
0048 class QRegularExpression;
0049 class QSize;
0050 class QSizeF;
0051 class QTextFormat;
0052 class QTextLength;
0053 class QTime;
0054 class QTransform;
0055 class QUrl;
0056 class QVariant;
0057 
0058 template<typename T>
0059 inline T qvariant_cast(const QVariant &);
0060 
0061 namespace QtPrivate {
0062 template<> constexpr inline bool qIsRelocatable<QVariant> = true;
0063 }
0064 class Q_CORE_EXPORT QVariant
0065 {
0066     template <typename T, typename... Args>
0067     using if_constructible = std::enable_if_t<
0068         std::conjunction_v<
0069             std::is_copy_constructible<q20::remove_cvref_t<T>>,
0070             std::is_destructible<q20::remove_cvref_t<T>>,
0071             std::is_constructible<q20::remove_cvref_t<T>, Args...>
0072         >,
0073     bool>;
0074 
0075     template <typename T>
0076     using if_rvalue = std::enable_if_t<!std::is_reference_v<T>, bool>;
0077 
0078     struct CborValueStandIn { qint64 n; void *c; int t; };
0079 public:
0080     struct PrivateShared
0081     {
0082     private:
0083         inline PrivateShared() : ref(1) { }
0084     public:
0085         static int computeOffset(PrivateShared *ps, size_t align);
0086         static size_t computeAllocationSize(size_t size, size_t align);
0087         static PrivateShared *create(size_t size, size_t align);
0088         static void free(PrivateShared *p);
0089 
0090         alignas(8) QAtomicInt ref;
0091         int offset;
0092 
0093         const void *data() const { return reinterpret_cast<const uchar *>(this) + offset; }
0094         void *data() { return reinterpret_cast<uchar *>(this) + offset; }
0095     };
0096 
0097     struct Private
0098     {
0099         static constexpr size_t MaxInternalSize = 3 * sizeof(void *);
0100         template <size_t S> static constexpr bool FitsInInternalSize = S <= MaxInternalSize;
0101         template<typename T> static constexpr bool CanUseInternalSpace =
0102                 (QTypeInfo<T>::isRelocatable && FitsInInternalSize<sizeof(T)> && alignof(T) <= alignof(double));
0103         static constexpr bool canUseInternalSpace(const QtPrivate::QMetaTypeInterface *type)
0104         {
0105             Q_ASSERT(type);
0106             return QMetaType::TypeFlags(type->flags) & QMetaType::RelocatableType &&
0107                    size_t(type->size) <= MaxInternalSize && size_t(type->alignment) <= alignof(double);
0108         }
0109 
0110         union
0111         {
0112             uchar data[MaxInternalSize] = {};
0113             PrivateShared *shared;
0114             double _forAlignment; // we want an 8byte alignment on 32bit systems as well
0115         } data;
0116         quintptr is_shared : 1;
0117         quintptr is_null : 1;
0118         quintptr packedType : sizeof(QMetaType) * 8 - 2;
0119 
0120         constexpr Private() noexcept : is_shared(false), is_null(true), packedType(0) {}
0121         explicit Private(const QtPrivate::QMetaTypeInterface *iface) noexcept;
0122         template <typename T> explicit Private(std::piecewise_construct_t, const T &t);
0123 
0124         const void *storage() const
0125         { return is_shared ? data.shared->data() : &data.data; }
0126 
0127         template<typename T> const T &get() const
0128         { return *static_cast<const T *>(storage()); }
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 // need this so that gcc >= 3.4 allocates 32 bits for Type
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     // primitives
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     // trivial, trivially-copyable or COW
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     // conditionally noexcept trivial or trivially-copyable
0283     // (most of these are noexcept on 64-bit)
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     // not noexcept
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); // converts to QString
0309 
0310 #if !defined(Q_CC_GHS)
0311     // GHS has an ICE with this code; use the simplified version below
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 // QT_CONFIG(regularexpression)
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 // QT_BOOTSTRAPPED
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         // If possible we reuse the current QVariant private.
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         /* needs is_copy_constructible for variants semantics, is_move_constructible so that moveConstruct works
0534           (but copy_constructible implies move_constructble, so don't bother checking)
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         // handle special cases
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(); // we want the type stored in QVariant to always be registered
0554 
0555         // We only try to move if the type is actually moveable and not if T is const
0556         // as in const int i; QVariant::fromValue(std::move(i));
0557         if constexpr (std::conjunction_v<std::is_move_constructible<Type>, std::negation<std::is_const<T>>>)
0558             return moveConstruct(QMetaType::fromType<Type>(), std::addressof(value));
0559         else
0560             return copyConstruct(mt, std::addressof(value));
0561     }
0562 
0563     template<typename T>
0564 #ifndef Q_QDOC
0565     static inline auto fromValue(const T &value)
0566         noexcept(std::is_nothrow_copy_constructible_v<T> && Private::CanUseInternalSpace<T>)
0567         -> std::enable_if_t<std::is_copy_constructible_v<T> && std::is_destructible_v<T>, QVariant>
0568 #else
0569     static inline QVariant fromValue(const T &value)
0570 #endif
0571     {
0572         if constexpr (std::is_null_pointer_v<T>)
0573             return QVariant(QMetaType::fromType<std::nullptr_t>());
0574         else if constexpr (std::is_same_v<T, QVariant>)
0575             return value;
0576         else if constexpr (std::is_same_v<T, std::monostate>)
0577             return QVariant();
0578         return QVariant(QMetaType::fromType<T>(), std::addressof(value));
0579     }
0580 
0581     template<typename... Types>
0582     static inline QVariant fromStdVariant(const std::variant<Types...> &value)
0583     {
0584         return fromStdVariantImpl(value);
0585     }
0586 
0587     template<typename... Types>
0588     static QVariant fromStdVariant(std::variant<Types...> &&value)
0589     {
0590         return fromStdVariantImpl(std::move(value));
0591     }
0592 
0593     static QVariant fromMetaType(QMetaType type, const void *copy = nullptr);
0594 
0595     template<typename T>
0596     bool canConvert() const
0597     { return canConvert(QMetaType::fromType<T>()); }
0598 
0599     template<typename T>
0600     bool canView() const
0601     { return canView(QMetaType::fromType<T>()); }
0602 
0603     static QPartialOrdering compare(const QVariant &lhs, const QVariant &rhs);
0604 
0605 private:
0606     template <typename StdVariant>
0607     static QVariant fromStdVariantImpl(StdVariant &&v)
0608     {
0609         if (Q_UNLIKELY(v.valueless_by_exception()))
0610             return QVariant();
0611         auto visitor = [](auto &&arg) {
0612             return QVariant::fromValue(q23::forward_like<StdVariant>(arg));
0613         };
0614         return std::visit(visitor, std::forward<StdVariant>(v));
0615     }
0616 
0617     friend bool comparesEqual(const QVariant &a, const QVariant &b)
0618     { return a.equals(b); }
0619     Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QVariant)
0620 
0621 #ifndef QT_NO_DEBUG_STREAM
0622     template <typename T>
0623     friend auto operator<<(const QDebug &debug, const T &variant) -> std::enable_if_t<std::is_same_v<T, QVariant>, QDebug> {
0624         return  variant.qdebugHelper(debug);
0625     }
0626     QDebug qdebugHelper(QDebug) const;
0627 #endif
0628 
0629     template <typename T>
0630     friend T *get_if(QVariant *v) noexcept
0631     {
0632         // data() will detach from is_null, returning non-nullptr
0633         if (!v || v->d.type() != QMetaType::fromType<T>())
0634             return nullptr;
0635         return static_cast<T*>(v->data());
0636     }
0637     template <typename T>
0638     friend const T *get_if(const QVariant *v) noexcept
0639     {
0640         // (const) data() will not detach from is_null, return nullptr
0641         if (!v || v->d.is_null || v->d.type() != QMetaType::fromType<T>())
0642             return nullptr;
0643         return static_cast<const T*>(v->data());
0644     }
0645 
0646 #define Q_MK_GET(cvref) \
0647     template <typename T> \
0648     friend T cvref get(QVariant cvref v) \
0649     { \
0650         if constexpr (std::is_const_v<T cvref>) \
0651             Q_ASSERT(!v.d.is_null); \
0652         Q_ASSERT(v.d.type() == QMetaType::fromType<q20::remove_cvref_t<T>>()); \
0653         return static_cast<T cvref>(*get_if<T>(&v)); \
0654     } \
0655     /* end */
0656     Q_MK_GET(&)
0657     Q_MK_GET(const &)
0658     Q_MK_GET(&&)
0659     Q_MK_GET(const &&)
0660 #undef Q_MK_GET
0661 
0662     static QVariant moveConstruct(QMetaType type, void *data);
0663     static QVariant copyConstruct(QMetaType type, const void *data);
0664 
0665     template<typename T>
0666     friend inline T qvariant_cast(const QVariant &);
0667     template<typename T>
0668     friend inline T qvariant_cast(QVariant &&);
0669 
0670 protected:
0671     Private d;
0672     void create(int type, const void *copy);
0673     void create(QMetaType type, const void *copy);
0674     bool equals(const QVariant &other) const;
0675     bool convert(int type, void *ptr) const;
0676     bool view(int type, void *ptr);
0677 
0678 private:
0679     // force compile error, prevent QVariant(bool) to be called
0680     inline QVariant(void *) = delete;
0681     // QVariant::Type is marked as \obsolete, but we don't want to
0682     // provide a constructor from its intended replacement,
0683     // QMetaType::Type, instead, because the idea behind these
0684     // constructors is flawed in the first place. But we also don't
0685     // want QVariant(QMetaType::String) to compile and falsely be an
0686     // int variant, so delete this constructor:
0687     QVariant(QMetaType::Type) = delete;
0688 
0689     // used to setup the QVariant internals for the "real" inplace ctor
0690     QVariant(std::in_place_t, QMetaType type);
0691     // helper for emplace
0692     void *prepareForEmplace(QMetaType type);
0693 
0694     // These constructors don't create QVariants of the type associated
0695     // with the enum, as expected, but they would create a QVariant of
0696     // type int with the value of the enum value.
0697     // Use QVariant v = QColor(Qt::red) instead of QVariant v = Qt::red for
0698     // example.
0699     QVariant(Qt::GlobalColor) = delete;
0700     QVariant(Qt::BrushStyle) = delete;
0701     QVariant(Qt::PenStyle) = delete;
0702     QVariant(Qt::CursorShape) = delete;
0703 #ifdef QT_NO_CAST_FROM_ASCII
0704     // force compile error when implicit conversion is not wanted
0705     inline QVariant(const char *) = delete;
0706 #endif
0707 public:
0708     typedef Private DataPtr;
0709     inline DataPtr &data_ptr() { return d; }
0710     inline const DataPtr &data_ptr() const { return d; }
0711 };
0712 
0713 inline bool QVariant::isValid() const
0714 {
0715     return d.type().isValid(QT6_CALL_NEW_OVERLOAD);
0716 }
0717 
0718 #ifndef QT_NO_DATASTREAM
0719 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &s, QVariant &p);
0720 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &s, const QVariant &p);
0721 
0722 #if QT_DEPRECATED_SINCE(6, 0)
0723 QT_WARNING_PUSH
0724 QT_WARNING_DISABLE_DEPRECATED
0725 QT_DEPRECATED_VERSION_6_0
0726 inline QDataStream &operator>>(QDataStream &s, QVariant::Type &p)
0727 {
0728     quint32 u;
0729     s >> u;
0730     p = static_cast<QVariant::Type>(u);
0731     return s;
0732 }
0733 QT_DEPRECATED_VERSION_6_0
0734 inline QDataStream &operator<<(QDataStream &s, const QVariant::Type p)
0735 {
0736     s << static_cast<quint32>(p);
0737     return s;
0738 }
0739 QT_WARNING_POP
0740 #endif
0741 
0742 #endif
0743 
0744 inline bool QVariant::isDetached() const
0745 { return !d.is_shared || d.data.shared->ref.loadRelaxed() == 1; }
0746 
0747 inline void swap(QVariant &value1, QVariant &value2) noexcept
0748 { value1.swap(value2); }
0749 
0750 #ifndef QT_MOC
0751 
0752 template<typename T> inline T qvariant_cast(const QVariant &v)
0753 {
0754     QMetaType targetType = QMetaType::fromType<T>();
0755     if (v.d.type() == targetType)
0756         return v.d.get<T>();
0757     if constexpr (std::is_same_v<T,std::remove_const_t<std::remove_pointer_t<T>> const *>) {
0758         using nonConstT = std::remove_const_t<std::remove_pointer_t<T>> *;
0759         QMetaType nonConstTargetType = QMetaType::fromType<nonConstT>();
0760         if (v.d.type() == nonConstTargetType)
0761             return v.d.get<nonConstT>();
0762     }
0763 
0764     T t{};
0765     QMetaType::convert(v.metaType(), v.constData(), targetType, &t);
0766     return t;
0767 }
0768 
0769 template<typename T> inline T qvariant_cast(QVariant &&v)
0770 {
0771     QMetaType targetType = QMetaType::fromType<T>();
0772     if (v.d.type() == targetType) {
0773         if (!v.d.is_shared) {
0774             return std::move(*reinterpret_cast<T *>(v.d.data.data));
0775         } else {
0776             if (v.d.data.shared->ref.loadRelaxed() == 1)
0777                 return std::move(*reinterpret_cast<T *>(v.d.data.shared->data()));
0778             else
0779                 return v.d.get<T>();
0780         }
0781     }
0782     if constexpr (std::is_same_v<T, QVariant>) {
0783         // if the metatype doesn't match, but we want a QVariant, just return the current variant
0784         return v;
0785     } if constexpr (std::is_same_v<T,std::remove_const_t<std::remove_pointer_t<T>> const *>) {
0786         // moving a pointer is pointless, just do the same as the const & overload
0787         using nonConstT = std::remove_const_t<std::remove_pointer_t<T>> *;
0788         QMetaType nonConstTargetType = QMetaType::fromType<nonConstT>();
0789         if (v.d.type() == nonConstTargetType)
0790             return v.d.get<nonConstT>();
0791     }
0792 
0793     T t{};
0794     QMetaType::convert(v.metaType(), v.constData(), targetType, &t);
0795     return t;
0796 }
0797 
0798 #  ifndef QT_NO_VARIANT
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 #  endif
0806 
0807 #endif // QT_MOC
0808 
0809 #ifndef QT_NO_DEBUG_STREAM
0810 #if QT_DEPRECATED_SINCE(6, 0)
0811 QT_WARNING_PUSH
0812 QT_WARNING_DISABLE_DEPRECATED
0813 QT_DEPRECATED_VERSION_6_0
0814 Q_CORE_EXPORT QDebug operator<<(QDebug, const QVariant::Type);
0815 QT_WARNING_POP
0816 #endif
0817 #endif
0818 
0819 namespace QtPrivate {
0820 class Q_CORE_EXPORT QVariantTypeCoercer
0821 {
0822 public:
0823     // ### Qt7: Pass QMetaType as value rather than const ref.
0824     const void *convert(const QVariant &value, const QMetaType &type);
0825     const void *coerce(const QVariant &value, const QMetaType &type);
0826 
0827 private:
0828     QVariant converted;
0829 };
0830 }
0831 
0832 template<typename Pointer>
0833 class QVariantRef
0834 {
0835 private:
0836     const Pointer *m_pointer = nullptr;
0837 
0838 public:
0839     explicit QVariantRef(const Pointer *reference) : m_pointer(reference) {}
0840     QVariantRef(const QVariantRef &) = default;
0841     QVariantRef(QVariantRef &&) = default;
0842     ~QVariantRef() = default;
0843 
0844     operator QVariant() const;
0845     QVariantRef &operator=(const QVariant &value);
0846     QVariantRef &operator=(const QVariantRef &value) { return operator=(QVariant(value)); }
0847     QVariantRef &operator=(QVariantRef &&value) { return operator=(QVariant(value)); }
0848 
0849     friend void swap(QVariantRef a, QVariantRef b)
0850     {
0851         QVariant tmp = a;
0852         a = b;
0853         b = std::move(tmp);
0854     }
0855 };
0856 
0857 class Q_CORE_EXPORT QVariantConstPointer
0858 {
0859 private:
0860     QVariant m_variant;
0861 
0862 public:
0863     explicit QVariantConstPointer(QVariant variant);
0864 
0865     QVariant operator*() const;
0866     const QVariant *operator->() const;
0867 };
0868 
0869 template<typename Pointer>
0870 class QVariantPointer
0871 {
0872 private:
0873     const Pointer *m_pointer = nullptr;
0874 
0875 public:
0876     explicit QVariantPointer(const Pointer *pointer) : m_pointer(pointer) {}
0877     QVariantRef<Pointer> operator*() const { return QVariantRef<Pointer>(m_pointer); }
0878     Pointer operator->() const { return *m_pointer; }
0879 };
0880 
0881 QT_END_NAMESPACE
0882 
0883 #endif // QVARIANT_H