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 
39 #ifndef BLOCXX_AUTOPTR_HPP_INCLUDE_GUARD_
40 #define BLOCXX_AUTOPTR_HPP_INCLUDE_GUARD_
41 #include "blocxx/BLOCXX_config.h"
42 
43 namespace BLOCXX_NAMESPACE
44 {
45 
61 // TODO: Rename this
62 template <class X> class AutoPtr
63 {
64 private:
65  X* _ptr;
66 
67  // no copying
68  AutoPtr(const AutoPtr& a);
69  AutoPtr& operator= (const AutoPtr& a);
70 
71 public:
72  typedef X element_type;
73 
79  explicit AutoPtr(X* p = 0);
80 
84  ~AutoPtr();
85 
94  AutoPtr& operator= (X* p);
95 
100  X& operator*() const;
101 
105  X* operator->() const;
106 
110  X* get() const;
111 
117  X* release();
118 
125  void reset(X* p=0);
126 };
127 
128 template <class X>
129 inline AutoPtr<X>::AutoPtr(X* p) : _ptr(p) {}
130 
131 template <class X>
133 {
134  if (p != _ptr)
135  {
136  reset();
137  _ptr = p;
138  }
139  return *this;
140 }
141 
142 template <class X>
144 {
145  typedef char type_must_be_complete[sizeof(X)];
146  delete _ptr;
147 }
148 
149 template <class X>
150 inline X& AutoPtr<X>::operator*() const { return *_ptr;}
151 
152 template <class X>
153 inline X* AutoPtr<X>::operator->() const { return _ptr;}
154 
155 template <class X>
156 inline X* AutoPtr<X>::get() const { return _ptr;}
157 
158 template <class X>
160 {
161  X* rval = _ptr;
162  _ptr = 0;
163  return rval;
164 }
165 
166 template <class X>
167 inline void AutoPtr<X>::reset(X* p)
168 {
169  delete _ptr;
170  _ptr = p;
171 }
172 
184 template <class X> class AutoPtrVec
185 {
186 private:
187  X* _ptr;
188 
189  // no copying
190  AutoPtrVec(const AutoPtrVec& a);
191  AutoPtrVec& operator= (const AutoPtrVec& a);
192 
193 public:
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 
263 template <class X>
264 inline AutoPtrVec<X>::AutoPtrVec(X* p) : _ptr(p) {}
265 
266 template <class X>
268 {
269  if (p != _ptr)
270  {
271  reset();
272  _ptr = p;
273  }
274  return *this;
275 }
276 
277 template <class X>
279 {
280  typedef char type_must_be_complete[sizeof(X)];
281  delete [] _ptr;
282 }
283 
284 template <class X>
285 X& AutoPtrVec<X>::operator*() const { return *_ptr;}
286 
287 template <class X>
288 X* AutoPtrVec<X>::operator->() const { return _ptr;}
289 
290 template <class X>
291 X& AutoPtrVec<X>::operator[](unsigned i) { return _ptr[i]; }
292 
293 template <class X>
294 const X& AutoPtrVec<X>::operator[](unsigned i) const { return _ptr[i]; }
295 
296 template <class X>
297 X* AutoPtrVec<X>::get() const { return _ptr;}
298 
299 template <class X>
301 {
302  X* rval = _ptr;
303  _ptr = 0;
304  return rval;
305 }
306 
307 template <class X>
309 {
310  delete [] _ptr;
311  _ptr = p;
312 }
313 
314 } // end namespace BLOCXX_NAMESPACE
315 
316 #endif