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