HepMC event record
include/HepMC/Data/SmartPointer.h
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014 The HepMC collaboration (see AUTHORS for details)
5 //
6 /// @brief Definition of \b template \b class SmartPointer
7 
8 #ifndef HEPMC_DATA_SMARTPOINTER_H
9 #define HEPMC_DATA_SMARTPOINTER_H
10 
11 #include "HepMC/Common.h"
12 
13 #if defined(HEPMC_HAS_CXX11) || defined(HEPMC_HAS_CXX0X_GCC_ONLY)
14 
15 #include <memory>
16 namespace HepMC {
17  using std::weak_ptr;
18  using std::shared_ptr;
19  using std::make_shared;
20  using std::dynamic_pointer_cast;
21  using std::const_pointer_cast;
22 }
23 
24 #else
25 #error At least partial C++ 2011 support is required!
26 #endif
27 
28 
29 namespace HepMC {
30 
31 
32  /// @class HepMC::SmartPointer
33  /// @brief Smart pointer for HepMC objects
34  ///
35  /// Uses shared_ptr to manage the object memory
36  ///
37  /// @note SmartPointer can be created from raw pointer. This allows
38  /// for implicit conversions when passing raw pointer as argument to
39  /// the constructors or other functions in HepMC classes for backward compatibility.
40  /// SmartPointer ensures only one shared_ptr manages
41  /// the object in such conversions. Note, however, that use of such conversion is deprecated.
42  ///
43  /// @note Requires managed class to have member field \c weah_ptr<T> \c \b m_this
44  /// used to keep track of shared pointer created to manage the object
45  ///
46  /// @ingroup data
47  ///
48  template<class T>
49  class SmartPointer {
50  public:
51 
52  /// @name Constructors
53  //@{
54 
55  /// Default constructor (NULL pointer)
56  SmartPointer();
57 
58  /// Copy constructor
59  SmartPointer( const SmartPointer<T> &rhs );
60 
61  /// Construct SmartPointer using shared pointer
62  ///
63  /// @note It's advised not to use shared_ptr<T> when using SmartPointer
64  /// @note This constructor should be used only in combination with make_shared<T>
65  SmartPointer( const shared_ptr<T> &rhs );
66 
67  /// Constructor creating shared pointer from raw pointer
68  SmartPointer( T *raw_pointer );
69 
70  //@}
71 
72 
73  /// @name Accessors
74  //@{
75 
76  /// Assignment
77  SmartPointer& operator=(const SmartPointer &rhs) { m_data = rhs.m_data; return *this; }
78 
79  /// Equality test
80  bool operator==(const SmartPointer &rhs) const { return m_data == rhs.m_data; }
81  /// Inequality test
82  bool operator!=(const SmartPointer &rhs) const { return m_data != rhs.m_data; }
83  /// Less-than comparison
84  bool operator<(const SmartPointer &rhs) const { return m_data < rhs.m_data; }
85 
86  /// Non-const access to the contained shared_ptr, with non-const contained type
87  const shared_ptr<T> operator->() { return m_data; }
88  /// Non-const dereferencing to a reference of the contained type
89  T& operator*() { return *m_data; }
90 
91  /// Const access to the contained shared_ptr, with const contained type
92  /// @note Hurrah for trickery!
93  const shared_ptr<const T> operator->() const { return const_pointer_cast<const T>(m_data); }
94  /// Const dereferencing to a const reference of the contained type
95  const T& operator*() const { return *m_data; }
96 
97  /// Bool cast operator
98  /// @note This should ideally use the 'safe bool idiom' in C++98 -- in C++11 an implicit explicit
99  /// cast / contextual conversion with the new 'explicit' keyword will be used for safety
100  #ifdef HEPMC_HAS_CXX11
101  explicit
102  #endif
103  operator bool() const { return (bool) m_data; }
104 
105  //@}
106 
107 
108  /// @name Deprecated functions
109  //@{
110 
111  #ifndef HEPMC_NO_DEPRECATED
112 
113  /// Cast to raw pointer
114  /// @deprecated Should not be used at all
115  HEPMC_DEPRECATED("Use smart pointers instead of raw pointers")
116  operator T*() { return m_data.get(); }
117 
118  /// Cast to bool
119  operator bool() { return (bool) m_data.get(); }
120 
121  #endif
122 
123  //@}
124 
125 
126  private:
127 
128  /// @name Fields
129  //@{
130  /// Shared pointer
131  shared_ptr<T> m_data;
132  //@}
133 
134  };
135 
136 
137  /// @name Typedefs for smart pointers to HepMC classes
138  //@{
139  typedef SmartPointer<class GenParticle> GenParticlePtr; //!< Smart pointer to GenParticle
140  typedef SmartPointer<class GenVertex> GenVertexPtr; //!< Smart pointer to GenVertex
141 
142  typedef SmartPointer<const class GenParticle> ConstGenParticlePtr; //!< Const smart pointer to GenParticle
143  typedef SmartPointer<const class GenVertex> ConstGenVertexPtr; //!< Const smart pointer to GenVertex
144 
145  typedef shared_ptr<class GenPdfInfo> GenPdfInfoPtr; //!< Shared pointer to GenPdfInfo
146  typedef shared_ptr<class GenHeavyIon> GenHeavyIonPtr; //!< Shared pointer to GenHeavyIon
147  typedef shared_ptr<class GenCrossSection> GenCrossSectionPtr; //!< Shared pointer to GenCrossSection
148  //@}
149 
150 
151 } // namespace HepMC
152 
153 #include "HepMC/Data/SmartPointer.icc"
154 
155 #endif
const shared_ptr< const T > operator->() const
const T & operator*() const
Const dereferencing to a const reference of the contained type.
SmartPointer & operator=(const SmartPointer &rhs)
Assignment.
bool operator==(const SmartPointer &rhs) const
Equality test.
SmartPointer< class GenParticle > GenParticlePtr
Smart pointer to GenParticle.
SmartPointer< const class GenParticle > ConstGenParticlePtr
Const smart pointer to GenParticle.
shared_ptr< class GenPdfInfo > GenPdfInfoPtr
Shared pointer to GenPdfInfo.
SmartPointer< const class GenVertex > ConstGenVertexPtr
Const smart pointer to GenVertex.
bool operator!=(const SmartPointer &rhs) const
Inequality test.
shared_ptr< class GenCrossSection > GenCrossSectionPtr
Shared pointer to GenCrossSection.
T & operator*()
Non-const dereferencing to a reference of the contained type.
shared_ptr< class GenHeavyIon > GenHeavyIonPtr
Shared pointer to GenHeavyIon.
Definition of template class SmartPointer.
const shared_ptr< T > operator->()
Non-const access to the contained shared_ptr, with non-const contained type.
SmartPointer< class GenVertex > GenVertexPtr
Smart pointer to GenVertex.
bool operator<(const SmartPointer &rhs) const
Less-than comparison.
SmartPointer()
Default constructor (NULL pointer)