Move libcds 1.6.0 from SVN
[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 <cds/ref.h>
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 cds::lock::scoped_lock<Lock> AutoLock;
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( size_t nMapSize, size_t nLoadFactor )
25         {}
26
27         bool find( const Key& key )
28         {
29             AutoLock al( m_lock );
30             return base_class::find( key ) != base_class::end();
31         }
32
33         bool insert( const Key& key, const Value& val )
34         {
35             AutoLock al( m_lock );
36             return base_class::insert( typename base_class::value_type(key, val)).second;
37         }
38
39         template <typename T, typename Func>
40         bool insert( const Key& key, const T& val, Func func )
41         {
42             AutoLock al( m_lock );
43             std::pair<typename base_class::iterator, bool> pRet = base_class::insert( typename base_class::value_type(key, Value() ));
44             if ( pRet.second ) {
45                 cds::unref(func)( pRet.first->second, val );
46                 return true;
47             }
48             return false;
49         }
50
51         template <typename T, typename Func>
52         std::pair<bool, bool> ensure( const T& key, 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                 cds::unref(func)( true, *pRet.first );
58                 return std::make_pair( true, true );
59             }
60             else {
61                 cds::unref(func)( false, *pRet.first );
62                 return std::make_pair( true, false );
63             }
64         }
65
66         bool erase( const Key& key )
67         {
68             AutoLock al( m_lock );
69             return base_class::erase( key ) != 0;
70         }
71
72         template <typename T, typename Func>
73         bool erase( const T& key, Func func )
74         {
75             AutoLock al( m_lock );
76             typename base_class::iterator it = base_class::find( key );
77             if ( it != base_class::end() ) {
78                 cds::unref(func)( (*it) );
79                 base_class::erase( it );
80                 return true;
81             }
82             return false;
83         }
84
85         std::ostream& dump( std::ostream& stm ) { return stm; }
86     };
87 }   // namespace map
88
89 #endif  // #ifndef __CDSUNIT_STD_MAP_GCC_H