Reference-counting helpers - Equality operators

* Added == and != operators; they can only be used if the referenced types are
from the same hierarchy
 * Tests for the above
This commit is contained in:
Emmanuel BENOîT 2019-01-02 17:52:56 +01:00
parent b916df3b62
commit a4a2a9eda4
3 changed files with 167 additions and 0 deletions
include/ebcl

View file

@ -159,6 +159,19 @@ class T_RcPtr
// Access the object; undefined behavior if get() == nullptr.
Type* operator -> ( ) const;
Type& operator * ( ) const;
// ---------------------------------------------------------------------
// Comparison operators
template<
typename Q ,
T_EnableForHierarchy< Type , Q > = true
> bool operator== ( T_RcPtr< Q > const& p ) const noexcept;
template<
typename Q ,
T_EnableForHierarchy< Type , Q > = true
> bool operator!= ( T_RcPtr< Q > const& p ) const noexcept;
};
template< typename T >

View file

@ -295,6 +295,30 @@ inline T& T_RcPtr< T >::operator * ( ) const
return *target_;
}
/*----------------------------------------------------------------------------*/
template< typename T >
template<
typename Q ,
T_EnableForHierarchy< T , Q >
> inline bool T_RcPtr< T >::operator== (
T_RcPtr< Q > const& p ) const noexcept
{
return (char const*) this == (char const*) &p
|| (char const*) p.target_ == (char const*) target_;
}
template< typename T >
template<
typename Q ,
T_EnableForHierarchy< T , Q >
> inline bool T_RcPtr< T >::operator!= (
T_RcPtr< Q > const& p ) const noexcept
{
return (char const*) this != (char const*) &p
&& (char const*) p.target_ != (char const*) target_;
}
} // namespace ebcl
#endif // _H_EBCL_INLINE_REFCOUNT