libassa  3.5.1
Public Member Functions | Private Attributes | List of all members
ASSA::AutoPtr< X > Class Template Reference

AutoPtr is based on SGI implementation of a auto_ptr template that makes memory handling a little bit easier. More...

#include <AutoPtr.h>

Public Member Functions

 AutoPtr (X *p_=0)
 Construct an AutoPtr from a raw pointer. More...
 
 AutoPtr (AutoPtr &a_)
 Construct AutoPtr from another AutoPtr. More...
 
template<typename T >
 AutoPtr (AutoPtr< T > &a_)
 Construct AutoPtr from another AutoPtr of different (but related) type. More...
 
AutoPtroperator= (AutoPtr &a_)
 Assignment operator deletes memory it owns and transfers the ownership from a_ to itself. More...
 
template<class T >
AutoPtroperator= (AutoPtr< T > &a_)
 Assignment from another AutoPtr of a different but related type. More...
 
 ~AutoPtr ()
 When AutoPtr goes out of scope, the object it owns is deleted. More...
 
X & operator* () const
 Smart pointer dereferencing. More...
 
X * operator-> () const
 Smart pointer dereferencing. More...
 
X * get () const
 Get a raw memory pointer without changing ownership status. More...
 
X * release ()
 Give up the ownership of the memory. More...
 
void reset (X *p_=0)
 Forcibly delete the managed object and assume the ownership of a_. More...
 
 AutoPtr (AutoPtrRef< X > ref_)
 Automagic conversions. More...
 
AutoPtroperator= (AutoPtrRef< X > ref_)
 
template<typename T >
 operator AutoPtrRef< T > ()
 
template<typename T >
 operator AutoPtr< T > ()
 

Private Attributes

X * m_ptr
 Pointer to the object we own. More...
 

Detailed Description

template<class X>
class ASSA::AutoPtr< X >

AutoPtr is based on SGI implementation of a auto_ptr template that makes memory handling a little bit easier.

AutoPtr interface does not completely confirm to that of auto_ptr as specified in C++ Standard.

Definition at line 49 of file AutoPtr.h.

Constructor & Destructor Documentation

◆ AutoPtr() [1/4]

template<class X>
ASSA::AutoPtr< X >::AutoPtr ( X *  p_ = 0)
inlineexplicit

Construct an AutoPtr from a raw pointer.

The word 'explicit' disallows implicit construction of objects, for example in function calls.

Parameters
p_pointer (defaults to NULL) to assume ownerwhip for.

Definition at line 61 of file AutoPtr.h.

61 : m_ptr (p_) { /* no-op */ }
X * m_ptr
Pointer to the object we own.
Definition: AutoPtr.h:52

◆ AutoPtr() [2/4]

template<class X>
ASSA::AutoPtr< X >::AutoPtr ( AutoPtr< X > &  a_)
inline

Construct AutoPtr from another AutoPtr.

Parameters
a_AutoPtr object that gives up its ownership.

Definition at line 67 of file AutoPtr.h.

67 : m_ptr (a_.release ()) {/* no-op */ }
X * m_ptr
Pointer to the object we own.
Definition: AutoPtr.h:52

◆ AutoPtr() [3/4]

template<class X>
template<typename T >
ASSA::AutoPtr< X >::AutoPtr ( AutoPtr< T > &  a_)
inline

Construct AutoPtr from another AutoPtr of different (but related) type.

A pointer to T must be convertible to a pointer to X.

Note
Nonconstant parameter
Parameters
a_AutoPtr object that is released of ownership.

Definition at line 76 of file AutoPtr.h.

76 : m_ptr (a_.release ()) { /* no-op */ }
X * m_ptr
Pointer to the object we own.
Definition: AutoPtr.h:52

◆ ~AutoPtr()

template<class X>
ASSA::AutoPtr< X >::~AutoPtr ( )
inline

When AutoPtr goes out of scope, the object it owns is deleted.

Not owning anything has no effect.

Definition at line 102 of file AutoPtr.h.

References ASSA::AutoPtrRef< R >::m_ptr.

102  {
103  if (m_ptr) {
104  delete m_ptr;
105  }
106  }
X * m_ptr
Pointer to the object we own.
Definition: AutoPtr.h:52

◆ AutoPtr() [4/4]

template<class X>
ASSA::AutoPtr< X >::AutoPtr ( AutoPtrRef< X >  ref_)
inline

Automagic conversions.

These operations convert an AutoPtr into/from an AutoPtrRef as needed. This allows on-the-fly conversion between AutoPtr of different but related types (parent/child):

AutoPtr<Derived> FooReturnsAutoPtr () { ... };
AutoPtr<Base> aptr = FooReturnsAutoPtr ();

Definition at line 159 of file AutoPtr.h.

159 : m_ptr (ref_.m_ptr) { /* no-op */ }
X * m_ptr
Pointer to the object we own.
Definition: AutoPtr.h:52

Member Function Documentation

◆ get()

template<class X>
X* ASSA::AutoPtr< X >::get ( ) const
inline

Get a raw memory pointer without changing ownership status.

Usefull when you need to pass a pointer to the function.

Returns
The raw pointer being managed.

Definition at line 123 of file AutoPtr.h.

References ASSA::AutoPtrRef< R >::m_ptr.

Referenced by ASSA::Logger::log_open().

123 { return m_ptr; }
X * m_ptr
Pointer to the object we own.
Definition: AutoPtr.h:52

◆ operator AutoPtr< T >()

template<class X>
template<typename T >
ASSA::AutoPtr< X >::operator AutoPtr< T > ( )
inline

Definition at line 173 of file AutoPtr.h.

173 { return AutoPtr<T> (release ()); }
X * release()
Give up the ownership of the memory.
Definition: AutoPtr.h:130

◆ operator AutoPtrRef< T >()

template<class X>
template<typename T >
ASSA::AutoPtr< X >::operator AutoPtrRef< T > ( )
inline

Definition at line 170 of file AutoPtr.h.

170 { return AutoPtrRef<T> (release ()); }
X * release()
Give up the ownership of the memory.
Definition: AutoPtr.h:130

◆ operator*()

template<class X>
X& ASSA::AutoPtr< X >::operator* ( ) const
inline

Smart pointer dereferencing.

Definition at line 111 of file AutoPtr.h.

References ASSA::AutoPtrRef< R >::m_ptr.

111 { return *m_ptr; }
X * m_ptr
Pointer to the object we own.
Definition: AutoPtr.h:52

◆ operator->()

template<class X>
X* ASSA::AutoPtr< X >::operator-> ( ) const
inline

Smart pointer dereferencing.

Definition at line 116 of file AutoPtr.h.

References ASSA::AutoPtrRef< R >::m_ptr.

116 { return m_ptr; }
X * m_ptr
Pointer to the object we own.
Definition: AutoPtr.h:52

◆ operator=() [1/3]

template<class X>
AutoPtr& ASSA::AutoPtr< X >::operator= ( AutoPtr< X > &  a_)
inline

Assignment operator deletes memory it owns and transfers the ownership from a_ to itself.

Parameters
a_another AutoPtr of the same type.

Definition at line 83 of file AutoPtr.h.

References ASSA::AutoPtr< X >::release().

83  {
84  reset (a_.release ());
85  return *this;
86  }
void reset(X *p_=0)
Forcibly delete the managed object and assume the ownership of a_.
Definition: AutoPtr.h:140

◆ operator=() [2/3]

template<class X>
template<class T >
AutoPtr& ASSA::AutoPtr< X >::operator= ( AutoPtr< T > &  a_)
inline

Assignment from another AutoPtr of a different but related type.

Note
Nonconstant parameter
Parameters
a_AutoPtr to assume ownership of

Definition at line 93 of file AutoPtr.h.

References ASSA::AutoPtr< X >::release().

93  {
94  reset (a_.release ());
95  return *this;
96  }
void reset(X *p_=0)
Forcibly delete the managed object and assume the ownership of a_.
Definition: AutoPtr.h:140

◆ operator=() [3/3]

template<class X>
AutoPtr& ASSA::AutoPtr< X >::operator= ( AutoPtrRef< X >  ref_)
inline

Definition at line 161 of file AutoPtr.h.

References ASSA::AutoPtrRef< R >::m_ptr.

161  {
162  if (ref_.m_ptr != get ()) {
163  delete m_ptr;
164  m_ptr = ref_.m_ptr;
165  }
166  return *this;
167  }
X * m_ptr
Pointer to the object we own.
Definition: AutoPtr.h:52
X * get() const
Get a raw memory pointer without changing ownership status.
Definition: AutoPtr.h:123

◆ release()

template<class X>
X* ASSA::AutoPtr< X >::release ( )
inline