Replace NULL with nullptr
[libcds.git] / cds / gc / details / retired_ptr.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_GC_DETAILS_RETIRED_PTR_H
4 #define __CDS_GC_DETAILS_RETIRED_PTR_H
5
6 #include <cds/details/defs.h>
7
8 namespace cds { namespace gc {
9     /// Common implementation details for any GC
10     namespace details {
11
12         /// Pointer to function to free (destruct and deallocate) retired pointer of specific type
13         typedef void (* free_retired_ptr_func )( void * );
14
15         /// Retired pointer
16         /**
17             Pointer to an object that is ready to delete.
18         */
19         struct retired_ptr {
20             /// Pointer type
21             typedef void *          pointer;
22
23             pointer                 m_p    ;        ///< retired pointer
24             free_retired_ptr_func   m_funcFree    ; ///< pointer to the destructor function
25
26             /// Comparison of two retired pointers
27             static bool less( const retired_ptr& p1, const retired_ptr& p2 )
28             {
29                 return p1.m_p < p2.m_p;
30             }
31
32             /// Default ctor initializes pointer to \p nullptr
33             retired_ptr()
34                 : m_p( nullptr )
35                 , m_funcFree( nullptr )
36             {}
37
38             /// Ctor
39             retired_ptr( pointer p, free_retired_ptr_func func )
40                 : m_p( p ),
41                 m_funcFree( func )
42             {}
43
44             /// Typecasting ctor
45             template <typename T>
46             retired_ptr( T * p, void (* pFreeFunc)(T *))
47                 : m_p( reinterpret_cast<pointer>( p ) )
48                 , m_funcFree( reinterpret_cast< free_retired_ptr_func >( pFreeFunc ))
49             {}
50
51             /// Assignment operator
52             retired_ptr& operator =( const retired_ptr& s)
53             {
54                 m_p = s.m_p;
55                 m_funcFree = s.m_funcFree;
56                 return *this;
57             }
58
59             /// Invokes destructor function for the pointer
60             void free()
61             {
62                 assert( m_funcFree );
63                 assert( m_p );
64                 m_funcFree( m_p );
65
66                 CDS_STRICT_DO( m_p = nullptr );
67                 CDS_STRICT_DO( m_funcFree = nullptr );
68             }
69         };
70
71         //@cond
72         static inline bool operator <( const retired_ptr& p1, const retired_ptr& p2 )
73         {
74             return retired_ptr::less( p1, p2 );
75         }
76
77         static inline bool operator ==( const retired_ptr& p1, const retired_ptr& p2 )
78         {
79             return p1.m_p == p2.m_p;
80         }
81
82         static inline bool operator !=( const retired_ptr& p1, const retired_ptr& p2 )
83         {
84             return !(p1 == p2);
85         }
86         //@endcond
87
88
89     }  // namespace details
90 }}   // namespace cds::gc
91
92 #endif // #ifndef __CDS_GC_DETAILS_RETIRED_PTR_H