thepeg is hosted by Hepforge, IPPP Durham
ThePEG 2.3.0
PtrTraits.h
1// -*- C++ -*-
2//
3// PtrTraits.h is a part of ThePEG - Toolkit for HEP Event Generation
4// Copyright (C) 1999-2019 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
13namespace ThePEG {
14namespace Pointer {
15
20struct PtrTraitsType {};
21
38template <class T>
39struct PtrTraits: public PtrTraitsType {};
40
44template <class T>
45struct 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
105template <class T>
106struct 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
167template <class T1, class T2>
168T1 dynamic_ptr_cast(const T2 & t2) { return PtrTraits<T1>::DynamicCast(t2); }
169
170
174template <class T1, class T2>
175T1 const_ptr_cast(const T2 & t2) { return PtrTraits<T1>::ConstCast(t2); }
176
180template <typename Ptr>
181inline Ptr ptr_new() { return PtrTraits<Ptr>::create(); }
182
186template <typename Ptr>
188 return PtrTraits<Ptr>::create(t);
189}
190
194template <typename T>
195inline typename Ptr<T>::pointer new_ptr() {
196 return PtrTraits< typename Ptr<T>::pointer >::create();
197}
198
202template <typename T>
203inline typename Ptr<T>::pointer new_ptr(const T & t) {
204 return PtrTraits< typename Ptr<T>::pointer >::create(t);
205}
206
210template <typename TPtr, typename T>
211inline TPtr ptr_cast(T * t) {
212 return PtrTraits<TPtr>::PtrCast(t);
213}
214
218template <typename TPtr, typename T>
219inline 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 */
RCPtr is a reference counted (smart) pointer.
Definition: RCPtr.h:60
TPtr ptr_cast_const(const T *t)
Simple interface to the PtrTraits<Ptr>::PtrCast()
Definition: PtrTraits.h:219
T1 dynamic_ptr_cast(const T2 &t2)
Replacement for the standard dynamic_cast.
Definition: PtrTraits.h:168
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
Ptr< T >::pointer new_ptr()
Simple interface to the PtrTraits<Ptr>::create()
Definition: PtrTraits.h:195
Ptr ptr_new()
Simple interface to the PtrTraits<Ptr>::create()
Definition: PtrTraits.h:181
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
PtrTraitsType is an empty non-polymorphic base class for all PtrTraits classes.
Definition: PtrTraits.h:20
static void destroy(pointer tp)
Destroy the object pointed to.
Definition: PtrTraits.h:76
T & reference
Template argument typedef.
Definition: PtrTraits.h:50
static pointer PtrCast(T *t)
Cast from a basic pointer.
Definition: PtrTraits.h:92
static pointer create(const_reference t)
Create an copy of an object and return a pointer to it.
Definition: PtrTraits.h:71
const T & const_reference
Template argument typedef.
Definition: PtrTraits.h:52
T value_type
Template argument typedef.
Definition: PtrTraits.h:48
static pointer ConstCast(const T *t)
Cast away constness.
Definition: PtrTraits.h:87
T * pointer
Template argument typedef.
Definition: PtrTraits.h:54
T * const_pointer
Template argument typedef.
Definition: PtrTraits.h:56
static pointer DynamicCast(R *r)
Cast dynamically.
Definition: PtrTraits.h:82
static T * barePointer(T *p)
Return the bare pointer of the given pointer object.
Definition: PtrTraits.h:61
static pointer create()
Create an object and return a pointer to it.
Definition: PtrTraits.h:66
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 value_type
Template argument typedef.
Definition: PtrTraits.h:109
static const_pointer ConstCast(const T *r)
Do not cast away constness.
Definition: PtrTraits.h:150
T * pointer
Template argument typedef.
Definition: PtrTraits.h:115
static void destroy(pointer tp)
Destroy the object pointed to.
Definition: PtrTraits.h:137
T * const_pointer
Template argument typedef.
Definition: PtrTraits.h:117
static const T * barePointer(const T *p)
Return the bare pointer of the given pointer object.
Definition: PtrTraits.h:122
static const_pointer PtrCast(const T *t)
Cast from a basic pointer.
Definition: PtrTraits.h:155
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
const T & const_reference
Template argument typedef.
Definition: PtrTraits.h:113
The PtrTraits class is used everywhere in ThePEG to interface to the pointers which are handled.
Definition: PtrTraits.h:39
Ptr is a templated class to provide typedefs for pointers types ThePEG should use for a given type.
Definition: Ptr.h:35