Remove CDS_RVALUE_SUPPORT, CDS_MOVE_SEMANTICS_SUPPORT macros and emulating code
[libcds.git] / cds / container / striped_map / std_map.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_MAP_ADAPTER_H
4 #define __CDS_CONTAINER_STRIPED_MAP_STD_MAP_ADAPTER_H
5
6 #include <cds/container/striped_set/adapter.h>
7 #include <map>
8
9 //@cond
10 namespace cds { namespace container {
11     namespace striped_set {
12
13         // Copy policy for map
14         template <typename Key, typename T, typename Traits, typename Alloc>
15         struct copy_item_policy< std::map< Key, T, Traits, Alloc > >
16         {
17             typedef std::map< Key, T, Traits, Alloc > map_type;
18             typedef typename map_type::value_type pair_type;
19             typedef typename map_type::iterator    iterator;
20
21             void operator()( map_type& map, iterator itWhat )
22             {
23                 map.insert( *itWhat );
24             }
25         };
26
27         // Swap item policy
28         template <typename Key, typename T, typename Traits, typename Alloc>
29         struct swap_item_policy< std::map< Key, T, Traits, Alloc > >
30         {
31             typedef std::map< Key, T, Traits, Alloc > map_type;
32             typedef typename map_type::value_type pair_type;
33             typedef typename map_type::iterator    iterator;
34
35             void operator()( map_type& map, iterator itWhat )
36             {
37                 std::pair< typename map_type::iterator, bool > ret = map.insert( pair_type( itWhat->first, typename pair_type::second_type() ));
38                 assert( ret.second )    ;   // successful insertion
39                 std::swap( ret.first->second, itWhat->second );
40             }
41         };
42
43         // Move policy for map
44         template <typename Key, typename T, typename Traits, typename Alloc>
45         struct move_item_policy< std::map< Key, T, Traits, Alloc > >
46         {
47             typedef std::map< Key, T, Traits, Alloc > map_type;
48             typedef typename map_type::value_type pair_type;
49             typedef typename map_type::iterator    iterator;
50
51             void operator()( map_type& map, iterator itWhat  )
52             {
53                 map.insert( std::move( *itWhat ) );
54             }
55         };
56     }   // namespace striped_set
57 }} // namespace cds::container
58
59 namespace cds { namespace intrusive { namespace striped_set {
60
61     /// std::set adapter for hash set bucket
62     template <typename Key, typename T, class Traits, class Alloc, CDS_SPEC_OPTIONS>
63     class adapt< std::map< Key, T, Traits, Alloc>, CDS_OPTIONS >
64     {
65     public:
66         typedef std::map< Key, T, Traits, Alloc>     container_type          ;   ///< underlying container type
67
68     private:
69         /// Adapted container type
70         class adapted_container: public cds::container::striped_set::adapted_container
71         {
72         public:
73             typedef typename container_type::value_type     value_type  ;   ///< value type stored in the container
74             typedef typename container_type::key_type       key_type;
75             typedef typename container_type::mapped_type    mapped_type;
76             typedef typename container_type::iterator       iterator ;   ///< container iterator
77             typedef typename container_type::const_iterator const_iterator ;    ///< container const iterator
78
79             static bool const has_find_with = false;
80             static bool const has_erase_with = false;
81
82         private:
83             //@cond
84             typedef typename cds::opt::select<
85                 typename cds::opt::value<
86                     typename cds::opt::find_option<
87                         cds::opt::copy_policy< cds::container::striped_set::move_item >
88                         , CDS_OPTIONS
89                     >::type
90                 >::copy_policy
91                 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
92                 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type>
93                 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
94             >::type copy_item;
95             //@endcond
96
97         private:
98             //@cond
99             container_type  m_Map;
100             //@endcond
101
102         public:
103
104             template <typename Q, typename Func>
105             bool insert( const Q& key, Func f )
106             {
107                 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ) );
108                 if ( res.second )
109                     ::cds::unref(f)( *res.first );
110                 return res.second;
111             }
112
113 #       ifdef CDS_EMPLACE_SUPPORT
114             template <typename Q, typename... Args>
115             bool emplace( Q&& key, Args&&... args )
116             {
117 #           if CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION < 40800 || CDS_COMPILER == CDS_COMPILER_CLANG && !defined(__LIBCPP_VERSION)
118                 // GCC < 4.8: std::map has no "emplace" member function. Emulate it
119                 std::pair<iterator, bool> res = m_Map.insert( value_type( std::forward<Q>(key), mapped_type( std::forward<Args>(args)...)));
120 #           else
121                 std::pair<iterator, bool> res = m_Map.emplace( std::forward<Q>(key), std::move(mapped_type( std::forward<Args>(args)...)));
122 #           endif
123                 return res.second;
124             }
125 #       endif
126
127             template <typename Q, typename Func>
128             std::pair<bool, bool> ensure( const Q& key, Func func )
129             {
130                 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ));
131                 cds::unref(func)( res.second, *res.first );
132                 return std::make_pair( true, res.second );
133             }
134
135             template <typename Q, typename Func>
136             bool erase( const Q& key, Func f )
137             {
138                 iterator it = m_Map.find( key_type(key) );
139                 if ( it == m_Map.end() )
140                     return false;
141                 cds::unref(f)( *it );
142                 m_Map.erase( it );
143                 return true;
144             }
145
146             template <typename Q, typename Func>
147             bool find( Q& val, Func f )
148             {
149                 iterator it = m_Map.find( key_type(val) );
150                 if ( it == m_Map.end() )
151                     return false;
152                 cds::unref(f)( *it, val );
153                 return true;
154             }
155
156             /// Clears the container
157             void clear()
158             {
159                 m_Map.clear();
160             }
161
162             iterator begin()                { return m_Map.begin(); }
163             const_iterator begin() const    { return m_Map.begin(); }
164             iterator end()                  { return m_Map.end(); }
165             const_iterator end() const      { return m_Map.end(); }
166
167             void move_item( adapted_container& /*from*/, iterator itWhat )
168             {
169                 assert( m_Map.find( itWhat->first ) == m_Map.end() );
170                 copy_item()( m_Map, itWhat );
171             }
172
173             size_t size() const
174             {
175                 return m_Map.size();
176             }
177         };
178
179     public:
180         typedef adapted_container type ; ///< Result of \p adapt metafunction
181     };
182 }}} // namespace cds::intrusive::striped_set
183
184 //@endcond
185
186 #endif // #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_MAP_ADAPTER_H