870ab5f06cd78b538744436403be68865e3f31f5
[libcds.git] / cds / container / details / make_lazy_kvlist.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_CONTAINER_DETAILS_MAKE_LAZY_KVLIST_H
4 #define __CDS_CONTAINER_DETAILS_MAKE_LAZY_KVLIST_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 K, typename T, class Traits>
14         struct make_lazy_kvlist
15         {
16             typedef Traits original_type_traits;
17
18             typedef GC      gc;
19             typedef K       key_type;
20             typedef T       value_type;
21             typedef std::pair<key_type const, value_type>       pair_type;
22
23             struct node_type: public intrusive::lazy_list::node<gc, typename original_type_traits::lock_type>
24             {
25                 pair_type   m_Data;
26
27                 template <typename Q>
28                 node_type( Q const& key )
29                     : m_Data( key, value_type() )
30                 {}
31
32                 template <typename Q, typename R>
33                 explicit node_type( std::pair<Q, R> const& pair )
34                     : m_Data( pair )
35                 {}
36
37                 template <typename Q, typename R>
38                 node_type( Q const& key, R const& value )
39                     : m_Data( key, value )
40                 {}
41
42                 template <typename Ky, typename... Args>
43                 node_type( Ky&& key, Args&&... args )
44                     : m_Data( std::forward<Ky>(key), std::move( value_type( std::forward<Args>(args)...)))
45                 {}
46             };
47
48             typedef typename original_type_traits::allocator::template rebind<node_type>::other  allocator_type;
49             typedef cds::details::Allocator< node_type, allocator_type >                cxx_allocator;
50
51             struct node_deallocator
52             {
53                 void operator ()( node_type * pNode )
54                 {
55                     cxx_allocator().Delete( pNode );
56                 }
57             };
58
59             struct key_field_accessor {
60                 key_type const& operator()( node_type const& pair )
61                 {
62                     return pair.m_Data.first;
63                 }
64             };
65
66             typedef typename opt::details::make_comparator< key_type, original_type_traits >::type key_comparator;
67
68             template <typename Less>
69             struct less_wrapper {
70                 typedef cds::details::compare_wrapper< node_type, cds::opt::details::make_comparator_from_less<Less>, key_field_accessor >    type;
71             };
72
73             struct type_traits: public original_type_traits
74             {
75                 typedef intrusive::lazy_list::base_hook< opt::gc<gc> >  hook;
76                 typedef node_deallocator                    disposer;
77                 typedef cds::details::compare_wrapper< node_type, key_comparator, key_field_accessor > compare;
78             };
79
80             typedef intrusive::LazyList<gc, node_type, type_traits>  type;
81         };
82     }   // namespace details
83     //@endcond
84
85 }}  // namespace cds::container
86
87 #endif  // #ifndef __CDS_CONTAINER_DETAILS_MAKE_LAZY_KVLIST_H