3 #ifndef __CDSUNIT_STD_HASH_MAP_VC_H
4 #define __CDSUNIT_STD_HASH_MAP_VC_H
7 #include <functional> // ref
8 #include <mutex> //unique_lock
11 template <typename Key, typename Value, typename Lock, class Alloc = CDS_DEFAULT_ALLOCATOR>
12 class StdHashMap: public stdext::hash_map<Key, Value, stdext::hash_compare<Key, std::less<Key> >, Alloc>
16 typedef std::unique_lock<Lock> AutoLock;
17 typedef stdext::hash_map<Key, Value, stdext::hash_compare<Key, std::less<Key> >, Alloc> base_class;
19 typedef typename base_class::mapped_type value_type;
20 typedef typename base_class::value_type pair_type;
21 typedef size_t item_counter;
23 StdHashMap( size_t nMapSize, size_t nLoadFactor )
26 bool find( const Key& key )
28 AutoLock al( m_lock );
29 return base_class::find( key ) != base_class::end();
32 bool insert( const Key& key, const Value& val )
34 AutoLock al( m_lock );
35 return base_class::insert( base_class::value_type(key, val)).second;
38 template <typename T, typename Func>
39 bool insert( const Key& key, const T& val, Func func )
41 AutoLock al( m_lock );
42 std::pair<base_class::iterator, bool> pRet = base_class::insert( base_class::value_type(key, Value() ));
44 func( pRet.first->second, val );
50 template <typename T, typename Func>
51 std::pair<bool, bool> ensure( const T& key, Func func )
53 AutoLock al( m_lock );
54 std::pair<base_class::iterator, bool> pRet = base_class::insert( base_class::value_type(key, Value() ));
56 func( true, *pRet.first );
57 return std::make_pair( true, true );
60 func( false, *pRet.first );
61 return std::make_pair( true, false );
65 bool erase( const Key& key )
67 AutoLock al( m_lock );
68 return base_class::erase( key ) != 0;
71 template <typename T, typename Func>
72 bool erase( const T& key, Func func )
74 AutoLock al( m_lock );
75 base_class::iterator it = base_class::find( key );
76 if ( it != base_class::end() ) {
78 return base_class::erase( key ) != 0;
84 std::ostream& dump( std::ostream& stm ) { return stm; }
88 #endif // #ifndef __CDSUNIT_STD_HASH_MAP_VC_H