/* Copyright (c) 2007 Till Adam This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef ITEMPAYLOADINTERNALS_P_H #define ITEMPAYLOADINTERNALS_P_H #include #include #include #include #include #include #include #include #include #include #include "exception.h" //@cond PRIVATE Doxygen 1.7.1 hangs processing this file. so skip it. //for more info, see https://bugzilla.gnome.org/show_bug.cgi?id=531637 /* WARNING * The below is an implementation detail of the Item class. It is not to be * considered public API, and subject to change without notice */ namespace Akonadi { namespace Internal { template struct has_clone_method { private: template struct sfinae { }; struct No { }; struct Yes { No no[2]; }; template static No test(...); template static Yes test(sfinae *); public: static const bool value = sizeof (test(0)) == sizeof (Yes) ; }; template struct clone_traits_helper { // runtime error (commented in) or compiletime error (commented out)? // ### runtime error, until we check has_clone_method in the // ### Item::payload impl directly... template static T *clone(U) { return 0; } }; template struct clone_traits_helper { static T *clone(T *t) { return t ? t->clone() : 0 ; } }; template struct clone_traits : clone_traits_helper::value> {}; template struct shared_pointer_traits { static const bool defined = false; }; template struct shared_pointer_traits< boost::shared_ptr > { static const bool defined = true; typedef T element_type; template struct make { typedef boost::shared_ptr type; }; typedef QSharedPointer next_shared_ptr; }; template struct shared_pointer_traits< QSharedPointer > { static const bool defined = true; typedef T element_type; template struct make { typedef QSharedPointer type; }; typedef boost::shared_ptr next_shared_ptr; }; template struct is_shared_pointer { static const bool value = shared_pointer_traits::defined; }; template struct get_hierarchy_root; template struct get_hierarchy_root_recurse : get_hierarchy_root { }; template struct get_hierarchy_root_recurse : boost::mpl::identity { }; template struct get_hierarchy_root : get_hierarchy_root_recurse< T, typename ::KPIMUtils::SuperClass::Type > { }; template struct get_hierarchy_root< boost::shared_ptr > { typedef boost::shared_ptr< typename get_hierarchy_root::type > type; }; template struct get_hierarchy_root< QSharedPointer > { typedef QSharedPointer< typename get_hierarchy_root::type > type; }; /** @internal Payload type traits. Implements specialized handling for polymorphic types and smart pointers. The default one is never used (as isPolymorphic is always false) and only contains safe dummy implementations to make the compiler happy (in practice it will always optimized away anyway). */ template struct PayloadTrait { /// type of the payload object contained inside a shared pointer typedef T ElementType; // the metatype id for the element type, or for pointer-to-element // type, if in a shared pointer static int elementMetaTypeId() { return qMetaTypeId(); } /// type of the base class of the payload object inside a shared pointer, /// same as ElementType if there is no super class typedef typename KPIMUtils::SuperClass::Type SuperElementType; /// type of this payload object typedef T Type; /// type of the payload to store a base class of this payload /// (eg. a shared pointer containing a pointer to SuperElementType) /// same as Type if there is not super class typedef typename KPIMUtils::SuperClass::Type SuperType; /// indicates if this payload is polymorphic, that is is a shared pointer /// and has a known super class static const bool isPolymorphic = false; /// checks an object of this payload type for being @c null static inline bool isNull(const Type &p) { Q_UNUSED(p); return true; } /// casts to Type from @c U /// throws a PayloadException if casting failed template static inline Type castFrom(const U &) { throw PayloadException("you should never get here"); } /// tests if casting from @c U to Type is possible template static inline bool canCastFrom(const U &) { return false; } /// cast to @c U from Type template static inline U castTo(const Type &) { throw PayloadException("you should never get here"); } template static T clone(const U &) { throw PayloadException("clone: you should never get here"); } /// defines the type of shared pointer used (0: none, > 0: boost::shared_ptr, QSharedPointer, ...) static const unsigned int sharedPointerId = 0; }; /** @internal Payload type trait specialization for boost::shared_ptr for documentation of the various members, see above */ template struct PayloadTrait > { typedef T ElementType; static int elementMetaTypeId() { return qMetaTypeId(); } typedef typename KPIMUtils::SuperClass::Type SuperElementType; typedef boost::shared_ptr Type; typedef boost::shared_ptr SuperType; static const bool isPolymorphic = !boost::is_same::value; static inline bool isNull(const Type &p) { return p.get() == 0; } template static inline Type castFrom(const boost::shared_ptr &p) { const Type sp = boost::dynamic_pointer_cast(p); if (sp.get() != 0 || p.get() == 0) { return sp; } throw PayloadException("boost::dynamic_pointer_cast failed"); } template static inline bool canCastFrom(const boost::shared_ptr &p) { const Type sp = boost::dynamic_pointer_cast(p); return sp.get() != 0 || p.get() == 0; } template static inline boost::shared_ptr castTo(const Type &p) { const boost::shared_ptr sp = boost::dynamic_pointer_cast(p); return sp; } static boost::shared_ptr clone(const QSharedPointer &t) { if (T *nt = clone_traits::clone(t.data())) { return boost::shared_ptr(nt); } else { return boost::shared_ptr(); } } static const unsigned int sharedPointerId = 1; }; /** @internal Payload type trait specialization for QSharedPointer for documentation of the various members, see above */ template struct PayloadTrait > { typedef T ElementType; static int elementMetaTypeId() { return qMetaTypeId(); } typedef typename KPIMUtils::SuperClass::Type SuperElementType; typedef QSharedPointer Type; typedef QSharedPointer SuperType; static const bool isPolymorphic = !boost::is_same::value; static inline bool isNull(const Type &p) { return p.isNull(); } template static inline Type castFrom(const QSharedPointer &p) { const Type sp = qSharedPointerDynamicCast(p); if (!sp.isNull() || p.isNull()) { return sp; } throw PayloadException("qSharedPointerDynamicCast failed"); } template static inline bool canCastFrom(const QSharedPointer &p) { const Type sp = qSharedPointerDynamicCast(p); return !sp.isNull() || p.isNull(); } template static inline QSharedPointer castTo(const Type &p) { const QSharedPointer sp = qSharedPointerDynamicCast(p); return sp; } static QSharedPointer clone(const boost::shared_ptr &t) { if (T *nt = clone_traits::clone(t.get())) { return QSharedPointer(nt); } else { return QSharedPointer(); } } static const unsigned int sharedPointerId = 2; }; } /** * @internal * Non-template base class for the payload container. * @note Move to Internal namespace for KDE5 */ struct PayloadBase { virtual ~PayloadBase() { } virtual PayloadBase *clone() const = 0; virtual const char *typeName() const = 0; }; /** * @internal * Container for the actual payload object. * @note Move to Internal namespace for KDE5 */ template struct Payload : public PayloadBase { Payload() { } Payload(const T &p) : payload(p) { } PayloadBase *clone() const { return new Payload(const_cast* >(this)->payload); } const char *typeName() const { return typeid (const_cast*>(this)).name(); } T payload; }; /** * @internal * abstract, will therefore always fail to compile for pointer payloads */ template struct Payload : public PayloadBase { }; namespace Internal { /** @internal Basically a dynamic_cast that also works across DSO boundaries. */ template inline Payload *payload_cast(PayloadBase *payloadBase) { Payload *p = dynamic_cast*>(payloadBase); // try harder to cast, workaround for some gcc issue with template instances in multiple DSO's if (!p && payloadBase && strcmp(payloadBase->typeName(), typeid (p).name()) == 0) { p = static_cast*>(payloadBase); } return p; } } } //@endcond #endif