blocxx
AutoPtr.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Vintela, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Vintela, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
38
39#ifndef BLOCXX_AUTOPTR_HPP_INCLUDE_GUARD_
40#define BLOCXX_AUTOPTR_HPP_INCLUDE_GUARD_
41#include "blocxx/BLOCXX_config.h"
42
43namespace BLOCXX_NAMESPACE
44{
45
61// TODO: Rename this
62template <class X> class AutoPtr
63{
64private:
65 X* _ptr;
66
67 // no copying
68 AutoPtr(const AutoPtr& a);
70
71public:
72 typedef X element_type;
73
79 explicit AutoPtr(X* p = 0);
80
85
95
100 X& operator*() const;
101
105 X* operator->() const;
106
110 X* get() const;
111
118
125 void reset(X* p=0);
126};
127
128template <class X>
129inline AutoPtr<X>::AutoPtr(X* p) : _ptr(p) {}
130
131template <class X>
133{
134 if (p != _ptr)
135 {
136 reset();
137 _ptr = p;
138 }
139 return *this;
140}
141
142template <class X>
144{
145 typedef char type_must_be_complete[sizeof(X)];
146 delete _ptr;
147}
148
149template <class X>
150inline X& AutoPtr<X>::operator*() const { return *_ptr;}
151
152template <class X>
153inline X* AutoPtr<X>::operator->() const { return _ptr;}
154
155template <class X>
156inline X* AutoPtr<X>::get() const { return _ptr;}
157
158template <class X>
160{
161 X* rval = _ptr;
162 _ptr = 0;
163 return rval;
164}
165
166template <class X>
167inline void AutoPtr<X>::reset(X* p)
168{
169 delete _ptr;
170 _ptr = p;
171}
172
184template <class X> class AutoPtrVec
185{
186private:
188
189 // no copying
192
193public:
194 typedef X element_type;
195
201 explicit AutoPtrVec(X* p = 0);
202
206 ~AutoPtrVec();
207
216 AutoPtrVec& operator= (X* p);
217
222 X& operator*() const;
223
227 X* operator->() const;
228
233 X& operator[](unsigned n);
234
239 const X& operator[](unsigned i) const;
240
244 X* get() const;
245
251 X* release();
252
259 void reset(X* p=0);
260};
261
262
263template <class X>
264inline AutoPtrVec<X>::AutoPtrVec(X* p) : _ptr(p) {}
265
266template <class X>
268{
269 if (p != _ptr)
270 {
271 reset();
272 _ptr = p;
273 }
274 return *this;
275}
276
277template <class X>
279{
280 typedef char type_must_be_complete[sizeof(X)];
281 delete [] _ptr;
282}
283
284template <class X>
285X& AutoPtrVec<X>::operator*() const { return *_ptr;}
286
287template <class X>
288X* AutoPtrVec<X>::operator->() const { return _ptr;}
289
290template <class X>
291X& AutoPtrVec<X>::operator[](unsigned i) { return _ptr[i]; }
292
293template <class X>
294const X& AutoPtrVec<X>::operator[](unsigned i) const { return _ptr[i]; }
295
296template <class X>
297X* AutoPtrVec<X>::get() const { return _ptr;}
298
299template <class X>
301{
302 X* rval = _ptr;
303 _ptr = 0;
304 return rval;
305}
306
307template <class X>
309{
310 delete [] _ptr;
311 _ptr = p;
312}
313
314} // end namespace BLOCXX_NAMESPACE
315
316#endif
void reset(X *p=0)
Delete the object the underlying pointer points to and take ownership of a new pointer.
Definition AutoPtr.hpp:167
AutoPtr & operator=(const AutoPtr &a)
X * release()
Release ownership of the underlying pointer.
Definition AutoPtr.hpp:159
~AutoPtr()
Destroy this AutoPtr object and the object it points to.
Definition AutoPtr.hpp:143
AutoPtr(const AutoPtr &a)
AutoPtr(X *p=0)
Construct a new AutoPtr object that assumes ownership of a given pointer.
Definition AutoPtr.hpp:129
~AutoPtrVec()
Destroy this AutoPtrVec object and the array it points to.
Definition AutoPtr.hpp:278
void reset(X *p=0)
Delete the array the underlying pointer points to and take ownership of a new array pointer.
Definition AutoPtr.hpp:308
X & operator[](unsigned n)
Definition AutoPtr.hpp:291
AutoPtrVec & operator=(const AutoPtrVec &a)
X * release()
Release ownership of the underlying array.
Definition AutoPtr.hpp:300
AutoPtrVec(const AutoPtrVec &a)
Taken from RFC 1321.