00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef ITEMPAYLOADINTERNALS_P_H
00021 #define ITEMPAYLOADINTERNALS_P_H
00022
00023 #include <kpimutils/supertrait.h>
00024
00025 #include <QtCore/QtGlobal>
00026 #include <QtCore/QSharedPointer>
00027
00028 #include <boost/shared_ptr.hpp>
00029 #include <boost/type_traits/is_same.hpp>
00030 #include <typeinfo>
00031
00032 #include "exception.h"
00033
00034
00035
00036
00037
00038
00039 namespace Akonadi {
00040 namespace Internal {
00041
00048 template <typename T> struct PayloadTrait
00049 {
00051 typedef T ElementType;
00054 typedef typename KPIMUtils::SuperClass<T>::Type SuperElementType;
00056 typedef T Type;
00060 typedef typename KPIMUtils::SuperClass<T>::Type SuperType;
00063 static const bool isPolymorphic = false;
00065 static inline bool isNull( const Type & ) { return true; }
00068 template <typename U> static inline Type castFrom( const U& )
00069 {
00070 throw PayloadException( "you should never get here" );
00071 }
00073 template <typename U> static inline bool canCastFrom( const U& )
00074 {
00075 return false;
00076 }
00078 template <typename U> static inline U castTo( const Type& )
00079 {
00080 throw PayloadException( "you should never get here" );
00081 }
00082 };
00083
00089 template <typename T> struct PayloadTrait<boost::shared_ptr<T> >
00090 {
00091 typedef T ElementType;
00092 typedef typename KPIMUtils::SuperClass<T>::Type SuperElementType;
00093 typedef boost::shared_ptr<T> Type;
00094 typedef boost::shared_ptr<typename KPIMUtils::SuperClass<T>::Type> SuperType;
00095 static const bool isPolymorphic = !boost::is_same<ElementType, SuperElementType>::value;
00096 static inline bool isNull( const Type &p ) { return p.get() == 0; }
00097 template <typename U> static inline Type castFrom( const boost::shared_ptr<U> &p )
00098 {
00099 const Type sp = boost::dynamic_pointer_cast<T,U>( p );
00100 if ( sp.get() != 0 || p.get() == 0 )
00101 return sp;
00102 throw PayloadException( "boost::dynamic_pointer_cast failed" );
00103 }
00104 template <typename U> static inline bool canCastFrom( const boost::shared_ptr<U> &p )
00105 {
00106 const Type sp = boost::dynamic_pointer_cast<T,U>( p );
00107 return sp.get() != 0 || p.get() == 0;
00108 }
00109 template <typename U> static inline boost::shared_ptr<U> castTo( const Type &p )
00110 {
00111 const boost::shared_ptr<U> sp = boost::dynamic_pointer_cast<U,T>( p );
00112 return sp;
00113 }
00114 };
00115
00121 template <typename T> struct PayloadTrait<QSharedPointer<T> >
00122 {
00123 typedef T ElementType;
00124 typedef typename KPIMUtils::SuperClass<T>::Type SuperElementType;
00125 typedef QSharedPointer<T> Type;
00126 typedef QSharedPointer<typename KPIMUtils::SuperClass<T>::Type> SuperType;
00127 static const bool isPolymorphic = !boost::is_same<ElementType, SuperElementType>::value;
00128 static inline bool isNull( const Type &p ) { return p.isNull(); }
00129 template <typename U> static inline Type castFrom( const QSharedPointer<U> &p )
00130 {
00131 const Type sp = qSharedPointerDynamicCast<T,U>( p );
00132 if ( !sp.isNull() || p.isNull() )
00133 return sp;
00134 throw PayloadException( "qSharedPointerDynamicCast failed" );
00135 }
00136 template <typename U> static inline bool canCastFrom( const QSharedPointer<U> &p )
00137 {
00138 const Type sp = qSharedPointerDynamicCast<T,U>( p );
00139 return !sp.isNull() || p.isNull();
00140 }
00141 template <typename U> static inline QSharedPointer<U> castTo( const Type &p )
00142 {
00143 const QSharedPointer<U> sp = qSharedPointerDynamicCast<U,T>( p );
00144 return sp;
00145 }
00146 };
00147
00148 }
00149
00155 struct PayloadBase
00156 {
00157 virtual ~PayloadBase() { }
00158 virtual PayloadBase * clone() const = 0;
00159 virtual const char* typeName() const = 0;
00160 };
00161
00167 template <typename T>
00168 struct Payload : public PayloadBase
00169 {
00170 Payload( T p ) { payload = p; }
00171 Payload( const Payload& other )
00172 {
00173 payload = other.payload;
00174 }
00175 Payload & operator=( const Payload & other )
00176 {
00177 payload = other.payload;
00178 }
00179
00180 PayloadBase * clone() const
00181 {
00182 return new Payload<T>( const_cast<Payload<T>* >(this)->payload);
00183 }
00184
00185 const char* typeName() const
00186 {
00187 return typeid(const_cast<Payload<T>*> (this)).name();
00188 }
00189
00190 T payload;
00191 };
00192
00197 template <typename T>
00198 struct Payload<T*> : public PayloadBase
00199 {
00200 };
00201
00202 namespace Internal {
00203
00208 template <typename T> inline Payload<T>* payload_cast( PayloadBase* payloadBase )
00209 {
00210 Payload<T> *p = dynamic_cast<Payload<T>*>( payloadBase );
00211
00212 if ( !p && strcmp( payloadBase->typeName(), typeid(p).name() ) == 0 ) {
00213 p = static_cast<Payload<T>*>( payloadBase );
00214 }
00215 return p;
00216 }
00217
00218 }
00219
00220 }
00221
00222 #endif
00223