From 59d871c0e3c63612c70013872ebcca8b43d72e2c Mon Sep 17 00:00:00 2001 From: Mattia Basaglia Date: Thu, 30 Jul 2015 11:25:22 +0200 Subject: [PATCH] Release residual items in the HashedCache destructor, to avoid more error messages --- libs/container/cache.h | 129 +++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 64 deletions(-) diff --git a/libs/container/cache.h b/libs/container/cache.h index 3788fb14..0adb73ef 100644 --- a/libs/container/cache.h +++ b/libs/container/cache.h @@ -95,83 +95,84 @@ pointer operator->() const { template, typename CreationPolicy = DefaultCreationPolicy > class HashedCache : public CreationPolicy { -typedef SharedValue Element; -typedef HashTable map_type; + typedef SharedValue Element; + typedef HashTable map_type; -map_type m_map; + map_type m_map; public: -explicit HashedCache( const CreationPolicy& creation = CreationPolicy() ) - : CreationPolicy( creation ), m_map( 256 ){ -} -~HashedCache(){ - ASSERT_MESSAGE( empty(), "HashedCache::~HashedCache: not empty" ); -} - -typedef typename map_type::iterator iterator; -typedef typename map_type::value_type value_type; + explicit HashedCache( const CreationPolicy& creation = CreationPolicy() ) + : CreationPolicy( creation ), m_map( 256 ){ + } + ~HashedCache(){ + ASSERT_MESSAGE( empty(), "HashedCache::~HashedCache: not empty" ); + for ( auto i = begin(); i != end(); ) + { + auto next = i; + ++next; + release(i); + i = next; + } + } -iterator begin(){ - return m_map.begin(); -} -iterator end(){ - return m_map.end(); -} + typedef typename map_type::iterator iterator; + typedef typename map_type::value_type value_type; -bool empty() const { - return m_map.empty(); -} + iterator begin(){ + return m_map.begin(); + } + iterator end(){ + return m_map.end(); + } -iterator find( const Key& key ){ - return m_map.find( key ); -} + bool empty() const { + return m_map.empty(); + } -void capture( iterator i ){ - ( *i ).second.increment(); -} -void release( iterator i ){ - if ( ( *i ).second.decrement() == 0 ) { - CreationPolicy::destroy( ( *i ).second.get() ); - m_map.erase( i ); + iterator find( const Key& key ){ + return m_map.find( key ); } -} -#if 1 -Element& capture( const Key& key ){ -#if 0 - Element& elem = m_map[key]; - if ( elem.increment() == 1 ) { - elem.set( CreationPolicy::construct( key ) ); + void capture( iterator i ){ + ( *i ).second.increment(); } - return elem; -#else - iterator i = m_map.emplace( key, Element() ).first; - if ( ( *i ).second.increment() == 1 ) { - ( *i ).second.set( CreationPolicy::construct( ( *i ).first ) ); + void release( iterator i ){ + if ( ( *i ).second.decrement() == 0 ) { + CreationPolicy::destroy( ( *i ).second.get() ); + m_map.erase( i ); + } } - return ( *i ).second; -#endif -} -#else -value_type& capture( const Key& key ){ - iterator i = m_map.find( key ); - if ( i == m_map.end() ) { - i = m_map.insert( key, Element() ); - ( *i ).second.set( CreationPolicy::construct( ( *i ).first ) ); + + Element& capture( const Key& key ){ + /*Element& elem = m_map[key]; + if ( elem.increment() == 1 ) { + elem.set( CreationPolicy::construct( key ) ); + } + return elem;*/ + iterator i = m_map.emplace( key, Element() ).first; + if ( ( *i ).second.increment() == 1 ) { + ( *i ).second.set( CreationPolicy::construct( ( *i ).first ) ); + } + return ( *i ).second; + } + /*value_type& capture( const Key& key ){ + iterator i = m_map.find( key ); + if ( i == m_map.end() ) { + i = m_map.insert( key, Element() ); + ( *i ).second.set( CreationPolicy::construct( ( *i ).first ) ); + } + ( *i ).second.increment(); + return ( *i ); + }*/ + void release( const Key& key ){ + iterator i = m_map.find( key ); + ASSERT_MESSAGE( i != m_map.end(), "releasing a non-existent object\n" ); + release( i ); } - ( *i ).second.increment(); - return ( *i ); -} -#endif -void release( const Key& key ){ - iterator i = m_map.find( key ); - ASSERT_MESSAGE( i != m_map.end(), "releasing a non-existent object\n" ); - release( i ); -} -void clear(){ - m_map.clear(); -} + void clear(){ + m_map.clear(); + } }; -- 2.39.2