b2a7112ed5c25a639c19bbd37977a7283408ef6b
[libcds.git] / cds / opt / value_cleaner.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef CDSLIB_OPT_ITEM_DISPOSER_H
32 #define CDSLIB_OPT_ITEM_DISPOSER_H
33
34 #include <cds/details/defs.h>
35
36 namespace cds { namespace opt {
37
38     /// [type-option] value cleaning
39     /**
40         The cleaner is a functor called when an item is removed from a container.
41         Note, the cleaner should not delete (deallocate) the value \p val passed in.
42         However, if the \p value_type type is a structure that contains dynamically allocated
43         field(s), the cleaning functor may deallocate it and initialize to default value (usually, \p nullptr).
44
45         The interface for type \p value_type is:
46         \code
47         struct myCleaner {
48             void operator ()( value_type& val )
49             {
50                 ...
51                 // code to cleanup \p val
52             }
53         }
54         \endcode
55
56         Predefined option types:
57             \li opt::v::empty_cleaner
58     */
59     template <typename Type>
60     struct value_cleaner {
61         //@cond
62         template <typename BASE> struct pack: public BASE
63         {
64             typedef Type value_cleaner;
65         };
66         //@endcond
67     };
68
69     namespace v {
70
71         /// Empty cleaner
72         /**
73             One of available type for \p opt::value_cleaner option.
74             This cleaner is empty, i.e. it does not do any cleaning.
75         */
76         struct empty_cleaner
77         {
78             //@cond
79             template <typename T>
80             void operator()( T& /*val*/ )
81             {}
82             //@endcond
83         };
84
85         /// Cleaner that calls destructor of type \p T
86         /**
87             One of available type for \p opt::value_cleaner option.
88         */
89         struct destruct_cleaner
90         {
91             //@cond
92             template <typename T>
93             void operator()( T& val )
94             {
95                 (&val)->T::~T();
96             }
97             //@endcond
98         };
99
100     }   // namespace v
101 }}  // namespace cds::opt
102
103 #endif // #ifndef CDSLIB_OPT_ITEM_DISPOSER_H