3 #ifndef __CDSUNIT_STD_MAP_VC_H
4 #define __CDSUNIT_STD_MAP_VC_H
7 #include <functional> // ref
8 #include <mutex> //unique_lock
11 template <typename Key, typename Value, typename Lock, class Alloc = CDS_DEFAULT_ALLOCATOR>
12 class StdMap: public std::map<Key, Value, std::less<Key>, Alloc>
15 typedef std::unique_lock<Lock> AutoLock;
16 typedef std::map<Key, Value, std::less<Key>, Alloc> base_class;
18 typedef typename base_class::mapped_type value_type;
19 typedef typename base_class::value_type pair_type;
20 typedef size_t item_counter;
22 StdMap( size_t nMapSize, size_t nLoadFactor )
25 bool find( const Key& key )
27 AutoLock al( m_lock );
28 return base_class::find( key ) != base_class::end();
31 bool insert( const Key& key, const Value& val )
33 AutoLock al( m_lock );
34 return base_class::insert( base_class::value_type(key, val)).second;
37 template <typename T, typename Func>
38 bool insert( const Key& key, const T& val, Func func )
40 AutoLock al( m_lock );
41 std::pair<base_class::iterator, bool> pRet = base_class::insert( base_class::value_type(key, Value() ));
43 func( pRet.first->second, val );
49 template <typename T, typename Func>
50 std::pair<bool, bool> ensure( const T& key, Func func )
52 AutoLock al( m_lock );
53 std::pair<base_class::iterator, bool> pRet = base_class::insert( base_class::value_type(key, Value() ));
55 func( true, *pRet.first );
56 return std::make_pair( true, true );
59 func( false, *pRet.first );
60 return std::make_pair( true, false );
64 bool erase( const Key& key )
66 AutoLock al( m_lock );
67 return base_class::erase( key ) != 0;
70 template <typename T, typename Func>
71 bool erase( const T& key, Func func )
73 AutoLock al( m_lock );
74 base_class::iterator it = base_class::find( key );
75 if ( it != base_class::end() ) {
78 base_class::erase( it );
84 std::ostream& dump( std::ostream& stm ) { return stm; }
88 #endif // #ifndef __CDSUNIT_STD_MAP_VC_H