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