From 30cc7048ac2cf7aeeae0f4a8e8e11a835faf5545 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Sun, 3 May 2015 19:59:06 +0100 Subject: [PATCH] refs #413 Decomposed dbus-related functions of CValueObject into Mixin::DBusByTuple. --- src/blackmisc/containerbase.h | 5 +- src/blackmisc/dictionary.h | 5 +- src/blackmisc/logpattern.h | 5 +- src/blackmisc/pq/measurementunit.h | 6 +- src/blackmisc/pq/physicalquantity.h | 4 +- src/blackmisc/pq/units.h | 27 ++--- src/blackmisc/propertyindexvariantmap.h | 5 +- src/blackmisc/valueobject.h | 134 +++++++++++++++++------- src/blackmisc/valueobject_policy.h | 5 + src/blackmisc/variant.h | 4 +- 10 files changed, 130 insertions(+), 70 deletions(-) diff --git a/src/blackmisc/containerbase.h b/src/blackmisc/containerbase.h index 85bf96268..696b941cf 100644 --- a/src/blackmisc/containerbase.h +++ b/src/blackmisc/containerbase.h @@ -160,8 +160,9 @@ namespace BlackMisc //! \copydoc BlackMisc::CValueObject::getMetaTypeId int getMetaTypeId() const { return qMetaTypeId>(); } + public: //! \copydoc BlackMisc::CValueObject::marshallToDbus - virtual void marshallToDbus(QDBusArgument &argument) const override + void marshallToDbus(QDBusArgument &argument) const { argument.beginArray(qMetaTypeId()); std::for_each(derived().cbegin(), derived().cend(), [ & ](const T & value) { argument << value; }); @@ -169,7 +170,7 @@ namespace BlackMisc } //! \copydoc BlackMisc::CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override + void unmarshallFromDbus(const QDBusArgument &argument) { argument.beginArray(); while (!argument.atEnd()) { T value; argument >> value; derived().insert(value); } diff --git a/src/blackmisc/dictionary.h b/src/blackmisc/dictionary.h index 0aaf77e89..8bcfd350f 100644 --- a/src/blackmisc/dictionary.h +++ b/src/blackmisc/dictionary.h @@ -418,14 +418,15 @@ namespace BlackMisc return str += "}"; } + public: //! \copydoc BlackMisc::CValueObject::marshallToDbus - virtual void marshallToDbus(QDBusArgument &argument) const override + void marshallToDbus(QDBusArgument &argument) const { argument << m_impl; } //! \copydoc BlackMisc::CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override + void unmarshallFromDbus(const QDBusArgument &argument) { argument >> m_impl; } diff --git a/src/blackmisc/logpattern.h b/src/blackmisc/logpattern.h index 63f344ec1..e79fcc43d 100644 --- a/src/blackmisc/logpattern.h +++ b/src/blackmisc/logpattern.h @@ -86,11 +86,12 @@ namespace BlackMisc //! \copydoc CValueObject::convertToQString() virtual QString convertToQString(bool i18n = false) const override; + public: //! \copydoc CValueObject::marshallToDbus() - virtual void marshallToDbus(QDBusArgument &argument) const override; + void marshallToDbus(QDBusArgument &argument) const; //! \copydoc CValueObject::marshallFromDbus() - virtual void unmarshallFromDbus(const QDBusArgument &argument) override; + void unmarshallFromDbus(const QDBusArgument &argument); private: bool checkInvariants() const; diff --git a/src/blackmisc/pq/measurementunit.h b/src/blackmisc/pq/measurementunit.h index f55de0d0a..36da00787 100644 --- a/src/blackmisc/pq/measurementunit.h +++ b/src/blackmisc/pq/measurementunit.h @@ -253,21 +253,21 @@ namespace BlackMisc return this->getSymbol(i18n); } + public: //! \copydoc CValueObject::marshallToDbus - virtual void marshallToDbus(QDBusArgument &argument) const override + void marshallToDbus(QDBusArgument &argument) const { argument << this->m_symbol; } //! \copydoc CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &) override + void unmarshallFromDbus(const QDBusArgument &) { // the concrete implementations will override this default // this is required so I can also stream None (*this) = CMeasurementUnit::None(); } - public: //! Default constructor for meta system //! \remarks Only public because the need, to use this with the metasystem CMeasurementUnit() : m_name("none"), m_symbol(""), m_epsilon(0), m_displayDigits(0) diff --git a/src/blackmisc/pq/physicalquantity.h b/src/blackmisc/pq/physicalquantity.h index 31711465f..b60c9cf94 100644 --- a/src/blackmisc/pq/physicalquantity.h +++ b/src/blackmisc/pq/physicalquantity.h @@ -193,10 +193,10 @@ namespace BlackMisc void makeNegative(); //! \copydoc CValueObject::marshallToDbus - virtual void marshallToDbus(QDBusArgument &argument) const override; + void marshallToDbus(QDBusArgument &argument) const; //! \copydoc CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override; + void unmarshallFromDbus(const QDBusArgument &argument); //! \copydoc CValueObject::qHash uint getValueHash() const; diff --git a/src/blackmisc/pq/units.h b/src/blackmisc/pq/units.h index 44388a637..3012fa6b2 100644 --- a/src/blackmisc/pq/units.h +++ b/src/blackmisc/pq/units.h @@ -135,9 +135,8 @@ namespace BlackMisc return u; } - protected: //! \copydoc CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override + void unmarshallFromDbus(const QDBusArgument &argument) { QString unitName; argument >> unitName; @@ -226,9 +225,8 @@ namespace BlackMisc return u; } - protected: //! \copydoc CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override + void unmarshallFromDbus(const QDBusArgument &argument) { QString unitName; argument >> unitName; @@ -308,9 +306,8 @@ namespace BlackMisc return u; } - protected: //! \copydoc CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override + void unmarshallFromDbus(const QDBusArgument &argument) { QString unitName; argument >> unitName; @@ -399,9 +396,8 @@ namespace BlackMisc return u; } - protected: //! \copydoc CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override + void unmarshallFromDbus(const QDBusArgument &argument) { QString unitName; argument >> unitName; @@ -507,9 +503,8 @@ namespace BlackMisc return u; } - protected: //! \copydoc CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override + void unmarshallFromDbus(const QDBusArgument &argument) { QString unitName; argument >> unitName; @@ -590,9 +585,8 @@ namespace BlackMisc return u; } - protected: //! \copydoc CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override + void unmarshallFromDbus(const QDBusArgument &argument) { QString unitName; argument >> unitName; @@ -692,9 +686,8 @@ namespace BlackMisc return u; } - protected: //! \copydoc CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override + void unmarshallFromDbus(const QDBusArgument &argument) { QString unitName; argument >> unitName; @@ -818,9 +811,8 @@ namespace BlackMisc return u; } - protected: //! \copydoc CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override + void unmarshallFromDbus(const QDBusArgument &argument) { QString unitName; argument >> unitName; @@ -884,9 +876,8 @@ namespace BlackMisc return u; } - protected: //! \copydoc CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override + void unmarshallFromDbus(const QDBusArgument &argument) { QString unitName; argument >> unitName; diff --git a/src/blackmisc/propertyindexvariantmap.h b/src/blackmisc/propertyindexvariantmap.h index 5b46fd6b2..12013a01b 100644 --- a/src/blackmisc/propertyindexvariantmap.h +++ b/src/blackmisc/propertyindexvariantmap.h @@ -157,11 +157,12 @@ namespace BlackMisc //! \copydoc CValueObject::convertToQString virtual QString convertToQString(bool i18n = false) const override; + public: //! \copydoc CValueObject::marshallToDbus - virtual void marshallToDbus(QDBusArgument &argument) const override; + void marshallToDbus(QDBusArgument &argument) const; //! \copydoc CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override; + void unmarshallFromDbus(const QDBusArgument &argument); }; } diff --git a/src/blackmisc/valueobject.h b/src/blackmisc/valueobject.h index faea69d15..d041921d2 100644 --- a/src/blackmisc/valueobject.h +++ b/src/blackmisc/valueobject.h @@ -115,12 +115,6 @@ namespace BlackMisc //! From JSON static void convertFromJson(const QJsonObject &) {} - - //! Marshall to DBus - static void marshallToDbus(QDBusArgument &) {} - - //! Unmarshall from DBus - static void unmarshallFromDbus(const QDBusArgument &) {} }; /*! @@ -267,6 +261,98 @@ namespace BlackMisc class HashByTuple {}; + /*! + * CRTP class template from which a derived class can inherit common methods dealing with marshalling instances by metatuple. + */ + template + class DBusByTuple : private Private::EncapsulationBreaker + { + 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; + } + + //! Marshall without begin/endStructure, for when composed within another object + void marshallToDbus(QDBusArgument &arg) const + { + baseMarshall(static_cast(*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(*derived()), arg); + using BlackMisc::operator>>; + arg >> Private::EncapsulationBreaker::toMetaTuple(*derived()); + } + + private: + const Derived *derived() const { return static_cast(this); } + Derived *derived() { return static_cast(this); } + + template static void baseMarshall(const T &base, QDBusArgument &arg) { base.marshallToDbus(arg); } + template static void baseUnmarshall(T &base, const QDBusArgument &arg) { base.unmarshallFromDbus(arg); } + static void baseMarshall(const CEmpty &, QDBusArgument &) {} + static void baseUnmarshall(CEmpty &, const QDBusArgument &) {} + }; + + /*! + * Specialization of DBusByTuple for classes not registered with the tuple system. + */ + template + class DBusByTuple + { + 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; + } + + //! Do nothing + void marshallToDbus(QDBusArgument &arg) const { baseMarshall(static_cast(*derived()), arg); } + + //! Do nothing + void unmarshallFromDbus(const QDBusArgument &arg) { baseUnmarshall(static_cast(*derived()), arg); } + + private: + const Derived *derived() const { return static_cast(this); } + Derived *derived() { return static_cast(this); } + + template static void baseMarshall(const T &base, QDBusArgument &arg) { base.marshallToDbus(arg); } + template static void baseUnmarshall(T &base, const QDBusArgument &arg) { base.unmarshallFromDbus(arg); } + static void baseMarshall(const CEmpty &, QDBusArgument &) {} + static void baseUnmarshall(CEmpty &, const QDBusArgument &) {} + }; + } /*! @@ -283,13 +369,13 @@ namespace BlackMisc public Base, public Mixin::MetaType, public Mixin::HashByTuple::value>, + public Mixin::DBusByTuple::value>, private CValueObjectPolicy::Equals::template Ops, private CValueObjectPolicy::LessThan::template Ops, private CValueObjectPolicy::Compare::template Ops { static_assert(std::is_same::value || IsValueObject::value, "Base must be either CEmpty or derived from CValueObject"); - using DBusPolicy = typename CValueObjectPolicy::DBus; using JsonPolicy = typename CValueObjectPolicy::Json; using PropertyIndexPolicy = typename CValueObjectPolicy::PropertyIndex; @@ -330,24 +416,6 @@ namespace BlackMisc return ostr; } - //! Unmarshalling operator >>, DBus to object - friend const QDBusArgument &operator>>(const QDBusArgument &arg, Derived &obj) - { - arg.beginStructure(); - static_cast(obj).unmarshallFromDbus(arg); // virtual method is protected in Derived - arg.endStructure(); - return arg; - } - - //! Marshalling operator <<, object to DBus - friend QDBusArgument &operator<<(QDBusArgument &arg, const Derived &obj) - { - arg.beginStructure(); - static_cast(obj).marshallToDbus(arg); // virtual method is protected in Derived - arg.endStructure(); - return arg; - } - //! operator >> for JSON friend const QJsonObject &operator>>(const QJsonObject &json, Derived &valueObject) { @@ -480,19 +548,11 @@ namespace BlackMisc using Mixin::MetaType::getMetaTypeId; public: - //! Marshall to DBus - virtual void marshallToDbus(QDBusArgument &argument) const - { - BaseOrDummy::marshallToDbus(argument); - DBusPolicy::marshallImpl(argument, *derived()); - } + //! \copydoc BlackMisc::Mixin::DBusByTuple::marshallToDbus + using Mixin::DBusByTuple::value>::marshallToDbus; - //! Unmarshall from DBus - virtual void unmarshallFromDbus(const QDBusArgument &argument) - { - BaseOrDummy::unmarshallFromDbus(argument); - DBusPolicy::unmarshallImpl(argument, *derived()); - } + //! \copydoc BlackMisc::Mixin::DBusByTuple::unmarshallFromDbus + using Mixin::DBusByTuple::value>::unmarshallFromDbus; private: const Derived *derived() const { return static_cast(this); } diff --git a/src/blackmisc/valueobject_policy.h b/src/blackmisc/valueobject_policy.h index 65346bfe1..77bfd231f 100644 --- a/src/blackmisc/valueobject_policy.h +++ b/src/blackmisc/valueobject_policy.h @@ -339,6 +339,11 @@ namespace BlackMisc template static void unmarshallImpl(const QDBusArgument &, T &) {} }; + + //! \private Detect the policy of T, following inheritance. + template ::DBus> struct IsMetaTuple : public std::false_type {}; + template struct IsMetaTuple : public std::true_type {}; + template struct IsMetaTuple : public IsMetaTuple {}; } namespace Json diff --git a/src/blackmisc/variant.h b/src/blackmisc/variant.h index 6d4595c0a..cb4bbbe0f 100644 --- a/src/blackmisc/variant.h +++ b/src/blackmisc/variant.h @@ -185,10 +185,10 @@ namespace BlackMisc virtual void convertFromJson(const QJsonObject &json) override; //! \copydoc CValueObject::marshallToDbus - virtual void marshallToDbus(QDBusArgument &argument) const override; + void marshallToDbus(QDBusArgument &argument) const; //! \copydoc CValueObject::unmarshallFromDbus - virtual void unmarshallFromDbus(const QDBusArgument &argument) override; + void unmarshallFromDbus(const QDBusArgument &argument); //! Equal operator. friend bool operator ==(const CVariant &a, const CVariant &b) { return compare(a, b) == 0; }