ThePEG  1.8.0
PtrTraits.h
1 // -*- C++ -*-
2 //
3 // PtrTraits.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2011 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 2 of the GPL, see COPYING for details.
7 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
8 //
9 #ifndef ThePEG_PtrTraits_H
10 #define ThePEG_PtrTraits_H
11 // This is the declaration of the PtrTraits class.
12 
13 namespace ThePEG {
14 namespace Pointer {
15 
20 struct PtrTraitsType {};
21 
38 template <class T>
39 struct PtrTraits: public PtrTraitsType {};
40 
44 template <class T>
45 struct PtrTraits<T *>: public PtrTraitsType {
46 
48  typedef T value_type;
50  typedef T & reference;
52  typedef const T & const_reference;
54  typedef T * pointer;
56  typedef T * const_pointer;
57 
61  static T * barePointer(T * p) { return p; }
62 
66  static pointer create() { return new T; }
67 
71  static pointer create(const_reference t) { return new T(t); }
72 
76  static void destroy(pointer tp) { delete tp; }
77 
81  template <class R>
82  static pointer DynamicCast(R * r) { return dynamic_cast<pointer>(r); }
83 
87  static pointer ConstCast(const T * t) { return const_cast<pointer>(t); }
88 
92  static pointer PtrCast(T * t) { return t; }
93 
97  static const bool reference_counted = false;
98 
99 };
100 
105 template <class T>
106 struct PtrTraits<const T *>: public PtrTraitsType {
107 
109  typedef T value_type;
111  typedef T & reference;
113  typedef const T & const_reference;
115  typedef T * pointer;
117  typedef T * const_pointer;
118 
122  static const T * barePointer(const T * p) { return p; }
123 
127  static pointer create() { return new T; }
128 
132  static pointer create(const_reference t) { return new T(t); }
133 
137  static void destroy(pointer tp) { delete tp; }
138 
142  template <class R>
143  static const_pointer DynamicCast(const R * r) {
144  return dynamic_cast<const_pointer>(r);
145  }
146 
150  static const_pointer ConstCast(const T * r) { return r; }
151 
155  static const_pointer PtrCast(const T * t) { return t; }
156 
160  static const bool reference_counted = false;
161 
162 };
163 
167 template <class T1, class T2>
168 T1 dynamic_ptr_cast(const T2 & t2) { return PtrTraits<T1>::DynamicCast(t2); }
169 
170 
174 template <class T1, class T2>
175 T1 const_ptr_cast(const T2 & t2) { return PtrTraits<T1>::ConstCast(t2); }
176 
180 template <typename Ptr>
181 inline Ptr ptr_new() { return PtrTraits<Ptr>::create(); }
182 
186 template <typename Ptr>
188  return PtrTraits<Ptr>::create(t);
189 }
190 
194 template <typename T>
195 inline typename Ptr<T>::pointer new_ptr() {
196  return PtrTraits< typename Ptr<T>::pointer >::create();
197 }
198 
202 template <typename T>
203 inline typename Ptr<T>::pointer new_ptr(const T & t) {
204  return PtrTraits< typename Ptr<T>::pointer >::create(t);
205 }
206 
210 template <typename TPtr, typename T>
211 inline TPtr ptr_cast(T * t) {
212  return PtrTraits<TPtr>::PtrCast(t);
213 }
214 
218 template <typename TPtr, typename T>
219 inline TPtr ptr_cast_const(const T * t) {
220  return PtrTraits<TPtr>::PtrCast(const_cast<T*>(t));
221 }
222 
223 
224 }
225 }
226 
227 #endif /* ThePEG_PtrTraitsH */