Loading...
Searching...
No Matches
AutoPtr.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// AutoPtr.h
//------------------------------------------------------------------------------
// Copyright (C) 1999,2005 Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#ifndef AUTO_PTR_H
#define AUTO_PTR_H
#include <cstdlib> // NULL definition
namespace ASSA {
template<typename R> class AutoPtrRef {
public:
explicit AutoPtrRef (R* p_) : m_ptr (p_) { /* no-op */ }
R* m_ptr;
};
template<class X> class AutoPtr {
private:
X* m_ptr;
public:
explicit AutoPtr (X* p_ = 0) : m_ptr (p_) { /* no-op */ }
AutoPtr (AutoPtr& a_) : m_ptr (a_.release ()) {/* no-op */ }
template<typename T>
AutoPtr (AutoPtr<T>& a_) : m_ptr (a_.release ()) { /* no-op */ }
AutoPtr& operator= (AutoPtr& a_) {
reset (a_.release ());
return *this;
}
template<class T> AutoPtr& operator=(AutoPtr<T>& a_) {
reset (a_.release ());
return *this;
}
~AutoPtr () {
if (m_ptr) {
delete m_ptr;
}
}
X& operator*() const { return *m_ptr; }
X* operator->() const { return m_ptr; }
X* get () const { return m_ptr; }
X* release () {
X* tmp = m_ptr;
m_ptr = NULL;
return tmp;
}
void reset (X* p_ = 0) {
if (p_ != m_ptr) {
delete m_ptr;
m_ptr = p_;
}
}
AutoPtr (AutoPtrRef<X> ref_) : m_ptr (ref_.m_ptr) { /* no-op */ }
AutoPtr& operator=(AutoPtrRef<X> ref_) {
if (ref_.m_ptr != get ()) {
delete m_ptr;
m_ptr = ref_.m_ptr;
}
return *this;
}
template<typename T>
operator AutoPtrRef<T> () { return AutoPtrRef<T> (release ()); }
template<typename T>
operator AutoPtr<T> () { return AutoPtr<T> (release ()); }
};
template<typename R> class AutoPtrArrayRef {
public:
explicit AutoPtrArrayRef (R* p_) : m_ptr (p_) { /* no-op */ }
R* m_ptr;
};
template<class X> class AutoPtrArray {
private:
X* m_ptr;
public:
explicit AutoPtrArray (X* p_ = 0) : m_ptr (p_) { /* no-op */ }
AutoPtrArray (AutoPtrArray& a_) : m_ptr (a_.release ()) {/* no-op */ }
template<typename T>
AutoPtrArray (AutoPtrArray<T>& a_)
: m_ptr (a_.release ()) { /* no-op */ }
AutoPtrArray& operator= (AutoPtrArray& a_) {
reset (a_.release ());
return *this;
}
template<class T>
AutoPtrArray& operator=(AutoPtrArray<T>& a_) {
reset (a_.release ());
return *this;
}
~AutoPtrArray () {
if (m_ptr) {
delete [] m_ptr;
}
}
X& operator*() const { return *m_ptr; }
X* operator->() const { return m_ptr; }
X& operator[] (int i) const {
X* array = get ();
return (array [i]);
}
X* get () const { return m_ptr; }
X* release () {
X* tmp = m_ptr;
m_ptr = NULL;
return tmp;
}
void reset (X* p_ = 0) {
if (p_ != m_ptr) {
delete [] m_ptr;
m_ptr = p_;
}
}
AutoPtrArray (AutoPtrArrayRef<X> ref_) : m_ptr (ref_.m_ptr) { /* no-op */ }
AutoPtrArray& operator=(AutoPtrArrayRef<X> ref_) {
if (ref_.m_ptr != get ()) {
delete [] m_ptr;
m_ptr = ref_.m_ptr;
}
return *this;
}
template<typename T>
operator AutoPtrArrayRef<T> () { return AutoPtrArrayRef<T> (release ()); }
template<typename T>
operator AutoPtrArray<T> () { return AutoPtrArray<T> (release ()); }
};
} // end namespace ASSA
#endif /* AUTO_PTR_H */
ASSA::AutoPtrArrayRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:185
ASSA::AutoPtrArrayRef::AutoPtrArrayRef
AutoPtrArrayRef(R *p_)
Definition AutoPtr.h:187
ASSA::AutoPtrArrayRef::m_ptr
R * m_ptr
Definition AutoPtr.h:189
ASSA::AutoPtrArray
AutoPtrArray handles memory management of an array of objects.
Definition AutoPtr.h:198
ASSA::AutoPtrArray::operator=
AutoPtrArray & operator=(AutoPtrArray< T > &a_)
Assignment from another AutoPtrArray of a different but related type.
Definition AutoPtr.h:246
ASSA::AutoPtrArray::m_ptr
X * m_ptr
Pointer to the object we own.
Definition AutoPtr.h:201
ASSA::AutoPtrArray::operator=
AutoPtrArray & operator=(AutoPtrArray &a_)
Assignment operator deletes memory it owns and transfers the ownership from a_ to itself.
Definition AutoPtr.h:235
ASSA::AutoPtrArray::AutoPtrArray
AutoPtrArray(X *p_=0)
Construct an AutoPtrArray from a raw pointer.
Definition AutoPtr.h:210
ASSA::AutoPtrArray::operator=
AutoPtrArray & operator=(AutoPtrArrayRef< X > ref_)
Definition AutoPtr.h:322
ASSA::AutoPtrArray::AutoPtrArray
AutoPtrArray(AutoPtrArray< T > &a_)
Construct AutoPtrArray from another AutoPtrArray of different (but related) type.
Definition AutoPtr.h:226
ASSA::AutoPtrArray::~AutoPtrArray
~AutoPtrArray()
When AutoPtrArray goes out of scope, the object it owns is deleted.
Definition AutoPtr.h:255
ASSA::AutoPtrArray::operator->
X * operator->() const
Smart pointer dereferencing.
Definition AutoPtr.h:269
ASSA::AutoPtrArray::AutoPtrArray
AutoPtrArray(AutoPtrArray &a_)
Construct AutoPtrArray from another AutoPtrArray.
Definition AutoPtr.h:216
ASSA::AutoPtrArray::reset
void reset(X *p_=0)
Forcibly delete the managed object and assume the ownership of a_.
Definition AutoPtr.h:301
ASSA::AutoPtrArray::release
X * release()
Give up the ownership of the memory.
Definition AutoPtr.h:291
ASSA::AutoPtrArray::get
X * get() const
Get a raw memory pointer without changing ownership status.
Definition AutoPtr.h:284
ASSA::AutoPtrArray::operator[]
X & operator[](int i) const
Access operator.
Definition AutoPtr.h:274
ASSA::AutoPtrArray::operator*
X & operator*() const
Smart pointer dereferencing.
Definition AutoPtr.h:264
ASSA::AutoPtrArray::AutoPtrArray
AutoPtrArray(AutoPtrArrayRef< X > ref_)
Automagic conversions.
Definition AutoPtr.h:320
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::AutoPtrRef::AutoPtrRef
AutoPtrRef(R *p_)
Definition AutoPtr.h:34
ASSA::AutoPtrRef::m_ptr
R * m_ptr
Definition AutoPtr.h:36
ASSA::AutoPtr
AutoPtr is based on SGI implementation of a auto_ptr template that makes memory handling a little bit...
Definition AutoPtr.h:49
ASSA::AutoPtr::reset
void reset(X *p_=0)
Forcibly delete the managed object and assume the ownership of a_.
Definition AutoPtr.h:140
ASSA::AutoPtr::AutoPtr
AutoPtr(X *p_=0)
Construct an AutoPtr from a raw pointer.
Definition AutoPtr.h:61
ASSA::AutoPtr::operator*
X & operator*() const
Smart pointer dereferencing.
Definition AutoPtr.h:111
ASSA::AutoPtr::release
X * release()
Give up the ownership of the memory.
Definition AutoPtr.h:130
ASSA::AutoPtr::AutoPtr
AutoPtr(AutoPtr< T > &a_)
Construct AutoPtr from another AutoPtr of different (but related) type.
Definition AutoPtr.h:76
ASSA::AutoPtr::get
X * get() const
Get a raw memory pointer without changing ownership status.
Definition AutoPtr.h:123
ASSA::AutoPtr::AutoPtr
AutoPtr(AutoPtrRef< X > ref_)
Automagic conversions.
Definition AutoPtr.h:159
ASSA::AutoPtr::operator=
AutoPtr & operator=(AutoPtr &a_)
Assignment operator deletes memory it owns and transfers the ownership from a_ to itself.
Definition AutoPtr.h:83
ASSA::AutoPtr::m_ptr
X * m_ptr
Pointer to the object we own.
Definition AutoPtr.h:52
ASSA::AutoPtr::~AutoPtr
~AutoPtr()
When AutoPtr goes out of scope, the object it owns is deleted.
Definition AutoPtr.h:102
ASSA::AutoPtr::AutoPtr
AutoPtr(AutoPtr &a_)
Construct AutoPtr from another AutoPtr.
Definition AutoPtr.h:67
ASSA::AutoPtr::operator=
AutoPtr & operator=(AutoPtr< T > &a_)
Assignment from another AutoPtr of a different but related type.
Definition AutoPtr.h:93
ASSA::AutoPtr::operator->
X * operator->() const
Smart pointer dereferencing.
Definition AutoPtr.h:116
ASSA::AutoPtr::operator=
AutoPtr & operator=(AutoPtrRef< X > ref_)
Definition AutoPtr.h:161
ASSA
Definition Acceptor.h:40