Uses different pass count for different parallel queue test cases
[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 val
52             }
53         }
54         \endcode
55
56         Predefined option types:
57             \li opt::v::empty_cleaner
58             \li opt::v::destruct_cleaner
59             \li opt::v::auto_cleaner
60     */
61     template <typename Type>
62     struct value_cleaner {
63         //@cond
64         template <typename BASE> struct pack: public BASE
65         {
66             typedef Type value_cleaner;
67         };
68         //@endcond
69     };
70
71     namespace v {
72
73         /// Empty cleaner
74         /**
75             One of available type for \p opt::value_cleaner option.
76             This cleaner is empty, i.e. it does not do any cleaning.
77         */
78         struct empty_cleaner
79         {
80             //@cond
81             template <typename T>
82             void operator()( T& /*val*/ )
83             {}
84             //@endcond
85         };
86
87         /// Cleaner that calls destructor of type \p T
88         /**
89             One of available type for \p opt::value_cleaner option.
90         */
91         struct destruct_cleaner
92         {
93             //@cond
94             template <typename T>
95             void operator()( T& val )
96             {
97                 ( &val )->~T();
98             }
99             //@endcond
100         };
101
102         /// Cleaner that calls \p T destructor if it is not trivial
103         /**
104             If \p T has non-trivial destructor (<tt> std::is_trivially_destructible<T>::value </tt> is \p false),
105             the cleaner calls \p T destructor.
106
107             If <tt> std::is_trivially_destructible<T>::value </tt> is \p true, the cleaner is empty - no
108             destructor of \p T is called.
109         */
110         struct auto_cleaner
111         {
112             //@cond
113             template <typename T>
114             typename std::enable_if< std::is_trivially_destructible<T>::value >::type
115             operator()( T& /*val*/ )
116             {}
117
118             template <typename T>
119             typename std::enable_if< !std::is_trivially_destructible<T>::value >::type
120             operator()( T& val )
121             {
122                 ( &val )->~T();
123             }
124             //@endcond
125         };
126
127     }   // namespace v
128 }}  // namespace cds::opt
129
130 #endif // #ifndef CDSLIB_OPT_ITEM_DISPOSER_H