Eliminated some MSVC++ warning C4503 "decorated name length exceeded"
[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 <mutex>    //unique_lock
8
9 namespace set2 {
10     template <typename Value, typename Less, typename Lock,
11         class Alloc = typename CDS_DEFAULT_ALLOCATOR::template rebind<Value>::other
12     >
13     class StdSet: public std::set<Value, Less, Alloc>
14     {
15         Lock m_lock;
16         typedef std::unique_lock<Lock> scoped_lock;
17         typedef std::set<Value, Less, Alloc> base_class;
18     public:
19         typedef typename base_class::key_type value_type;
20
21         StdSet( size_t nMapSize, size_t nLoadFactor )
22         {}
23
24         template <typename Key>
25         bool find( const Key& key )
26         {
27             value_type v( key );
28             scoped_lock al( m_lock );
29             return base_class::find( v ) != base_class::end();
30         }
31
32         bool insert( value_type const& v )
33         {
34             scoped_lock al( m_lock );
35             return base_class::insert( v ).second;
36         }
37
38         template <typename Key, typename Func>
39         bool insert( Key const& key, Func func )
40         {
41             scoped_lock al( m_lock );
42             std::pair<typename base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
43             if ( pRet.second ) {
44                 func( *pRet.first );
45                 return true;
46             }
47             return false;
48         }
49
50         template <typename T, typename Func>
51         std::pair<bool, bool> ensure( const T& key, Func func )
52         {
53             scoped_lock al( m_lock );
54             std::pair<typename base_class::iterator, bool> pRet = base_class::insert( value_type( key ));
55             if ( pRet.second ) {
56                 func( true, *pRet.first, key );
57                 return std::make_pair( true, true );
58             }
59             else {
60                 func( false, *pRet.first, key );
61                 return std::make_pair( true, false );
62             }
63         }
64
65         template <typename Key>
66         bool erase( const Key& key )
67         {
68             scoped_lock al( m_lock );
69             return base_class::erase( value_type(key) ) != 0;
70         }
71
72         template <typename T, typename Func>
73         bool erase( const T& key, Func func )
74         {
75             scoped_lock al( m_lock );
76             typename base_class::iterator it = base_class::find( value_type(key) );
77             if ( it != base_class::end() ) {
78                 func( *it );
79
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 set2
89
90 #endif  // #ifndef __CDSUNIT_STD_MAP_VC_H