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