• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • akonadi
itempayloadinternals_p.h
1 /*
2  Copyright (c) 2007 Till Adam <adam@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #ifndef ITEMPAYLOADINTERNALS_P_H
21 #define ITEMPAYLOADINTERNALS_P_H
22 
23 #include <kpimutils/supertrait.h>
24 
25 #include <QtCore/QtGlobal>
26 #include <QtCore/QSharedPointer>
27 #include <QtCore/QMetaType>
28 
29 #include <boost/shared_ptr.hpp>
30 #include <boost/type_traits/is_same.hpp>
31 #include <boost/mpl/eval_if.hpp>
32 #include <boost/mpl/identity.hpp>
33 #include <boost/utility/enable_if.hpp>
34 
35 #include <typeinfo>
36 
37 #include "exception.h"
38 
39 //@cond PRIVATE Doxygen 1.7.1 hangs processing this file. so skip it.
40 //for more info, see https://bugzilla.gnome.org/show_bug.cgi?id=531637
41 
42 /* WARNING
43  * The below is an implementation detail of the Item class. It is not to be
44  * considered public API, and subject to change without notice
45  */
46 
47 namespace Akonadi {
48 namespace Internal {
49 
50 template <typename T>
51 struct has_clone_method {
52 private:
53  template <typename S, S * (S::*)() const> struct sfinae {};
54  struct No {};
55  struct Yes { No no[2]; };
56  template <typename S> static No test( ... );
57  template <typename S> static Yes test( sfinae<S,&S::clone> * );
58 public:
59  static const bool value = sizeof( test<T>(0) ) == sizeof( Yes ) ;
60 };
61 
62 template <typename T, bool b>
63 struct clone_traits_helper {
64  // runtime error (commented in) or compiletime error (commented out)?
65  // ### runtime error, until we check has_clone_method in the
66  // ### Item::payload<T> impl directly...
67  template <typename U>
68  static T * clone( U ) { return 0; }
69 };
70 
71 template <typename T>
72 struct clone_traits_helper<T,true> {
73  static T * clone( T * t ) { return t ? t->clone() : 0 ; }
74 };
75 
76 template <typename T>
77 struct clone_traits : clone_traits_helper<T, has_clone_method<T>::value> {};
78 
79 template <typename T>
80 struct shared_pointer_traits {
81  static const bool defined = false;
82 };
83 
84 template <typename T>
85 struct shared_pointer_traits< boost::shared_ptr<T> > {
86  static const bool defined = true;
87  typedef T element_type;
88  template <typename S>
89  struct make { typedef boost::shared_ptr<S> type; };
90  typedef QSharedPointer<T> next_shared_ptr;
91 };
92 
93 template <typename T>
94 struct shared_pointer_traits< QSharedPointer<T> > {
95  static const bool defined = true;
96  typedef T element_type;
97  template <typename S>
98  struct make { typedef QSharedPointer<S> type; };
99  typedef boost::shared_ptr<T> next_shared_ptr;
100 };
101 
102 template <typename T>
103 struct is_shared_pointer {
104  static const bool value = shared_pointer_traits<T>::defined;
105 };
106 
107 template <typename T>
108 struct get_hierarchy_root;
109 
110 template <typename T, typename S>
111 struct get_hierarchy_root_recurse
112  : get_hierarchy_root<S> {};
113 
114 template <typename T>
115 struct get_hierarchy_root_recurse<T,T>
116  : boost::mpl::identity<T> {};
117 
118 template <typename T>
119 struct get_hierarchy_root
120  : get_hierarchy_root_recurse< T, typename ::KPIMUtils::SuperClass<T>::Type > {};
121 
122 template <typename T>
123 struct get_hierarchy_root< boost::shared_ptr<T> > {
124  typedef boost::shared_ptr< typename get_hierarchy_root<T>::type > type;
125 };
126 
127 template <typename T>
128 struct get_hierarchy_root< QSharedPointer<T> > {
129  typedef QSharedPointer< typename get_hierarchy_root<T>::type > type;
130 };
131 
138 template <typename T> struct PayloadTrait
139 {
141  typedef T ElementType;
142  // the metatype id for the element type, or for pointer-to-element
143  // type, if in a shared pointer
144  static int elementMetaTypeId() { return qMetaTypeId<T>(); }
147  typedef typename KPIMUtils::SuperClass<T>::Type SuperElementType;
149  typedef T Type;
153  typedef typename KPIMUtils::SuperClass<T>::Type SuperType;
156  static const bool isPolymorphic = false;
158  static inline bool isNull( const Type & ) { return true; }
161  template <typename U> static inline Type castFrom( const U& )
162  {
163  throw PayloadException( "you should never get here" );
164  }
166  template <typename U> static inline bool canCastFrom( const U& )
167  {
168  return false;
169  }
171  template <typename U> static inline U castTo( const Type& )
172  {
173  throw PayloadException( "you should never get here" );
174  }
175  template <typename U> static T clone( const U & )
176  {
177  throw PayloadException( "clone: you should never get here" );
178  }
180  static const unsigned int sharedPointerId = 0;
181 };
182 
188 template <typename T> struct PayloadTrait<boost::shared_ptr<T> >
189 {
190  typedef T ElementType;
191  static int elementMetaTypeId() { return qMetaTypeId<T*>(); }
192  typedef typename KPIMUtils::SuperClass<T>::Type SuperElementType;
193  typedef boost::shared_ptr<ElementType> Type;
194  typedef boost::shared_ptr<SuperElementType> SuperType;
195  static const bool isPolymorphic = !boost::is_same<ElementType, SuperElementType>::value;
196  static inline bool isNull( const Type &p ) { return p.get() == 0; }
197  template <typename U> static inline Type castFrom( const boost::shared_ptr<U> &p )
198  {
199  const Type sp = boost::dynamic_pointer_cast<T,U>( p );
200  if ( sp.get() != 0 || p.get() == 0 )
201  return sp;
202  throw PayloadException( "boost::dynamic_pointer_cast failed" );
203  }
204  template <typename U> static inline bool canCastFrom( const boost::shared_ptr<U> &p )
205  {
206  const Type sp = boost::dynamic_pointer_cast<T,U>( p );
207  return sp.get() != 0 || p.get() == 0;
208  }
209  template <typename U> static inline boost::shared_ptr<U> castTo( const Type &p )
210  {
211  const boost::shared_ptr<U> sp = boost::dynamic_pointer_cast<U>( p );
212  return sp;
213  }
214  static boost::shared_ptr<T> clone( const QSharedPointer<T> & t ) {
215  if ( T * nt = clone_traits<T>::clone( t.data() ) )
216  return boost::shared_ptr<T>( nt );
217  else
218  return boost::shared_ptr<T>();
219  }
220  static const unsigned int sharedPointerId = 1;
221 };
222 
228 template <typename T> struct PayloadTrait<QSharedPointer<T> >
229 {
230  typedef T ElementType;
231  static int elementMetaTypeId() { return qMetaTypeId<T*>(); }
232  typedef typename KPIMUtils::SuperClass<T>::Type SuperElementType;
233  typedef QSharedPointer<T> Type;
234  typedef QSharedPointer<SuperElementType> SuperType;
235  static const bool isPolymorphic = !boost::is_same<ElementType, SuperElementType>::value;
236  static inline bool isNull( const Type &p ) { return p.isNull(); }
237  template <typename U> static inline Type castFrom( const QSharedPointer<U> &p )
238  {
239  const Type sp = qSharedPointerDynamicCast<T,U>( p );
240  if ( !sp.isNull() || p.isNull() )
241  return sp;
242  throw PayloadException( "qSharedPointerDynamicCast failed" );
243  }
244  template <typename U> static inline bool canCastFrom( const QSharedPointer<U> &p )
245  {
246  const Type sp = qSharedPointerDynamicCast<T,U>( p );
247  return !sp.isNull() || p.isNull();
248  }
249  template <typename U> static inline QSharedPointer<U> castTo( const Type &p )
250  {
251  const QSharedPointer<U> sp = qSharedPointerDynamicCast<U,T>( p );
252  return sp;
253  }
254  static QSharedPointer<T> clone( const boost::shared_ptr<T> & t ) {
255  if ( T * nt = clone_traits<T>::clone( t.get() ) )
256  return QSharedPointer<T>( nt );
257  else
258  return QSharedPointer<T>();
259  }
260  static const unsigned int sharedPointerId = 2;
261 };
262 
263 }
264 
270 struct PayloadBase
271 {
272  virtual ~PayloadBase() { }
273  virtual PayloadBase * clone() const = 0;
274  virtual const char* typeName() const = 0;
275 };
276 
282 template <typename T>
283 struct Payload : public PayloadBase
284 {
285  Payload() {};
286  Payload( const T& p ) : payload( p ) {}
287 
288  PayloadBase * clone() const
289  {
290  return new Payload<T>( const_cast<Payload<T>* >(this)->payload);
291  }
292 
293  const char* typeName() const
294  {
295  return typeid(const_cast<Payload<T>*> (this)).name();
296  }
297 
298  T payload;
299 };
300 
305 template <typename T>
306 struct Payload<T*> : public PayloadBase
307 {
308 };
309 
310 namespace Internal {
311 
316 template <typename T> inline Payload<T>* payload_cast( PayloadBase* payloadBase )
317 {
318  Payload<T> *p = dynamic_cast<Payload<T>*>( payloadBase );
319  // try harder to cast, workaround for some gcc issue with template instances in multiple DSO's
320  if ( !p && payloadBase && strcmp( payloadBase->typeName(), typeid(p).name() ) == 0 ) {
321  p = static_cast<Payload<T>*>( payloadBase );
322  }
323  return p;
324 }
325 
326 }
327 
328 }
329 //@endcond
330 
331 #endif
332 
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

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

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal