Added copyright and license
[libcds.git] / cds / container / details / make_lazy_list.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-2016
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_CONTAINER_DETAILS_MAKE_LAZY_LIST_H
32 #define CDSLIB_CONTAINER_DETAILS_MAKE_LAZY_LIST_H
33
34 #include <cds/details/binary_functor_wrapper.h>
35
36 namespace cds { namespace container {
37
38     //@cond
39     namespace details {
40
41         template <class GC, typename T, class Traits>
42         struct make_lazy_list
43         {
44             typedef GC      gc;
45             typedef T       value_type;
46             typedef Traits original_type_traits;
47
48             struct node_type : public intrusive::lazy_list::node<gc, typename original_type_traits::lock_type>
49             {
50                 value_type  m_Value;
51
52                 node_type()
53                 {}
54
55                 template <typename Q>
56                 node_type( Q const& v )
57                     : m_Value(v)
58                 {}
59                 template <typename... Args>
60                 node_type( Args&&... args )
61                     : m_Value( std::forward<Args>(args)...)
62                 {}
63             };
64
65             typedef typename original_type_traits::allocator::template rebind<node_type>::other  allocator_type;
66             typedef cds::details::Allocator< node_type, allocator_type >                cxx_allocator;
67
68             struct node_deallocator
69             {
70                 void operator ()( node_type * pNode )
71                 {
72                     cxx_allocator().Delete( pNode );
73                 }
74             };
75
76             typedef typename std::conditional< original_type_traits::sort,
77                 typename opt::details::make_comparator< value_type, original_type_traits >::type,
78                 typename opt::details::make_equal_to< value_type, original_type_traits >::type
79             >::type key_comparator;
80
81             struct value_accessor {
82                 value_type const & operator()( node_type const & node ) const
83                 {
84                     return node.m_Value;
85                 }
86             };
87
88             template <typename Less>
89             struct less_wrapper {
90                 typedef cds::details::compare_wrapper< node_type, cds::opt::details::make_comparator_from_less<Less>, value_accessor > type;
91             };
92
93             template <typename Equal>
94             struct equal_to_wrapper {
95                 typedef cds::details::predicate_wrapper< node_type, Equal, value_accessor > type;
96             };
97
98             struct intrusive_traits: public original_type_traits
99             {
100                 typedef intrusive::lazy_list::base_hook< opt::gc<gc> >  hook;
101                 typedef node_deallocator               disposer;
102                 static CDS_CONSTEXPR const opt::link_check_type link_checker = cds::intrusive::lazy_list::traits::link_checker;
103
104                 typedef typename std::conditional< std::is_same< typename original_type_traits::equal_to, cds::opt::none >::value,
105                     cds::opt::none,
106                     typename equal_to_wrapper< typename original_type_traits::equal_to >::type
107                 >::type equal_to;
108
109                 typedef typename std::conditional<
110                     original_type_traits::sort
111                        || !std::is_same<typename original_type_traits::compare, cds::opt::none>::value
112                        || !std::is_same<typename original_type_traits::less, cds::opt::none>::value,
113                     cds::details::compare_wrapper<
114                         node_type,
115                         typename opt::details::make_comparator< value_type, original_type_traits >::type,
116                         value_accessor
117                     >,
118                     cds::opt::none
119                 >::type compare;
120             };
121
122             typedef intrusive::LazyList<gc, node_type, intrusive_traits>  type;
123         };
124     }   // namespace details
125     //@endcond
126
127 }}  // namespace cds::container
128
129 #endif  // #ifndef CDSLIB_CONTAINER_DETAILS_MAKE_MICHAEL_LIST_H