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