7#include <QtCore/qatomic.h>
8#include <QtCore/qcontainerfwd.h>
9#include <QtCore/qmetatype.h>
10#ifndef QT_NO_DEBUG_STREAM
11#include <QtCore/qdebug.h>
15#include <QtCore/q20type_traits.h>
16#include <QtCore/q23utility.h>
19#if !defined(QT_LEAN_HEADERS) || QT_LEAN_HEADERS < 1
20# include <QtCore/qlist.h>
21# include <QtCore/qstringlist.h>
22# include <QtCore/qbytearraylist.h>
23# include <QtCore/qhash.h>
24# include <QtCore/qmap.h>
25# include <QtCore/qobject.h>
30QT_ENABLE_P0846_SEMANTICS_FOR(get_if)
31QT_ENABLE_P0846_SEMANTICS_FOR(
get)
65 template <
typename T,
typename... Args>
66 using if_constructible = std::enable_if_t<
68 std::is_copy_constructible<q20::remove_cvref_t<T>>,
69 std::is_destructible<q20::remove_cvref_t<T>>,
70 std::is_constructible<q20::remove_cvref_t<T>, Args...>
75 using if_rvalue = std::enable_if_t<!std::is_reference_v<T>,
bool>;
77 struct CborValueStandIn {
qint64 n;
void *
c;
int t; };
85 static size_t computeAllocationSize(
size_t size,
size_t align);
92 const void *
data()
const {
return reinterpret_cast<const uchar *
>(
this) +
offset; }
98 static constexpr size_t MaxInternalSize = 3 *
sizeof(
void *);
99 template <
size_t S>
static constexpr bool FitsInInternalSize = S <= MaxInternalSize;
100 template<
typename T>
static constexpr bool CanUseInternalSpace =
106 size_t(
type->size) <= MaxInternalSize && size_t(
type->alignment) <=
alignof(double);
119 constexpr Private() noexcept : is_shared(
false), is_null(true), packedType(0) {}
121 template <
typename T>
explicit Private(std::piecewise_construct_t,
const T &
t);
124 {
return is_shared ?
data.shared->data() : &
data.data; }
127 template<
typename T>
const T &
get()
const
128 {
return *
static_cast<const T *
>(CanUseInternalSpace<T> ? &
data.data :
data.shared->data()); }
141#if QT_DEPRECATED_SINCE(6, 0)
145 Bool = QMetaType::Bool,
146 Int = QMetaType::Int,
147 UInt = QMetaType::UInt,
148 LongLong = QMetaType::LongLong,
149 ULongLong = QMetaType::ULongLong,
150 Double = QMetaType::Double,
151 Char = QMetaType::QChar,
152 Map = QMetaType::QVariantMap,
153 List = QMetaType::QVariantList,
154 String = QMetaType::QString,
155 StringList = QMetaType::QStringList,
156 ByteArray = QMetaType::QByteArray,
157 BitArray = QMetaType::QBitArray,
158 Date = QMetaType::QDate,
159 Time = QMetaType::QTime,
160 DateTime = QMetaType::QDateTime,
161 Url = QMetaType::QUrl,
162 Locale = QMetaType::QLocale,
163 Rect = QMetaType::QRect,
164 RectF = QMetaType::QRectF,
165 Size = QMetaType::QSize,
166 SizeF = QMetaType::QSizeF,
167 Line = QMetaType::QLine,
168 LineF = QMetaType::QLineF,
169 Point = QMetaType::QPoint,
170 PointF = QMetaType::QPointF,
171#if QT_CONFIG(regularexpression)
174 Hash = QMetaType::QVariantHash,
175#if QT_CONFIG(easingcurve)
176 EasingCurve = QMetaType::QEasingCurve,
178 Uuid = QMetaType::QUuid,
179#if QT_CONFIG(itemmodel)
180 ModelIndex = QMetaType::QModelIndex,
181 PersistentModelIndex = QMetaType::QPersistentModelIndex,
185 Font = QMetaType::QFont,
186 Pixmap = QMetaType::QPixmap,
187 Brush = QMetaType::QBrush,
188 Color = QMetaType::QColor,
189 Palette = QMetaType::QPalette,
190 Image = QMetaType::QImage,
191 Polygon = QMetaType::QPolygon,
192 Region = QMetaType::QRegion,
193 Bitmap = QMetaType::QBitmap,
194 Cursor = QMetaType::QCursor,
195#if QT_CONFIG(shortcut)
196 KeySequence = QMetaType::QKeySequence,
198 Pen = QMetaType::QPen,
199 TextLength = QMetaType::QTextLength,
200 TextFormat = QMetaType::QTextFormat,
201 Transform = QMetaType::QTransform,
203 Vector2D = QMetaType::QVector2D,
204 Vector3D = QMetaType::QVector3D,
205 Vector4D = QMetaType::QVector4D,
207 PolygonF = QMetaType::QPolygonF,
208 Icon = QMetaType::QIcon,
211 SizePolicy = QMetaType::QSizePolicy,
214 LastType = 0xffffffff
223 template <
typename T,
typename ...Args>
224 using is_noexcept_constructible = std::conjunction<
225 std::bool_constant<Private::CanUseInternalSpace<T>>,
226 std::is_nothrow_constructible<T, Args...>
230 template <
typename T,
typename... Args,
231 if_constructible<T, Args...> =
true>
233 noexcept(is_noexcept_constructible<q20::remove_cvref_t<T>, Args...>
::value)
236 void *
data =
const_cast<void *
>(constData());
237 new (
data) T(std::forward<Args>(
args)...);
240 template <
typename T,
typename U,
typename... Args,
241 if_constructible<T, std::initializer_list<U> &, Args...> =
true>
242 explicit QVariant(std::in_place_type_t<T>, std::initializer_list<U> il, Args&&...
args)
243 noexcept(is_noexcept_constructible<q20::remove_cvref_t<T>,
244 std::initializer_list<U> &,
249 char *
data =
static_cast<char *
>(
const_cast<void *
>(constData()));
250 new (
data) T(il, std::forward<Args>(
args)...);
282 QVariant(
const QJsonValue &jsonValue)
noexcept(Private::FitsInInternalSize<
sizeof(CborValueStandIn)>);
284 QVariant(
QUuid uuid)
noexcept(Private::FitsInInternalSize<16>);
285#ifndef QT_NO_GEOM_VARIANT
301#ifndef QT_NO_CAST_FROM_ASCII
308#if !defined(Q_CC_GHS)
310 template <
typename T,
311 std::enable_if_t<std::disjunction_v<std::is_pointer<T>, std::is_member_pointer<T>>,
bool> =
false>
314 QVariant(
const volatile void *) =
delete;
317#if QT_CORE_REMOVED_SINCE(6, 5)
332 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(
QVariant)
337 int typeId()
const {
return metaType().id(); }
349#if QT_DEPRECATED_SINCE(6, 0)
351 bool canConvert(
int targetTypeId)
const
358 inline bool isValid()
const;
364 inline bool isDetached()
const;
366 int toInt(
bool *
ok =
nullptr)
const;
367 uint toUInt(
bool *
ok =
nullptr)
const;
373 qreal toReal(
bool *
ok =
nullptr)
const;
378 QChar toChar()
const;
386#ifndef QT_NO_GEOM_VARIANT
389 QRect toRect()
const;
390 QSize toSize()
const;
392 QLine toLine()
const;
397#if QT_CONFIG(regularexpression)
400#if QT_CONFIG(easingcurve)
403 QUuid toUuid()
const;
404#ifndef QT_BOOTSTRAPPED
411#if QT_CONFIG(itemmodel)
416#ifndef QT_NO_DATASTREAM
420#if QT_DEPRECATED_SINCE(6, 0)
430 int type =
d.type().id();
434 static const char *typeToName(
int typeId)
437 static Type nameToType(
const char *
name)
440 return metaType <= int(UserType) ? QVariant::Type(metaType) : UserType;
447 {
return d.storage(); }
448 inline const void *
data()
const {
return constData(); }
451 template <
typename T>
452 void verifySuitableForEmplace()
454 static_assert(!std::is_reference_v<T>,
455 "QVariant does not support reference types");
456 static_assert(!std::is_const_v<T>,
457 "QVariant does not support const types");
458 static_assert(std::is_copy_constructible_v<T>,
459 "QVariant requires that the type is copyable");
460 static_assert(std::is_destructible_v<T>,
461 "QVariant requires that the type is destructible");
464 template <
typename T,
typename... Args>
465 T &emplaceImpl(Args&&...
args)
467 verifySuitableForEmplace<T>();
468 auto data =
static_cast<T *
>(prepareForEmplace(QMetaType::fromType<T>()));
473 template <
typename T,
typename... Args,
474 if_constructible<T, Args...> =
true>
477 return emplaceImpl<T>(std::forward<Args>(
args)...);
480 template <
typename T,
typename U,
typename... Args,
481 if_constructible<T, std::initializer_list<U> &, Args...> =
true>
484 return emplaceImpl<T>(
list, std::forward<Args>(
args)...);
487 template<
typename T,
typename = std::enable_if_t<!std::is_same_v<std::decay_t<T>, QVariant>>>
490 using VT = std::decay_t<T>;
491 QMetaType metaType = QMetaType::fromType<VT>();
493 if (isDetached() &&
d.type() == metaType) {
494 *
reinterpret_cast<VT *
>(
const_cast<void *
>(constData())) = std::forward<T>(avalue);
496 *
this = QVariant::fromValue<VT>(std::forward<T>(avalue));
507 *
this = std::move(avalue);
512 {
return qvariant_cast<T>(*
this); }
524 {
return qvariant_cast<T>(std::move(*
this)); }
526 template<
typename T, if_rvalue<T> = true>
532 noexcept(std::is_nothrow_copy_constructible_v<T> && Private::CanUseInternalSpace<T>)
533 -> std::enable_if_t<std::conjunction_v<std::is_copy_constructible<T>,
540 using Type = std::remove_cv_t<T>;
541 if constexpr (std::is_null_pointer_v<Type>)
542 return QVariant(QMetaType::fromType<std::nullptr_t>());
543 else if constexpr (std::is_same_v<Type, QVariant>)
544 return std::forward<T>(
value);
545 else if constexpr (std::is_same_v<Type, std::monostate>)
547 QMetaType mt = QMetaType::fromType<Type>();
553 if constexpr (std::conjunction_v<std::is_move_constructible<Type>, std::negation<std::is_const<T>>>)
554 return moveConstruct(QMetaType::fromType<Type>(), std::addressof(
value));
556 return copyConstruct(mt, std::addressof(
value));
562 noexcept(std::is_nothrow_copy_constructible_v<T> && Private::CanUseInternalSpace<T>)
563 -> std::enable_if_t<std::is_copy_constructible_v<T> && std::is_destructible_v<T>,
QVariant>
568 if constexpr (std::is_null_pointer_v<T>)
569 return QVariant(QMetaType::fromType<std::nullptr_t>());
570 else if constexpr (std::is_same_v<T, QVariant>)
572 else if constexpr (std::is_same_v<T, std::monostate>)
574 return QVariant(QMetaType::fromType<T>(), std::addressof(
value));
577 template<
typename...
Types>
580 return fromStdVariantImpl(
value);
583 template<
typename...
Types>
586 return fromStdVariantImpl(std::move(
value));
591 {
return canConvert(QMetaType::fromType<T>()); }
595 {
return canView(QMetaType::fromType<T>()); }
600 template <
typename StdVariant>
601 static QVariant fromStdVariantImpl(StdVariant &&
v)
605 auto visitor = [](
auto &&
arg) {
608 return std::visit(visitor, std::forward<StdVariant>(
v));
612 {
return a.equals(
b); }
614 {
return !
a.equals(
b); }
615#ifndef QT_NO_DEBUG_STREAM
616 template <
typename T>
623 template <
typename T>
627 if (!
v ||
v->d.type() != QMetaType::fromType<T>())
629 return static_cast<T*
>(
v->data());
631 template <
typename T>
635 if (!
v ||
v->d.is_null ||
v->d.type() != QMetaType::fromType<T>())
637 return static_cast<const T*
>(
v->data());
640#define Q_MK_GET(cvref) \
641 template <typename T> \
642 friend T cvref get(QVariant cvref v) \
644 if constexpr (std::is_const_v<T cvref>) \
645 Q_ASSERT(!v.d.is_null); \
646 Q_ASSERT(v.d.type() == QMetaType::fromType<q20::remove_cvref_t<T>>()); \
647 return static_cast<T cvref>(*get_if<T>(&v)); \
697#ifdef QT_NO_CAST_FROM_ASCII
699 inline QVariant(
const char *) =
delete;
712#ifndef QT_NO_DATASTREAM
716#if QT_DEPRECATED_SINCE(6, 0)
724 p =
static_cast<QVariant::Type
>(u);
730 s << static_cast<quint32>(
p);
748 QMetaType targetType = QMetaType::fromType<T>();
749 if (
v.d.type() == targetType)
751 if constexpr (std::is_same_v<T,std::remove_const_t<std::remove_pointer_t<T>>
const *>) {
752 using nonConstT = std::remove_const_t<std::remove_pointer_t<T>> *;
753 QMetaType nonConstTargetType = QMetaType::fromType<nonConstT>();
754 if (
v.d.type() == nonConstTargetType)
755 return v.d.get<nonConstT>();
765 QMetaType targetType = QMetaType::fromType<T>();
766 if (
v.d.type() == targetType) {
767 if constexpr (QVariant::Private::CanUseInternalSpace<T>) {
768 return std::move(*
reinterpret_cast<T *
>(
v.d.data.data));
770 if (
v.d.data.shared->ref.loadRelaxed() == 1)
771 return std::move(*
reinterpret_cast<T *
>(
v.d.data.shared->data()));
776 if constexpr (std::is_same_v<T, QVariant>) {
779 }
if constexpr (std::is_same_v<T,std::remove_const_t<std::remove_pointer_t<T>>
const *>) {
781 using nonConstT = std::remove_const_t<std::remove_pointer_t<T>> *;
782 QMetaType nonConstTargetType = QMetaType::fromType<nonConstT>();
783 if (
v.d.type() == nonConstTargetType)
784 return v.d.get<nonConstT>();
795 return *
reinterpret_cast<const QVariant *
>(
v.constData());
801#ifndef QT_NO_DEBUG_STREAM
802#if QT_DEPRECATED_SINCE(6, 0)
824template<
typename Po
inter>
828 const Pointer *m_pointer =
nullptr;
861template<
typename Po
inter>
865 const Pointer *m_pointer =
nullptr;
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore \reentrant
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore \reentrant
\macro QT_RESTRICTED_CAST_FROM_ASCII
\inmodule QtCore \reentrant
Emulated const pointer to QVariant based on a pointer.
QVariantPointer is a template class that emulates a pointer to QVariant based on a pointer.
QVariantPointer(const Pointer *pointer)
Constructs a QVariantPointer from the given pointer.
Pointer operator->() const
Dereferences and returns the pointer.
QVariantRef< Pointer > operator*() const
Dereferences the QVariantPointer to a QVariantRef.
The QVariantRef acts as a non-const reference to a QVariant.
QVariantRef(const Pointer *reference)
Creates a QVariantRef from an pointer.
QVariantRef & operator=(const QVariantRef &value)
Assigns a new value to the value pointed to by the pointer this QVariantRef refers to.
QVariantRef(const QVariantRef &)=default
QVariantRef & operator=(QVariantRef &&value)
Assigns a new value to the value pointed to by the pointer this QVariantRef refers to.
QVariantRef(QVariantRef &&)=default
friend void swap(QVariantRef a, QVariantRef b)
QVariantRef & operator=(const QVariant &value)
Assigns a new value to the value pointed to by the pointer this QVariantRef refers to.
bool canView(QMetaType targetType) const
friend const T * get_if(const QVariant *v) noexcept
T & emplace(Args &&... args)
QVariant(const QPersistentModelIndex &modelIndex) noexcept(false)
const void * data() const
Returns a pointer to the contained object as a generic void* that cannot be written to.
bool canConvert() const
Returns true if the variant can be converted to the template type {T}, otherwise false.
QVariant() noexcept
Constructs an invalid variant.
QVariant(std::in_place_type_t< T >, Args &&... args) noexcept(is_noexcept_constructible< q20::remove_cvref_t< T >, Args... >::value)
T view()
Returns a mutable view of template type {T} on the stored value.
T & emplace(std::initializer_list< U > list, Args &&... args)
QVariant(const QRegularExpression &re) noexcept
QVariant(QVariant &&other) noexcept
Move-constructs a QVariant instance, making it point at the same object that other was pointing to.
bool isValid() const
Returns true if the storage type of this variant is not QMetaType::UnknownType; otherwise returns fal...
friend bool operator==(const QVariant &a, const QVariant &b)
Returns true if v1 and v2 are equal; otherwise returns false.
bool canView() const
Returns true if a mutable view of the template type {T} can be created on this variant,...
const DataPtr & data_ptr() const
static QVariant fromStdVariant(const std::variant< Types... > &value)
friend bool operator!=(const QVariant &a, const QVariant &b)
Returns false if v1 and v2 are equal; otherwise returns true.
friend T * get_if(QVariant *v) noexcept
If v contains an object of type T, returns a pointer to the contained object, otherwise returns \null...
QT_ASCII_CAST_WARN QVariant(const char *str) noexcept(false)
Constructs a new variant with a string value of val.
QVariant(const QModelIndex &modelIndex) noexcept(Private::FitsInInternalSize< 8+2 *sizeof(quintptr)>)
static auto fromValue(const T &value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::is_copy_constructible_v< T > &&std::is_destructible_v< T >, QVariant >
Returns a QVariant containing a copy of value.
void setValue(const QVariant &avalue)
Copies value over this QVariant.
void setValue(QVariant &&avalue)
Moves value over this QVariant.
friend auto operator<<(const QDebug &debug, const T &variant) -> std::enable_if_t< std::is_same_v< T, QVariant >, QDebug >
int typeId() const
Returns the storage type of the value stored in the variant.
static QVariant fromStdVariant(std::variant< Types... > &&value)
bool canConvert(QMetaType targetType) const
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
QVariant(std::in_place_type_t< T >, std::initializer_list< U > il, Args &&... args) noexcept(is_noexcept_constructible< q20::remove_cvref_t< T >, std::initializer_list< U > &, Args... >::value)
QVariant(const QEasingCurve &easing) noexcept(false)
void setValue(T &&avalue)
Stores a copy of value.
const void * constData() const
QHash< int, QWidget * > hash
[35multi]
QMap< QString, QString > map
[6]
Combined button and popup list for selecting options.
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber< float > toFloat(QByteArrayView a) noexcept
constexpr bool qIsRelocatable< QVariant >
T * construct_at(T *ptr, Args &&... args)
void toByteArray(QByteArray &)
static jboolean copy(JNIEnv *, jobject)
#define QT_WARNING_DISABLE_DEPRECATED
constexpr timespec operator*(const timespec &t1, int mul)
static QDBusError::ErrorType get(const char *name)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
void uint64_t uint64_t uint64_t value2
static ControlElement< T > * ptr(QWidget *widget)
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint GLintptr offset
GLsizei const void * pointer
static ISC_DATE toDate(QDate t)
static QList< QVariant > toList(char **buf, int count)
static ISC_TIME toTime(QTime t)
#define QT_ASCII_CAST_WARN
#define QT_DEPRECATED_VERSION_X_6_0(text)
#define QT_DEPRECATED_VERSION_6_0
static QStringList toStringList(const QJsonArray &jsonArray)
static int compare(quint64 a, quint64 b)
static int toInt(const QChar &qc, int R)
static double toDouble(Value v)
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &s, QVariant &p)
QVariant qvariant_cast< QVariant >(const QVariant &v)
Q_CORE_EXPORT QDataStream & operator<<(QDataStream &s, const QVariant &p)
T qvariant_cast(const QVariant &)
QUrl url("example.com")
[constructor-url-reference]
value toMap().value(key)
[3]
char * toString(const MyType &t)
[31]
const void * data() const
static constexpr bool canUseInternalSpace(const QtPrivate::QMetaTypeInterface *type)
const void * storage() const
const QtPrivate::QMetaTypeInterface * typeInterface() const
uchar data[MaxInternalSize]
constexpr Private() noexcept
QT_BEGIN_NAMESPACE bool toBool(const QString &str)