Discontinuous Galerkin Library
#include "dg/algorithm.h"
dg::ClonePtr< Cloneable > Struct Template Reference

Manager class that invokes the clone() method on the managed ptr when copied. More...

Public Member Functions

 ClonePtr (std::nullptr_t value=nullptr)
 init an empty ClonePtr More...
 
 ClonePtr (Cloneable *ptr)
 take ownership of the pointer More...
 
 ClonePtr (const Cloneable &src)
 clone the given value and manage More...
 
 ClonePtr (const ClonePtr &src)
 deep copy the given handle using the clone() method of Cloneable More...
 
ClonePtroperator= (const ClonePtr &src)
 deep copy the given handle using the clone() method of Cloneable More...
 
 ClonePtr (ClonePtr &&src) noexcept
 Steal resources (move construct) More...
 
ClonePtroperator= (ClonePtr &&src) noexcept
 Steal resources (move assignment) More...
 
void reset (Cloneable *ptr)
 Replace the managed object. More...
 
Cloneable * release () noexcept
 Releases ownership of managed object, get() returns nullptr after call. More...
 
void reset (const Cloneable &src)
 Clone the given object and replace the currently held one. More...
 
Cloneable * get ()
 Get a pointer to the object on the heap. More...
 
const Cloneable * get () const
 Get a constant pointer to the object on the heap. More...
 
Cloneable & operator* ()
 Dereference pointer to owned object, i.e. *get() More...
 
const Cloneable & operator* () const
 Dereference pointer to owned object, i.e. *get() More...
 
Cloneable * operator-> ()
 Dereference pointer to owned object, i.e. get() More...
 
const Cloneable * operator-> () const
 Dereference pointer to owned object, i.e. get() More...
 
 operator bool () const
 true if *this owns an object, false else More...
 

Friends

void swap (ClonePtr &first, ClonePtr &second)
 swap the managed pointers More...
 

Detailed Description

template<class Cloneable>
struct dg::ClonePtr< Cloneable >

Manager class that invokes the clone() method on the managed ptr when copied.

When copied invokes a deep copy using the clone() method. This class is most useful when a class needs to hold a polymorphic, Cloneable oject as a variable.

Template Parameters
Cloneablea type that may be uncopyable/unassignable but provides the clone() method with signature
  • Cloneable* clone() const;

Constructor & Destructor Documentation

◆ ClonePtr() [1/5]

template<class Cloneable >
dg::ClonePtr< Cloneable >::ClonePtr ( std::nullptr_t  value = nullptr)
inline

init an empty ClonePtr

◆ ClonePtr() [2/5]

template<class Cloneable >
dg::ClonePtr< Cloneable >::ClonePtr ( Cloneable *  ptr)
inline

take ownership of the pointer

Parameters
ptra pointer to object to manage

◆ ClonePtr() [3/5]

template<class Cloneable >
dg::ClonePtr< Cloneable >::ClonePtr ( const Cloneable &  src)
inline

clone the given value and manage

Note
Since the original object src is cloned, src is allowed to go out of scope after ClonePtr was initialized.
Parameters
srcan object to clone

◆ ClonePtr() [4/5]

template<class Cloneable >
dg::ClonePtr< Cloneable >::ClonePtr ( const ClonePtr< Cloneable > &  src)
inline

deep copy the given handle using the clone() method of Cloneable

Parameters
srcan oject to copy, clones the contained object if not empty

◆ ClonePtr() [5/5]

template<class Cloneable >
dg::ClonePtr< Cloneable >::ClonePtr ( ClonePtr< Cloneable > &&  src)
inlinenoexcept

Steal resources (move construct)

Parameters
srcan object to steal pointer from

Member Function Documentation

◆ get() [1/2]

template<class Cloneable >
Cloneable * dg::ClonePtr< Cloneable >::get ( )
inline

Get a pointer to the object on the heap.

Returns
a pointer to the Cloneable object or nullptr if no object owned

◆ get() [2/2]

template<class Cloneable >
const Cloneable * dg::ClonePtr< Cloneable >::get ( ) const
inline

Get a constant pointer to the object on the heap.

Returns
a pointer to the Cloneable object or nullptr if no object owned

◆ operator bool()

template<class Cloneable >
dg::ClonePtr< Cloneable >::operator bool ( ) const
inlineexplicit

true if *this owns an object, false else

◆ operator*() [1/2]

template<class Cloneable >
Cloneable & dg::ClonePtr< Cloneable >::operator* ( )
inline

Dereference pointer to owned object, i.e. *get()

◆ operator*() [2/2]

template<class Cloneable >
const Cloneable & dg::ClonePtr< Cloneable >::operator* ( ) const
inline

Dereference pointer to owned object, i.e. *get()

◆ operator->() [1/2]

template<class Cloneable >
Cloneable * dg::ClonePtr< Cloneable >::operator-> ( )
inline

Dereference pointer to owned object, i.e. get()

◆ operator->() [2/2]

template<class Cloneable >
const Cloneable * dg::ClonePtr< Cloneable >::operator-> ( ) const
inline

Dereference pointer to owned object, i.e. get()

◆ operator=() [1/2]

template<class Cloneable >
ClonePtr & dg::ClonePtr< Cloneable >::operator= ( ClonePtr< Cloneable > &&  src)
inlinenoexcept

Steal resources (move assignment)

Parameters
srcan object to steal pointer from

◆ operator=() [2/2]

template<class Cloneable >
ClonePtr & dg::ClonePtr< Cloneable >::operator= ( const ClonePtr< Cloneable > &  src)
inline

deep copy the given handle using the clone() method of Cloneable

Parameters
srcan oject to copy and swap

◆ release()

template<class Cloneable >
Cloneable * dg::ClonePtr< Cloneable >::release ( )
inlinenoexcept

Releases ownership of managed object, get() returns nullptr after call.

◆ reset() [1/2]

template<class Cloneable >
void dg::ClonePtr< Cloneable >::reset ( Cloneable *  ptr)
inline

Replace the managed object.

Take the ownership of the given pointer and delete the currently held one if non-empty

Parameters
ptra pointer to a new object to manage

◆ reset() [2/2]

template<class Cloneable >
void dg::ClonePtr< Cloneable >::reset ( const Cloneable &  src)
inline

Clone the given object and replace the currently held one.

Parameters
srca Cloneable object

Friends And Related Function Documentation

◆ swap

template<class Cloneable >
void swap ( ClonePtr< Cloneable > &  first,
ClonePtr< Cloneable > &  second 
)
friend

swap the managed pointers

This follows the discussion in https://stackoverflow.com/questions/5695548/public-friend-swap-member-function

Note
the std library does call this function via unqualified calls in many algorithms, for example in std::iter_swap, it is just std::swap that will not call it directly. Most of the time this is not an issue because we have move assignments now, but if you do want to enable free swap functions like these use:
using std::swap;
swap(a,b);
friend void swap(ClonePtr &first, ClonePtr &second)
swap the managed pointers
Definition: memory.h:87
Parameters
firstfirst instance
secondsecond instance

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