corelib/include/ebcl/Registration.hh

86 lines
2.6 KiB
C++
Raw Permalink Normal View History

/******************************************************************************/
/* REGISTRATION SUPPORT *******************************************************/
/******************************************************************************/
2018-12-23 18:29:11 +01:00
#ifndef _H_EBCL_REGISTRATION
#define _H_EBCL_REGISTRATION
#include <ebcl/Utilities.hh>
#include <ebcl/Pointers.hh>
namespace ebcl {
/*= 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
2018-12-23 18:29:11 +01:00
#endif // _H_EBCL_REGISTRATION
#include <ebcl/inline/Registration.hh>