• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi

itempayloadinternals_p.h

00001 /*
00002     Copyright (c) 2007 Till Adam <adam@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or modify it
00005     under the terms of the GNU Library General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or (at your
00007     option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful, but WITHOUT
00010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012     License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to the
00016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301, USA.
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 /* WARNING
00035  * The below is an implementation detail of the Item class. It is not to be
00036  * considered public API, and subject to change without notice
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   // try harder to cast, workaround for some gcc issue with template instances in multiple DSO's
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 

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal