test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
value_ptr.h
Go to the documentation of this file.
1 #ifndef FWCore_Utilities_value_ptr_h
2 #define FWCore_Utilities_value_ptr_h
3 
4 // ----------------------------------------------------------------------
5 //
6 // value_ptr.h - Smart pointer permitting copying of pointed-to object.
7 //
8 // Purpose: value_ptr provides a smart pointer template that provides
9 // sole ownership of the pointed-to object. When a value_ptr object is
10 // copied, the new value_ptr object is given a copy of the object pointed
11 // to by the original value_ptr object.
12 //
13 // The value_ptr_traits template is provided to allow specialization
14 // of the copying behavior. See the notes below.
15 //
16 // Use value_ptr only when deep-copying of the pointed-to object is
17 // desireable. Use boost::shared_ptr or std::shared_ptr when sharing
18 // of the pointed-to object is desirable. Use std::unique_ptr
19 // when no copying is desirable.
20 //
21 // The design of value_ptr is taken from Herb Sutter's More
22 // Exceptional C++, with modifications by Marc Paterno and further
23 // modifications by Walter Brown. This version is based on the
24 // ValuePtr found in the Fermilab ZOOM library.
25 //
26 //
27 // Supports the following syntax
28 // value_ptr<T> ptr(...);
29 // if (ptr) { ...
30 // Where the conditional will evaluate as true if and only if the
31 // pointer the value_ptr contains is not null.
32 //
33 // ----------------------------------------------------------------------
34 
35 #include <algorithm> // for std::swap()
36 #include <memory>
37 
38 #ifdef __GCCXML__
39 #define nullptr 0
40 #endif
41 
42 namespace edm {
43 
44  // --------------------------------------------------------------------
45  //
46  // Auxiliary traits class template providing default clone()
47  // Users should specialize this template for types that have their
48  // own self-copy operations; failure to do so may lead to slicing!
49  //
50  // --------------------------------------------------------------------
51 
52 
53  template <typename T>
55  static T* clone(T const* p) { return new T(*p); }
56  };
57 
58  // --------------------------------------------------------------------
59  //
60  // Copyable smart pointer class template
61  //
62  // --------------------------------------------------------------------
63 
64 
65  template <typename T>
66  class value_ptr {
67 
68  public:
69 
70  // --------------------------------------------------
71  // Default constructor/destructor:
72  // --------------------------------------------------
73 
75  explicit value_ptr(T* p) : myP(p) { }
76  ~value_ptr() { delete myP; }
77 
78  // --------------------------------------------------
79  // Copy constructor/copy assignment:
80  // --------------------------------------------------
81 
82  value_ptr(value_ptr const& orig) :
83  myP(createFrom(orig.myP)) {
84  }
85 
86  value_ptr& operator=(value_ptr const& orig) {
87  value_ptr<T> temp(orig);
88  swap(temp);
89  return *this;
90  }
91 
92 #ifndef __GCCXML__
93  // --------------------------------------------------
94  // Move constructor/move assignment:
95  // --------------------------------------------------
96 
97  value_ptr(value_ptr&& orig) :
98  myP(orig.myP) { orig.myP=nullptr; }
99 
101  if (myP!=orig.myP) {
102  delete myP;
103  myP=orig.myP;
104  orig.myP=nullptr;
105  }
106  return *this;
107  }
108 #endif
109 
110  // --------------------------------------------------
111  // Access mechanisms:
112  // --------------------------------------------------
113 
114  T& operator*() const { return *myP; }
115  T* operator->() const { return myP; }
116 
117  // --------------------------------------------------
118  // Manipulation:
119  // --------------------------------------------------
120 
121  void swap(value_ptr& orig) { std::swap(myP, orig.myP); }
122 
123  // --------------------------------------------------
124  // Copy-like construct/assign from compatible value_ptr<>:
125  // --------------------------------------------------
126 
127  template <typename U>
128  value_ptr(value_ptr<U> const& orig) :
129  myP(createFrom(orig.operator->())) {
130  }
131 
132  template <typename U>
134  value_ptr<T> temp(orig);
135  swap(temp);
136  return *this;
137  }
138 
139  // --------------------------------------------------
140  // Copy-like construct/assign from auto_ptr<>:
141  // --------------------------------------------------
142 
143  value_ptr(std::auto_ptr<T> orig) :
144  myP(orig.release()) {
145  }
146 
147  value_ptr& operator=(std::auto_ptr<T> orig) {
148  value_ptr<T> temp(orig);
149  swap(temp);
150  return *this;
151  }
152 
153 #ifndef __GCCXML__
154  // --------------------------------------------------
155  // Move-like construct/assign from unique_ptr<>:
156  // --------------------------------------------------
157 
158  value_ptr(std::unique_ptr<T> orig) :
159  myP(orig.release()) { orig=nullptr; }
160 
161  value_ptr& operator=(std::unique_ptr<T> orig) {
162  value_ptr<T> temp(orig);
163  swap(temp);
164  return *this;
165  }
166 #endif
167 
168  // The following typedef, function, and operator definition
169  // support the following syntax:
170  // value_ptr<T> ptr(..);
171  // if (ptr) { ...
172  // Where the conditional will evaluate as true if and only if the
173  // pointer value_ptr contains is not null.
174  private:
175  typedef void (value_ptr::*bool_type)() const;
177 
178  public:
179  operator bool_type() const {
180  return myP != nullptr ?
182  }
183 
184  private:
185 
186  // --------------------------------------------------
187  // Implementation aid:
188  // --------------------------------------------------
189 
190  template <typename U>
191  static T*
192  createFrom(U const* p) {
193  return p
195  : nullptr;
196  }
197 
198  // --------------------------------------------------
199  // Member data:
200  // --------------------------------------------------
201 
202  T* myP;
203 
204  }; // value_ptr
205 
206 
207  // --------------------------------------------------------------------
208  //
209  // Free-standing swap()
210  //
211  // --------------------------------------------------------------------
212 
213  template <typename T>
214  inline
215  void
216  swap(value_ptr<T>& vp1, value_ptr<T>& vp2) { vp1.swap(vp2); }
217 
218  // Do not allow nonsensical comparisons that the bool_type
219  // conversion operator definition above would otherwise allow.
220  // The function call inside the next 4 operator definitions is
221  // private, so compilation will fail if there is an attempt to
222  // instantiate these 4 operators.
223  template <typename T, typename U>
224  inline bool operator==(value_ptr<T> const& lhs, U const& rhs) {
226  return false;
227  }
228 
229  template <typename T, typename U>
230  inline bool operator!=(value_ptr<T> const& lhs, U const& rhs) {
232  return false;
233  }
234 
235  template <typename T, typename U>
236  inline bool operator==(U const& lhs, value_ptr<T> const& rhs) {
238  return false;
239  }
240 
241  template <typename T, typename U>
242  inline bool operator!=(U const& lhs, value_ptr<T> const& rhs) {
244  return false;
245  }
246 }
247 
248 
249 #endif // FWCoreUtilities_value_ptr_h
T * operator->() const
Definition: value_ptr.h:115
value_ptr & operator=(std::auto_ptr< T > orig)
Definition: value_ptr.h:147
value_ptr(value_ptr &&orig)
Definition: value_ptr.h:97
static T * clone(T const *p)
Definition: value_ptr.h:55
void swap(value_ptr &orig)
Definition: value_ptr.h:121
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
#define nullptr
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
static T * createFrom(U const *p)
Definition: value_ptr.h:192
value_ptr(T *p)
Definition: value_ptr.h:75
value_ptr & operator=(value_ptr< U > const &orig)
Definition: value_ptr.h:133
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
value_ptr(value_ptr< U > const &orig)
Definition: value_ptr.h:128
void this_type_does_not_support_comparisons() const
Definition: value_ptr.h:176
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
value_ptr & operator=(value_ptr &&orig)
Definition: value_ptr.h:100
value_ptr & operator=(value_ptr const &orig)
Definition: value_ptr.h:86
value_ptr & operator=(std::unique_ptr< T > orig)
Definition: value_ptr.h:161
value_ptr(std::unique_ptr< T > orig)
Definition: value_ptr.h:158
void(value_ptr::* bool_type)() const
Definition: value_ptr.h:175
T & operator*() const
Definition: value_ptr.h:114
value_ptr(value_ptr const &orig)
Definition: value_ptr.h:82
long double T
value_ptr(std::auto_ptr< T > orig)
Definition: value_ptr.h:143