thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.1.5
PtrTraits.h
1 // -*- C++ -*-
2 //
3 // PtrTraits.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2017 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 3 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 */
Ptr< T >::pointer new_ptr()
Simple interface to the PtrTraits<Ptr>::create()
Definition: PtrTraits.h:195
static const_pointer PtrCast(const T *t)
Cast from a basic pointer.
Definition: PtrTraits.h:155
The PtrTraits class is used everywhere in ThePEG to interface to the pointers which are handled...
Definition: PtrTraits.h:39
static const T * barePointer(const T *p)
Return the bare pointer of the given pointer object.
Definition: PtrTraits.h:122
static pointer create()
Create an object and return a pointer to it.
Definition: PtrTraits.h:66
T & reference
Template argument typedef.
Definition: PtrTraits.h:50
static pointer ConstCast(const T *t)
Cast away constness.
Definition: PtrTraits.h:87
static pointer PtrCast(T *t)
Cast from a basic pointer.
Definition: PtrTraits.h:92
T & reference
Template argument typedef.
Definition: PtrTraits.h:111
static pointer create()
Create an object and return a pointer to it.
Definition: PtrTraits.h:127
T * const_pointer
Template argument typedef.
Definition: PtrTraits.h:56
static T * barePointer(T *p)
Return the bare pointer of the given pointer object.
Definition: PtrTraits.h:61
T value_type
Template argument typedef.
Definition: PtrTraits.h:48
T1 dynamic_ptr_cast(const T2 &t2)
Replacement for the standard dynamic_cast.
Definition: PtrTraits.h:168
static void destroy(pointer tp)
Destroy the object pointed to.
Definition: PtrTraits.h:76
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
TPtr ptr_cast_const(const T *t)
Simple interface to the PtrTraits<Ptr>::PtrCast()
Definition: PtrTraits.h:219
static const_pointer ConstCast(const T *r)
Do not cast away constness.
Definition: PtrTraits.h:150
const T & const_reference
Template argument typedef.
Definition: PtrTraits.h:52
TPtr ptr_cast(T *t)
Simple interface to the PtrTraits<Ptr>::PtrCast()
Definition: PtrTraits.h:211
T1 const_ptr_cast(const T2 &t2)
Replacement for the standard const_cast.
Definition: PtrTraits.h:175
RCPtr is a reference counted (smart) pointer.
Definition: RCPtr.h:60
T * const_pointer
Template argument typedef.
Definition: PtrTraits.h:117
static pointer DynamicCast(R *r)
Cast dynamically.
Definition: PtrTraits.h:82
PtrTraitsType is an empty non-polymorphic base class for all PtrTraits classes.
Definition: PtrTraits.h:20
T * pointer
Template argument typedef.
Definition: PtrTraits.h:115
T value_type
Template argument typedef.
Definition: PtrTraits.h:109
Ptr is a templated class to provide typedefs for pointers types ThePEG should use for a given type...
Definition: Ptr.h:35
T * pointer
Template argument typedef.
Definition: PtrTraits.h:54
Ptr ptr_new()
Simple interface to the PtrTraits<Ptr>::create()
Definition: PtrTraits.h:181
static pointer create(const_reference t)
Create an copy of an object and return a pointer to it.
Definition: PtrTraits.h:132
static const_pointer DynamicCast(const R *r)
Cast dynamically.
Definition: PtrTraits.h:143
static pointer create(const_reference t)
Create an copy of an object and return a pointer to it.
Definition: PtrTraits.h:71
static void destroy(pointer tp)
Destroy the object pointed to.
Definition: PtrTraits.h:137
const T & const_reference
Template argument typedef.
Definition: PtrTraits.h:113