6 #ifndef SWIFT_MISC_VARIANT_H
7 #define SWIFT_MISC_VARIANT_H
10 #include <type_traits>
12 #include <QDBusArgument>
14 #include <QJsonObject>
17 #include <QSequentialIterable>
48 if (QMetaType::hasRegisteredConverterFunction<T, private_ns::IValueObjectMetaInfo *>()) {
return; }
49 bool ok = QMetaType::registerConverter<T, private_ns::IValueObjectMetaInfo *>(
50 private_ns::CValueObjectMetaInfo<T>::instance);
124 m_v = std::move(var);
135 template <
typename T>
138 static_assert(!std::is_same_v<
CVariant, std::decay_t<T>>,
"CVariant is an illegal type!");
143 template <
typename T>
146 static_assert(!std::is_same_v<
CVariant, std::decay_t<T>>,
"CVariant is an illegal type!");
151 template <
typename T>
154 m_v.setValue(std::forward<T>(value));
158 template <
typename T>
161 m_v.setValue(std::forward<T>(value));
165 template <
typename T>
172 template <
typename T>
181 template <
typename T>
184 return canConvert<T>() ? value<T>() : def;
200 template <
typename T>
203 return canConvert(qMetaTypeId<T>());
219 bool toBool()
const {
return m_v.toBool(); }
222 int toInt(
bool *ok =
nullptr)
const {
return m_v.toInt(ok); }
225 qlonglong
toLongLong(
bool *ok =
nullptr)
const {
return m_v.toLongLong(ok); }
228 qulonglong
toULongLong(
bool *ok =
nullptr)
const {
return m_v.toULongLong(ok); }
234 double toDouble(
bool *ok =
nullptr)
const {
return m_v.toDouble(ok); }
246 bool isNull()
const {
return m_v.isNull(); }
249 bool isValid()
const {
return m_v.isValid(); }
254 const int type = m_v.typeId();
255 return type >= QMetaType::Type::User ? QMetaType::Type::User :
static_cast<QMetaType::Type>(type);
259 const char *
typeName()
const {
return m_v.typeName(); }
330 bool isA(
int metaTypeId)
const;
338 private_ns::IValueObjectMetaInfo *getValueObjectMetaInfo()
const
340 return private_ns::getValueObjectMetaInfo(m_v);
342 void *data() {
return m_v.
data(); }
343 const void *data()
const {
return m_v.
data(); }
345 static int compareImpl(
const CVariant &,
const CVariant &);
346 size_t getValueHash()
const;
348 template <
typename T>
352 copy.convert(qMetaTypeId<T>());
353 return *
static_cast<const T *
>(copy.data());
355 template <
typename T>
358 return toImpl<QList<T>>();
360 template <
typename T>
361 CSequence<T> to(tag<CSequence<T>>)
const
363 return toImpl<CSequence<T>>();
365 template <
typename T>
368 using VT =
typename T::value_type;
380 bool isVariantList()
const;
386 namespace swift::misc::private_ns
389 template <
typename T>
390 void MetaTypeHelper<T>::maybeRegisterMetaList()
392 if constexpr (canConvertVariantList<T>(0))
400 QMetaType::registerConverter<T, QVector<CVariant>>([](
const T &list) ->
QVector<CVariant> {
401 return list.transform([](
const typename T::value_type &v) {
return CVariant::from(v); });
403 QMetaType::registerConverter<QVector<CVariant>, T>([](
const QVector<CVariant> &list) -> T {
404 return makeRange(list).transform([](
const CVariant &v) {
return v.to<
typename T::value_type>(); });
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
A sequence of log categories.
Non-owning reference to a CPropertyIndex with a subset of its features.
Streamable status message, e.g.
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
T to() const
Synonym for value().
friend size_t qHash(const CVariant &var)
qHash overload, needed for storing value in a QSet.
void reset(const QVariant &var)
Change the internal QVariant.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
CVariant(QVariant &&var) noexcept
Move-construct from a QVariant.
void setValue(T &&value)
Change the value.
CVariant(const QVariant &var)
Construct from a QVariant.
void marshallToDbus(QDBusArgument &argument) const
Marshall without begin/endStructure, for when composed within another object.
static CVariant fromValue(T &&value)
Construct a variant from a value.
CStatusMessage convertFromMemoizedJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
Call convertFromMemoizedJson, catch any CJsonException that is thrown and return it as CStatusMessage...
T value() const
Return the value converted to the type T.
T valueOrDefault(T def) const
Returns the value converted to the type T, or a default if it can not be converted.
bool isIntegral() const
True if this variant's type is an integral type.
double toDouble(bool *ok=nullptr) const
Convert this variant to double.
QJsonObject toMemoizedJson() const
To compact JSON format.
CVariant & operator=(QVariant &&var) noexcept
Change the internal QVariant.
CIcons::IconIndex toIcon() const
As icon, not implemented by all classes.
void unmarshalFromDataStream(QDataStream &stream)
Unmarshal a value from a QDataStream.
static void registerMetadata()
Register metadata.
qint64 toQInt64(bool *ok=nullptr) const
COnvert to qint64, which is used for all timestamps.
bool canConvert() const
True if this variant can be converted to the type T.
QString getClassName() const
Class name.
CVariant & operator=(const CVariant &other)=default
Copy assignment operator.
void convertFromMemoizedJson(const QJsonObject &json, bool allowFallbackToJson)
From compact JSON format.
void unmarshallFromDbus(const QDBusArgument &argument)
Unmarshall without begin/endStructure, for when composed within another object.
QMetaType metaType() const
Return the QMetaType of the type in this variant.
bool convert(int typeId)
Convert this variant to the type with the given metatype ID and return true if successful.
void swap(QVariant &other) noexcept
Swap the internal QVariant with another.
void set(T &&value)
Synonym for setValue().
CVariant & operator=(const QVariant &var)
Change the internal QVariant.
QUrl toUrl() const
Convert this variant to QUrl.
int userType() const
Return the metatype ID of the value in this variant.
CVariant(int typeId, const void *copy)
Construct a variant from the given type and opaque pointer.
CVariant(int)=delete
Avoid unexpected implicit cast to QVariant::Type. (Use CVariant::from() instead.)
bool isValid() const
True if this variant is valid.
bool toBool() const
Convert this variant to a bool.
void convertFromJson(const QJsonObject &json)
Assign from JSON object.
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
bool equalsPropertyByIndex(const CVariant &compareValue, CPropertyIndexRef index) const
Is given variant equal to value of property index?
qulonglong toULongLong(bool *ok=nullptr) const
Convert this variant to a unsigned longlong integer.
QDateTime toDateTime() const
Convert this variant to QDateTime.
const char * typeName() const
Return the typename of the value in this variant.
void clear()
Set the variant to null.
bool isArithmetic() const
True if this variant's type is an integral or floating-point type.
const QVariant & getQVariant() const
Return the internal QVariant.
CVariant(const char *string)
Implicit conversion from C string.
bool matches(const CVariant &event) const
If this is an event subscription, return true if it matches the given event.
QMetaType::Type type() const
Return the metatype ID of the value in this variant, or QMetaType::User if it is a user type.
CVariant(CVariant &&other) noexcept=default
Move constructor.
friend int compare(const CVariant &a, const CVariant &b)
Return negative, zero, or positive if a is less than, equal to, or greater than b.
qlonglong toLongLong(bool *ok=nullptr) const
Convert this variant to a longlong integer.
CVariant & operator=(CVariant &&other) noexcept=default
Move assignment operatior.
void swap(CVariant &other) noexcept
Swap this variant with another.
void reset(QVariant &&var)
Change the internal QVariant.
void marshalToDataStream(QDataStream &stream) const
Marshal a value to a QDataStream.
static CVariant from(T &&value)
Synonym for fromValue().
CVariant()
Default constructor.
QString convertToQString(bool i18n=false) const
Cast as QString.
CStatusMessage convertFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
Call convertFromJson, catch any CJsonException that is thrown and return it as CStatusMessage.
int toInt(bool *ok=nullptr) const
Convert this variant to an integer.
CVariant(const QString &string)
Implicit conversion from QString.
QPixmap toPixmap() const
Corresponding pixmap.
bool isNull() const
True if this variant is null.
bool canConvert(int typeId) const
True if this variant can be converted to the type with the given metatype ID.
int getMetaTypeId() const
Returns the Qt meta type ID of this object.
CVariant(const CVariant &)=default
Copy constructor.
bool isA(int metaTypeId) const
Returns true if this object is an instance of the class with the given meta type ID,...
QJsonObject toJson() const
Cast to JSON object.
QString toJsonString(QJsonDocument::JsonFormat format=QJsonDocument::Indented) const
Convenience function JSON as string.
CRTP class template which will generate marshalling operators for a derived class with its own marsha...
CRTP class template to generate non-member QDataStream streaming operators.
CRTP class template from which a derived class can inherit operator== implemented using its compare f...
CRTP class template which will generate marshalling operators for a derived class with its own marsha...
CRTP class template from which a derived class can inherit operator< implemented using its compare fu...
CRTP class template from which a derived class can inherit string streaming operations.
Free functions in swift::misc.
auto makeRange(I begin, I2 end) -> CRange< I >
Returns a CRange constructed from begin and end iterators of deduced types.
void registerMetaValueType()
This registers the value type T with the swift::misc:: meta type system, making it available for use ...
void push_back(QList< T >::parameter_type value)
QVariant fromValue(T &&value)
#define SWIFT_MISC_EXPORT
Export a class or function from the library.