Hymn to Beauty
C++ 3D Engine
Utility::LockBox< T > Class Template Reference

#include <LockBox.hpp>

Classes

class  Key
 

Public Member Functions

 LockBox ()=default
 Constructor that defaults to referencing no data. More...
 
 LockBox (std::shared_ptr< Key > key, T *data)
 Constructor. More...
 
 LockBox (const LockBox< T > &other)
 Make a new reference to an existing lockbox. More...
 
 LockBox (LockBox< T > &&other)
 
LockBox< T > & operator= (LockBox< T > rhs)
 Replace the currently held reference with a new one. More...
 
 ~LockBox ()
 
bool Open (std::shared_ptr< Key > key, const std::function< void(T &)> &forValidKey)
 Open a box to access underlying contents. More...
 
uint32_t RefCount () const
 Number of lockboxes with the same reference. More...
 

Friends

void swap (LockBox< T > &first, LockBox< T > &second)
 Swap two lockboxes with each other. More...
 

Detailed Description

template<typename T>
class Utility::LockBox< T >

Restricts access to data by protecting it with a key. Only those with the correct key may access the data, allowing the user to define the set of owners according to how the key is distributed. Others may keep a reference to the data by holding a lockbox, but without the key they have no way to be granted access to the inner contents, and must turn to an owner to work with it instead.

Constructor & Destructor Documentation

◆ LockBox() [1/4]

template<typename T >
Utility::LockBox< T >::LockBox ( )
default

Constructor that defaults to referencing no data.

◆ LockBox() [2/4]

template<typename T >
Utility::LockBox< T >::LockBox ( std::shared_ptr< Key key,
T *  data 
)

Constructor.

Parameters
keyA key that is allowed to open the box.
dataData that will be wrapped by the LockBox. This raw pointer is not copied or otherwise managed by LockBox, and it's the responsibility of the owner to make sure the pointer is valid for as long as the box is used.

◆ LockBox() [3/4]

template<typename T >
Utility::LockBox< T >::LockBox ( const LockBox< T > &  other)

Make a new reference to an existing lockbox.

Parameters
otherExisting LockBox whose data is referenced.

◆ LockBox() [4/4]

template<typename T >
Utility::LockBox< T >::LockBox ( LockBox< T > &&  other)

Move an existing reference into a new LockBox, invalidating the old one.

Parameters
otherExisting LockBox to move reference from.

◆ ~LockBox()

template<typename T >
Utility::LockBox< T >::~LockBox

Destroys a LockBox, increasing the reference count. Note that the wrapped object is not deleted since the box is only a thin wrapper.

Member Function Documentation

◆ Open()

template<typename T >
bool Utility::LockBox< T >::Open ( std::shared_ptr< Key key,
const std::function< void(T &)> &  forValidKey 
)

Open a box to access underlying contents.

Parameters
keyA key that must match the box.
forValidKeyIf |key| is valid, this function is called, passing a reference to the box contents.
Returns
True if the key matched, false otherwise.

◆ operator=()

template<typename T >
LockBox< T > & Utility::LockBox< T >::operator= ( LockBox< T >  rhs)

Replace the currently held reference with a new one.

Parameters
rhsLockBox with the new reference to hold.
Returns
Reference to the LockBox being assigned to.

◆ RefCount()

template<typename T >
uint32_t Utility::LockBox< T >::RefCount

Number of lockboxes with the same reference.

Returns
Reference count.

Friends And Related Function Documentation

◆ swap

template<typename T >
void swap ( LockBox< T > &  first,
LockBox< T > &  second 
)
friend

Swap two lockboxes with each other.

Parameters
firstFirst lockbox to swap.
secondSecond lockbox to swap.

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