hexed 0.3.0
 
Loading...
Searching...
No Matches
hexed::Mutual_ptr< T, U > Class Template Reference

For creating pairs of mutually connected pointers in a robust way. More...

#include <Mutual_ptr.hpp>

Inheritance diagram for hexed::Mutual_ptr< T, U >:
hexed::Ptr_base< T, U >

Public Member Functions

 Mutual_ptr (T *data)
 Constructs a Mutual_ptr permanently associated with data.
 
 ~Mutual_ptr ()
 unpairs
 
 Mutual_ptr (const Mutual_ptr &)=delete
 Copying is not supported, since what that ought to do to pairs is unclear.
 
Mutual_ptroperator= (const Mutual_ptr &)=delete
 
 Mutual_ptr (Mutual_ptr &&other)
 
Mutual_ptroperator= (Mutual_ptr &&other)
 Move semantics steal the other's pairing.
 
void pair (Ptr_base< U, T > &other)
 pair with another Mutual_ptr, breaking any existing pairs involving this or other
 
void unpair ()
 if this is in a pair, mutually unpairs this and its partner
 
 operator bool () const
 returns true iff currently paired
 
T & mine ()
 Obtain the object I am permanently associated with.
 
Ptr_base< U, T > * partner ()
 if paired, the Mutual_ptr this is currently paired with; else nullptr
 
U * get ()
 if paired, returns partner()'s mine(); else nullptr
 
U & operator* ()
 dereferencing obtains partner()s mine() (undefined if unpaired)
 
U * operator-> ()
 dereferencing members refers to partner()s mine() (undefined if unpaired)
 
U & value ()
 obtains reference to partner()'s mine() if paired and throws exception if not paired
 
const T & mine () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const Ptr_base< U, T > * partner () const
 if paired, the Mutual_ptr this is currently paired with; else nullptr
 
const U * get () const
 if paired, returns partner()'s mine(); else nullptr
 
const U & operator* () const
 dereferencing obtains partner()s mine() (undefined if unpaired)
 
const U * operator-> () const
 dereferencing members refers to partner()s mine() (undefined if unpaired)
 
const U & value () const
 obtains reference to partner()'s mine() if paired and throws exception if not paired
 

Additional Inherited Members

- Protected Member Functions inherited from hexed::Ptr_base< T, U >
void connect (Ptr_base< U, T > *other)
 Mutually connect us by calling both our connect_self.
 
void disconnect (Ptr_base< U, T > *other)
 Mutually disconnect us by calling both our disconnect_self.
 

Detailed Description

template<typename T, typename U>
class hexed::Mutual_ptr< T, U >

For creating pairs of mutually connected pointers in a robust way.

There are several instances in Hexed where two objects need to reference each other. If either one of them is deleted, the other needs to be notified to avoid creating a dangling pointer. This class implements that idea by creating a type of pointer that can be paired with another pointer to create a mutual reference, but breaking the connection from either end will mutually disconnect both pointers. A Mutual_ptr does not own any data. It only manages pairing and unpairing. A Mutual_ptr<T, U> is permanently associated with an object of type T and can point to objects of type U by pairing with a Mutual_ptr<U, T>, or more generally, any Ptr_base<U, T>.

Constructor & Destructor Documentation

◆ Mutual_ptr() [1/2]

template<typename T , typename U >
hexed::Mutual_ptr< T, U >::Mutual_ptr ( T * data)
inline

Constructs a Mutual_ptr permanently associated with data.

data cannot be null. Henceforth, the value of *data can be changed but its address cannot.

◆ Mutual_ptr() [2/2]

template<typename T , typename U >
hexed::Mutual_ptr< T, U >::Mutual_ptr ( Mutual_ptr< T, U > && other)
inline
See also
operator=(Mutual_ptr&& other)

Member Function Documentation

◆ mine() [1/2]

template<typename T , typename U >
T & hexed::Mutual_ptr< T, U >::mine ( )
inlinevirtual

Obtain the object I am permanently associated with.

Implements hexed::Ptr_base< T, U >.

◆ mine() [2/2]

template<typename T , typename U >
const T & hexed::Mutual_ptr< T, U >::mine ( ) const
inlinevirtual

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Implements hexed::Ptr_base< T, U >.

◆ operator=()

template<typename T , typename U >
Mutual_ptr & hexed::Mutual_ptr< T, U >::operator= ( Mutual_ptr< T, U > && other)
inline

Move semantics steal the other's pairing.

If other is unpaired, this will be unpaired. other is left unpaired with its mine intact.

◆ pair()

template<typename T , typename U >
void hexed::Mutual_ptr< T, U >::pair ( Ptr_base< U, T > & other)
inline

pair with another Mutual_ptr, breaking any existing pairs involving this or other

safe to call regardless of whether this and other were previously paired

◆ unpair()

template<typename T , typename U >
void hexed::Mutual_ptr< T, U >::unpair ( )
inline

if this is in a pair, mutually unpairs this and its partner

safe to call regardless of whether this is currently paired


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