libassa 3.5.1
Loading...
Searching...
No Matches
AutoPtr.h
Go to the documentation of this file.
1// -*- c++ -*-
2//------------------------------------------------------------------------------
3// AutoPtr.h
4//------------------------------------------------------------------------------
5// Copyright (C) 1999,2005 Vladislav Grinchenko
6//
7// This library is free software; you can redistribute it and/or
8// modify it under the terms of the GNU Library General Public
9// License as published by the Free Software Foundation; either
10// version 2 of the License, or (at your option) any later version.
11//------------------------------------------------------------------------------
12#ifndef AUTO_PTR_H
13#define AUTO_PTR_H
14
15#include <cstdlib> // NULL definition
16
23namespace ASSA {
24
32template<typename R> class AutoPtrRef {
33public:
34 explicit AutoPtrRef (R* p_) : m_ptr (p_) { /* no-op */ }
35
37};
38
49template<class X> class AutoPtr {
50private:
53
54public:
61 explicit AutoPtr (X* p_ = 0) : m_ptr (p_) { /* no-op */ }
62
67 AutoPtr (AutoPtr& a_) : m_ptr (a_.release ()) {/* no-op */ }
68
75 template<typename T>
76 AutoPtr (AutoPtr<T>& a_) : m_ptr (a_.release ()) { /* no-op */ }
77
84 reset (a_.release ());
85 return *this;
86 }
87
93 template<class T> AutoPtr& operator=(AutoPtr<T>& a_) {
94 reset (a_.release ());
95 return *this;
96 }
97
103 if (m_ptr) {
104 delete m_ptr;
105 }
106 }
107
111 X& operator*() const { return *m_ptr; }
112
116 X* operator->() const { return m_ptr; }
117
123 X* get () const { return m_ptr; }
124
130 X* release () {
131 X* tmp = m_ptr;
132 m_ptr = NULL;
133 return tmp;
134 }
135
140 void reset (X* p_ = 0) {
141 if (p_ != m_ptr) {
142 delete m_ptr;
143 m_ptr = p_;
144 }
145 }
146
159 AutoPtr (AutoPtrRef<X> ref_) : m_ptr (ref_.m_ptr) { /* no-op */ }
160
162 if (ref_.m_ptr != get ()) {
163 delete m_ptr;
164 m_ptr = ref_.m_ptr;
165 }
166 return *this;
167 }
168
169 template<typename T>
170 operator AutoPtrRef<T> () { return AutoPtrRef<T> (release ()); }
171
172 template<typename T>
173 operator AutoPtr<T> () { return AutoPtr<T> (release ()); }
174
176};
177
185template<typename R> class AutoPtrArrayRef {
186public:
187 explicit AutoPtrArrayRef (R* p_) : m_ptr (p_) { /* no-op */ }
188
190};
191
198template<class X> class AutoPtrArray {
199private:
202
203public:
210 explicit AutoPtrArray (X* p_ = 0) : m_ptr (p_) { /* no-op */ }
211
216 AutoPtrArray (AutoPtrArray& a_) : m_ptr (a_.release ()) {/* no-op */ }
217
225 template<typename T>
227 : m_ptr (a_.release ()) { /* no-op */ }
228
236 reset (a_.release ());
237 return *this;
238 }
239
245 template<class T>
247 reset (a_.release ());
248 return *this;
249 }
250
256 if (m_ptr) {
257 delete [] m_ptr;
258 }
259 }
260
264 X& operator*() const { return *m_ptr; }
265
269 X* operator->() const { return m_ptr; }
270
274 X& operator[] (int i) const {
275 X* array = get ();
276 return (array [i]);
277 }
278
284 X* get () const { return m_ptr; }
285
291 X* release () {
292 X* tmp = m_ptr;
293 m_ptr = NULL;
294 return tmp;
295 }
296
301 void reset (X* p_ = 0) {
302 if (p_ != m_ptr) {
303 delete [] m_ptr;
304 m_ptr = p_;
305 }
306 }
307
321
323 if (ref_.m_ptr != get ()) {
324 delete [] m_ptr;
325 m_ptr = ref_.m_ptr;
326 }
327 return *this;
328 }
329
330 template<typename T>
331 operator AutoPtrArrayRef<T> () { return AutoPtrArrayRef<T> (release ()); }
332
333 template<typename T>
334 operator AutoPtrArray<T> () { return AutoPtrArray<T> (release ()); }
335
337};
338
339} // end namespace ASSA
340
341#endif /* AUTO_PTR_H */
342
343
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:185
AutoPtrArray handles memory management of an array of objects.
Definition AutoPtr.h:198
AutoPtrArray & operator=(AutoPtrArray< T > &a_)
Assignment from another AutoPtrArray of a different but related type.
Definition AutoPtr.h:246
X * m_ptr
Pointer to the object we own.
Definition AutoPtr.h:201
AutoPtrArray & operator=(AutoPtrArray &a_)
Assignment operator deletes memory it owns and transfers the ownership from a_ to itself.
Definition AutoPtr.h:235
AutoPtrArray(X *p_=0)
Construct an AutoPtrArray from a raw pointer.
Definition AutoPtr.h:210
AutoPtrArray & operator=(AutoPtrArrayRef< X > ref_)
Definition AutoPtr.h:322
AutoPtrArray(AutoPtrArray< T > &a_)
Construct AutoPtrArray from another AutoPtrArray of different (but related) type.
Definition AutoPtr.h:226
~AutoPtrArray()
When AutoPtrArray goes out of scope, the object it owns is deleted.
Definition AutoPtr.h:255
X * operator->() const
Smart pointer dereferencing.
Definition AutoPtr.h:269
AutoPtrArray(AutoPtrArray &a_)
Construct AutoPtrArray from another AutoPtrArray.
Definition AutoPtr.h:216
void reset(X *p_=0)
Forcibly delete the managed object and assume the ownership of a_.
Definition AutoPtr.h:301
X * release()
Give up the ownership of the memory.
Definition AutoPtr.h:291
X * get() const
Get a raw memory pointer without changing ownership status.
Definition AutoPtr.h:284
X & operator[](int i) const
Access operator.
Definition AutoPtr.h:274
X & operator*() const
Smart pointer dereferencing.
Definition AutoPtr.h:264
AutoPtrArray(AutoPtrArrayRef< X > ref_)
Automagic conversions.
Definition AutoPtr.h:320
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
AutoPtrRef(R *p_)
Definition AutoPtr.h:34
AutoPtr is based on SGI implementation of a auto_ptr template that makes memory handling a little bit...
Definition AutoPtr.h:49
void reset(X *p_=0)
Forcibly delete the managed object and assume the ownership of a_.
Definition AutoPtr.h:140
AutoPtr(X *p_=0)
Construct an AutoPtr from a raw pointer.
Definition AutoPtr.h:61
X & operator*() const
Smart pointer dereferencing.
Definition AutoPtr.h:111
X * release()
Give up the ownership of the memory.
Definition AutoPtr.h:130
AutoPtr(AutoPtr< T > &a_)
Construct AutoPtr from another AutoPtr of different (but related) type.
Definition AutoPtr.h:76
X * get() const
Get a raw memory pointer without changing ownership status.
Definition AutoPtr.h:123
AutoPtr(AutoPtrRef< X > ref_)
Automagic conversions.
Definition AutoPtr.h:159
AutoPtr & operator=(AutoPtr &a_)
Assignment operator deletes memory it owns and transfers the ownership from a_ to itself.
Definition AutoPtr.h:83
X * m_ptr
Pointer to the object we own.
Definition AutoPtr.h:52
~AutoPtr()
When AutoPtr goes out of scope, the object it owns is deleted.
Definition AutoPtr.h:102
AutoPtr(AutoPtr &a_)
Construct AutoPtr from another AutoPtr.
Definition AutoPtr.h:67
AutoPtr & operator=(AutoPtr< T > &a_)
Assignment from another AutoPtr of a different but related type.
Definition AutoPtr.h:93
X * operator->() const
Smart pointer dereferencing.
Definition AutoPtr.h:116
AutoPtr & operator=(AutoPtrRef< X > ref_)
Definition AutoPtr.h:161