issue#11: cds: changed __CDS_ guard prefix to CDSLIB_ for all .h files
[libcds.git] / cds / opt / value_cleaner.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_OPT_ITEM_DISPOSER_H
4 #define CDSLIB_OPT_ITEM_DISPOSER_H
5
6 #include <cds/details/defs.h>
7
8 namespace cds { namespace opt {
9
10     /// [type-option] value cleaning
11     /**
12         The cleaner is a functor called when an item is removed from a container.
13         Note, the cleaner should not delete (deallocate) the value \p val passed in.
14         However, if the \p value_type type is a structure that contains dynamically allocated
15         field(s), the cleaning functor may deallocate it and initialize to default value (usually, \p nullptr).
16
17         The interface for type \p value_type is:
18         \code
19         struct myCleaner {
20             void operator ()( value_type& val )
21             {
22                 ...
23                 // code to cleanup \p val
24             }
25         }
26         \endcode
27
28         Predefined option types:
29             \li opt::v::empty_cleaner
30     */
31     template <typename Type>
32     struct value_cleaner {
33         //@cond
34         template <typename BASE> struct pack: public BASE
35         {
36             typedef Type value_cleaner;
37         };
38         //@endcond
39     };
40
41     namespace v {
42
43         /// Empty cleaner
44         /**
45             One of available type for opt::value_cleaner option.
46             This cleaner is empty, i.e. it does not do any cleaning.
47         */
48         struct empty_cleaner
49         {
50             //@cond
51             template <typename T>
52             void operator()( T& /*val*/ )
53             {}
54             //@endcond
55         };
56
57         /// Cleaner that calls destructor of type \p T
58         /**
59             One of available type for opt::value_cleaner option.
60         */
61         struct destruct_cleaner
62         {
63             //@cond
64             template <typename T>
65             void operator()( T& val )
66             {
67                 (&val)->T::~T();
68             }
69             //@endcond
70         };
71
72     }   // namespace v
73 }}  // namespace cds::opt
74
75 #endif // #ifndef CDSLIB_OPT_ITEM_DISPOSER_H