#ifndef __CDS_URCU_EXEMPT_PTR_H
#define __CDS_URCU_EXEMPT_PTR_H
+#include <type_traits>
#include <cds/details/defs.h>
namespace cds { namespace urcu {
/**
This special pointer class is intended for returning extracted node from RCU-based container.
The destructor (and \p release() member function) invokes <tt>RCU::retire_ptr< Disposer >()</tt> function to dispose the node.
- For non-intrusive containers from \p cds::container namespace \p Disposer is an invocation
+ For non-intrusive containers from \p cds::container namespace \p Disposer is usually an invocation
of node deallocator. For intrusive containers the disposer can be empty or it can trigger an event "node can be reused safely".
In any case, the exempt pointer concept keeps RCU semantics.
- You don't need use this helper class directly. Any RCU-based container defines a proper typedef for this template.
+ You don't need use this helper class directly. Any RCU-based container typedefs a simplified version of this template.
Template arguments:
- \p RCU - one of \ref cds_urcu_gc "RCU type"
- \p NodeType - container's node type
- \p ValueType - value type stored in container's node. For intrusive containers it is the same as \p NodeType
- \p Disposer - a disposer functor
- - \p Cast - a functor for casting from \p NodeType to \p ValueType. Usually, for intrusive containers \p Cast may be \p void.
+ - \p Cast - a functor for casting from \p NodeType to \p ValueType. For intrusive containers
+ the casting is usually disabled, i.e. \p Cast is \p void.
*/
template <
class RCU,
>
class exempt_ptr
{
- //TODO: use move semantics and explicit operator bool!
+ //@cond
+ struct trivial_cast {
+ ValueType * operator()( NodeType * p ) const
+ {
+ return p;
+ }
+ };
+ //@endcond
public:
typedef RCU rcu ; ///< RCU type - one of <tt>cds::urcu::gc< ... ></tt>
typedef NodeType node_type ; ///< Node type
typedef ValueType value_type ; ///< Value type
typedef Disposer disposer ; ///< Disposer calling when release
- typedef Cast node_to_value_cast ; ///< Functor converting \p node_type to \p value_type
+ /// Functor converting \p node_type to \p value_type
+ typedef typename std::conditional< std::is_same< Cast, void >::value, trivial_cast, Cast>::type node_to_value_cast;
private:
//@cond
node_type * m_pNode;
//@endcond
- private:
- //@cond
- // No copy-constructible
- exempt_ptr( exempt_ptr const& );
- exempt_ptr& operator=( exempt_ptr const& );
- //@endcond
-
public:
/// Constructs empty pointer
exempt_ptr() CDS_NOEXCEPT
- : m_pNode( null_ptr<node_type *>())
+ : m_pNode( nullptr )
{}
- /// Releases the pointer
- ~exempt_ptr()
- {
- release();
- }
-
- /// Checks if the pointer is \p NULL
- bool empty() const CDS_NOEXCEPT
- {
- return m_pNode == null_ptr<node_type *>();
- }
-
- /// Dereference operator
- value_type * operator->() const CDS_NOEXCEPT
- {
- return !empty() ? node_to_value_cast()( m_pNode ) : null_ptr<value_type *>();
- }
-
- /// Returns a reference to the value
- value_type& operator *() CDS_NOEXCEPT
- {
- assert( !empty());
- return *node_to_value_cast()( m_pNode );
- }
-
//@cond
- /// Assignment operator, the object should be empty. For internal use only
- exempt_ptr& operator =( node_type * pNode )
- {
- // release() cannot be called in this point since RCU should be locked
- assert( empty() );
- assert( rcu::is_locked() );
- m_pNode = pNode;
- return *this;
- }
+ /// Creates exempt pointer for \p pNode. Only for internal use.
+ explicit exempt_ptr( node_type * pNode ) CDS_NOEXCEPT
+ : m_pNode( pNode )
+ {}
+ explicit exempt_ptr( std::nullptr_t ) CDS_NOEXCEPT
+ : m_pNode( nullptr )
+ {}
//@endcond
- /// Disposes the pointer. Should be called only outside of RCU critical section
- void release()
+ /// Move ctor
+ exempt_ptr( exempt_ptr&& p ) CDS_NOEXCEPT
+ : m_pNode( p.m_pNode )
{
- assert( !rcu::is_locked() );
- if ( !empty() ) {
- rcu::template retire_ptr<disposer>( m_pNode );
- m_pNode = null_ptr<node_type *>();
- }
+ p.m_pNode = nullptr;
}
- };
- //@cond
- // Intrusive container specialization
- template <
- class RCU,
- typename NodeType,
- typename Disposer
- >
- class exempt_ptr< RCU, NodeType, NodeType, Disposer, void >
- {
- //TODO: use move semantics and explicit operator bool!
- public:
- typedef RCU rcu ; ///< RCU type - one of <tt>cds::urcu::gc< ... ></tt>
- typedef NodeType node_type ; ///< Node type
- typedef NodeType value_type ; ///< Node type
- typedef Disposer disposer ; ///< Disposer calling when release
- typedef void node_to_value_cast; ///< No casting is needed
-
- private:
- node_type * m_pNode;
-
- private:
- // No copy-constructible
- exempt_ptr( exempt_ptr const& );
- exempt_ptr& operator=( exempt_ptr const& );
-
- public:
- /// Constructs empty pointer
- exempt_ptr() CDS_NOEXCEPT
- : m_pNode( null_ptr<node_type *>())
- {}
+ /// The exempt pointer is not copy-constructible
+ exempt_ptr( exempt_ptr const& ) = delete;
/// Releases the pointer
~exempt_ptr()
release();
}
- /// Checks if the pointer is \p NULL
+ /// Checks if the pointer is \p nullptr
bool empty() const CDS_NOEXCEPT
{
- return m_pNode == null_ptr<node_type *>();
+ return m_pNode == nullptr;
}
- /// Dereference operator.
+ /// \p bool operator returns <tt>!empty()</tt>
+ explicit operator bool() const CDS_NOEXCEPT
+ {
+ return !empty();
+ }
+
+ /// Dereference operator
value_type * operator->() const CDS_NOEXCEPT
{
- return !empty() ? m_pNode : null_ptr<value_type *>();
+ return !empty() ? node_to_value_cast()(m_pNode) : nullptr;
}
/// Returns a reference to the value
value_type& operator *() CDS_NOEXCEPT
{
assert( !empty());
- return *m_pNode;
+ return *node_to_value_cast()( m_pNode );
}
- /// Assignment operator, the object should be empty. For internal use only
- exempt_ptr& operator =( node_type * pNode )
+ /// Move assignment. Can be called only outside of RCU critical section
+ exempt_ptr& operator =( exempt_ptr&& p ) CDS_NOEXCEPT
{
- // release() cannot be called in this point since RCU should be locked
- assert( empty() );
- assert( rcu::is_locked() );
- m_pNode = pNode;
+ release();
+ m_pNode = p.m_pNode;
+ p.m_pNode = nullptr;
return *this;
}
+ /// The exempt pointer is not copy-assignable
+ exempt_ptr& operator=(exempt_ptr const&) = delete;
+
/// Disposes the pointer. Should be called only outside of RCU critical section
void release()
{
- assert( !rcu::is_locked() );
if ( !empty() ) {
+ assert( !rcu::is_locked() );
rcu::template retire_ptr<disposer>( m_pNode );
- m_pNode = null_ptr<node_type *>();
+ m_pNode = nullptr;
}
}
};
- //@endcond
-
}} // namespace cds::urcu
#endif //#ifndef __CDS_URCU_EXEMPT_PTR_H