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