Reference counting helpers - Progress, tests

* Assignment copy/move operators
 * Boolean conversion / ! operator
 * Content access
This commit is contained in:
Emmanuel BENOîT 2019-01-02 15:50:21 +01:00
parent f0e46da2f5
commit ddaa46867d
3 changed files with 419 additions and 12 deletions
include/ebcl/inline

View file

@ -27,6 +27,12 @@ inline bool T_ReferenceCountedClass< B >::decreaseReferences_( ) noexcept
return ( -- referenceCounter_ ) == 0;
}
template< bool B >
inline uint32_t T_ReferenceCountedClass< B >::getReferenceCount( ) const noexcept
{
return uint32_t{ referenceCounter_ };
}
/*= T_RcPtr ==================================================================*/
@ -41,6 +47,30 @@ inline T_RcPtr< T >::~T_RcPtr( ) noexcept
clear( );
}
template< typename T >
template< typename... AT >
inline T_RcPtr< T > T_RcPtr< T >::New(
AT&& ... arguments )
{
T* const obj{ new T( std::forward< AT >( arguments ) ... ) };
T_RcPtr< T > ptr;
ptr.setTarget_( obj );
return ptr;
}
/*----------------------------------------------------------------------------*/
template< typename T >
inline void T_RcPtr< T >::setTarget_(
T_Target* t ) noexcept
{
assert( target_ == nullptr );
if ( t ) {
t->increaseReferences_( );
}
target_ = t;
}
template< typename T >
inline void T_RcPtr< T >::clear( ) noexcept
{
@ -55,11 +85,9 @@ inline void T_RcPtr< T >::clear( ) noexcept
template< typename T >
inline T_RcPtr< T >::T_RcPtr(
T_RcPtr< T > const& other ) noexcept
: target_{ other.target_ }
: target_{ nullptr }
{
if ( target_ ) {
target_->increaseReferences_( );
}
setTarget_( other.target_ );
}
template< typename T >
@ -68,11 +96,9 @@ template<
T_EnableForChild< T , Q >
> inline T_RcPtr< T >::T_RcPtr(
T_RcPtr< Q > const& other ) noexcept
: target_{ other.target_ }
: target_{ nullptr }
{
if ( target_ ) {
target_->increaseReferences_( );
}
setTarget_( other.target_ );
}
template< typename T >
@ -93,6 +119,45 @@ template<
}
}
template< typename T >
inline T_RcPtr< T >& T_RcPtr< T >::operator= (
T_Self const& other ) noexcept
{
clear( );
setTarget_( other.target_ );
return *this;
}
template< typename T >
template<
typename Q ,
T_EnableForChild< T , Q >
> inline T_RcPtr< T >& T_RcPtr< T >::operator= (
T_RcPtr< Q > const& other ) noexcept
{
clear( );
setTarget_( other.target_ );
return *this;
}
template< typename T >
template<
typename Q ,
T_EnableForParent< T , Q >
> inline T_RcPtr< T >& T_RcPtr< T >::operator= (
T_RcPtr< Q > const& other )
{
clear( );
if ( other.target_ ) {
T* const nt{ dynamic_cast< T* >( other.target_ ) };
if ( nt == nullptr ) {
throw std::bad_cast( );
}
setTarget_( nt );
}
return *this;
}
/*----------------------------------------------------------------------------*/
template< typename T >
@ -124,6 +189,80 @@ template<
}
}
template< typename T >
template<
typename Q ,
T_EnableForChild< T , Q >
> inline T_RcPtr< T >& T_RcPtr< T >::operator= (
T_RcPtr< Q >&& other ) noexcept
{
clear( );
target_ = other.target_;
other.target_ = nullptr;
return *this;
}
template< typename T >
template<
typename Q ,
T_EnableForParent< T , Q >
> inline T_RcPtr< T >& T_RcPtr< T >::operator= (
T_RcPtr< Q >&& other )
{
clear( );
if ( other.target_ ) {
T* const nt{ dynamic_cast< T* >( other.target_ ) };
if ( nt == nullptr ) {
throw std::bad_cast( );
}
other.target_ = nullptr;
target_ = nt;
}
return *this;
}
/*----------------------------------------------------------------------------*/
template< typename T >
inline T_RcPtr< T >::operator bool ( ) const noexcept
{
return target_ != nullptr;
}
template< typename T >
inline bool T_RcPtr< T >::operator! ( ) const noexcept
{
return target_ == nullptr;
}
/*----------------------------------------------------------------------------*/
template< typename T >
inline T_RcPtr< T >::operator T* ( ) const noexcept
{
return target_;
}
template< typename T >
inline T* T_RcPtr< T >::get( ) const noexcept
{
return target_;
}
template< typename T >
inline T* T_RcPtr< T >::operator -> ( ) const
{
assert( target_ != nullptr );
return target_;
}
template< typename T >
inline T& T_RcPtr< T >::operator * ( ) const
{
assert( target_ != nullptr );
return *target_;
}
} // namespace ebcl
#endif // _H_EBCL_INLINE_REFCOUNT