DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Types | Public Member Functions | Private Member Functions | Private Attributes
DGtal::OwningOrAliasingPtr< T > Class Template Reference

#include <OwningOrAliasingPtr.h>

Collaboration diagram for DGtal::OwningOrAliasingPtr< T >:
Collaboration graph
[legend]

Public Types

typedef T Value
typedef T * Pointer
typedef T & Reference

Public Member Functions

 OwningOrAliasingPtr (const T &aValue)
 OwningOrAliasingPtr (Pointer aPtr, bool isOwning=false)
 OwningOrAliasingPtr (const OwningOrAliasingPtr &other)
OwningOrAliasingPtroperator= (const OwningOrAliasingPtr &other)
 ~OwningOrAliasingPtr ()
Pointer operator-> () const
Pointer get () const
Reference operator* () const
bool isOwning () const
void selfDisplay (std::ostream &out) const
bool isValid () const

Private Member Functions

 BOOST_CONCEPT_ASSERT ((boost::CopyConstructible< T >))

Private Attributes

Pointer myPtr
bool myFlagIsOwning

Detailed Description

template<typename T>
class DGtal::OwningOrAliasingPtr< T >

Aim: This class describes a smart pointer that is, given the constructor called by the user, either an alias pointer on existing data or an owning pointer on a copy.

Description of template class 'OwningOrAliasingPtr'

Such a pointer may be used for a (big) member of class, which can be either given at construction (aliasing pointer) or default-constructed (owning pointer).

Such a pointer may be also used to make the user be able to decide whether the data should be copied (owning pointer) or not (aliasing pointer).

Obviously, the data are free'd at destruction in the case of an owning pointer.

Template Parameters:
Ta model of boost::CopyConstructible

Definition at line 72 of file OwningOrAliasingPtr.h.


Member Typedef Documentation

template<typename T>
typedef T* DGtal::OwningOrAliasingPtr< T >::Pointer

Definition at line 80 of file OwningOrAliasingPtr.h.

template<typename T>
typedef T& DGtal::OwningOrAliasingPtr< T >::Reference

Definition at line 81 of file OwningOrAliasingPtr.h.

template<typename T>
typedef T DGtal::OwningOrAliasingPtr< T >::Value

Definition at line 79 of file OwningOrAliasingPtr.h.


Constructor & Destructor Documentation

template<typename T>
DGtal::OwningOrAliasingPtr< T >::OwningOrAliasingPtr ( const T &  aValue)
inline

Constructor with copy.

Parameters:
aValuevalue, myPtr owns a copy of which.

Definition at line 44 of file OwningOrAliasingPtr.ih.

:myPtr( new T(aValue) ), myFlagIsOwning( true )
{
}
template<typename T>
DGtal::OwningOrAliasingPtr< T >::OwningOrAliasingPtr ( Pointer  aPtr,
bool  isOwning = false 
)
inline

Constructor without copy.

Parameters:
aPtrany pointer assigned to myPtr
isOwning'true' if myPtr must be an owning pointer 'false' if myPtr must be only an alias (default).

Definition at line 51 of file OwningOrAliasingPtr.ih.

:myPtr( aPtr ), myFlagIsOwning( aFlagIsOwning )
{
}
template<typename T>
DGtal::OwningOrAliasingPtr< T >::OwningOrAliasingPtr ( const OwningOrAliasingPtr< T > &  other)
inline

Copy constructor.

Parameters:
otherthe object to clone.

Definition at line 58 of file OwningOrAliasingPtr.ih.

References DGtal::OwningOrAliasingPtr< T >::myFlagIsOwning, and DGtal::OwningOrAliasingPtr< T >::myPtr.

: myFlagIsOwning( other.myFlagIsOwning )
{
ASSERT( myFlagIsOwning == other.myFlagIsOwning );
myPtr = new Value( *other.myPtr ); //copy of the data
else
myPtr = other.myPtr; //copy of the alias
}
template<typename T >
DGtal::OwningOrAliasingPtr< T >::~OwningOrAliasingPtr ( )
inline

Destructor.

Definition at line 90 of file OwningOrAliasingPtr.ih.

{
//free if @a myPtr owns the data
delete(myPtr);
}

Member Function Documentation

template<typename T>
DGtal::OwningOrAliasingPtr< T >::BOOST_CONCEPT_ASSERT ( (boost::CopyConstructible< T >)  )
private
template<typename T >
DGtal::OwningOrAliasingPtr< T >::Pointer DGtal::OwningOrAliasingPtr< T >::get ( ) const
inline

Access to the underlying pointer.

Returns:
the pointer.

Definition at line 103 of file OwningOrAliasingPtr.ih.

{
return myPtr;
}
template<typename T >
bool DGtal::OwningOrAliasingPtr< T >::isOwning ( ) const
inline

Tells whether the pointer owns the data or not.

Returns:
boolean equal to 'true' if myPtr owns the data 'false' otherwise.

Definition at line 128 of file OwningOrAliasingPtr.ih.

{
}
template<typename T >
bool DGtal::OwningOrAliasingPtr< T >::isValid ( ) const
inline

Checks the validity/consistency of the object.

Returns:
'true' if the object is valid, 'false' otherwise.

Definition at line 136 of file OwningOrAliasingPtr.ih.

{
return true;
}
template<typename T >
DGtal::OwningOrAliasingPtr< T >::Reference DGtal::OwningOrAliasingPtr< T >::operator* ( ) const
inline

Dereferencement.

Returns:
the data.

Definition at line 119 of file OwningOrAliasingPtr.ih.

{
ASSERT( myPtr != NULL );
return *myPtr;
}
template<typename T >
DGtal::OwningOrAliasingPtr< T >::Pointer DGtal::OwningOrAliasingPtr< T >::operator-> ( ) const
inline

Indirection.

Returns:
the pointer.

Definition at line 111 of file OwningOrAliasingPtr.ih.

{
return myPtr;
}
template<typename T >
DGtal::OwningOrAliasingPtr< T > & DGtal::OwningOrAliasingPtr< T >::operator= ( const OwningOrAliasingPtr< T > &  other)
inline

Assignment.

Parameters:
otherthe object to copy.
Returns:
a reference on 'this'.

Definition at line 71 of file OwningOrAliasingPtr.ih.

References DGtal::OwningOrAliasingPtr< T >::myFlagIsOwning, and DGtal::OwningOrAliasingPtr< T >::myPtr.

{
if ( this != &other )
{
//free old data (if needed)
delete(myPtr);
//acquire new data
myFlagIsOwning = other.myFlagIsOwning;
myPtr = new Value( *other.myPtr ); //copy of the data
else
myPtr = other.myPtr; //copy of the alias
}
return *this;
}
template<typename T >
void DGtal::OwningOrAliasingPtr< T >::selfDisplay ( std::ostream &  out) const
inline

Writes/Displays the object on an output stream.

Parameters:
outthe output stream where the object is written.

Definition at line 144 of file OwningOrAliasingPtr.ih.

{
out << "[OwningOrAliasingPtr]";
if (myPtr != NULL)
out << " " << myPtr << " " << (*myPtr);
else
out << " " << myPtr << " " << "NULL";
}

Field Documentation

template<typename T>
bool DGtal::OwningOrAliasingPtr< T >::myFlagIsOwning
private

Boolean that is equal to 'true' if myPtr owns the data 'false' otherwise

Definition at line 172 of file OwningOrAliasingPtr.h.

Referenced by DGtal::OwningOrAliasingPtr< T >::operator=(), and DGtal::OwningOrAliasingPtr< T >::OwningOrAliasingPtr().

template<typename T>
Pointer DGtal::OwningOrAliasingPtr< T >::myPtr
private

Owning or aliasing pointer to the data

Definition at line 166 of file OwningOrAliasingPtr.h.

Referenced by DGtal::OwningOrAliasingPtr< T >::operator=(), and DGtal::OwningOrAliasingPtr< T >::OwningOrAliasingPtr().


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