For creating pairs of mutually connected pointers in a robust way. More...
#include <Mutual_ptr.hpp>
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_ptr & | operator= (const Mutual_ptr &)=delete |
Mutual_ptr (Mutual_ptr &&other) | |
Mutual_ptr & | operator= (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 | |
![]() | |
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 . | |
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>
.
|
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.
|
inline |
operator=(Mutual_ptr&& other)
|
inlinevirtual |
Obtain the object I am permanently associated with.
Implements hexed::Ptr_base< T, U >.
|
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 >.
|
inline |
Move semantics steal the other's pairing.
If other
is unpaired, this
will be unpaired. other
is left unpaired with its mine
intact.
|
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
|
inline |
if this
is in a pair, mutually unpairs this
and its partner
safe to call regardless of whether this
is currently paired