Reference-counting helpers - Copy/move cons/ass w/ casts

Added tests and fixed a few bugs.
This commit is contained in:
Emmanuel BENOîT 2019-01-02 17:14:08 +01:00
parent e8e0e8101d
commit 6afe127d13
3 changed files with 189 additions and 2 deletions
include/ebcl

View file

@ -73,6 +73,9 @@ class T_RcPtr
T_AtomicRcClass , T_SimpleRcClass
>;
// All RcPtr's are friends
template< typename > friend class T_RcPtr;
private:
T_Target* target_;
void setTarget_( T_Target* t ) noexcept;
@ -88,6 +91,10 @@ class T_RcPtr
template< typename... AT >
static T_RcPtr< Type > New( AT&& ... arguments );
// Initialise from raw pointer. The instance must have a reference
// count of zero, and must have been allocated using new.
static T_RcPtr< Type > FromRaw( Type* ptr ) noexcept;
void clear( ) noexcept;
// ---------------------------------------------------------------------
@ -145,6 +152,17 @@ class T_RcPtr
template< typename T >
void swap( T_RcPtr< T >& lhs , T_RcPtr< T >& rhs ) noexcept;
template< typename T >
inline T_RcPtr< T > T_RcPtr< T >::FromRaw(
T* ptr ) noexcept
{
assert( ptr->getReferenceCount( ) == 0 );
T_RcPtr< T > rv;
ptr->increaseReferences_( );
rv.target_ = ptr;
return rv;
}
} // namespace
#endif // _H_EBCL_REFCOUNT

View file

@ -168,7 +168,9 @@ template<
T_RcPtr< Q >&& other ) noexcept
: target_{ nullptr }
{
std::swap( target_ , other.target_ );
T* const ptr{ (T*) other.target_ };
other.target_ = nullptr;
target_ = ptr;
}
template< typename T >