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
17 // is desireable. Use std::shared_ptr when sharing of the
18 // 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 namespace edm {
39 
40  // --------------------------------------------------------------------
41  //
42  // Auxiliary traits class template providing default clone()
43  // Users should specialize this template for types that have their
44  // own self-copy operations; failure to do so may lead to slicing!
45  //
46  // --------------------------------------------------------------------
47 
48 
49  template <typename T>
51  static T* clone(T const* p) { return new T(*p); }
52  };
53 
54  // --------------------------------------------------------------------
55  //
56  // Copyable smart pointer class template
57  //
58  // --------------------------------------------------------------------
59 
60 
61  template <typename T>
62  class value_ptr {
63 
64  public:
65 
66  // --------------------------------------------------
67  // Default constructor/destructor:
68  // --------------------------------------------------
69 
71  explicit value_ptr(T* p) : myP(p) { }
72  ~value_ptr() { delete myP; }
73 
74  // --------------------------------------------------
75  // Copy constructor/copy assignment:
76  // --------------------------------------------------
77 
78  value_ptr(value_ptr const& orig) :
79  myP(createFrom(orig.myP)) {
80  }
81 
82  value_ptr& operator=(value_ptr const& orig) {
83  value_ptr<T> temp(orig);
84  swap(temp);
85  return *this;
86  }
87 
88  // --------------------------------------------------
89  // Move constructor/move assignment:
90  // --------------------------------------------------
91 
92  value_ptr(value_ptr&& orig) :
93  myP(orig.myP) { orig.myP=nullptr; }
94 
96  if (myP!=orig.myP) {
97  delete myP;
98  myP=orig.myP;
99  orig.myP=nullptr;
100  }
101  return *this;
102  }
103 
104  // --------------------------------------------------
105  // Access mechanisms:
106  // --------------------------------------------------
107 
108  T& operator*() const { return *myP; }
109  T* operator->() const { return myP; }
110 
111  // --------------------------------------------------
112  // Manipulation:
113  // --------------------------------------------------
114 
115  void swap(value_ptr& orig) { std::swap(myP, orig.myP); }
116 
117  // --------------------------------------------------
118  // Copy-like construct/assign from compatible value_ptr<>:
119  // --------------------------------------------------
120 
121  template <typename U>
122  value_ptr(value_ptr<U> const& orig) :
123  myP(createFrom(orig.operator->())) {
124  }
125 
126  template <typename U>
128  value_ptr<T> temp(orig);
129  swap(temp);
130  return *this;
131  }
132 
133  // --------------------------------------------------
134  // Copy-like construct/assign from auto_ptr<>:
135  // --------------------------------------------------
136 
137  value_ptr(std::auto_ptr<T> orig) :
138  myP(orig.release()) {
139  }
140 
141  value_ptr& operator=(std::auto_ptr<T> orig) {
142  value_ptr<T> temp(orig);
143  swap(temp);
144  return *this;
145  }
146 
147  // --------------------------------------------------
148  // Move-like construct/assign from unique_ptr<>:
149  // --------------------------------------------------
150 
151  value_ptr(std::unique_ptr<T> orig) :
152  myP(orig.release()) { orig=nullptr; }
153 
154  value_ptr& operator=(std::unique_ptr<T> orig) {
155  value_ptr<T> temp(orig);
156  swap(temp);
157  return *this;
158  }
159 
160  // The following typedef, function, and operator definition
161  // support the following syntax:
162  // value_ptr<T> ptr(..);
163  // if (ptr) { ...
164  // Where the conditional will evaluate as true if and only if the
165  // pointer value_ptr contains is not null.
166  private:
167  typedef void (value_ptr::*bool_type)() const;
169 
170  public:
171  operator bool_type() const {
172  return myP != nullptr ?
174  }
175 
176  private:
177 
178  // --------------------------------------------------
179  // Implementation aid:
180  // --------------------------------------------------
181 
182  template <typename U>
183  static T*
184  createFrom(U const* p) {
185  return p
187  : nullptr;
188  }
189 
190  // --------------------------------------------------
191  // Member data:
192  // --------------------------------------------------
193 
194  T* myP;
195 
196  }; // value_ptr
197 
198 
199  // --------------------------------------------------------------------
200  //
201  // Free-standing swap()
202  //
203  // --------------------------------------------------------------------
204 
205  template <typename T>
206  inline
207  void
208  swap(value_ptr<T>& vp1, value_ptr<T>& vp2) { vp1.swap(vp2); }
209 
210  // Do not allow nonsensical comparisons that the bool_type
211  // conversion operator definition above would otherwise allow.
212  // The function call inside the next 4 operator definitions is
213  // private, so compilation will fail if there is an attempt to
214  // instantiate these 4 operators.
215  template <typename T, typename U>
216  inline bool operator==(value_ptr<T> const& lhs, U const& rhs) {
218  return false;
219  }
220 
221  template <typename T, typename U>
222  inline bool operator!=(value_ptr<T> const& lhs, U const& rhs) {
224  return false;
225  }
226 
227  template <typename T, typename U>
228  inline bool operator==(U const& lhs, value_ptr<T> const& rhs) {
230  return false;
231  }
232 
233  template <typename T, typename U>
234  inline bool operator!=(U const& lhs, value_ptr<T> const& rhs) {
236  return false;
237  }
238 }
239 
240 
241 #endif // FWCoreUtilities_value_ptr_h
T * operator->() const
Definition: value_ptr.h:109
value_ptr & operator=(std::auto_ptr< T > orig)
Definition: value_ptr.h:141
value_ptr(value_ptr &&orig)
Definition: value_ptr.h:92
static T * clone(T const *p)
Definition: value_ptr.h:51
void swap(value_ptr &orig)
Definition: value_ptr.h:115
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:184
value_ptr(T *p)
Definition: value_ptr.h:71
value_ptr & operator=(value_ptr< U > const &orig)
Definition: value_ptr.h:127
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
value_ptr(value_ptr< U > const &orig)
Definition: value_ptr.h:122
void this_type_does_not_support_comparisons() const
Definition: value_ptr.h:168
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
value_ptr & operator=(value_ptr &&orig)
Definition: value_ptr.h:95
value_ptr & operator=(value_ptr const &orig)
Definition: value_ptr.h:82
value_ptr & operator=(std::unique_ptr< T > orig)
Definition: value_ptr.h:154
value_ptr(std::unique_ptr< T > orig)
Definition: value_ptr.h:151
void(value_ptr::* bool_type)() const
Definition: value_ptr.h:167
T & operator*() const
Definition: value_ptr.h:108
value_ptr(value_ptr const &orig)
Definition: value_ptr.h:78
long double T
value_ptr(std::auto_ptr< T > orig)
Definition: value_ptr.h:137