]> git.xonotic.org Git - xonotic/netradiant.git/commitdiff
Release residual items in the HashedCache destructor, to avoid more error messages
authorMattia Basaglia <mattia.basaglia@gmail.com>
Thu, 30 Jul 2015 09:25:22 +0000 (11:25 +0200)
committerMattia Basaglia <mattia.basaglia@gmail.com>
Thu, 30 Jul 2015 09:25:22 +0000 (11:25 +0200)
libs/container/cache.h

index 3788fb14ab171e5448ba7b27fc2b5562718b4553..0adb73efb16b58787ac491c1108de2c0f7ab7355 100644 (file)
@@ -95,83 +95,84 @@ pointer operator->() const {
 template<typename Key, typename Cached, typename Hasher, typename KeyEqual = std::equal_to<Key>, typename CreationPolicy = DefaultCreationPolicy<Cached, Key> >
 class HashedCache : public CreationPolicy
 {
-typedef SharedValue<Cached> Element;
-typedef HashTable<Key, Element, Hasher, KeyEqual> map_type;
+       typedef SharedValue<Cached> Element;
+       typedef HashTable<Key, Element, Hasher, KeyEqual> 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();
+       }
 };