cbcbc6df45cce0f2ace8b811caedc144c5bcdd7e
[libcds.git] / tests / unit / map2 / std_map.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 <mutex>    //unique_lock
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 StdMap: public std::map<Key, Value, std::less<Key>, Alloc>
15     {
16         Lock m_lock;
17         typedef std::unique_lock<Lock> scoped_lock;
18         typedef std::map<Key, Value, std::less<Key>, Alloc> base_class;
19     public:
20         typedef typename base_class::mapped_type value_type;
21         typedef typename base_class::value_type  pair_type;
22         typedef size_t      item_counter;
23
24         StdMap()
25         {}
26
27         template <class Config>
28         StdMap( Config const& )
29         {}
30
31         bool contains( const Key& key )
32         {
33             scoped_lock al( m_lock );
34             return base_class::find( key ) != base_class::end();
35         }
36
37         bool insert( const Key& key, const Value& val )
38         {
39             scoped_lock al( m_lock );
40             return base_class::insert( typename base_class::value_type(key, val)).second;
41         }
42
43         template <typename T, typename Func>
44         bool insert( const Key& key, const T& val, Func func )
45         {
46             scoped_lock al( m_lock );
47             std::pair<typename base_class::iterator, bool> pRet = base_class::insert( typename base_class::value_type(key, Value() ));
48             if ( pRet.second ) {
49                 func( pRet.first->second, val );
50                 return true;
51             }
52             return false;
53         }
54
55         template <typename T, typename Func>
56         std::pair<bool, bool> update( const T& key, Func func, bool /*bAllowInsert*/ = true )
57         {
58             scoped_lock al( m_lock );
59             std::pair<typename base_class::iterator, bool> pRet = base_class::insert( typename base_class::value_type(key, Value() ));
60             if ( pRet.second ) {
61                 func( true, *pRet.first );
62                 return std::make_pair( true, true );
63             }
64             else {
65                 func( false, *pRet.first );
66                 return std::make_pair( true, false );
67             }
68         }
69
70         bool erase( const Key& key )
71         {
72             scoped_lock al( m_lock );
73             return base_class::erase( key ) != 0;
74         }
75
76         template <typename T, typename Func>
77         bool erase( const T& key, Func func )
78         {
79             scoped_lock al( m_lock );
80             typename base_class::iterator it = base_class::find( key );
81             if ( it != base_class::end() ) {
82                 func( (*it) );
83                 base_class::erase( it );
84                 return true;
85             }
86             return false;
87         }
88
89         std::ostream& dump( std::ostream& stm ) { return stm; }
90
91
92         // for testing
93         static CDS_CONSTEXPR bool const c_bExtractSupported = false;
94         static CDS_CONSTEXPR bool const c_bLoadFactorDepended = false;
95     };
96 }   // namespace map
97
98 #endif  // #ifndef CDSUNIT_STD_MAP_GCC_H