CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
own_ptr.h
Go to the documentation of this file.
1 #ifndef UTILITIES_GENERAL_OWN_PTR_H
2 #define UTILITIES_GENERAL_OWN_PTR_H
3 //
4 // a pointer which "owns" the pointed object
5 // similar to std auto_ptr but does allow standard copy constructor
6 // that passes ownership when copied
7 // lost ownership = zero pointer (safer...)
8 //
9 
10 //
11 // Version 1.0 V.I. 6/4/99
12 // Version 2.0 V.I. 23/4/2001
13 // Version 3.0 V.I. 29/1/2004
14 // policies introduced...
15 //
16 
17 namespace OwnerPolicy {
18  struct Transfer{
19  template <class X> static X * copy(X * &p) { X* it=p; p=0; return it;}
20  template <class X> static void remove(X * p) { delete p;}
21  };
22  struct Copy{
23  template <class X> static X * copy(X *p) { return (p) ? new X(*p) : 0;}
24  template <class X> static void remove(X * p) { delete p;}
25  };
26  struct Clone{
27  template <class X> static X * copy(X *p) { return (p) ? (*p).clone() : 0;}
28  template <class X> static void remove(X * p) { delete p;}
29  };
30  struct Replica{
31  template <class X> static X * copy(X *p) { return (p) ? (*p).replica() : 0;}
32  template <class X> static void remove(X * p) { delete p;}
33  };
34 }
35 
36 
39 template <class X, typename P=OwnerPolicy::Transfer> class own_ptr {
40 private:
41  X* ptr;
42 public:
43  typedef X element_type;
45  explicit own_ptr(X* p = 0) : ptr(p) {}
46 
48  own_ptr(const own_ptr& a) : ptr(a.release()) {}
49 
50 #ifndef CMS_NO_TEMPLATE_MEMBERS
51  template <class T, typename Q> own_ptr(const own_ptr<T,Q>& a)
53  : ptr(a.release()) {}
54 #endif
55  own_ptr& operator=(const own_ptr& a) {
57  if (a.get() != ptr) {
58  P::remove(ptr);
59  ptr = a.release();
60  }
61  return *this;
62  }
63 
64 #ifndef CMS_NO_TEMPLATE_MEMBERS
65  template <class T, typename Q> own_ptr& operator=(const own_ptr<T,Q>& a) {
67  if (a.get() != ptr) {
68  P::remove(ptr);
69  ptr = a.release();
70  }
71  return *this;
72  }
73 #endif
74  ~own_ptr() {
76  P::remove(ptr);
77  }
79  X& operator*() const { return *ptr; }
81  X* operator->() const { return ptr; }
83  X* get() const { return ptr; }
85  X* release() const {
86  return P::copy(const_cast<own_ptr<X,P>*>(this)->ptr);
87  }
88 
90  void reset(X* p = 0) {
91  if (p != ptr) {
92  P::remove(ptr);
93  ptr = p;
94  }
95  }
96 };
97 
98 #endif // UTILITIES_GENERAL_OWN_PTR_H
own_ptr & operator=(const own_ptr &a)
Definition: own_ptr.h:56
X * get() const
Definition: own_ptr.h:83
own_ptr(const own_ptr &a)
Definition: own_ptr.h:48
X * release() const
Definition: own_ptr.h:85
static X * copy(X *p)
Definition: own_ptr.h:23
X element_type
Definition: own_ptr.h:43
#define X(str)
Definition: MuonsGrabber.cc:48
own_ptr(X *p=0)
Definition: own_ptr.h:45
static X * copy(X *p)
Definition: own_ptr.h:27
~own_ptr()
Definition: own_ptr.h:75
void reset(X *p=0)
Definition: own_ptr.h:90
X & operator*() const
Definition: own_ptr.h:79
static X * copy(X *p)
Definition: own_ptr.h:31
double a
Definition: hdecay.h:121
static X * copy(X *&p)
Definition: own_ptr.h:19
X * operator->() const
Definition: own_ptr.h:81
X * ptr
Definition: own_ptr.h:41