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
32
33namespace casacore { //# NAMESPACE CASACORE - BEGIN
34
35// <summary>
36// Hold and delete pointers not deleted by object destructors
37// </summary>
38
39// <use visibility=export>
40// <reviewed reviewer="troberts" date="1995/07/29" tests="tPtrHolder">
41// </reviewed>
42
43// <prerequisite>
44// <li> module <linkto module=Exceptions>Exceptions</linkto>
45// </prerequisite>
46
47// <synopsis>
48// <src>PtrHolder</src>s hold allocated pointers which should be
49// deleted when an exception is thrown. Exceptions only call destructors
50// of objects. Thus, for example, storage allocated in a global function
51// (outside of an object)is not deleted. A <src>PtrHolder</src> solves
52// this problem: it merely holds the pointer and deletes it when it is
53// destroyed itself, e.g. when an exception is thrown or when the
54// function exits normally.
55// </synopsis>
56
57// <example>
58// <srcblock>
59// void func(Int *ptr); // some other function that takes a pointer
60// // ...
61// // True below means it's an array, False (the default) would mean
62// // a singleton object.
63// PtrHolder<Int> iholder(new Int[10000], True);
64// func(iholder); // converts automatically to ptr
65// (iholder.ptr() + 5) = 11; // use pointer explicitly
66// some_function_that_throws_exception(); // pointer is deleted
67// </srcblock>
68// </example>
69
70// <motivation>
71// Avoid leaks when throwing/catching exceptions.
72// </motivation>
73
74// <todo asof="2000/04/11">
75// <li> Use the autoptr class from the Standard Library
76// </todo>
77
78template<class T> class PtrHolder
79{
80public:
81 // The default constructor uses a null pointer.
82 [[deprecated("Use std::unique_ptr")]]
84
85 // Construct a <src>PtrHolder</src> from a pointer which MUST have
86 // been allocated from <src>new</src>, since the destructor will
87 // call <src>delete</src> on it. If the pointer is to an array,
88 // i.e. allocated with operator <src>new[]</src>, then
89 // <src>isCarray</src> should be set to True. (This parameter is
90 // required because C-arrays need to be deleted with
91 // <src>delete[]</src>.)
92 //
93 // After the pointer is placed into the holder, the user should
94 // not manually delete the pointer; the <src>PtrHolder</src>
95 // object will do that, unless <src>set()</src> or
96 // <src>clear()</src> is called with <src>deleteCurrentPtr</src>
97 // set to False. The pointer must also only be put into
98 // <em>one</em> holder to avoid double deletion.
99 [[deprecated("Use std::unique_ptr")]]
100 PtrHolder(T *pointer, Bool isCArray = False);
101
103
104 // Set the pointer to a new value. If <src>deleteCurrentPtr </src>is
105 // True (the default), then delete the existing pointer first. If
106 // <src>isCarray</src> is True, then the new pointer is assumed to
107 // have been allocated with <src>new[]</src>.
108 void set(T *pointer, Bool isCarray = False, Bool deleteCurrentPtr = True);
109
110 // Set the current pointer to null; if <src>deletePtr</src> is True
111 // (the default), then the current pointer is deleted first.
112 void clear(Bool deleteCurrentPtr = True);
113
114 // Release the pointer for use.
115 // <group>
116 T *ptr() { return ptr_p; }
117 const T *ptr() const { return ptr_p; }
118 // </group>
119
120 // Attempt to automatically release a pointer when required. If the
121 // compiler can't figure it out, you can use the <src>ptr()</src>
122 // member function directly.
123 operator T *() { return ptr_p; }
124 operator T *() const { return ptr_p; }
125 // </group>
126
127 // Make it possible to use -> on the pointer object.
128 T* operator->() const
129 { return ptr_p; }
130
131 // See if the pointer points to a C-array.
132 Bool isCArray() const {return isCarray_p;}
133
134private:
135 //# Undefined and inaccessible
136 PtrHolder(const PtrHolder<T> &other);
138
139 //# We'd also like the following to be undefined and inaccessible,
140 //# unfortunately CFront doesn't seem to let you do that.
141 //# void *operator new(size_t s);
142
143 //# Put functionality in one place
145
147 //# If space were critical, we could make isCarray_p a char
149};
150
151
152
153// <summary>
154// Hold and delete pointers not deleted by object destructors
155// </summary>
156
157// <use visibility=export>
158// <reviewed reviewer="" date="" tests="tPtrHolder">
159// </reviewed>
160
161// <prerequisite>
162// <li> module <linkto module=Exceptions>Exceptions</linkto>
163// </prerequisite>
164
165// <synopsis>
166// <src>SPtrHolder</src>s hold allocated pointers to non-array objects
167// which should be deleted when an exception is thrown.
168// SPtrHolder is similar to PtrHolder, but easier to use and only valid
169// for pointer to a single object, thus not to a C-array of objects.
170// </synopsis>
171
172// <example>
173// <srcblock>
174// void func(Table *ptr); // some other function that takes a pointer
175// // ...
176// // True below means it's an array, False (the default) would mean
177// // a singleton object.
178// SPtrHolder<Int> iholder(new Table(...));
179// func(iholder); // converts automatically to ptr
180// Table* tab = iholder.transfer(); // transfer ownership
181// </srcblock>
182// If an exception is thrown in function <src>func</src>, the Table will be
183// deleted automatically. After the function call, the ownership is tranfered
184// back to the 'user'
185// </example>
186
187// <motivation>
188// <src>std::auto_ptr</src> is harder to use and its future is unclear.
189// <br>
190// <src>PtrHolder</src> is not fully inlined and has C-array overhead.
191// Furthermore the automatic conversion to a T* is dangerous, because the
192// programmer may not be aware that the pointer is maybe taken over.
193// </motivation>
194
195
196template<class T> class SPtrHolder
197{
198public:
199 // Construct an <src>SPtrHolder</src> from a pointer which MUST have
200 // been allocated from <src>new</src>, since the destructor will
201 // After the pointer is placed into the holder, the user should
202 // not manually delete the pointer unless the transfer function is called.
203 // The pointer must also only be put into
204 // <em>one</em> holder to avoid double deletion.
205 explicit SPtrHolder (T* ptr = 0)
206 : itsPtr(ptr) {}
207
209 { delete itsPtr; }
210
211 // Reset the pointer.
212 void reset (T* ptr)
213 { if (ptr != itsPtr) { delete itsPtr; itsPtr = ptr; }}
214
215 // Transfer ownership of the pointer.
216 // I.e. return the pointer and set it to 0 in the object.
218 { T* ptr = itsPtr; itsPtr = 0; return ptr; }
219
220 // Release the pointer.
221 void release()
222 { itsPtr = 0; }
223
224 // Make it possible to dereference the pointer object.
225 // <group>
227 { return *itsPtr; }
228 const T& operator*() const
229 { return *itsPtr; }
230 // </group>
231
232 // Make it possible to use -> on the pointer object.
233 T* operator->() const
234 { return itsPtr; }
235
236 // Get the pointer for use.
237 // <group>
238 T* ptr()
239 { return itsPtr; }
240 const T* ptr() const
241 { return itsPtr; }
242 // </group>
243
244private:
245 // SPrtHolder cannot be copied.
246 // <group>
249 // </group>
250
251 //# The pointer itself.
253};
254
255
256
257} //# NAMESPACE CASACORE - END
258
259#ifndef CASACORE_NO_AUTO_TEMPLATES
260#include <casacore/casa/Utilities/PtrHolder.tcc>
261#endif //# CASACORE_NO_AUTO_TEMPLATES
262#endif
Hold and delete pointers not deleted by object destructors.
Definition PtrHolder.h:197
T * ptr()
Get the pointer for use.
Definition PtrHolder.h:238
T * transfer()
Transfer ownership of the pointer.
Definition PtrHolder.h:217
const T & operator*() const
Definition PtrHolder.h:228
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:233
void reset(T *ptr)
Reset the pointer.
Definition PtrHolder.h:212
T & operator*()
Make it possible to dereference the pointer object.
Definition PtrHolder.h:226
const T * ptr() const
Definition PtrHolder.h:240
SPtrHolder(T *ptr=0)
Construct an SPtrHolder from a pointer which MUST have been allocated from new, since the destructor ...
Definition PtrHolder.h:205
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:116
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:117
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
Bool isCarray_p
Definition PtrHolder.h:148
T * operator->() const
Make it possible to use -> on the pointer object.
Definition PtrHolder.h:128
void delete_pointer_if_necessary()
Bool isCArray() const
See if the pointer points to a C-array.
Definition PtrHolder.h:132
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
T * ptr_p
Definition PtrHolder.h:146
PtrHolder< T > & operator=(const PtrHolder< T > &other)