casacore
Loading...
Searching...
No Matches
PtrHolder.h
Go to the documentation of this file.
1// # PtrHolder.h: Hold and delete pointers not deleted by object destructors
2// # Copyright (C) 1994,1995,1999,2000
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_PTRHOLDER_H
27#define CASA_PTRHOLDER_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31
32namespace casacore { // # NAMESPACE CASACORE - BEGIN
33
34// <summary>
35// Hold and delete pointers not deleted by object destructors
36// </summary>
37
38// <use visibility=export>
39// <reviewed reviewer="troberts" date="1995/07/29" tests="tPtrHolder">
40// </reviewed>
41
42// <prerequisite>
43// <li> module <linkto module=Exceptions>Exceptions</linkto>
44// </prerequisite>
45
46// <synopsis>
47// <src>PtrHolder</src>s hold allocated pointers which should be
48// deleted when an exception is thrown. Exceptions only call destructors
49// of objects. Thus, for example, storage allocated in a global function
50// (outside of an object)is not deleted. A <src>PtrHolder</src> solves
51// this problem: it merely holds the pointer and deletes it when it is
52// destroyed itself, e.g. when an exception is thrown or when the
53// function exits normally.
54// </synopsis>
55
56// <example>
57// <srcblock>
58// void func(Int *ptr); // some other function that takes a pointer
59// // ...
60// // True below means it's an array, False (the default) would mean
61// // a singleton object.
62// PtrHolder<Int> iholder(new Int[10000], True);
63// func(iholder); // converts automatically to ptr
64// (iholder.ptr() + 5) = 11; // use pointer explicitly
65// some_function_that_throws_exception(); // pointer is deleted
66// </srcblock>
67// </example>
68
69// <motivation>
70// Avoid leaks when throwing/catching exceptions.
71// </motivation>
72
73// <todo asof="2000/04/11">
74// <li> Use the autoptr class from the Standard Library
75// </todo>
76
77template <class T>
78class PtrHolder {
79 public:
80 // The default constructor uses a null pointer.
81 [[deprecated("Use std::unique_ptr")]]
83
84 // Construct a <src>PtrHolder</src> from a pointer which MUST have
85 // been allocated from <src>new</src>, since the destructor will
86 // call <src>delete</src> on it. If the pointer is to an array,
87 // i.e. allocated with operator <src>new[]</src>, then
88 // <src>isCarray</src> should be set to True. (This parameter is
89 // required because C-arrays need to be deleted with
90 // <src>delete[]</src>.)
91 //
92 // After the pointer is placed into the holder, the user should
93 // not manually delete the pointer; the <src>PtrHolder</src>
94 // object will do that, unless <src>set()</src> or
95 // <src>clear()</src> is called with <src>deleteCurrentPtr</src>
96 // set to False. The pointer must also only be put into
97 // <em>one</em> holder to avoid double deletion.
98 [[deprecated("Use std::unique_ptr")]]
100
102
103 // Set the pointer to a new value. If <src>deleteCurrentPtr </src>is
104 // True (the default), then delete the existing pointer first. If
105 // <src>isCarray</src> is True, then the new pointer is assumed to
106 // have been allocated with <src>new[]</src>.
107 void set(T *pointer, Bool isCarray = False, Bool deleteCurrentPtr = True);
108
109 // Set the current pointer to null; if <src>deletePtr</src> is True
110 // (the default), then the current pointer is deleted first.
111 void clear(Bool deleteCurrentPtr = True);
112
113 // Release the pointer for use.
114 // <group>
115 T *ptr() { return ptr_p; }
116 const T *ptr() const { return ptr_p; }
117 // </group>
118
119 // Attempt to automatically release a pointer when required. If the
120 // compiler can't figure it out, you can use the <src>ptr()</src>
121 // member function directly.
122 operator T *() { return ptr_p; }
123 operator T *() const { return ptr_p; }
124 // </group>
125
126 // Make it possible to use -> on the pointer object.
127 T *operator->() const { return ptr_p; }
128
129 // See if the pointer points to a C-array.
130 Bool isCArray() const { return isCarray_p; }
131
132 private:
133 // # Undefined and inaccessible
134 PtrHolder(const PtrHolder<T> &other);
136
137 // # We'd also like the following to be undefined and inaccessible,
138 // # unfortunately CFront doesn't seem to let you do that.
139 // # void *operator new(size_t s);
140
141 // # Put functionality in one place
143
145 // # If space were critical, we could make isCarray_p a char
147};
148
149// <summary>
150// Hold and delete pointers not deleted by object destructors
151// </summary>
152
153// <use visibility=export>
154// <reviewed reviewer="" date="" tests="tPtrHolder">
155// </reviewed>
156
157// <prerequisite>
158// <li> module <linkto module=Exceptions>Exceptions</linkto>
159// </prerequisite>
160
161// <synopsis>
162// <src>SPtrHolder</src>s hold allocated pointers to non-array objects
163// which should be deleted when an exception is thrown.
164// SPtrHolder is similar to PtrHolder, but easier to use and only valid
165// for pointer to a single object, thus not to a C-array of objects.
166// </synopsis>
167
168// <example>
169// <srcblock>
170// void func(Table *ptr); // some other function that takes a pointer
171// // ...
172// // True below means it's an array, False (the default) would mean
173// // a singleton object.
174// SPtrHolder<Int> iholder(new Table(...));
175// func(iholder); // converts automatically to ptr
176// Table* tab = iholder.transfer(); // transfer ownership
177// </srcblock>
178// If an exception is thrown in function <src>func</src>, the Table will be
179// deleted automatically. After the function call, the ownership is tranfered
180// back to the 'user'
181// </example>
182
183// <motivation>
184// <src>std::auto_ptr</src> is harder to use and its future is unclear.
185// <br>
186// <src>PtrHolder</src> is not fully inlined and has C-array overhead.
187// Furthermore the automatic conversion to a T* is dangerous, because the
188// programmer may not be aware that the pointer is maybe taken over.
189// </motivation>
190
191template <class T>
193 public:
194 // Construct an <src>SPtrHolder</src> from a pointer which MUST have
195 // been allocated from <src>new</src>, since the destructor will
196 // After the pointer is placed into the holder, the user should
197 // not manually delete the pointer unless the transfer function is called.
198 // The pointer must also only be put into
199 // <em>one</em> holder to avoid double deletion.
200 explicit SPtrHolder(T *ptr = 0) : itsPtr(ptr) {}
201
202 ~SPtrHolder() { delete itsPtr; }
203
204 // Reset the pointer.
205 void reset(T *ptr) {
206 if (ptr != itsPtr) {
207 delete itsPtr;
208 itsPtr = ptr;
209 }
210 }
211
212 // Transfer ownership of the pointer.
213 // I.e. return the pointer and set it to 0 in the object.
214 T *transfer() {
215 T *ptr = itsPtr;
216 itsPtr = 0;
217 return ptr;
218 }
219
220 // Release the pointer.
221 void release() { itsPtr = 0; }
222
223 // Make it possible to dereference the pointer object.
224 // <group>
225 T &operator*() { return *itsPtr; }
226 const T &operator*() const { return *itsPtr; }
227 // </group>
228
229 // Make it possible to use -> on the pointer object.
230 T *operator->() const { return itsPtr; }
231
232 // Get the pointer for use.
233 // <group>
234 T *ptr() { return itsPtr; }
235 const T *ptr() const { return itsPtr; }
236 // </group>
237
238 private:
239 // SPrtHolder cannot be copied.
240 // <group>
243 // </group>
244
245 // # The pointer itself.
247};
248
249} // namespace casacore
250
251#ifndef CASACORE_NO_AUTO_TEMPLATES
252#include <casacore/casa/Utilities/PtrHolder.tcc>
253#endif // # CASACORE_NO_AUTO_TEMPLATES
254#endif
T * ptr()
Get the pointer for use.
Definition PtrHolder.h:234
T * transfer()
Transfer ownership of the pointer.
Definition PtrHolder.h:214
const T & operator*() const
Definition PtrHolder.h:226
void release()
Release the pointer.
Definition PtrHolder.h:221
SPtrHolder(const SPtrHolder< T > &other)
SPrtHolder cannot be copied.
SPtrHolder< T > & operator=(const SPtrHolder< T > &other)
T * operator->() const
Make it possible to use -> on the pointer object.
Definition PtrHolder.h:230
void reset(T *ptr)
Reset the pointer.
Definition PtrHolder.h:205
T & operator*()
Make it possible to dereference the pointer object.
Definition PtrHolder.h:225
const T * ptr() const
Definition PtrHolder.h:235
SPtrHolder(T *ptr=0)
Construct an SPtrHolder from a pointer which MUST have been allocated from new, since the destructor ...
Definition PtrHolder.h:200
PtrHolder()
The default constructor uses a null pointer.
PtrHolder(T *pointer, Bool isCArray=False)
Construct a PtrHolder from a pointer which MUST have been allocated from new, since the destructor wi...
void set(T *pointer, Bool isCarray=False, Bool deleteCurrentPtr=True)
Set the pointer to a new value.
T * ptr()
Release the pointer for use.
Definition PtrHolder.h:115
void clear(Bool deleteCurrentPtr=True)
Set the current pointer to null; if deletePtr is True (the default), then the current pointer is dele...
const T * ptr() const
Definition PtrHolder.h:116
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
Bool isCarray_p
Definition PtrHolder.h:146
T * operator->() const
Make it possible to use -> on the pointer object.
Definition PtrHolder.h:127
void delete_pointer_if_necessary()
Bool isCArray() const
See if the pointer points to a C-array.
Definition PtrHolder.h:130
value_type * pointer
Definition Block.h:590
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
PtrHolder(const PtrHolder< T > &other)
const Bool True
Definition aipstype.h:41
T * ptr_p
Definition PtrHolder.h:144
Block< T > & operator=(const T &val)
Set all values in the block to "val".
Definition Block.h:536