Added container::IterableList<HP>
[libcds.git] / cds / container / details / make_iterable_kvlist.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_ITERABLE_KVLIST_H
32 #define CDSLIB_CONTAINER_DETAILS_MAKE_ITERABLE_KVLIST_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 K, typename T, class Traits>
42         struct make_iterable_kvlist
43         {
44             typedef Traits original_type_traits;
45
46             typedef GC       gc;
47             typedef K        key_type;
48             typedef T        value_type;
49             typedef std::pair<key_type const, value_type> pair_type;
50
51             typedef intrusive::iterable_list::node< pair_type > node_type;
52
53             typedef typename original_type_traits::allocator::template rebind<pair_type>::other data_allocator_type;
54             typedef cds::details::Allocator< pair_type, allocator_type > cxx_data_allocator;
55
56             typedef typename original_type_traits::memory_model memory_model;
57
58             struct node_disposer
59             {
60                 void operator ()( node_type * pNode )
61                 {
62                     cxx_data_allocator().Delete( pNode->data.load( memory_model::memory_order_relaxed ) );
63                 }
64             };
65
66             struct key_field_accessor {
67                 key_type const& operator()( node_type const& node )
68                 {
69                     pair_type const* p = node.data.load( memory_model::memory_order_relaxed );
70                     assert( p != nullptr )
71                     return p->first;
72                 }
73             };
74
75             typedef typename opt::details::make_comparator< key_type, original_type_traits >::type key_comparator;
76
77             template <typename Less>
78             struct less_wrapper {
79                 typedef cds::details::compare_wrapper< node_type, cds::opt::details::make_comparator_from_less<Less>, key_field_accessor > type;
80             };
81
82             struct intrusive_traits: public original_type_traits
83             {
84                 typedef node_disposer disposer;
85                 typedef cds::details::compare_wrapper< node_type, key_comparator, key_field_accessor > compare;
86                 static const opt::link_check_type link_checker = intrusive::iterable_list::traits::link_checker;
87             };
88
89             typedef intrusive::IterableList<gc, node_type, intrusive_traits> type;
90         };
91     }   // namespace details
92     //@endcond
93
94 }}  // namespace cds::container
95
96 #endif  // #ifndef CDSLIB_CONTAINER_DETAILS_MAKE_ITERABLE_KVLIST_H