20477c3d870c12b8c9f2666d7d14b1672489ed81
[libcds.git] / cds / container / details / make_lazy_list.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_CONTAINER_DETAILS_MAKE_LAZY_LIST_H
4 #define CDSLIB_CONTAINER_DETAILS_MAKE_LAZY_LIST_H
5
6 #include <cds/details/binary_functor_wrapper.h>
7
8 namespace cds { namespace container {
9
10     //@cond
11     namespace details {
12
13         template <class GC, typename T, class Traits>
14         struct make_lazy_list
15         {
16             typedef GC      gc;
17             typedef T       value_type;
18             typedef Traits original_type_traits;
19
20             struct node_type : public intrusive::lazy_list::node<gc, typename original_type_traits::lock_type>
21             {
22                 value_type  m_Value;
23
24                 node_type()
25                 {}
26
27                 template <typename Q>
28                 node_type( Q const& v )
29                     : m_Value(v)
30                 {}
31                 template <typename... Args>
32                 node_type( Args&&... args )
33                     : m_Value( std::forward<Args>(args)...)
34                 {}
35             };
36
37             typedef typename original_type_traits::allocator::template rebind<node_type>::other  allocator_type;
38             typedef cds::details::Allocator< node_type, allocator_type >                cxx_allocator;
39
40             struct node_deallocator
41             {
42                 void operator ()( node_type * pNode )
43                 {
44                     cxx_allocator().Delete( pNode );
45                 }
46             };
47
48             typedef typename std::conditional< original_type_traits::sort,
49                 typename opt::details::make_comparator< value_type, original_type_traits >::type,
50                 typename opt::details::make_equal_to< value_type, original_type_traits >::type
51             >::type key_comparator;
52
53             struct value_accessor {
54                 value_type const & operator()( node_type const & node ) const
55                 {
56                     return node.m_Value;
57                 }
58             };
59
60             template <typename Less>
61             struct less_wrapper {
62                 typedef cds::details::compare_wrapper< node_type, cds::opt::details::make_comparator_from_less<Less>, value_accessor > type;
63             };
64
65             template <typename Equal>
66             struct equal_to_wrapper {
67                 typedef cds::details::predicate_wrapper< node_type, Equal, value_accessor > type;
68             };
69
70             struct intrusive_traits: public original_type_traits
71             {
72                 typedef intrusive::lazy_list::base_hook< opt::gc<gc> >  hook;
73                 typedef node_deallocator               disposer;
74                 static CDS_CONSTEXPR const opt::link_check_type link_checker = cds::intrusive::lazy_list::traits::link_checker;
75
76                 typedef typename std::conditional< std::is_same< typename original_type_traits::equal_to, cds::opt::none >::value,
77                     cds::opt::none,
78                     typename equal_to_wrapper< typename original_type_traits::equal_to >::type
79                 >::type equal_to;
80
81                 typedef typename std::conditional<
82                     original_type_traits::sort
83                        || !std::is_same<typename original_type_traits::compare, cds::opt::none>::value
84                        || !std::is_same<typename original_type_traits::less, cds::opt::none>::value,
85                     cds::details::compare_wrapper<
86                         node_type,
87                         typename opt::details::make_comparator< value_type, original_type_traits >::type,
88                         value_accessor
89                     >,
90                     cds::opt::none
91                 >::type compare;
92             };
93
94             typedef intrusive::LazyList<gc, node_type, intrusive_traits>  type;
95         };
96     }   // namespace details
97     //@endcond
98
99 }}  // namespace cds::container
100
101 #endif  // #ifndef CDSLIB_CONTAINER_DETAILS_MAKE_MICHAEL_LIST_H