3 #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_HASH_MAP_MSVC_ADAPTER_H
4 #define __CDS_CONTAINER_STRIPED_MAP_STD_HASH_MAP_MSVC_ADAPTER_H
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
10 #include <cds/container/striped_set/adapter.h>
14 namespace cds { namespace container {
15 namespace striped_set {
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 > >
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;
25 void operator()( map_type& map, iterator itWhat )
27 std::pair< typename map_type::iterator, bool> res = map.insert( *itWhat );
28 assert( res.second ) ; // succesful insert
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 > >
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;
40 void operator()( map_type& map, iterator itWhat )
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 );
49 // Move policy for map
50 template <typename Key, typename T, typename Traits, typename Alloc>
51 struct move_item_policy< stdext::hash_map< Key, T, Traits, Alloc > >
53 typedef stdext::hash_map< Key, T, Traits, Alloc > map_type;
54 typedef typename map_type::value_type pair_type;
55 typedef typename map_type::iterator iterator;
57 void operator()( map_type& map, iterator itWhat )
59 map.insert( std::move( *itWhat ) );
62 } // namespace striped_set
63 }} // namespace cds::container
65 namespace cds { namespace intrusive { namespace striped_set {
67 /// stdext::hash_map adapter for hash map bucket
68 template <typename Key, typename T, class Traits, class Alloc, CDS_SPEC_OPTIONS>
69 class adapt< stdext::hash_map< Key, T, Traits, Alloc>, CDS_OPTIONS >
72 typedef stdext::hash_map< Key, T, Traits, Alloc> container_type ; ///< underlying container type
75 /// Adapted container type
76 class adapted_container: public cds::container::striped_set::adapted_container
79 typedef typename container_type::value_type value_type ; ///< value type stored in the container
80 typedef typename container_type::key_type key_type;
81 typedef typename container_type::mapped_type mapped_type;
82 typedef typename container_type::iterator iterator ; ///< container iterator
83 typedef typename container_type::const_iterator const_iterator ; ///< container const iterator
85 static bool const has_find_with = false;
86 static bool const has_erase_with = false;
90 typedef typename cds::opt::select<
91 typename cds::opt::value<
92 typename cds::opt::find_option<
93 cds::opt::copy_policy< cds::container::striped_set::move_item >
97 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
98 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type>
99 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
105 container_type m_Map;
109 template <typename Q, typename Func>
110 bool insert( const Q& key, Func f )
112 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ) );
114 ::cds::unref(f)( *res.first );
118 template <typename Q, typename Func>
119 std::pair<bool, bool> ensure( const Q& val, Func func )
121 std::pair<iterator, bool> res = m_Map.insert( value_type( val, mapped_type() ));
122 ::cds::unref(func)( res.second, *res.first );
123 return std::make_pair( true, res.second );
126 template <typename Q, typename Func>
127 bool erase( const Q& key, Func f )
129 iterator it = m_Map.find( key_type(key) );
130 if ( it == m_Map.end() )
132 ::cds::unref(f)( *it );
137 template <typename Q, typename Func>
138 bool find( Q& val, Func f )
140 iterator it = m_Map.find( key_type(val) );
141 if ( it == m_Map.end() )
143 ::cds::unref(f)( *it, val );
152 iterator begin() { return m_Map.begin(); }
153 const_iterator begin() const { return m_Map.begin(); }
154 iterator end() { return m_Map.end(); }
155 const_iterator end() const { return m_Map.end(); }
157 void move_item( adapted_container& /*from*/, iterator itWhat )
159 assert( m_Map.find( itWhat->first ) == m_Map.end() );
160 copy_item()( m_Map, itWhat );
170 typedef adapted_container type ; ///< Result of \p adapt metafunction
173 }}} // namespace cds::intrusive::striped_set
178 #endif // #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_HASH_MAP_MSVC_ADAPTER_H