86 lines
2.6 KiB
C++
86 lines
2.6 KiB
C++
|
/******************************************************************************/
|
||
|
/* REGISTRATION SUPPORT *******************************************************/
|
||
|
/******************************************************************************/
|
||
|
|
||
|
#ifndef _H_LW_LIB_REGISTRATION
|
||
|
#define _H_LW_LIB_REGISTRATION
|
||
|
#include <lw/lib/Utilities.hh>
|
||
|
#include <lw/lib/Pointers.hh>
|
||
|
namespace lw {
|
||
|
|
||
|
|
||
|
/*= REGISTRATION SUPPORT =====================================================*/
|
||
|
|
||
|
/* This class is meant to be used with any class that supports registration of
|
||
|
* items (for example the VFS to which VFS drivers are added).
|
||
|
*
|
||
|
* It must be returned from registration methods, and will serve as a way to
|
||
|
* unregister an item, either directly using the unregister() method, or
|
||
|
* automatically when the instance is destroyed (if automatic unregistration
|
||
|
* is active, which is the default).
|
||
|
*/
|
||
|
class T_RegisteredItem
|
||
|
{
|
||
|
public:
|
||
|
using F_Unregister = std::function< void( void* ) >;
|
||
|
using SP_Unregister = T_SharedPtr< F_Unregister >;
|
||
|
|
||
|
private:
|
||
|
using F_Destructor_ = std::function< void( void* ) >;
|
||
|
using WP_Unregister_ = T_WeakPtr< F_Unregister >;
|
||
|
|
||
|
bool automatic_;
|
||
|
WP_Unregister_ unregisterFunction_;
|
||
|
void* data_;
|
||
|
F_Destructor_ dataDestructor_;
|
||
|
|
||
|
public:
|
||
|
/* Initialise an empty item (indicates registration failure) */
|
||
|
T_RegisteredItem( ) noexcept;
|
||
|
|
||
|
/* Initialise a registered item */
|
||
|
T_RegisteredItem( SP_Unregister& unregisterFunction ,
|
||
|
void* data ,
|
||
|
F_Destructor_ destructor ) noexcept;
|
||
|
template< typename T >
|
||
|
T_RegisteredItem( SP_Unregister& unregisterFunction ,
|
||
|
T* data ) noexcept;
|
||
|
|
||
|
/* Destructor. Will unregister the item if automatic mode is
|
||
|
* enabled */
|
||
|
~T_RegisteredItem( );
|
||
|
|
||
|
/* Copy is disabled */
|
||
|
T_RegisteredItem( T_RegisteredItem const& ) = delete;
|
||
|
T_RegisteredItem& operator =( T_RegisteredItem const& ) = delete;
|
||
|
|
||
|
/* Move and swap */
|
||
|
T_RegisteredItem( T_RegisteredItem&& other ) noexcept;
|
||
|
T_RegisteredItem& operator =( T_RegisteredItem&& other ) noexcept;
|
||
|
friend M_DECLARE_SWAP( T_RegisteredItem );
|
||
|
|
||
|
/* Set whether this registrable will unregister automatically when
|
||
|
* it is deleted. */
|
||
|
void automatic( bool v ) noexcept;
|
||
|
bool automatic( ) const noexcept;
|
||
|
|
||
|
/* Unregister this item. May be called multiple times; the first
|
||
|
* call will unregister the item, other calls will be ignored.
|
||
|
*/
|
||
|
void unregister( ) noexcept;
|
||
|
|
||
|
/* Registration check */
|
||
|
operator bool( ) const noexcept;
|
||
|
|
||
|
private:
|
||
|
void clear( ) noexcept;
|
||
|
void deleteData( ) noexcept;
|
||
|
};
|
||
|
M_CLASS_POINTERS( RegisteredItem );
|
||
|
M_DECLARE_SWAP( T_RegisteredItem );
|
||
|
|
||
|
|
||
|
} // namespace
|
||
|
#endif // _H_LW_LIB_REGISTRATION
|
||
|
#include <lw/lib/inline/Registration.hh>
|