refs #628 Add mixins which use the metaclass system instead of the tuple system.

This commit is contained in:
Mathew Sutcliffe
2016-03-25 19:01:38 +00:00
parent 34bc76e74c
commit 4f3637a046
6 changed files with 273 additions and 0 deletions

View File

@@ -13,6 +13,7 @@
#define BLACKMISC_DBUS_H
#include "blackmisc/tuple.h"
#include "blackmisc/metaclass.h"
#include "blackmisc/inheritancetraits.h"
#include <QDBusArgument>
#include <type_traits>
@@ -88,6 +89,43 @@ namespace BlackMisc
static void baseUnmarshall(CEmpty *, const QDBusArgument &) {}
};
/*!
* CRTP class template from which a derived class can inherit common methods dealing with marshalling instances by metaclass.
*
* \see BLACKMISC_DECLARE_USING_MIXIN_DBUS
*/
template <class Derived>
class DBusByMetaClass : public DBusOperators<Derived>
{
public:
//! Marshall without begin/endStructure, for when composed within another object
void marshallToDbus(QDBusArgument &arg) const
{
baseMarshall(static_cast<const BaseOfT<Derived> *>(derived()), arg);
auto meta = introspect<Derived>().without(MetaFlags<DisabledForMarshalling>());
meta.forEachMember(*derived(), [ & ](const auto &member) { arg << member; });
}
//! Unmarshall without begin/endStructure, for when composed within another object
void unmarshallFromDbus(const QDBusArgument &arg)
{
baseUnmarshall(static_cast<BaseOfT<Derived> *>(derived()), arg);
auto meta = introspect<Derived>().without(MetaFlags<DisabledForMarshalling>());
meta.forEachMember(*derived(), [ & ](auto &member) { arg >> member; });
}
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 &) {}
static void baseMarshall(const CEmpty *, QDBusArgument &) {}
static void baseUnmarshall(CEmpty *, 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.