Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:38:30

0001 // -*- C++ -*-
0002 //
0003 // PtrTraits.h is a part of ThePEG - Toolkit for HEP Event Generation
0004 // Copyright (C) 1999-2019 Leif Lonnblad
0005 //
0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 #ifndef ThePEG_PtrTraits_H
0010 #define ThePEG_PtrTraits_H
0011 // This is the declaration of the PtrTraits class.
0012 
0013 namespace ThePEG {
0014 namespace Pointer {
0015 
0016 /**
0017  * PtrTraitsType is an empty non-polymorphic base class for all
0018  * PtrTraits classes.
0019  */
0020 struct PtrTraitsType {};
0021 
0022 /**
0023  * The PtrTraits class is used everywhere in ThePEG to
0024  * interface to the pointers which are handled. In particular, ThePEG
0025  * never uses new or delete but always
0026  * PtrTraits<P>::create and
0027  * PtrTraits<P>::destroy (to be precise the destroy method
0028  * is never used since all pointers are assumed to be reference
0029  * counted or in another way garbage collected). Also ThePEG
0030  * always uses dynamic_ptr_cast (rather than the standard
0031  * dynamic_cast) which in turn calls the
0032  * PtrTraits<P>::DynamicCast.
0033  *
0034  * In this file is also defined the specialized std::iterator_traits
0035  * for the reference counted pointers.
0036  *
0037  */
0038 template <class T>
0039 struct PtrTraits: public PtrTraitsType {};
0040 
0041 /**
0042  * Specialization of the PtrTraits class for standard bare pointers.
0043  */
0044 template <class T>
0045 struct PtrTraits<T *>: public PtrTraitsType {
0046 
0047   /** Template argument typedef. */
0048   typedef T value_type;
0049   /** Template argument typedef. */
0050   typedef T & reference;
0051   /** Template argument typedef. */
0052   typedef const T & const_reference;
0053   /** Template argument typedef. */
0054   typedef T * pointer;
0055   /** Template argument typedef. */
0056   typedef T * const_pointer;
0057 
0058   /**
0059    * Return the bare pointer of the given pointer object.
0060    */
0061   static T * barePointer(T * p) { return p; }
0062 
0063   /**
0064    * Create an object and return a pointer to it.
0065    */
0066   static pointer create() { return new T; }
0067 
0068   /**
0069    * Create an copy of an object and return a pointer to it.
0070    */
0071   static pointer create(const_reference t) { return new T(t); }
0072 
0073   /**
0074    * Destroy the object pointed to.
0075    */
0076   static void destroy(pointer tp) { delete tp; }
0077 
0078   /**
0079    * Cast dynamically.
0080    */
0081   template <class R>
0082   static pointer DynamicCast(R * r) { return dynamic_cast<pointer>(r); }
0083 
0084   /**
0085    * Cast away constness.
0086    */
0087   static pointer ConstCast(const T * t) { return const_cast<pointer>(t); }
0088 
0089   /**
0090    * Cast from a basic pointer.
0091    */
0092   static pointer PtrCast(T * t) { return t; }
0093 
0094   /**
0095    * The bare pointer is not reference counted.
0096    */
0097   static const bool reference_counted = false;
0098 
0099 };
0100 
0101 /**
0102  * Specialization of the PtrTraits class for standard bare
0103  * const pointers.
0104  */
0105 template <class T>
0106 struct PtrTraits<const T *>: public PtrTraitsType {
0107 
0108   /** Template argument typedef. */
0109   typedef T value_type;
0110   /** Template argument typedef. */
0111   typedef T & reference;
0112   /** Template argument typedef. */
0113   typedef const T & const_reference;
0114   /** Template argument typedef. */
0115   typedef T * pointer;
0116   /** Template argument typedef. */
0117   typedef T * const_pointer;
0118 
0119   /**
0120    * Return the bare pointer of the given pointer object.
0121    */
0122   static const T * barePointer(const T * p) { return p; }
0123 
0124   /**
0125    * Create an object and return a pointer to it.
0126    */
0127   static pointer create() { return new T; }
0128 
0129   /**
0130    * Create an copy of an object and return a pointer to it.
0131    */
0132   static pointer create(const_reference t) { return new T(t); }
0133 
0134   /**
0135    * Destroy the object pointed to.
0136    */
0137   static void destroy(pointer tp) { delete tp; }
0138 
0139   /**
0140    * Cast dynamically.
0141    */
0142   template <class R>
0143   static const_pointer DynamicCast(const R * r) {
0144     return dynamic_cast<const_pointer>(r);
0145   }
0146 
0147   /**
0148    * Do not cast away constness.
0149    */
0150   static const_pointer ConstCast(const T * r) { return r; }
0151 
0152   /**
0153    * Cast from a basic pointer.
0154    */
0155   static const_pointer PtrCast(const T * t) { return t; }
0156 
0157   /**
0158    * The bare pointer is not reference counted.
0159    */
0160   static const bool reference_counted = false;
0161 
0162 };
0163 
0164 /**
0165  * Replacement for the standard dynamic_cast
0166  */
0167 template <class T1, class T2>
0168 T1 dynamic_ptr_cast(const T2 & t2) { return PtrTraits<T1>::DynamicCast(t2); }
0169 
0170 
0171 /**
0172  * Replacement for the standard const_cast
0173  */
0174 template <class T1, class T2>
0175 T1 const_ptr_cast(const T2 & t2) { return PtrTraits<T1>::ConstCast(t2); }
0176 
0177 /**
0178  * Simple interface to the PtrTraits<Ptr>::create()
0179  */
0180 template <typename Ptr>
0181 inline Ptr ptr_new() { return PtrTraits<Ptr>::create(); }
0182 
0183 /**
0184  * Simple interface to the PtrTraits<Ptr>::create()
0185  */
0186 template <typename Ptr>
0187 inline Ptr ptr_new(typename PtrTraits<Ptr>::const_reference t) {
0188   return PtrTraits<Ptr>::create(t);
0189 }
0190 
0191 /**
0192  * Simple interface to the PtrTraits<Ptr>::create()
0193  */
0194 template <typename T>
0195 inline typename Ptr<T>::pointer new_ptr() {
0196   return PtrTraits< typename Ptr<T>::pointer >::create();
0197 }
0198 
0199 /**
0200  * Simple interface to the PtrTraits<Ptr>::create()
0201  */
0202 template <typename T>
0203 inline typename Ptr<T>::pointer new_ptr(const T & t) {
0204   return PtrTraits< typename Ptr<T>::pointer >::create(t);
0205 }
0206 
0207 /**
0208  * Simple interface to the PtrTraits<Ptr>::PtrCast()
0209  */
0210 template <typename TPtr, typename T>
0211 inline TPtr ptr_cast(T * t) {
0212   return PtrTraits<TPtr>::PtrCast(t);
0213 }
0214 
0215 /**
0216  * Simple interface to the PtrTraits<Ptr>::PtrCast()
0217  */
0218 template <typename TPtr, typename T>
0219 inline TPtr ptr_cast_const(const T * t) {
0220   return PtrTraits<TPtr>::PtrCast(const_cast<T*>(t));
0221 }
0222 
0223 
0224 }
0225 }
0226 
0227 #endif /* ThePEG_PtrTraitsH */