Bifrost SDK
Bifrost SDK documentation
Amino::PtrGuard< T > Class Template Reference

Helper guard to allow mutation on a pointee for the lifescope of the PtrGuard<T> but then reassigning to the source Ptr upon its destruction. More...

#include <Ptr.h>

Public Member Functions

 PtrGuard (Ptr< T > &src)
 Constructor. More...
 
 PtrGuard (Ptr< T > &src, PtrGuardUniqueFlag)
 Constructor for the guard when the pointer is known to be uniquely owned at compile time. More...
 
template<class Y , class = if_convertible_from<Y>>
 PtrGuard (Ptr< Y > &src)
 Conversion constructor. More...
 
 PtrGuard (PtrGuard &&) noexcept=default
 Move constructor. More...
 
 PtrGuard (PtrGuard const &) noexcept=delete
 PtrGuard are not copy constructible. More...
 
PtrGuardoperator= (PtrGuard &&) noexcept=delete
 PtrGuard are not move assignable. More...
 
PtrGuardoperator= (PtrGuard const &) noexcept=delete
 PtrGuard are not copy assignable. More...
 
 ~PtrGuard ()
 Destructor. More...
 
T & operator* ()
 Returns a reference to the uniquely owned object. More...
 
T * operator-> ()
 Returns a pointer to the uniquely owned object. More...
 
const T & operator* () const
 Returns a const reference to the uniquely owned object. More...
 
const T * operator-> () const
 Returns a const pointer to the uniquely owned object. More...
 

Detailed Description

template<typename T>
class Amino::PtrGuard< T >

Helper guard to allow mutation on a pointee for the lifescope of the PtrGuard<T> but then reassigning to the source Ptr upon its destruction.

This effectively calls Amino::Ptr<T>::toMutable() upon construction and Amino::MutablePtr<T>::toImmutable() upon destruction. It can be more convenient and safer than transferring ownership manually since reassignment in the original Ptr is guaranteed and may therefore avoid programmatic errors (for example forgetting to assign back on an early return).

{
Amino::PtrGuard<MyClass> guard{this->m_ptr};
// The m_ptr member ownership was transferred to the guard.
assert(!this->m_ptr);
// It's safe to mutate the pointee without introducing side effects.
guard->modifyYourself();
// Ok to do early return. The member "this->m_ptr" will be assigned
// back to contain the mutated pointee upon the PtrGuard's destruction.
if (someCondition) return;
// Still safe to mutate as long as the guard lives.
guard->modifySomethingElse();
}
// The m_ptr member was restored to contain the mutated pointee upon the
// PtrGuard's destruction.
assert(this->m_ptr);
assert(this->m_ptr.unique());
Helper guard to allow mutation on a pointee for the lifescope of the PtrGuard<T> but then reassigning...
Definition: Ptr.h:957

Definition at line 957 of file Ptr.h.

Constructor & Destructor Documentation

◆ PtrGuard() [1/5]

template<typename T >
Amino::PtrGuard< T >::PtrGuard ( Ptr< T > &  src)
inlineexplicit

Constructor.

Warning
The PtrGuard is holding a reference to the source src Ptr. This pointer must therefore live at least as long as the PtrGuard lives.

Definition at line 1380 of file Ptr.h.

◆ PtrGuard() [2/5]

template<typename T >
Amino::PtrGuard< T >::PtrGuard ( Ptr< T > &  src,
PtrGuardUniqueFlag   
)
inlineexplicit

Constructor for the guard when the pointer is known to be uniquely owned at compile time.

See also
PtrGuardUniqueFlag.

Definition at line 1385 of file Ptr.h.

◆ PtrGuard() [3/5]

template<typename T >
template<class Y , class >
Amino::PtrGuard< T >::PtrGuard ( Ptr< Y > &  src)
inlineexplicit

Conversion constructor.

Warning
The PtrGuard is holding a reference to the source src Ptr. This pointer must therefore live at least as long as the PtrGuard lives.

Definition at line 1393 of file Ptr.h.

◆ PtrGuard() [4/5]

template<typename T >
Amino::PtrGuard< T >::PtrGuard ( PtrGuard< T > &&  )
defaultnoexcept

Move constructor.

Warning
This should not be used typically, but is necessary for implementing createPtrGuard(), even if the compiler will elide the call (RVO - Return Value Optimization).

◆ PtrGuard() [5/5]

template<typename T >
Amino::PtrGuard< T >::PtrGuard ( PtrGuard< T > const &  )
deletenoexcept

PtrGuard are not copy constructible.

◆ ~PtrGuard()

template<typename T >
Amino::PtrGuard< T >::~PtrGuard
inline

Destructor.

Restore the orinal source pointer reference to contain the mutated pointee.

Definition at line 1398 of file Ptr.h.

Member Function Documentation

◆ operator*() [1/2]

template<typename T >
T & Amino::PtrGuard< T >::operator* ( )
inline

Returns a reference to the uniquely owned object.

Definition at line 1008 of file Ptr.h.

◆ operator*() [2/2]

template<typename T >
const T & Amino::PtrGuard< T >::operator* ( ) const
inline

Returns a const reference to the uniquely owned object.

Definition at line 1014 of file Ptr.h.

◆ operator->() [1/2]

template<typename T >
T * Amino::PtrGuard< T >::operator-> ( )
inline

Returns a pointer to the uniquely owned object.

Definition at line 1011 of file Ptr.h.

◆ operator->() [2/2]

template<typename T >
const T * Amino::PtrGuard< T >::operator-> ( ) const
inline

Returns a const pointer to the uniquely owned object.

Definition at line 1017 of file Ptr.h.

◆ operator=() [1/2]

template<typename T >
PtrGuard & Amino::PtrGuard< T >::operator= ( PtrGuard< T > &&  )
deletenoexcept

PtrGuard are not move assignable.

Note
They could be, if we judge there are valid uses for it.

◆ operator=() [2/2]

template<typename T >
PtrGuard & Amino::PtrGuard< T >::operator= ( PtrGuard< T > const &  )
deletenoexcept

PtrGuard are not copy assignable.