ug4
ug::OwnedPtr< T > Class Template Reference

Holds and automatically deletes a pointer, similar to std::auto_ptr. USE WITH CARE! More...

#include <owned_pointer.h>

Public Types

typedef T * TPtr
 
typedef T & TRef
 

Public Member Functions

TPtrget () const
 
 operator bool () const
 
TRef operator* () const
 
TPtr operator-> () const
 
OwnedPtroperator= (const OwnedPtr &op)
 Transfers ownership of the associated object from ap to this. More...
 
 OwnedPtr (const OwnedPtr &op)
 Transfers ownership of the associated object from ap to this. More...
 
 OwnedPtr (TPtr p=0)
 
void reset (TPtr p=0)
 

Private Member Functions

void invalidate () const
 This method is a hack, to allow that only one instance owns the pointer. More...
 

Private Attributes

TPtr m_p
 

Detailed Description

template<class T>
class ug::OwnedPtr< T >

Holds and automatically deletes a pointer, similar to std::auto_ptr. USE WITH CARE!

WARNING: USE WITH CARE! Class shows uncommon behavior when copied.

The behavior of this class is very similar to std::auto_ptr and somewhat similar to boost::scoped_ptr. It takes a pointer in its constructor and automatically deletes the associated object in its destructor or in a call to reset. The most notable property of OwnedPtr is that an OwnedPtr 'owns' the associated object. That means that two OwnedPtrs should never point to the same instance. The copy-constructor thus shows the somewhat irritating behavior of invalidating the original copy and transferring the ownership of the associated object to the new copy of OwnedPtr. This behavior again is the same in std::auto_ptr, however, in contrary to std::auto_ptr, instances of OwnedPtr can be used in standard containers like std::vector. This is explicitly not advised, since many algorithms working with iterators or standard containers will not work with OwnedPtrs. Storing OwnedPtrs in lib_grids attachments may sometimes be useful. Therefore this slightly dangerous class was introduced.

Member Typedef Documentation

◆ TPtr

template<class T >
typedef T* ug::OwnedPtr< T >::TPtr

◆ TRef

template<class T >
typedef T& ug::OwnedPtr< T >::TRef

Constructor & Destructor Documentation

◆ OwnedPtr() [1/2]

template<class T >
ug::OwnedPtr< T >::OwnedPtr ( TPtr  p = 0)
inline

References p.

◆ OwnedPtr() [2/2]

template<class T >
ug::OwnedPtr< T >::OwnedPtr ( const OwnedPtr< T > &  op)
inline

Transfers ownership of the associated object from ap to this.

Note that ap looses ownership of the associated object. ap thus is not const at all, despite beeing declared const.

References ug::OwnedPtr< T >::get(), ug::OwnedPtr< T >::invalidate(), and ug::OwnedPtr< T >::m_p.

Member Function Documentation

◆ get()

template<class T >
TPtr& ug::OwnedPtr< T >::get ( ) const
inline

◆ invalidate()

template<class T >
void ug::OwnedPtr< T >::invalidate ( ) const
inlineprivate

This method is a hack, to allow that only one instance owns the pointer.

References ug::OwnedPtr< T >::m_p.

Referenced by ug::OwnedPtr< T >::operator=(), and ug::OwnedPtr< T >::OwnedPtr().

◆ operator bool()

template<class T >
ug::OwnedPtr< T >::operator bool ( ) const
inline

◆ operator*()

template<class T >
TRef ug::OwnedPtr< T >::operator* ( ) const
inline

◆ operator->()

template<class T >
TPtr ug::OwnedPtr< T >::operator-> ( ) const
inline

◆ operator=()

template<class T >
OwnedPtr& ug::OwnedPtr< T >::operator= ( const OwnedPtr< T > &  op)
inline

Transfers ownership of the associated object from ap to this.

Note that ap looses ownership of the associated object. ap thus is not const at all, despite beeing declared const.

References ug::OwnedPtr< T >::get(), ug::OwnedPtr< T >::invalidate(), and ug::OwnedPtr< T >::reset().

◆ reset()

template<class T >
void ug::OwnedPtr< T >::reset ( TPtr  p = 0)
inline

References ug::OwnedPtr< T >::m_p, and p.

Referenced by ug::OwnedPtr< T >::operator=().

Member Data Documentation

◆ m_p


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