libUTL++
utl::AutoPtr< T > Class Template Reference

A "smart" pointer that can also be dumb. More...

#include <AutoPtr.h>

Public Member Functions

 AutoPtr ()
 Constructor. More...
 
 AutoPtr (T *ptr, bool owner=true)
 Constructor. More...
 
 AutoPtr (AutoPtr< T > &&rhs) noexcept
 Move constructor (moving AutoPtr<T>). More...
 
template<typename NonT >
 AutoPtr (AutoPtr< NonT > &&rhs) noexcept
 Move constructor (moving AutoPtr<NonT>). More...
 
 ~AutoPtr ()
 Destructor. More...
 
Misc. Accessors
T * get () const
 Get the pointer. More...
 
bool isNull () const
 Determine whether the pointer is nullptr. More...
 
bool isOwner () const
 Get owner flag. More...
 
Manipulation
void clear ()
 Delete the pointee if the owner flag is true. More...
 
T * release ()
 Release ownership (while keeping the pointer). More...
 
void set (T *ptr, bool owner=true)
 Set new pointer and ownership flag. More...
 
template<typename NonT >
void setNonT (NonT *ptr, bool owner=true)
 Set new pointer and ownership flag. More...
 
Assignment Operators
void steal (AutoPtr< T > &rhs)
 Steal from AutoPtr<T>. More...
 
template<typename NonT >
void stealNonT (AutoPtr< NonT > &rhs)
 Steal from AutoPtr<NonT>. More...
 
AutoPtr< T > & operator= (AutoPtr< T > &&rhs) noexcept
 Move assignment from AutoPtr<T>. More...
 
template<typename NonT >
AutoPtr< T > & operator= (const AutoPtr< NonT > &&rhs) noexcept
 Move assignment from AutoPtr<NonT>. More...
 
AutoPtr< T > & operator= (T *ptr)
 Assignment operator from T*. More...
 
template<typename NonT >
AutoPtr< T > & operator= (NonT *ptr)
 Assignment operator from NonT*. More...
 
Pointer Dereference and Conversion Operators
T & operator* () const
 Pointer dereference operator. More...
 
T * operator-> () const
 Member access operator. More...
 
 operator T* () const
 Conversion to pointer. More...
 

Detailed Description

template<typename T = Object>
class utl::AutoPtr< T >

A "smart" pointer that can also be dumb.

AutoPtr is similar to std::unique_ptr<>, but it attaches an ownership flag to the underlying pointer, instead of always being responsible for deleting the referenced object.

AutoPtr assumes ownership in all cases except these:

  • 1. move-construction/assignment from another AutoPtr that does not have ownership
  • 2. after set() is called with owner = false explicitly specified
  • 3. after release() is called

AutoPtr<> isn't really in competition with std::unique_ptr<>, because the ability to act like a "dumb/regular" pointer is either helpful or not helpful in each case. When it's not helpful, there's no reason to consider the use of AutoPtr<>.

Author
Adam McKee

Definition at line 32 of file AutoPtr.h.

Constructor & Destructor Documentation

◆ AutoPtr() [1/4]

template<typename T = Object>
utl::AutoPtr< T >::AutoPtr ( )
inline

Constructor.

Definition at line 36 of file AutoPtr.h.

References utl::init().

◆ AutoPtr() [2/4]

template<typename T = Object>
utl::AutoPtr< T >::AutoPtr ( T *  ptr,
bool  owner = true 
)
inline

Constructor.

Parameters
ptrpointer
ownerowner flag

Definition at line 46 of file AutoPtr.h.

References utl::init().

◆ AutoPtr() [3/4]

template<typename T = Object>
utl::AutoPtr< T >::AutoPtr ( AutoPtr< T > &&  rhs)
inlinenoexcept

Move constructor (moving AutoPtr<T>).

Definition at line 53 of file AutoPtr.h.

References utl::init().

◆ AutoPtr() [4/4]

template<typename T = Object>
template<typename NonT >
utl::AutoPtr< T >::AutoPtr ( AutoPtr< NonT > &&  rhs)
inlinenoexcept

Move constructor (moving AutoPtr<NonT>).

Definition at line 61 of file AutoPtr.h.

References utl::init().

◆ ~AutoPtr()

template<typename T = Object>
utl::AutoPtr< T >::~AutoPtr ( )
inline

Destructor.

Definition at line 68 of file AutoPtr.h.

References utl::deInit().

Member Function Documentation

◆ get()

template<typename T = Object>
T* utl::AutoPtr< T >::get ( ) const
inline

Get the pointer.

Definition at line 77 of file AutoPtr.h.

Referenced by utl::AutoPtr< T >::operator T*(), utl::AutoPtr< T >::steal(), and utl::AutoPtr< T >::stealNonT().

◆ isNull()

template<typename T = Object>
bool utl::AutoPtr< T >::isNull ( ) const
inline

Determine whether the pointer is nullptr.

Definition at line 84 of file AutoPtr.h.

◆ isOwner()

template<typename T = Object>
bool utl::AutoPtr< T >::isOwner ( ) const
inline

Get owner flag.

Definition at line 91 of file AutoPtr.h.

Referenced by utl::AutoPtr< T >::steal(), and utl::AutoPtr< T >::stealNonT().

◆ clear()

template<typename T = Object>
void utl::AutoPtr< T >::clear ( )
inline

Delete the pointee if the owner flag is true.

Set pointer = nullptr.

Definition at line 104 of file AutoPtr.h.

◆ release()

template<typename T = Object>
T* utl::AutoPtr< T >::release ( )
inline

Release ownership (while keeping the pointer).

Returns
the underlying pointer

Definition at line 116 of file AutoPtr.h.

Referenced by utl::AutoPtr< T >::steal(), and utl::AutoPtr< T >::stealNonT().

◆ set()

template<typename T = Object>
void utl::AutoPtr< T >::set ( T *  ptr,
bool  owner = true 
)
inline

Set new pointer and ownership flag.

Definition at line 124 of file AutoPtr.h.

◆ setNonT()

template<typename T = Object>
template<typename NonT >
void utl::AutoPtr< T >::setNonT ( NonT *  ptr,
bool  owner = true 
)
inline

Set new pointer and ownership flag.

Definition at line 137 of file AutoPtr.h.

References ASSERTD.

◆ steal()

template<typename T = Object>
void utl::AutoPtr< T >::steal ( AutoPtr< T > &  rhs)
inline

Steal from AutoPtr<T>.

Definition at line 148 of file AutoPtr.h.

References utl::AutoPtr< T >::get(), utl::AutoPtr< T >::isOwner(), and utl::AutoPtr< T >::release().

◆ stealNonT()

template<typename T = Object>
template<typename NonT >
void utl::AutoPtr< T >::stealNonT ( AutoPtr< NonT > &  rhs)
inline

Steal from AutoPtr<NonT>.

Definition at line 157 of file AutoPtr.h.

References utl::AutoPtr< T >::get(), utl::AutoPtr< T >::isOwner(), and utl::AutoPtr< T >::release().

◆ operator=() [1/4]

template<typename T = Object>
AutoPtr<T>& utl::AutoPtr< T >::operator= ( AutoPtr< T > &&  rhs)
inlinenoexcept

Move assignment from AutoPtr<T>.

Definition at line 164 of file AutoPtr.h.

◆ operator=() [2/4]

template<typename T = Object>
template<typename NonT >
AutoPtr<T>& utl::AutoPtr< T >::operator= ( const AutoPtr< NonT > &&  rhs)
inlinenoexcept

Move assignment from AutoPtr<NonT>.

Definition at line 172 of file AutoPtr.h.

◆ operator=() [3/4]

template<typename T = Object>
AutoPtr<T>& utl::AutoPtr< T >::operator= ( T *  ptr)
inline

Assignment operator from T*.

Definition at line 179 of file AutoPtr.h.

◆ operator=() [4/4]

template<typename T = Object>
template<typename NonT >
AutoPtr<T>& utl::AutoPtr< T >::operator= ( NonT *  ptr)
inline

Assignment operator from NonT*.

Definition at line 187 of file AutoPtr.h.

◆ operator*()

template<typename T = Object>
T& utl::AutoPtr< T >::operator* ( ) const
inline

Pointer dereference operator.

Definition at line 197 of file AutoPtr.h.

◆ operator->()

template<typename T = Object>
T* utl::AutoPtr< T >::operator-> ( ) const
inline

Member access operator.

Definition at line 203 of file AutoPtr.h.

◆ operator T*()

template<typename T = Object>
utl::AutoPtr< T >::operator T* ( ) const
inline

Conversion to pointer.

Definition at line 209 of file AutoPtr.h.

References ASSERTD, utl::deInit(), utl::AutoPtr< T >::get(), and utl::init().


The documentation for this class was generated from the following file: