00001 // -*- C++ -*- 00002 // 00003 // PtrTraits.h is a part of ThePEG - Toolkit for HEP Event Generation 00004 // Copyright (C) 1999-2007 Leif Lonnblad 00005 // 00006 // ThePEG is licenced under version 2 of the GPL, see COPYING for details. 00007 // Please respect the MCnet academic guidelines, see GUIDELINES for details. 00008 // 00009 #ifndef ThePEG_PtrTraits_H 00010 #define ThePEG_PtrTraits_H 00011 // This is the declaration of the PtrTraits class. 00012 00013 namespace ThePEG { 00014 namespace Pointer { 00015 00020 struct PtrTraitsType {}; 00021 00038 template <class T> 00039 struct PtrTraits: public PtrTraitsType {}; 00040 00044 template <class T> 00045 struct PtrTraits<T *>: public PtrTraitsType { 00046 00048 typedef T value_type; 00050 typedef T & reference; 00052 typedef const T & const_reference; 00054 typedef T * pointer; 00056 typedef T * const_pointer; 00057 00061 static T * barePointer(T * p) { return p; } 00062 00066 static pointer create() { return new T; } 00067 00071 static pointer create(const_reference t) { return new T(t); } 00072 00076 static void destroy(pointer tp) { delete tp; } 00077 00081 template <class R> 00082 static pointer DynamicCast(R * r) { return dynamic_cast<pointer>(r); } 00083 00087 static pointer ConstCast(const T * t) { return const_cast<pointer>(t); } 00088 00092 static pointer PtrCast(T * t) { return t; } 00093 00097 static const bool reference_counted = false; 00098 00099 }; 00100 00105 template <class T> 00106 struct PtrTraits<const T *>: public PtrTraitsType { 00107 00109 typedef T value_type; 00111 typedef T & reference; 00113 typedef const T & const_reference; 00115 typedef T * pointer; 00117 typedef T * const_pointer; 00118 00122 static const T * barePointer(const T * p) { return p; } 00123 00127 static pointer create() { return new T; } 00128 00132 static pointer create(const_reference t) { return new T(t); } 00133 00137 static void destroy(pointer tp) { delete tp; } 00138 00142 template <class R> 00143 static const_pointer DynamicCast(const R * r) { 00144 return dynamic_cast<const_pointer>(r); 00145 } 00146 00150 static const_pointer ConstCast(const T * r) { return r; } 00151 00155 static const_pointer PtrCast(const T * t) { return t; } 00156 00160 static const bool reference_counted = false; 00161 00162 }; 00163 00167 template <class T1, class T2> 00168 T1 dynamic_ptr_cast(const T2 & t2) { return PtrTraits<T1>::DynamicCast(t2); } 00169 00170 00174 template <class T1, class T2> 00175 T1 const_ptr_cast(const T2 & t2) { return PtrTraits<T1>::ConstCast(t2); } 00176 00180 template <typename Ptr> 00181 inline Ptr ptr_new() { return PtrTraits<Ptr>::create(); } 00182 00186 template <typename Ptr> 00187 inline Ptr ptr_new(typename PtrTraits<Ptr>::const_reference t) { 00188 return PtrTraits<Ptr>::create(t); 00189 } 00190 00194 template <typename T> 00195 inline typename Ptr<T>::pointer new_ptr() { 00196 return PtrTraits< typename Ptr<T>::pointer >::create(); 00197 } 00198 00202 template <typename T> 00203 inline typename Ptr<T>::pointer new_ptr(const T & t) { 00204 return PtrTraits< typename Ptr<T>::pointer >::create(t); 00205 } 00206 00210 template <typename TPtr, typename T> 00211 inline TPtr ptr_cast(T * t) { 00212 return PtrTraits<TPtr>::PtrCast(t); 00213 } 00214 00218 template <typename TPtr, typename T> 00219 inline TPtr ptr_cast_const(const T * t) { 00220 return PtrTraits<TPtr>::PtrCast(const_cast<T*>(t)); 00221 } 00222 00223 00224 } 00225 } 00226 00227 #endif /* ThePEG_PtrTraitsH */