EPICS ARCHIVER V4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups
AutoPtr.h
1 // -*- c++ -*-
2 
3 #ifndef _AUTO_PTR_H_
4 #define _AUTO_PTR_H_
5 
6 // system
7 #include <stdlib.h> // size_t
8 
9 // tools
10 #include "tools/NoCopy.h"
11 
13 
24 template<class T>class AutoPtr
25 {
26 public:
27  AutoPtr() : ptr(0) {};
28 
30  AutoPtr(T *in) : ptr(in) {};
31 
34  {
35  ptr = rhs.release();
36  }
37 
40  {
41  assign(0);
42  }
43 
46  {
47  assign(rhs.release());
48  return *this;
49  }
50 
53  {
54  assign(p);
55  return *this;
56  }
57 
59  operator bool () const
60  {
61  return ptr != 0;
62  }
63 
65  T &operator *() const
66  {
67  return *ptr;
68  }
69 
71  T *operator ->() const
72  {
73  return ptr;
74  }
75 
77  T *get() const
78  {
79  return ptr;
80  }
81 
83  operator T * () const
84  {
85  return ptr;
86  }
87 
89  void assign(T *new_ptr)
90  {
91  if (ptr)
92  delete ptr;
93  ptr = new_ptr;
94  }
95 
102  T * release()
103  {
104  T *tmp = ptr;
105  ptr = 0;
106  return tmp;
107  }
108 
109 private:
110  PROHIBIT_DEFAULT_COPY(AutoPtr);
111  T *ptr;
112 };
113 
120 template<class T>class AutoArrayPtr
121 {
122 public:
124  AutoArrayPtr() : arr(0) {};
125 
127  AutoArrayPtr(T *in) : arr(in) {};
128 
131  {
132  arr = rhs.release();
133  }
134 
137  {
138  assign(0);
139  }
140 
143  {
144  assign(rhs.release());
145  return *this;
146  }
147 
150  {
151  assign(p);
152  return *this;
153  }
154 
156  operator bool () const
157  {
158  return arr != 0;
159  }
160 
162  T * get() const
163  {
164  return arr;
165  }
166 
168  T & operator [] (size_t i) const
169  {
170  return arr[i];
171  }
172 
174  void assign(T *new_arr)
175  {
176  if (arr)
177  delete [] arr;
178  arr = new_arr;
179  }
180 
187  T * release()
188  {
189  T *tmp = arr;
190  arr = 0;
191  return tmp;
192  }
193 
194 private:
195  PROHIBIT_DEFAULT_COPY(AutoArrayPtr);
196  T *arr;
197 };
198 
199 #endif
AutoPtr(AutoPtr &rhs)
Copying from other AutoPtr causes rhs to release ownership.
Definition: AutoPtr.h:33
T & operator*() const
Allow access just like ordinary pointer.
Definition: AutoPtr.h:65
T * operator->() const
Allow access just like ordinary pointer.
Definition: AutoPtr.h:71
An auto-pointer for arrays.
Definition: AutoPtr.h:120
AutoArrayPtr & operator=(AutoArrayPtr &rhs)
Copying from other AutoArrayPtr causes rhs to release ownership.
Definition: AutoPtr.h:142
AutoArrayPtr()
Create emptry AutoArrayPtr.
Definition: AutoPtr.h:124
AutoPtr(T *in)
Assign pointer to AutoPtr.
Definition: AutoPtr.h:30
AutoPtr & operator=(AutoPtr &rhs)
Copying from other AutoPtr causes rhs to release ownership.
Definition: AutoPtr.h:45
AutoArrayPtr(AutoArrayPtr &rhs)
Copying from other AutoArrayPtr causes rhs to release ownership.
Definition: AutoPtr.h:130
AutoArrayPtr(T *in)
Assign pointer to AutoArrayPtr.
Definition: AutoPtr.h:127
~AutoArrayPtr()
Destructor deletes owned pointer.
Definition: AutoPtr.h:136
T & operator[](size_t i) const
Access one array element.
Definition: AutoPtr.h:168
Holds pointer, deletes in destructor.
Definition: AutoPtr.h:24
void assign(T *new_ptr)
Assign a new pointer, deleting existing one.
Definition: AutoPtr.h:89
T * release()
Release ownership.
Definition: AutoPtr.h:102
~AutoPtr()
Destructor deletes owned pointer.
Definition: AutoPtr.h:39
void assign(T *new_arr)
Assign a new pointer, deleting existing one.
Definition: AutoPtr.h:174
T * release()
Release ownership.
Definition: AutoPtr.h:187