mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-08 12:45:35 +08:00
refs #413 Breaking circular dependencies by moving mixin classes from valueobject.h to the files where they fit best:
* DBus mixins moved to dbus.h * Json mixins moved to json.h * Icon mixin moved to icon.h * Comparison mixins moved to compare.h * Hash and string mixins moved to blackmiscfreefunctions.h * Index mixin moved to propertyindexvariantmap.h * MetaType mixins moved to variant.h * registerMetaValueType moved to variant.h * valueobject_private.h renamed to variant_private.h
This commit is contained in:
@@ -16,9 +16,11 @@
|
||||
#include "dbus.h"
|
||||
#include "tuple.h"
|
||||
#include "json.h"
|
||||
#include "icons.h"
|
||||
#include "compare.h"
|
||||
#include "variant.h"
|
||||
#include "propertyindexvariantmap.h"
|
||||
#include "iconlist.h"
|
||||
#include "blackmiscfreefunctions.h"
|
||||
#include "valueobject_private.h"
|
||||
#include <QtDBus/QDBusMetaType>
|
||||
#include <QString>
|
||||
#include <QtGlobal>
|
||||
@@ -33,30 +35,6 @@
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
class CPropertyIndex;
|
||||
class CPropertyIndexList;
|
||||
class CPropertyIndexVariantMap;
|
||||
class CIcon;
|
||||
class CVariant;
|
||||
class CEmpty;
|
||||
|
||||
/*!
|
||||
* This registers the value type T with the BlackMisc meta type system,
|
||||
* making it available for use with the extended feature set of BlackMisc::CVariant.
|
||||
*
|
||||
* The implementation (ab)uses the QMetaType converter function registration mechanism
|
||||
* to store a type-erased representation of the set of operations supported by T.
|
||||
* Unlike the singleton pattern, this approach means that CVariant can be used in plugins.
|
||||
*/
|
||||
template <typename T>
|
||||
void registerMetaValueType()
|
||||
{
|
||||
if (QMetaType::hasRegisteredConverterFunction<T, Private::IValueObjectMetaInfo *>()) { return; }
|
||||
auto converter = [](const T &) { static Private::CValueObjectMetaInfo<T> info; return &info; };
|
||||
bool ok = QMetaType::registerConverter<T, Private::IValueObjectMetaInfo *>(converter);
|
||||
Q_ASSERT(ok);
|
||||
Q_UNUSED(ok);
|
||||
}
|
||||
|
||||
/*!
|
||||
* Default base class for CValueObject.
|
||||
@@ -81,512 +59,6 @@ namespace BlackMisc
|
||||
~CEmpty() = default;
|
||||
};
|
||||
|
||||
namespace Mixin
|
||||
{
|
||||
|
||||
/*!
|
||||
* CRTP class template from which a derived class can inherit common methods dealing with the metatype of the class.
|
||||
*/
|
||||
template <class Derived, class... AdditionalTypes>
|
||||
class MetaType
|
||||
{
|
||||
public:
|
||||
//! Register metadata
|
||||
static void registerMetadata()
|
||||
{
|
||||
Private::MetaTypeHelper<Derived>::maybeRegisterMetaType();
|
||||
|
||||
[](...){}((qRegisterMetaType<AdditionalTypes>(), qDBusRegisterMetaType<AdditionalTypes>(), 0)...);
|
||||
}
|
||||
|
||||
//! Returns the Qt meta type ID of this object.
|
||||
int getMetaTypeId() const
|
||||
{
|
||||
return Private::MetaTypeHelper<Derived>::maybeGetMetaTypeId();
|
||||
}
|
||||
|
||||
//! Returns true if this object is an instance of the class with the given meta type ID, or one of its subclasses.
|
||||
bool isA(int metaTypeId) const
|
||||
{
|
||||
if (metaTypeId == QMetaType::UnknownType) { return false; }
|
||||
if (metaTypeId == getMetaTypeId()) { return true; }
|
||||
return baseIsA(static_cast<const MetaBaseOfT<Derived> *>(derived()), metaTypeId);
|
||||
}
|
||||
|
||||
//! Method to return CVariant
|
||||
//! \deprecated Use CVariant::to() instead.
|
||||
CVariant toCVariant() const;
|
||||
|
||||
//! Set from CVariant
|
||||
//! \deprecated Use CVariant::from() instead.
|
||||
void convertFromCVariant(const CVariant &variant);
|
||||
|
||||
//! Return QVariant, used with DBus QVariant lists
|
||||
//! \deprecated Use QVariant::fromValue() instead.
|
||||
QVariant toQVariant() const
|
||||
{
|
||||
return Private::MetaTypeHelper<Derived>::maybeToQVariant(*derived());
|
||||
}
|
||||
|
||||
//! Set from QVariant
|
||||
//! \deprecated Use QVariant::value() instead.
|
||||
void convertFromQVariant(const QVariant &variant)
|
||||
{
|
||||
return Private::MetaTypeHelper<Derived>::maybeConvertFromQVariant(*derived(), variant);
|
||||
}
|
||||
|
||||
private:
|
||||
const Derived *derived() const { return static_cast<const Derived *>(this); }
|
||||
Derived *derived() { return static_cast<Derived *>(this); }
|
||||
|
||||
template <typename Base2> static bool baseIsA(const Base2 *base, int metaTypeId) { return base->isA(metaTypeId); }
|
||||
static bool baseIsA(const void *, int) { return false; }
|
||||
};
|
||||
|
||||
/*!
|
||||
* Variant of MetaType mixin which also registers QList<Derived> with the type system.
|
||||
*/
|
||||
template <class Derived>
|
||||
class MetaTypeAndQList : public MetaType<Derived, QList<Derived>>
|
||||
{};
|
||||
|
||||
/*!
|
||||
* When a derived class and a base class both inherit from Mixin::MetaType,
|
||||
* the derived class uses this macro to disambiguate the inherited members.
|
||||
*/
|
||||
# define BLACKMISC_DECLARE_USING_MIXIN_METATYPE(DERIVED) \
|
||||
using ::BlackMisc::Mixin::MetaType<DERIVED>::registerMetadata; \
|
||||
using ::BlackMisc::Mixin::MetaType<DERIVED>::getMetaTypeId; \
|
||||
using ::BlackMisc::Mixin::MetaType<DERIVED>::isA; \
|
||||
using ::BlackMisc::Mixin::MetaType<DERIVED>::toCVariant; \
|
||||
using ::BlackMisc::Mixin::MetaType<DERIVED>::toQVariant; \
|
||||
using ::BlackMisc::Mixin::MetaType<DERIVED>::convertFromCVariant; \
|
||||
using ::BlackMisc::Mixin::MetaType<DERIVED>::convertFromQVariant;
|
||||
|
||||
/*!
|
||||
* When a derived class and a base class both inherit from Mixin::MetaType,
|
||||
* the derived class uses this macro to disambiguate the inherited members.
|
||||
*/
|
||||
# define BLACKMISC_DECLARE_USING_MIXIN_METATYPE_AND_QLIST(DERIVED) \
|
||||
using ::BlackMisc::Mixin::MetaTypeAndQList<DERIVED>::registerMetadata; \
|
||||
using ::BlackMisc::Mixin::MetaTypeAndQList<DERIVED>::getMetaTypeId; \
|
||||
using ::BlackMisc::Mixin::MetaTypeAndQList<DERIVED>::isA; \
|
||||
using ::BlackMisc::Mixin::MetaTypeAndQList<DERIVED>::toCVariant; \
|
||||
using ::BlackMisc::Mixin::MetaTypeAndQList<DERIVED>::toQVariant; \
|
||||
using ::BlackMisc::Mixin::MetaTypeAndQList<DERIVED>::convertFromCVariant; \
|
||||
using ::BlackMisc::Mixin::MetaTypeAndQList<DERIVED>::convertFromQVariant;
|
||||
|
||||
/*!
|
||||
* CRTP class template from which a derived class can inherit common methods dealing with hashing instances by metatuple.
|
||||
*/
|
||||
template <class Derived>
|
||||
class HashByTuple : private Private::EncapsulationBreaker
|
||||
{
|
||||
public:
|
||||
//! qHash overload, needed for storing value in a QSet.
|
||||
friend uint qHash(const Derived &value, uint seed = 0)
|
||||
{
|
||||
return ::qHash(hashImpl(value), seed);
|
||||
}
|
||||
|
||||
private:
|
||||
static uint hashImpl(const Derived &value)
|
||||
{
|
||||
return BlackMisc::qHash(toMetaTuple(value)) ^ baseHash(static_cast<const BaseOfT<Derived> *>(&value));
|
||||
}
|
||||
|
||||
template <typename T> static uint baseHash(const T *base) { return qHash(*base); }
|
||||
static uint baseHash(const void *) { return 0; }
|
||||
};
|
||||
|
||||
/*!
|
||||
* CRTP class template which will generate marshalling operators for a derived class with its own marshalling implementation.
|
||||
*/
|
||||
template <class Derived>
|
||||
class DBusOperators
|
||||
{
|
||||
public:
|
||||
//! Unmarshalling operator >>, DBus to object
|
||||
friend const QDBusArgument &operator>>(const QDBusArgument &arg, Derived &obj)
|
||||
{
|
||||
arg.beginStructure();
|
||||
obj.unmarshallFromDbus(arg);
|
||||
arg.endStructure();
|
||||
return arg;
|
||||
}
|
||||
|
||||
//! Marshalling operator <<, object to DBus
|
||||
friend QDBusArgument &operator<<(QDBusArgument &arg, const Derived &obj)
|
||||
{
|
||||
arg.beginStructure();
|
||||
obj.marshallToDbus(arg);
|
||||
arg.endStructure();
|
||||
return arg;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* CRTP class template from which a derived class can inherit common methods dealing with marshalling instances by metatuple.
|
||||
*/
|
||||
template <class Derived>
|
||||
class DBusByTuple : public DBusOperators<Derived>, private Private::EncapsulationBreaker
|
||||
{
|
||||
public:
|
||||
//! Marshall without begin/endStructure, for when composed within another object
|
||||
void marshallToDbus(QDBusArgument &arg) const
|
||||
{
|
||||
baseMarshall(static_cast<const BaseOfT<Derived> *>(derived()), arg);
|
||||
using BlackMisc::operator<<;
|
||||
arg << Private::EncapsulationBreaker::toMetaTuple(*derived());
|
||||
}
|
||||
|
||||
//! Unmarshall without begin/endStructure, for when composed within another object
|
||||
void unmarshallFromDbus(const QDBusArgument &arg)
|
||||
{
|
||||
baseUnmarshall(static_cast<BaseOfT<Derived> *>(derived()), arg);
|
||||
using BlackMisc::operator>>;
|
||||
arg >> Private::EncapsulationBreaker::toMetaTuple(*derived());
|
||||
}
|
||||
|
||||
private:
|
||||
const Derived *derived() const { return static_cast<const Derived *>(this); }
|
||||
Derived *derived() { return static_cast<Derived *>(this); }
|
||||
|
||||
template <typename T> static void baseMarshall(const T *base, QDBusArgument &arg) { base->marshallToDbus(arg); }
|
||||
template <typename T> static void baseUnmarshall(T *base, const QDBusArgument &arg) { base->unmarshallFromDbus(arg); }
|
||||
static void baseMarshall(const void *, QDBusArgument &) {}
|
||||
static void baseUnmarshall(void *, const QDBusArgument &) {}
|
||||
};
|
||||
|
||||
/*!
|
||||
* When a derived class and a base class both inherit from Mixin::DBusByTuple,
|
||||
* the derived class uses this macro to disambiguate the inherited members.
|
||||
*/
|
||||
# define BLACKMISC_DECLARE_USING_MIXIN_DBUS(DERIVED) \
|
||||
using ::BlackMisc::Mixin::DBusByTuple<DERIVED>::marshallToDbus; \
|
||||
using ::BlackMisc::Mixin::DBusByTuple<DERIVED>::unmarshallFromDbus;
|
||||
|
||||
/*!
|
||||
* CRTP class template which will generate marshalling operators for a derived class with its own marshalling implementation.
|
||||
*/
|
||||
template <class Derived>
|
||||
class JsonOperators
|
||||
{
|
||||
public:
|
||||
//! operator >> for JSON
|
||||
friend const QJsonObject &operator>>(const QJsonObject &json, Derived &obj)
|
||||
{
|
||||
obj.convertFromJson(json);
|
||||
return json;
|
||||
}
|
||||
|
||||
//! operator >> for JSON
|
||||
friend const QJsonValue &operator>>(const QJsonValue &json, Derived &obj)
|
||||
{
|
||||
obj.convertFromJson(json.toObject());
|
||||
return json;
|
||||
}
|
||||
|
||||
//! operator >> for JSON
|
||||
friend const QJsonValueRef &operator>>(const QJsonValueRef &json, Derived &obj)
|
||||
{
|
||||
obj.convertFromJson(json.toObject());
|
||||
return json;
|
||||
}
|
||||
|
||||
//! operator << for JSON
|
||||
friend QJsonArray &operator<<(QJsonArray &json, const Derived &obj)
|
||||
{
|
||||
json.append(obj.toJson());
|
||||
return json;
|
||||
}
|
||||
|
||||
//! operator << for JSON
|
||||
friend QJsonObject &operator<<(QJsonObject &json, const std::pair<QString, const Derived &> &value)
|
||||
{
|
||||
json.insert(value.first, QJsonValue(value.second.toJson()));
|
||||
return json;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* CRTP class template from which a derived class can inherit common methods dealing with JSON by metatuple.
|
||||
*/
|
||||
template <class Derived>
|
||||
class JsonByTuple : public JsonOperators<Derived>, private Private::EncapsulationBreaker
|
||||
{
|
||||
public:
|
||||
//! Cast to JSON object
|
||||
QJsonObject toJson() const
|
||||
{
|
||||
QJsonObject json = BlackMisc::serializeJson(Private::EncapsulationBreaker::toMetaTuple(*derived()));
|
||||
return Json::appendJsonObject(json, baseToJson(static_cast<const BaseOfT<Derived> *>(derived())));
|
||||
}
|
||||
|
||||
//! Assign from JSON object
|
||||
void convertFromJson(const QJsonObject &json)
|
||||
{
|
||||
baseConvertFromJson(static_cast<BaseOfT<Derived> *>(derived()), json);
|
||||
BlackMisc::deserializeJson(json, Private::EncapsulationBreaker::toMetaTuple(*derived()));
|
||||
}
|
||||
|
||||
private:
|
||||
const Derived *derived() const { return static_cast<const Derived *>(this); }
|
||||
Derived *derived() { return static_cast<Derived *>(this); }
|
||||
|
||||
template <typename T> static QJsonObject baseToJson(const T *base) { return base->toJson(); }
|
||||
template <typename T> static void baseConvertFromJson(T *base, const QJsonObject &json) { base->convertFromJson(json); }
|
||||
static QJsonObject baseToJson(const void *) { return {}; }
|
||||
static void baseConvertFromJson(void *, const QJsonObject &) {}
|
||||
};
|
||||
|
||||
/*!
|
||||
* When a derived class and a base class both inherit from Mixin::JsonByTuple,
|
||||
* the derived class uses this macro to disambiguate the inherited members.
|
||||
*/
|
||||
# define BLACKMISC_DECLARE_USING_MIXIN_JSON(DERIVED) \
|
||||
using ::BlackMisc::Mixin::JsonByTuple<DERIVED>::toJson; \
|
||||
using ::BlackMisc::Mixin::JsonByTuple<DERIVED>::convertFromJson;
|
||||
|
||||
/*!
|
||||
* CRTP class template from which a derived class can inherit operator== implemented using its compare function.
|
||||
*/
|
||||
template <class Derived>
|
||||
class EqualsByCompare
|
||||
{
|
||||
public:
|
||||
//! Equals
|
||||
friend bool operator ==(const Derived &a, const Derived &b) { return compare(a, b) == 0; }
|
||||
|
||||
//! Not equal
|
||||
friend bool operator !=(const Derived &a, const Derived &b) { return compare(a, b) != 0; }
|
||||
};
|
||||
|
||||
/*!
|
||||
* CRTP class template from which a derived class can inherit operator== implemented by metatuple.
|
||||
*/
|
||||
template <class Derived>
|
||||
class EqualsByTuple : private Private::EncapsulationBreaker
|
||||
{
|
||||
public:
|
||||
//! Equals
|
||||
friend bool operator ==(const Derived &a, const Derived &b) { return equals(a, b); }
|
||||
|
||||
//! Not equal
|
||||
friend bool operator !=(const Derived &a, const Derived &b) { return ! equals(a, b); }
|
||||
|
||||
private:
|
||||
static bool equals(const Derived &a, const Derived &b)
|
||||
{
|
||||
return toMetaTuple(a) == toMetaTuple(b) && baseEquals(static_cast<const BaseOfT<Derived> *>(&a), static_cast<const BaseOfT<Derived> *>(&b));
|
||||
}
|
||||
template <typename T> static bool baseEquals(const T *a, const T *b) { return *a == *b; }
|
||||
static bool baseEquals(const void *, const void *) { return true; }
|
||||
};
|
||||
|
||||
/*!
|
||||
* CRTP class template from which a derived class can inherit operator< implemented using its compare function.
|
||||
*/
|
||||
template <class Derived>
|
||||
class LessThanByCompare
|
||||
{
|
||||
public:
|
||||
//! Less than
|
||||
friend bool operator <(const Derived &a, const Derived &b) { return compare(a, b) < 0; }
|
||||
|
||||
//! Greater than
|
||||
friend bool operator >(const Derived &a, const Derived &b) { return compare(a, b) > 0; }
|
||||
|
||||
//! Less than or equal
|
||||
friend bool operator <=(const Derived &a, const Derived &b) { return compare(a, b) <= 0; }
|
||||
|
||||
//! Greater than or equal
|
||||
friend bool operator >=(const Derived &a, const Derived &b) { return compare(a, b) >= 0; }
|
||||
};
|
||||
|
||||
/*!
|
||||
* CRTP class template from which a derived class can inherit operator< implemented by metatuple.
|
||||
*/
|
||||
template <class Derived>
|
||||
class LessThanByTuple : private Private::EncapsulationBreaker
|
||||
{
|
||||
public:
|
||||
//! Less than
|
||||
friend bool operator <(const Derived &a, const Derived &b) { return less(a, b); }
|
||||
|
||||
//! Greater than
|
||||
friend bool operator >(const Derived &a, const Derived &b) { return less(b, a); }
|
||||
|
||||
//! Less than or equal
|
||||
friend bool operator <=(const Derived &a, const Derived &b) { return ! less(b, a); }
|
||||
|
||||
//! Greater than or equal
|
||||
friend bool operator >=(const Derived &a, const Derived &b) { return ! less(a, b); }
|
||||
|
||||
private:
|
||||
static bool less(const Derived &a, const Derived &b)
|
||||
{
|
||||
if (baseLess(static_cast<const BaseOfT<Derived> *>(&a), static_cast<const BaseOfT<Derived> *>(&b))) { return true; }
|
||||
return toMetaTuple(a) < toMetaTuple(b);
|
||||
}
|
||||
template <typename T> static bool baseLess(const T *a, const T *b) { return *a < *b; }
|
||||
static bool baseLess(const void *, const void *) { return false; }
|
||||
};
|
||||
|
||||
/*!
|
||||
* CRTP class template from which a derived class can inherit non-member compare() implemented by metatuple.
|
||||
*/
|
||||
template <class Derived>
|
||||
class CompareByTuple : private Private::EncapsulationBreaker
|
||||
{
|
||||
public:
|
||||
//! Return negative, zero, or positive if a is less than, equal to, or greater than b.
|
||||
friend int compare(const Derived &a, const Derived &b) { return compareImpl(a, b); }
|
||||
|
||||
private:
|
||||
static int compareImpl(const Derived &a, const Derived &b)
|
||||
{
|
||||
int baseCmp = baseCompare(static_cast<const BaseOfT<Derived> *>(&a), static_cast<const BaseOfT<Derived> *>(&b));
|
||||
if (baseCmp) { return baseCmp; }
|
||||
return BlackMisc::compare(toMetaTuple(a), toMetaTuple(b));
|
||||
}
|
||||
template <typename T> static int baseCompare(const T *a, const T *b) { return compare(*a, *b); }
|
||||
static int baseCompare(const void *, const void *) { return 0; }
|
||||
};
|
||||
|
||||
/*!
|
||||
* CRTP class template from which a derived class can inherit string streaming operations.
|
||||
*/
|
||||
template <class Derived>
|
||||
class String
|
||||
{
|
||||
public:
|
||||
//! Stream << overload to be used in debugging messages
|
||||
friend QDebug operator<<(QDebug debug, const Derived &obj)
|
||||
{
|
||||
debug << obj.stringForStreaming();
|
||||
return debug;
|
||||
}
|
||||
|
||||
//! Operator << when there is no debug stream
|
||||
friend QNoDebug operator<<(QNoDebug nodebug, const Derived &obj)
|
||||
{
|
||||
Q_UNUSED(obj);
|
||||
return nodebug;
|
||||
}
|
||||
|
||||
//! Operator << based on text stream
|
||||
friend QTextStream &operator<<(QTextStream &stream, const Derived &obj)
|
||||
{
|
||||
stream << obj.stringForStreaming();
|
||||
return stream;
|
||||
}
|
||||
|
||||
//! Operator << for QDataStream
|
||||
friend QDataStream &operator<<(QDataStream &stream, const Derived &obj)
|
||||
{
|
||||
stream << obj.stringForStreaming();
|
||||
return stream;
|
||||
}
|
||||
|
||||
//! Stream operator << for std::cout
|
||||
friend std::ostream &operator<<(std::ostream &ostr, const Derived &obj)
|
||||
{
|
||||
ostr << obj.stringForStreaming().toStdString();
|
||||
return ostr;
|
||||
}
|
||||
|
||||
//! Cast as QString
|
||||
QString toQString(bool i18n = false) const { return derived()->convertToQString(i18n); }
|
||||
|
||||
//! Cast to pretty-printed QString
|
||||
QString toFormattedQString(bool i18n = false) const { return derived()->toQString(i18n); }
|
||||
|
||||
//! To std string
|
||||
std::string toStdString(bool i18n = false) const { return derived()->convertToQString(i18n).toStdString(); }
|
||||
|
||||
//! String for streaming operators
|
||||
QString stringForStreaming() const { return derived()->convertToQString(); }
|
||||
|
||||
private:
|
||||
const Derived *derived() const { return static_cast<const Derived *>(this); }
|
||||
Derived *derived() { return static_cast<Derived *>(this); }
|
||||
};
|
||||
|
||||
/*!
|
||||
* When a derived class and a base class both inherit from Mixin::String,
|
||||
* the derived class uses this macro to disambiguate the inherited members.
|
||||
*/
|
||||
# define BLACKMISC_DECLARE_USING_MIXIN_STRING(DERIVED) \
|
||||
using ::BlackMisc::Mixin::String<DERIVED>::toQString; \
|
||||
using ::BlackMisc::Mixin::String<DERIVED>::toFormattedQString; \
|
||||
using ::BlackMisc::Mixin::String<DERIVED>::toStdString; \
|
||||
using ::BlackMisc::Mixin::String<DERIVED>::stringForStreaming;
|
||||
|
||||
/*!
|
||||
* CRTP class template from which a derived class can inherit property indexing functions.
|
||||
*/
|
||||
template <class Derived>
|
||||
class Index
|
||||
{
|
||||
public:
|
||||
//! Base class enums
|
||||
enum ColumnIndex
|
||||
{
|
||||
IndexPixmap = 10, // manually set to avoid circular dependencies
|
||||
IndexIcon,
|
||||
IndexString
|
||||
};
|
||||
|
||||
//! Update by variant map
|
||||
//! \return number of values changed, with skipEqualValues equal values will not be changed
|
||||
CPropertyIndexList apply(const BlackMisc::CPropertyIndexVariantMap &indexMap, bool skipEqualValues = false); // implemented later due to cyclic include dependency
|
||||
|
||||
//! Set property by index
|
||||
void setPropertyByIndex(const CVariant &variant, const CPropertyIndex &index); // implemented later due to cyclic include dependency
|
||||
|
||||
//! Property by index
|
||||
CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const; // implemented later due to cyclic include dependency
|
||||
|
||||
//! Property by index as String
|
||||
QString propertyByIndexAsString(const CPropertyIndex &index, bool i18n = false) const; // implemented later due to cyclic include dependency
|
||||
|
||||
//! Is given variant equal to value of property index?
|
||||
bool equalsPropertyByIndex(const CVariant &compareValue, const CPropertyIndex &index) const; // implemented later due to cyclic include dependency
|
||||
|
||||
private:
|
||||
const Derived *derived() const { return static_cast<const Derived *>(this); }
|
||||
Derived *derived() { return static_cast<Derived *>(this); }
|
||||
};
|
||||
|
||||
/*!
|
||||
* CRTP class template from which a derived class can inherit icon-related functions.
|
||||
*/
|
||||
template <class Derived, CIcons::IconIndex IconIndex = CIcons::StandardIconUnknown16>
|
||||
class Icon
|
||||
{
|
||||
public:
|
||||
//! As icon, not implemented by all classes
|
||||
CIcon toIcon() const; // implemented later due to cyclic include dependency
|
||||
|
||||
//! As pixmap, required for most GUI views
|
||||
QPixmap toPixmap() const; // implemented later due to cyclic include dependency
|
||||
|
||||
private:
|
||||
const Derived *derived() const { return static_cast<const Derived *>(this); }
|
||||
Derived *derived() { return static_cast<Derived *>(this); }
|
||||
};
|
||||
|
||||
/*!
|
||||
* When a derived class and a base class both inherit from Mixin::Icon,
|
||||
* the derived class uses this macro to disambiguate the inherited members.
|
||||
*/
|
||||
# define BLACKMISC_DECLARE_USING_MIXIN_ICON(DERIVED) \
|
||||
using ::BlackMisc::Mixin::Icon<DERIVED>::toIcon; \
|
||||
using ::BlackMisc::Mixin::Icon<DERIVED>::toPixmap;
|
||||
|
||||
}
|
||||
|
||||
/*!
|
||||
* Standard implementation of CValueObject using meta tuple system.
|
||||
*
|
||||
@@ -702,108 +174,4 @@ namespace BlackMisc
|
||||
|
||||
} // namespace
|
||||
|
||||
// TODO Includes due to cyclic dependencies can be removed when CValueObject is split into parts along policy boundaries.
|
||||
#include "variant.h"
|
||||
#include "propertyindex.h"
|
||||
#include "propertyindexlist.h"
|
||||
#include "iconlist.h"
|
||||
|
||||
// TODO Implementations of templates that must appear after those includes, should be moved at the same time that policies are refactored.
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace Mixin
|
||||
{
|
||||
template <class Derived, class... AdditionalTypes>
|
||||
CVariant MetaType<Derived, AdditionalTypes...>::toCVariant() const
|
||||
{
|
||||
return CVariant(derived()->toQVariant());
|
||||
}
|
||||
template <class Derived, class... AdditionalTypes>
|
||||
void MetaType<Derived, AdditionalTypes...>::convertFromCVariant(const CVariant &variant)
|
||||
{
|
||||
derived()->convertFromQVariant(variant.getQVariant());
|
||||
}
|
||||
template <class Derived>
|
||||
CPropertyIndexList Index<Derived>::apply(const BlackMisc::CPropertyIndexVariantMap &indexMap, bool skipEqualValues)
|
||||
{
|
||||
if (indexMap.isEmpty()) return {};
|
||||
|
||||
CPropertyIndexList changed;
|
||||
const auto &map = indexMap.map();
|
||||
for (auto it = map.begin(); it != map.end(); ++it)
|
||||
{
|
||||
const CVariant value = it.value().toCVariant();
|
||||
const CPropertyIndex index = it.key();
|
||||
if (skipEqualValues)
|
||||
{
|
||||
bool equal = derived()->equalsPropertyByIndex(value, index);
|
||||
if (equal) { continue; }
|
||||
}
|
||||
derived()->setPropertyByIndex(value, index);
|
||||
changed.push_back(index);
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
template <class Derived>
|
||||
void Index<Derived>::setPropertyByIndex(const CVariant &variant, const CPropertyIndex &index)
|
||||
{
|
||||
if (index.isMyself())
|
||||
{
|
||||
derived()->convertFromCVariant(variant);
|
||||
return;
|
||||
}
|
||||
|
||||
// not all classes have implemented nesting
|
||||
const QString m = QString("Property by index not found (setter), index: ").append(index.toQString());
|
||||
qFatal("%s", qPrintable(m));
|
||||
}
|
||||
template <class Derived>
|
||||
CVariant Index<Derived>::propertyByIndex(const CPropertyIndex &index) const
|
||||
{
|
||||
if (index.isMyself())
|
||||
{
|
||||
return derived()->toCVariant();
|
||||
}
|
||||
auto i = index.frontCasted<ColumnIndex>();
|
||||
switch (i)
|
||||
{
|
||||
case IndexIcon:
|
||||
return CVariant::from(derived()->toIcon());
|
||||
case IndexPixmap:
|
||||
return CVariant::from(derived()->toPixmap());
|
||||
case IndexString:
|
||||
return CVariant(derived()->toQString());
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// not all classes have implemented nesting
|
||||
const QString m = QString("Property by index not found, index: ").append(index.toQString());
|
||||
qFatal("%s", qPrintable(m));
|
||||
return {};
|
||||
}
|
||||
template <class Derived>
|
||||
QString Index<Derived>::propertyByIndexAsString(const CPropertyIndex &index, bool i18n) const
|
||||
{
|
||||
// default implementation, requires propertyByIndex
|
||||
return derived()->propertyByIndex(index).toQString(i18n);
|
||||
}
|
||||
template <class Derived>
|
||||
bool Index<Derived>::equalsPropertyByIndex(const CVariant &compareValue, const CPropertyIndex &index) const
|
||||
{
|
||||
return derived()->propertyByIndex(index) == compareValue;
|
||||
}
|
||||
template <class Derived, CIcons::IconIndex IconIndex>
|
||||
CIcon Icon<Derived, IconIndex>::toIcon() const
|
||||
{
|
||||
return CIconList::iconByIndex(IconIndex);
|
||||
}
|
||||
template <class Derived, CIcons::IconIndex IconIndex>
|
||||
QPixmap Icon<Derived, IconIndex>::toPixmap() const
|
||||
{
|
||||
return derived()->toIcon().toPixmap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // guard
|
||||
|
||||
Reference in New Issue
Block a user