DGtal  1.2.0
Public Types | Public Member Functions | Private Member Functions | Private Attributes
DGtal::CowPtr< T > Class Template Reference

Aim: Copy on write shared pointer. More...

#include <DGtal/base/CowPtr.h>

Inheritance diagram for DGtal::CowPtr< T >:
[legend]

Public Types

typedef T element_type
 

Public Member Functions

 CowPtr (T *p=0) noexcept
 
 CowPtr (const CowPtr &r) noexcept
 
 CowPtr (const CountedPtr< T > &r, bool)
 
CowPtroperator= (const CowPtr &r)
 
const T & operator* () const noexcept
 
const T * operator-> () const noexcept
 
const T * get () const noexcept
 
template<typename U = T, typename std::enable_if< ! std::is_const< U >::value >::type * = nullptr>
T & operator* ()
 
template<typename U = T, typename std::enable_if< ! std::is_const< U >::value >::type * = nullptr>
T * operator-> ()
 
template<typename U = T, typename std::enable_if< ! std::is_const< U >::value >::type * = nullptr>
T * get ()
 
bool operator== (const T *other) const
 
bool operator!= (const T *other) const
 
unsigned int count () const
 
T * drop ()
 
bool unique () const noexcept
 
void selfDisplay (std::ostream &out) const
 
bool isValid () const
 

Private Member Functions

void copy ()
 

Private Attributes

CountedPtr< T > myPtr
 

Detailed Description

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

Aim: Copy on write shared pointer.

Description of template class 'CowPtr'

Use reference counting as long as the pointed object is not modified. When it is about to be modified, copy it and modify the copy.

Taken from http://ootips.org/yonat/4dev/smart-pointers.html

Definition at line 67 of file CowPtr.h.

Member Typedef Documentation

◆ element_type

template<typename T >
typedef T DGtal::CowPtr< T >::element_type

Definition at line 72 of file CowPtr.h.

Constructor & Destructor Documentation

◆ CowPtr() [1/3]

template<typename T >
DGtal::CowPtr< T >::CowPtr ( T *  p = 0)
inlineexplicitnoexcept

Definition at line 74 of file CowPtr.h.

74 : myPtr(p) {}
CountedPtr< T > myPtr
Definition: CowPtr.h:182

◆ CowPtr() [2/3]

template<typename T >
DGtal::CowPtr< T >::CowPtr ( const CowPtr< T > &  r)
inlinenoexcept

Definition at line 76 of file CowPtr.h.

76 : myPtr(r.myPtr) {}

◆ CowPtr() [3/3]

template<typename T >
DGtal::CowPtr< T >::CowPtr ( const CountedPtr< T > &  r,
bool   
)
inline

Builds a copy-on-write pointer from a counted pointer. Requires an extra dummy parameter in order to solve ambiguities when casting from Clone<T> to CowPtr<T>.

Parameters
rany counted pointer
Note
Since 1.0, there is no more the direct constructor CowPtr<T>::CowPtr( CountedPtr<T> ). Indeed, it was creating an ambiguity when using conversion operator in class Clone<T>. More precisely it was impossible to have two conversion operators: Clone<T>::operator CountedPtr<T> and Clone<T>::operator CowPtr<T> since this was creating a compilation ambiguity. This is why there is now a dummy bool parameter in this constructor while both conversion operators Clone<T>::operator CountedPtr<T> and Clone<T>::operator CowPtr<T> are present.
struct B {};
struct A {
CowPtr<B> _bcow;
CountedPtr<B> _bcounted;
// both constructions below are valid and do lazy copy if possible.
A( Clone<B> b1, Clone<B> b2 ) : _bcow( b1 ), _bcounted( b2 ) {}
};

Definition at line 106 of file CowPtr.h.

106 : myPtr( r ) {}

Member Function Documentation

◆ copy()

template<typename T >
void DGtal::CowPtr< T >::copy ( )
inlineprivate

Definition at line 186 of file CowPtr.h.

187  {
188  if (!myPtr.unique()) {
189  T* old_p = myPtr.get();
190  myPtr = CountedPtr<T>(new T(*old_p));
191  }
192  }

References DGtal::CowPtr< T >::myPtr.

Referenced by DGtal::CowPtr< T >::get(), DGtal::CowPtr< T >::operator*(), and DGtal::CowPtr< T >::operator->().

◆ count()

template<typename T >
unsigned int DGtal::CowPtr< T >::count ( ) const
inline

For debug.

Definition at line 173 of file CowPtr.h.

173 { return myPtr.count(); }

References DGtal::CowPtr< T >::myPtr.

◆ drop()

template<typename T >
T* DGtal::CowPtr< T >::drop ( )
inline

Definition at line 174 of file CowPtr.h.

174 { return myPtr.drop(); }

References DGtal::CowPtr< T >::myPtr.

◆ get() [1/2]

template<typename T >
template<typename U = T, typename std::enable_if< ! std::is_const< U >::value >::type * = nullptr>
T* DGtal::CowPtr< T >::get ( )
inline

Definition at line 145 of file CowPtr.h.

145 {copy(); return myPtr.get();}
void copy()
Definition: CowPtr.h:186

References DGtal::CowPtr< T >::copy(), and DGtal::CowPtr< T >::myPtr.

◆ get() [2/2]

template<typename T >
const T* DGtal::CowPtr< T >::get ( ) const
inlinenoexcept

Definition at line 117 of file CowPtr.h.

117 {return myPtr.get();}

References DGtal::CowPtr< T >::myPtr.

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

◆ isValid()

template<typename T >
bool DGtal::CowPtr< T >::isValid ( ) const

Checks the validity/consistency of the object.

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

Referenced by DGtal::Image< TImageContainer >::isValid().

◆ operator!=()

template<typename T >
bool DGtal::CowPtr< T >::operator!= ( const T *  other) const
inline

Inequality operator !=

Parameters
otherany other pointer.
Returns
'true' if pointed address is different from other.

Definition at line 164 of file CowPtr.h.

165  {
166  return get() != other;
167  }
const T * get() const noexcept
Definition: CowPtr.h:117

References DGtal::CowPtr< T >::get().

◆ operator*() [1/2]

template<typename T >
template<typename U = T, typename std::enable_if< ! std::is_const< U >::value >::type * = nullptr>
T& DGtal::CowPtr< T >::operator* ( )
inline

Definition at line 139 of file CowPtr.h.

139 {copy(); return *myPtr;}

References DGtal::CowPtr< T >::copy(), and DGtal::CowPtr< T >::myPtr.

◆ operator*() [2/2]

template<typename T >
const T& DGtal::CowPtr< T >::operator* ( ) const
inlinenoexcept

Definition at line 115 of file CowPtr.h.

115 {return *myPtr;}

References DGtal::CowPtr< T >::myPtr.

◆ operator->() [1/2]

template<typename T >
template<typename U = T, typename std::enable_if< ! std::is_const< U >::value >::type * = nullptr>
T* DGtal::CowPtr< T >::operator-> ( )
inline

Definition at line 142 of file CowPtr.h.

142 {copy(); return myPtr.get();}

References DGtal::CowPtr< T >::copy(), and DGtal::CowPtr< T >::myPtr.

◆ operator->() [2/2]

template<typename T >
const T* DGtal::CowPtr< T >::operator-> ( ) const
inlinenoexcept

Definition at line 116 of file CowPtr.h.

116 {return myPtr.get();}

References DGtal::CowPtr< T >::myPtr.

◆ operator=()

template<typename T >
CowPtr& DGtal::CowPtr< T >::operator= ( const CowPtr< T > &  r)
inline

Definition at line 107 of file CowPtr.h.

108  {
109  if (this != &r)
110  myPtr = r.myPtr;
111  return *this;
112  }

References DGtal::CowPtr< T >::myPtr.

◆ operator==()

template<typename T >
bool DGtal::CowPtr< T >::operator== ( const T *  other) const
inline

Equality operator ==

Parameters
otherany other pointer.
Returns
'true' if pointed address is equal to other.

Definition at line 153 of file CowPtr.h.

154  {
155  return get() == other;
156  }

References DGtal::CowPtr< T >::get().

◆ selfDisplay()

template<typename T >
void DGtal::CowPtr< T >::selfDisplay ( std::ostream &  out) const

Writes/Displays the object on an output stream.

Parameters
outthe output stream where the object is written.

◆ unique()

template<typename T >
bool DGtal::CowPtr< T >::unique ( ) const
inlinenoexcept

Definition at line 175 of file CowPtr.h.

175 { return myPtr.unique(); }

References DGtal::CowPtr< T >::myPtr.

Field Documentation

◆ myPtr

template<typename T >
CountedPtr<T> DGtal::CowPtr< T >::myPtr
private

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