/* Copyright (C) 2014 * swift project Community / Contributors * * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, * including this file, may be copied, modified, propagated, or distributed except according to the terms * contained in the LICENSE file. */ //! \file #ifndef BLACKMISC_VALUEOBJECT_POLICY_H #define BLACKMISC_VALUEOBJECT_POLICY_H #include "tuple.h" #include #include namespace BlackMisc { template struct CValueObjectStdTuplePolicy; namespace Policy { namespace Private { //! \private Alias for the policy of the base class of T template using Inherit = CValueObjectStdTuplePolicy; //! \private using BlackMisc::Private::EncapsulationBreaker; } namespace MetaType { //! CValueObjectStdTuple default registerMetadata policy struct Default { //! Register with QMetaType template static void registerImpl() { qRegisterMetaType(); qDBusRegisterMetaType(); } }; //! CValueObjectStdTuple registerMetadata policy which inherits the policy of the base class struct Inherit { //! Register with QMetaType template static void registerImpl() { Private::Inherit::MetaType::template registerImpl(); } }; //! CValueObjectStdTuple registerMetadata policy which also registers QList struct DefaultAndQList { //! Register with QMetaType template static void registerImpl() { Default::registerImpl(); Default::registerImpl>(); } }; } namespace Equals { //! CValueObjectStdTuple policy for a class which should not have an equals operator struct None { //! Inner class template which actually bestows the operators via the Barton-Nackman trick template struct Ops {}; }; //! CValueObjectStdTuple equals operator policy which inherits the policy of the base class struct Inherit { //! Inner class template which actually bestows the operators via the Barton-Nackman trick template struct Ops : public CValueObjectStdTuplePolicy::Equals::template Ops {}; }; //! CValueObjectStdTuple policy for a class whose meta tuple members can be compared by the equals operator struct MetaTuple { //! Inner class template which actually bestows the operators via the Barton-Nackman trick template struct Ops : private Private::EncapsulationBreaker { //! Equals operator friend bool operator ==(const T &a, const T &b) { return Private::EncapsulationBreaker::toMetaTuple(a) == Private::EncapsulationBreaker::toMetaTuple(b); } //! Not equals operator friend bool operator !=(const T &a, const T &b) { return !(a == b); } }; }; //! Some classes define their own custom equals operator; this policy provides a not equals operator which simply negates the result struct OwnEquals { //! Inner class template which actually bestows the operators via the Barton-Nackman trick template struct Ops { //! Not equals operator; class T already has its own equals operator friend bool operator !=(const T &a, const T &b) { return !(a == b); } }; }; } namespace LessThan { //! CValueObjectStdTuple policy for a class which should not have a less than operator struct None { //! Inner class template which actually bestows the operators via the Barton-Nackman trick template struct Ops {}; }; //! CValueObjectStdTuple less than operator policy which inherits the policy of the base class struct Inherit { //! Inner class template which actually bestows the operators via the Barton-Nackman trick template struct Ops : public CValueObjectStdTuplePolicy::LessThan::template Ops {}; }; //! CValueObjectStdTuple policy for a class whose meta tuple members can be compared by the less than operator struct MetaTuple { //! Inner class template which actually bestows the operators via the Barton-Nackman trick template struct Ops : private Private::EncapsulationBreaker { //! Less than operator friend bool operator <(const T &a, const T &b) { return Private::EncapsulationBreaker::toMetaTuple(a) < Private::EncapsulationBreaker::toMetaTuple(b); } //! Greater than operator friend bool operator >(const T &a, const T &b) { return b < a; } //! Greater or equal operator friend bool operator >=(const T &a, const T &b) { return !(a < b); } //! Less or equal operator friend bool operator <=(const T &a, const T &b) { return !(b < a); } }; }; //! Some classes define their own custom less than operator; this policy implements other comparison operators in terms of this one struct OwnLessThan { //! Inner class template which actually bestows the operators via the Barton-Nackman trick template struct Ops { //! Greater than operator; class T already has its own less than operator friend bool operator >(const T &a, const T &b) { return b < a; } //! Greater or equal operator; class T already has its own less than operator friend bool operator >=(const T &a, const T &b) { return !(a < b); } //! Less or equal operator; class T already has its own less than operator friend bool operator <=(const T &a, const T &b) { return !(b < a); } }; }; } namespace Compare { //! CValueObjectStdTuple policy for a class without polymorphic comparison support struct None { //! Policy implementation of CValueObject::compareImpl template static bool compareImpl(const T &, const T &) { qFatal("Not implemented"); return 0; } }; //! CValueObjectStdTuple polymorphic comparison policy which inherits the policy of the base class struct Inherit { //! Policy implementation of CValueObject::compareImpl template static bool compareImpl(const T &a, const T &b) { return Private::Inherit::Compare::template compareImpl(a, b); } }; //! CValueObjectStdTuple policy for a class with default metatuple-based polymorphic comparison support struct MetaTuple : private Private::EncapsulationBreaker { //! Policy implementation of CValueObject::compareImpl template static bool compareImpl(const T &a, const T &b) { return compare(Private::EncapsulationBreaker::toMetaTuple(a), Private::EncapsulationBreaker::toMetaTuple(b)); } }; //! CValueObjectStdTuple policy for a class which implements its own custom poylymorphic comparison support struct Own { //! Policy implementation of CValueObject::compareImpl template static bool compareImpl(const T &, const T &) { return 0; } }; } namespace Hash { //! CValueObjectStdTuple policy for a class without hashing support struct None { //! \copydoc BlackMisc::CValueObject::getHashValue template static uint hashImpl(const T &) { qFatal("Not implemented"); return 0; } }; //! CValueObjectStdTuple hashing policy which inherits the policy of the base class struct Inherit { //! \copydoc BlackMisc::CValueObject::getHashValue template static uint hashImpl(const T &obj) { return Private::Inherit::Hash::template hashImpl(obj); } }; //! CValueObjectStdTuple policy for a class with default metatuple-based hashing support struct MetaTuple : private Private::EncapsulationBreaker { //! \copydoc BlackMisc::CValueObject::getHashValue template static uint hashImpl(const T &obj) { return qHash(Private::EncapsulationBreaker::toMetaTuple(obj)); } }; //! CValueObjectStdTuple policy for a class which implements its own custom hashing support struct Own { //! \copydoc BlackMisc::CValueObject::getHashValue template static uint hashImpl(const T &) { return 0; } }; } namespace DBus { //! CValueObjectStdTuple policy for a class without DBus marshalling support struct None { //! \copydoc BlackMisc::CValueObject::marshallToDbus template static void marshallImpl(QDBusArgument &, const T &) { qFatal("Not implemented"); } //! \copydoc BlackMisc::CValueObject::unmarshallFromDbus template static void unmarshallImpl(const QDBusArgument &, T &) { qFatal("Not implemented"); } }; //! CValueObjectStdTuple marshalling policy which inherits the policy of the base class struct Inherit { //! \copydoc BlackMisc::CValueObject::marshallToDbus template static void marshallImpl(QDBusArgument &arg, const T &obj) { Private::Inherit::DBus::template marshallImpl(arg, obj); } //! \copydoc BlackMisc::CValueObject::unmarshallFromDbus template static void unmarshallImpl(const QDBusArgument &arg, T &obj) { return Private::Inherit::DBus::template unmarshallImpl(arg, obj); } }; //! CValueObjectStdTuple policy for a class with default metatuple-based DBus marshalling support struct MetaTuple : private Private::EncapsulationBreaker { //! \copydoc BlackMisc::CValueObject::marshallToDbus template static void marshallImpl(QDBusArgument &arg, const T &obj) { arg << Private::EncapsulationBreaker::toMetaTuple(obj); } //! \copydoc BlackMisc::CValueObject::unmarshallFromDbus template static void unmarshallImpl(const QDBusArgument &arg, T &obj) { arg >> Private::EncapsulationBreaker::toMetaTuple(obj); } }; //! CValueObjectStdTuple policy for a class which implements its own custom DBus marshalling support struct Own { //! \copydoc BlackMisc::CValueObject::marshallToDbus template static void marshallImpl(QDBusArgument &, const T &) {} //! \copydoc BlackMisc::CValueObject::unmarshallFromDbus template static void unmarshallImpl(const QDBusArgument &, T &) {} }; } namespace Json { //! CValueObjectStdTuple policy for a class without JSON support struct None { //! \copydoc BlackMisc::serializeJson template static QJsonObject serializeImpl(const T &) { qFatal("Not implemented"); return {}; } //! \copydoc BlackMisc::deserializeJson template static void deserializeImpl(const QJsonObject &, T &) { qFatal("Not implemented"); } }; //! CValueObjectStdTuple JSON policy which inherits the policy of the base class struct Inherit { //! \copydoc BlackMisc::serializeJson template static QJsonObject serializeImpl(const T &obj) { return Private::Inherit::Json::template serializeImpl(obj); } //! \copydoc BlackMisc::deserializeJson template static void deserializeImpl(const QJsonObject &json, T &obj) { Private::Inherit::Json::template deserializeImpl(json, obj); } }; //! CValueObjectStdTuple policy for a class with default metatuple-based JSON support struct MetaTuple : private Private::EncapsulationBreaker { //! \copydoc BlackMisc::serializeJson template static QJsonObject serializeImpl(const T &obj) { return BlackMisc::serializeJson(Private::EncapsulationBreaker::toMetaTuple(obj)); } //! \copydoc BlackMisc::deserializeJson template static void deserializeImpl(const QJsonObject &json, T &obj) { BlackMisc::deserializeJson(json, Private::EncapsulationBreaker::toMetaTuple(obj)); } }; //! CValueObjectStdTuple policy for a class which implements its own custom JSON support struct Own { //! \copydoc BlackMisc::serializeJson template static QJsonObject serializeImpl(const T &) { return {}; } //! \copydoc BlackMisc::deserializeJson template static void deserializeImpl(const QJsonObject &, T &) {} }; } } } #endif