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 when sharing of the pointed-to
18 // object is desireable. Use boost::scoped_ptr when no copying is
19 // desireable.
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 
70  value_ptr() : myP(0) { }
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 #if defined( __GXX_EXPERIMENTAL_CXX0X__)
89  value_ptr(value_ptr && orig) :
90  myP(orig.myP) { orig.myP=0; }
91 
92  value_ptr& operator=(value_ptr && orig) {
93  if (myP!=orig.myP) {
94  delete myP;
95  myP=orig.myP;
96  orig.myP=0;
97  }
98  return *this;
99  }
100 #endif
101 
102 
103  // --------------------------------------------------
104  // Access mechanisms:
105  // --------------------------------------------------
106 
107  T& operator*() const { return *myP; }
108  T* operator->() const { return myP; }
109 
110  // --------------------------------------------------
111  // Manipulation:
112  // --------------------------------------------------
113 
114  void swap(value_ptr& orig) { std::swap(myP, orig.myP); }
115 
116  // --------------------------------------------------
117  // Copy-like construct/assign from compatible value_ptr<>:
118  // --------------------------------------------------
119 
120  template <typename U>
121  value_ptr(value_ptr<U> const& orig) :
122  myP(createFrom(orig.operator->())) {
123  }
124 
125  template <typename U>
127  value_ptr<T> temp(orig);
128  swap(temp);
129  return *this;
130  }
131 
132  // --------------------------------------------------
133  // Copy-like construct/assign from auto_ptr<>:
134  // --------------------------------------------------
135 
136  value_ptr(std::auto_ptr<T> orig) :
137  myP(orig.release()) {
138  }
139 
140  value_ptr& operator=(std::auto_ptr<T> orig) {
141  value_ptr<T> temp(orig);
142  swap(temp);
143  return *this;
144  }
145 
146  // The following typedef, function, and operator definition
147  // support the following syntax:
148  // value_ptr<T> ptr(..);
149  // if (ptr) { ...
150  // Where the conditional will evaluate as true if and only if the
151  // pointer value_ptr contains is not null.
152  private:
153  typedef void (value_ptr::*bool_type)() const;
155 
156  public:
157  operator bool_type() const {
158  return myP != 0 ?
160  }
161 
162  private:
163 
164  // --------------------------------------------------
165  // Implementation aid:
166  // --------------------------------------------------
167 
168  template <typename U>
169  static T*
170  createFrom(U const* p) {
171  return p
173  : 0;
174  }
175 
176  // --------------------------------------------------
177  // Member data:
178  // --------------------------------------------------
179 
180  T * myP;
181 
182  }; // value_ptr
183 
184 
185  // --------------------------------------------------------------------
186  //
187  // Free-standing swap()
188  //
189  // --------------------------------------------------------------------
190 
191  template <typename T>
192  inline
193  void
194  swap(value_ptr<T>& vp1, value_ptr<T>& vp2) { vp1.swap(vp2); }
195 
196  // Do not allow nonsensical comparisons that the bool_type
197  // conversion operator definition above would otherwise allow.
198  // The function call inside the next 4 operator definitions is
199  // private, so compilation will fail if there is an attempt to
200  // instantiate these 4 operators.
201  template <typename T, typename U>
202  inline bool operator==(value_ptr<T> const& lhs, U const& rhs) {
204  return false;
205  }
206 
207  template <typename T, typename U>
208  inline bool operator!=(value_ptr<T> const& lhs, U const& rhs) {
210  return false;
211  }
212 
213  template <typename T, typename U>
214  inline bool operator==(U const& lhs, value_ptr<T> const& rhs) {
216  return false;
217  }
218 
219  template <typename T, typename U>
220  inline bool operator!=(U const& lhs, value_ptr<T> const& rhs) {
222  return false;
223  }
224 }
225 
226 
227 #endif // FWCoreUtilities_value_ptr_h
T * operator->() const
Definition: value_ptr.h:108
value_ptr & operator=(std::auto_ptr< T > orig)
Definition: value_ptr.h:140
static T * clone(T const *p)
Definition: value_ptr.h:51
void swap(value_ptr &orig)
Definition: value_ptr.h:114
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
static T * createFrom(U const *p)
Definition: value_ptr.h:170
value_ptr(T *p)
Definition: value_ptr.h:71
value_ptr & operator=(value_ptr< U > const &orig)
Definition: value_ptr.h:126
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
value_ptr(value_ptr< U > const &orig)
Definition: value_ptr.h:121
void this_type_does_not_support_comparisons() const
Definition: value_ptr.h:154
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
value_ptr & operator=(value_ptr const &orig)
Definition: value_ptr.h:82
void(value_ptr::* bool_type)() const
Definition: value_ptr.h:153
T & operator*() const
Definition: value_ptr.h:107
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:136