6 #ifndef SWIFT_MISC_VARIANT_H
7 #define SWIFT_MISC_VARIANT_H
10 #include <type_traits>
12 #include <QDBusArgument>
14 #include <QJsonObject>
15 #include <QMetaSequence>
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 m_v.setValue(std::forward<T>(value));
150 template <
typename T>
159 template <
typename T>
162 return canConvert<T>() ? value<T>() : def;
178 template <
typename T>
181 return canConvert(qMetaTypeId<T>());
197 bool toBool()
const {
return m_v.toBool(); }
200 int toInt(
bool *ok =
nullptr)
const {
return m_v.toInt(ok); }
203 qlonglong
toLongLong(
bool *ok =
nullptr)
const {
return m_v.toLongLong(ok); }
206 qulonglong
toULongLong(
bool *ok =
nullptr)
const {
return m_v.toULongLong(ok); }
212 double toDouble(
bool *ok =
nullptr)
const {
return m_v.toDouble(ok); }
224 bool isNull()
const {
return m_v.isNull(); }
227 bool isValid()
const {
return m_v.isValid(); }
232 const int type = m_v.typeId();
233 return type >= QMetaType::Type::User ? QMetaType::Type::User :
static_cast<QMetaType::Type>(type);
237 const char *
typeName()
const {
return m_v.typeName(); }
308 bool isA(
int metaTypeId)
const;
316 private_ns::IValueObjectMetaInfo *getValueObjectMetaInfo()
const
318 return private_ns::getValueObjectMetaInfo(m_v);
320 void *data() {
return m_v.
data(); }
321 const void *data()
const {
return m_v.
data(); }
323 static int compareImpl(
const CVariant &,
const CVariant &);
324 size_t getValueHash()
const;
326 template <
typename T>
330 copy.convert(qMetaTypeId<T>());
331 return *
static_cast<const T *
>(copy.data());
333 template <
typename T>
336 return toImpl<QList<T>>();
338 template <
typename T>
339 CSequence<T> to(tag<CSequence<T>>)
const
341 return toImpl<CSequence<T>>();
343 template <
typename T>
346 using VT =
typename T::value_type;
358 bool isVariantList()
const;
364 namespace swift::misc::private_ns
367 template <
typename T>
368 void MetaTypeHelper<T>::maybeRegisterMetaList()
370 if constexpr (canConvertVariantList<T>(0))
378 QMetaType::registerConverter<T, QVector<CVariant>>([](
const T &list) ->
QVector<CVariant> {
379 return list.transform([](
const typename T::value_type &v) {
return CVariant::fromValue(v); });
381 QMetaType::registerConverter<QVector<CVariant>, T>([](
const QVector<CVariant> &list) -> T {
382 return makeRange(list).transform([](
const CVariant &v) {
return v.value<
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...
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.
CVariant()=default
Default constructor.
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.
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::fromValue() 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.
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.