Replace cds::lock::scoped_lock with std::unique_lock, remove cds/lock/scoped_lock.h
[libcds.git] / tests / unit / set2 / std_set.h
1 //$$CDS-header$$
2
3 #ifndef __CDSUNIT_STD_SET_VC_H
4 #define __CDSUNIT_STD_SET_VC_H
5
6 #include <set>
7 #include <functional>   // ref
8 #include <mutex>    //unique_lock
9
10 namespace set2 {
11     template <typename Value, typename Less, typename Lock,
12         class Alloc = typename CDS_DEFAULT_ALLOCATOR::template rebind<Value>::other
13     >
14     class StdSet: public std::set<Value, Less, Alloc>
15     {
16         Lock m_lock;
17         typedef std::unique_lock<Lock> AutoLock;
18         typedef std::set<Value, Less, Alloc> base_class;
19     public:
20         typedef typename base_class::key_type value_type;
21
22         StdSet( size_t nMapSize, size_t nLoadFactor )
23         {}
24
25         template <typename Key>
26         bool find( const Key& key )
27         {
28             value_type v( key );
29             AutoLock al( m_lock );
30             return base_class::find( v ) != base_class::end();
31         }
32
33         bool insert( value_type const& v )
34         {
35             AutoLock al( m_lock );
36             return base_class::insert( v ).second;
37         }
38
39         template <typename Key, typename Func>
40         bool insert( Key const& key, Func func )
41         {
42             AutoLock al( m_lock );
43             std::pair<typename base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
44             if ( pRet.second ) {
45                 func( *pRet.first );
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( value_type( key ));
56             if ( pRet.second ) {
57                 func( true, *pRet.first, key );
58                 return std::make_pair( true, true );
59             }
60             else {
61                 func( false, *pRet.first, key );
62                 return std::make_pair( true, false );
63             }
64         }
65
66         template <typename Key>
67         bool erase( const Key& key )
68         {
69             AutoLock al( m_lock );
70             return base_class::erase( value_type(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( value_type(key) );
78             if ( it != base_class::end() ) {
79                 func( *it );
80
81                 base_class::erase( it );
82                 return true;
83             }
84             return false;
85         }
86
87         std::ostream& dump( std::ostream& stm ) { return stm; }
88     };
89 } // namespace set2
90
91 #endif  // #ifndef __CDSUNIT_STD_MAP_VC_H