3 #ifndef __CDSUNIT_STD_MAP_GCC_H
4 #define __CDSUNIT_STD_MAP_GCC_H
7 #include <functional> // ref
8 #include <mutex> //unique_lock
12 template <typename Key, typename Value, typename Lock,
13 class Alloc = typename CDS_DEFAULT_ALLOCATOR::template rebind<std::pair<Key const, Value> >::other
15 class StdMap: public std::map<Key, Value, std::less<Key>, Alloc>
18 typedef std::unique_lock<Lock> AutoLock;
19 typedef std::map<Key, Value, std::less<Key>, Alloc> base_class;
21 typedef typename base_class::mapped_type value_type;
22 typedef typename base_class::value_type pair_type;
23 typedef size_t item_counter;
25 StdMap( size_t nMapSize, size_t nLoadFactor )
28 bool find( const Key& key )
30 AutoLock al( m_lock );
31 return base_class::find( key ) != base_class::end();
34 bool insert( const Key& key, const Value& val )
36 AutoLock al( m_lock );
37 return base_class::insert( typename base_class::value_type(key, val)).second;
40 template <typename T, typename Func>
41 bool insert( const Key& key, const T& val, Func func )
43 AutoLock al( m_lock );
44 std::pair<typename base_class::iterator, bool> pRet = base_class::insert( typename base_class::value_type(key, Value() ));
46 func( pRet.first->second, val );
52 template <typename T, typename Func>
53 std::pair<bool, bool> ensure( const T& key, Func func )
55 AutoLock al( m_lock );
56 std::pair<typename base_class::iterator, bool> pRet = base_class::insert( typename base_class::value_type(key, Value() ));
58 func( true, *pRet.first );
59 return std::make_pair( true, true );
62 func( false, *pRet.first );
63 return std::make_pair( true, false );
67 bool erase( const Key& key )
69 AutoLock al( m_lock );
70 return base_class::erase( key ) != 0;
73 template <typename T, typename Func>
74 bool erase( const T& key, Func func )
76 AutoLock al( m_lock );
77 typename base_class::iterator it = base_class::find( key );
78 if ( it != base_class::end() ) {
80 base_class::erase( it );
86 std::ostream& dump( std::ostream& stm ) { return stm; }
90 #endif // #ifndef __CDSUNIT_STD_MAP_GCC_H