corelib/include/ebcl/inline/Registration.hh

124 lines
2.8 KiB
C++

/******************************************************************************/
/* REGISTRATION SUPPORT - INLINE CODE *****************************************/
/******************************************************************************/
#ifndef _H_LW_LIB_INLINE_REGISTRATION
#define _H_LW_LIB_INLINE_REGISTRATION
#include <lw/lib/Registration.hh>
namespace lw {
/*= T_RegisteredItem ============================================================*/
inline T_RegisteredItem::T_RegisteredItem( ) noexcept
: automatic_( false ) , unregisterFunction_( ) ,
data_( nullptr ) , dataDestructor_( )
{ }
inline T_RegisteredItem::T_RegisteredItem(
SP_Unregister& unregisterFunction ,
void* data ,
F_Destructor_ destructor ) noexcept
: automatic_( true ) , unregisterFunction_( unregisterFunction ) ,
data_( data ) , dataDestructor_( destructor )
{ }
template< typename T >
inline T_RegisteredItem::T_RegisteredItem(
SP_Unregister& unregisterFunction ,
T* data ) noexcept
: T_RegisteredItem( unregisterFunction , data ,
[]( void* d ) {
reinterpret_cast< T* >( d )->~T( );
::operator delete( d );
} )
{ }
template< >
inline T_RegisteredItem::T_RegisteredItem(
SP_Unregister& unregisterFunction ,
void* data ) noexcept
: T_RegisteredItem( unregisterFunction , data , [](void*){} )
{ }
inline T_RegisteredItem::~T_RegisteredItem( )
{
clear( );
}
/*----------------------------------------------------------------------------*/
inline T_RegisteredItem::T_RegisteredItem(
T_RegisteredItem&& other ) noexcept
: T_RegisteredItem( )
{
swap( *this , other );
}
inline T_RegisteredItem& T_RegisteredItem::operator =(
T_RegisteredItem&& other ) noexcept
{
clear( );
swap( *this , other );
return *this;
}
inline M_DECLARE_SWAP( T_RegisteredItem )
{
using std::swap;
swap( lhs.automatic_ , rhs.automatic_ );
swap( lhs.unregisterFunction_ , rhs.unregisterFunction_ );
swap( lhs.data_ , rhs.data_ );
swap( lhs.dataDestructor_ , rhs.dataDestructor_ );
}
/*----------------------------------------------------------------------------*/
inline void T_RegisteredItem::automatic(
const bool v ) noexcept
{
automatic_ = v;
}
inline bool T_RegisteredItem::automatic( ) const noexcept
{
return automatic_;
}
/*----------------------------------------------------------------------------*/
inline void T_RegisteredItem::unregister( ) noexcept
{
if ( data_ && unregisterFunction_ ) {
(*unregisterFunction_)( data_ );
deleteData( );
}
}
inline T_RegisteredItem::operator bool( ) const noexcept
{
return bool( data_ );
}
inline void T_RegisteredItem::clear( ) noexcept
{
if ( automatic_ ) {
unregister( );
} else {
deleteData( );
}
}
inline void T_RegisteredItem::deleteData( ) noexcept
{
if ( data_ != nullptr ) {
if ( dataDestructor_ ) {
dataDestructor_( data_ );
}
data_ = nullptr;
}
}
} // namespace lw
#endif // _H_LW_LIB_INLINE_REGISTRATION