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  explicit value_ptr(T* p = 0) : myP(p) { }
71  ~value_ptr() { delete myP; }
72 
73  // --------------------------------------------------
74  // Copy constructor/copy assignment:
75  // --------------------------------------------------
76 
77  value_ptr(value_ptr const& orig) :
78  myP(createFrom(orig.myP)) {
79  }
80 
81  value_ptr& operator= (value_ptr const& orig) {
82  value_ptr<T> temp(orig);
83  swap(temp);
84  return *this;
85  }
86 
87  // --------------------------------------------------
88  // Access mechanisms:
89  // --------------------------------------------------
90 
91  T& operator*() const { return *myP; }
92  T* operator->() const { return myP; }
93 
94  // --------------------------------------------------
95  // Manipulation:
96  // --------------------------------------------------
97 
98  void swap(value_ptr& orig) { std::swap(myP, orig.myP); }
99 
100  // --------------------------------------------------
101  // Copy-like construct/assign from compatible value_ptr<>:
102  // --------------------------------------------------
103 
104  template <typename U>
105  value_ptr(value_ptr<U> const& orig) :
106  myP(createFrom(orig.operator->())) {
107  }
108 
109  template <typename U>
111  value_ptr<T> temp(orig);
112  swap(temp);
113  return *this;
114  }
115 
116  // --------------------------------------------------
117  // Copy-like construct/assign from auto_ptr<>:
118  // --------------------------------------------------
119 
120  value_ptr(std::auto_ptr<T> orig) :
121  myP(orig.release()) {
122  }
123 
124  value_ptr& operator=(std::auto_ptr<T> orig) {
125  value_ptr<T> temp(orig);
126  swap(temp);
127  return *this;
128  }
129 
130  // The following typedef, function, and operator definition
131  // support the following syntax:
132  // value_ptr<T> ptr(..);
133  // if (ptr) { ...
134  // Where the conditional will evaluate as true if and only if the
135  // pointer value_ptr contains is not null.
136  private:
137  typedef void (value_ptr::*bool_type)() const;
139 
140  public:
141  operator bool_type() const {
142  return myP != 0 ?
144  }
145 
146  private:
147 
148  // --------------------------------------------------
149  // Implementation aid:
150  // --------------------------------------------------
151 
152  template <typename U>
153  T*
154  createFrom(U const* p) const {
155  return p
157  : 0;
158  }
159 
160  // --------------------------------------------------
161  // Member data:
162  // --------------------------------------------------
163 
164  T * myP;
165 
166  }; // value_ptr
167 
168 
169  // --------------------------------------------------------------------
170  //
171  // Free-standing swap()
172  //
173  // --------------------------------------------------------------------
174 
175  template <typename T>
176  inline
177  void
178  swap(value_ptr<T>& vp1, value_ptr<T>& vp2) { vp1.swap(vp2); }
179 
180  // Do not allow nonsensical comparisons that the bool_type
181  // conversion operator definition above would otherwise allow.
182  // The function call inside the next 4 operator definitions is
183  // private, so compilation will fail if there is an attempt to
184  // instantiate these 4 operators.
185  template <typename T, typename U>
186  bool operator==(value_ptr<T> const& lhs, U const& rhs) {
188  return false;
189  }
190 
191  template <typename T, typename U>
192  bool operator!=(value_ptr<T> const& lhs, U const& rhs) {
194  return false;
195  }
196 
197  template <typename T, typename U>
198  bool operator==(U const& lhs, value_ptr<T> const& rhs) {
200  return false;
201  }
202 
203  template <typename T, typename U>
204  bool operator!=(U const& lhs, value_ptr<T> const& rhs) {
206  return false;
207  }
208 }
209 
210 
211 #endif // FWCoreUtilities_value_ptr_h
T * operator->() const
Definition: value_ptr.h:92
value_ptr & operator=(std::auto_ptr< T > orig)
Definition: value_ptr.h:124
static T * clone(T const *p)
Definition: value_ptr.h:51
void swap(value_ptr &orig)
Definition: value_ptr.h:98
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
T * createFrom(U const *p) const
Definition: value_ptr.h:154
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
value_ptr & operator=(value_ptr< U > const &orig)
Definition: value_ptr.h:110
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
value_ptr(value_ptr< U > const &orig)
Definition: value_ptr.h:105
void this_type_does_not_support_comparisons() const
Definition: value_ptr.h:138
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
value_ptr & operator=(value_ptr const &orig)
Definition: value_ptr.h:81
void(value_ptr::* bool_type)() const
Definition: value_ptr.h:137
value_ptr(T *p=0)
Definition: value_ptr.h:70
T & operator*() const
Definition: value_ptr.h:91
value_ptr(value_ptr const &orig)
Definition: value_ptr.h:77
long double T
value_ptr(std::auto_ptr< T > orig)
Definition: value_ptr.h:120