5e9c8c8b9cff4fcd25f9adf4fad406e906b7914d
[libcds.git] / tests / unit / map2 / std_hash_map_vc.h
1 //$$CDS-header$$
2
3 #ifndef __CDSUNIT_STD_HASH_MAP_VC_H
4 #define __CDSUNIT_STD_HASH_MAP_VC_H
5
6 #include <hash_map>
7 #include <functional>   // ref
8
9 namespace map2 {
10     template <typename Key, typename Value, typename Lock, class Alloc = CDS_DEFAULT_ALLOCATOR>
11     class StdHashMap: public stdext::hash_map<Key, Value, stdext::hash_compare<Key, std::less<Key> >, Alloc>
12     {
13     public:
14         Lock m_lock;
15         typedef cds::lock::scoped_lock<Lock> AutoLock;
16         typedef stdext::hash_map<Key, Value, stdext::hash_compare<Key, std::less<Key> >, Alloc>   base_class;
17     public:
18         typedef typename base_class::mapped_type value_type;
19         typedef typename base_class::value_type  pair_type;
20         typedef size_t      item_counter;
21
22         StdHashMap( size_t nMapSize, size_t nLoadFactor )
23         {}
24
25         bool find( const Key& key )
26         {
27             AutoLock al( m_lock );
28             return base_class::find( key ) != base_class::end();
29         }
30
31         bool insert( const Key& key, const Value& val )
32         {
33             AutoLock al( m_lock );
34             return base_class::insert( base_class::value_type(key, val)).second;
35         }
36
37         template <typename T, typename Func>
38         bool insert( const Key& key, const T& val, Func func )
39         {
40             AutoLock al( m_lock );
41             std::pair<base_class::iterator, bool> pRet = base_class::insert( base_class::value_type(key, Value() ));
42             if ( pRet.second ) {
43                 func( pRet.first->second, val );
44                 return true;
45             }
46             return false;
47         }
48
49         template <typename T, typename Func>
50         std::pair<bool, bool> ensure( const T& key, Func func )
51         {
52             AutoLock al( m_lock );
53             std::pair<base_class::iterator, bool> pRet = base_class::insert( base_class::value_type(key, Value() ));
54             if ( pRet.second ) {
55                 func( true, *pRet.first );
56                 return std::make_pair( true, true );
57             }
58             else {
59                 func( false, *pRet.first );
60                 return std::make_pair( true, false );
61             }
62         }
63
64         bool erase( const Key& key )
65         {
66             AutoLock al( m_lock );
67             return base_class::erase( key ) != 0;
68         }
69
70         template <typename T, typename Func>
71         bool erase( const T& key, Func func )
72         {
73             AutoLock al( m_lock );
74             base_class::iterator it = base_class::find( key );
75             if ( it != base_class::end() ) {
76                 func( *it );
77                 return base_class::erase( key ) != 0;
78             }
79             return false;
80         }
81
82
83         std::ostream& dump( std::ostream& stm ) { return stm; }
84     };
85 }   // namespace map2
86
87 #endif  // #ifndef __CDSUNIT_STD_HASH_MAP_VC_H