2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_CONTAINER_CACHE_H)
23 #define INCLUDED_CONTAINER_CACHE_H
26 #include "container/hashtable.h"
27 #include "memory/allocator.h"
29 template<typename Type, typename Parameter>
30 class DefaultCreationPolicy
33 Type* construct(const Parameter& parameter)
35 return New<Type>().scalar(parameter);
39 Delete<Type>().scalar(p);
43 template<typename Type>
46 typedef Type value_type;
47 typedef value_type* pointer;
48 typedef value_type& reference;
55 : m_count(0), m_value(0)
60 ASSERT_MESSAGE(m_count == 0 , "destroying a referenced object\n");
62 void set(pointer value)
70 std::size_t increment()
74 std::size_t decrement()
76 ASSERT_MESSAGE(!empty(), "destroying a non-existent object\n");
87 reference operator*() const
89 ASSERT_NOTNULL(m_value);
92 pointer operator->() const
94 return &(operator*());
100 /// \brief Caches values that are uniquely identified by a key.
102 /// - Automatically removes objects that are no longer referenced.
104 /// \param Key Uniquely identifies each element.
105 /// \param Cached The type to be cached. Must define a constructor that accepts \c Key.
106 /// \param CreationPolicy Must define 'Cached* construct(const Key&)' and 'void destroy(Cached*)'. The lifetime of the \c Key passed to 'construct' is guaranteed to be longer than the subsequent matched call to 'destroy'.
107 template<typename Key, typename Cached, typename Hasher, typename KeyEqual = std::equal_to<Key>, typename CreationPolicy = DefaultCreationPolicy<Cached, Key> >
108 class HashedCache : public CreationPolicy
110 typedef SharedValue<Cached> Element;
111 typedef HashTable<Key, Element, Hasher, KeyEqual> map_type;
116 explicit HashedCache(const CreationPolicy& creation = CreationPolicy())
117 : CreationPolicy(creation), m_map(256)
122 ASSERT_MESSAGE(empty(), "HashedCache::~HashedCache: not empty");
125 typedef typename map_type::iterator iterator;
126 typedef typename map_type::value_type value_type;
130 return m_map.begin();
139 return m_map.empty();
142 iterator find(const Key& key)
144 return m_map.find(key);
147 void capture(iterator i)
149 (*i).value.increment();
151 void release(iterator i)
153 if((*i).value.decrement() == 0)
155 CreationPolicy::destroy((*i).value.get());
161 Element& capture(const Key& key)
164 Element& elem = m_map[key];
165 if(elem.increment() == 1)
167 elem.set(CreationPolicy::construct(key));
171 iterator i = m_map.insert(key, Element());
172 if((*i).value.increment() == 1)
174 (*i).value.set(CreationPolicy::construct((*i).key));
180 value_type& capture(const Key& key)
182 iterator i = m_map.find(key);
185 i = m_map.insert(key, Element());
186 (*i).value.set(CreationPolicy::construct((*i).key));
188 (*i).value.increment();
192 void release(const Key& key)
194 iterator i = m_map.find(key);
195 ASSERT_MESSAGE(i != m_map.end(), "releasing a non-existent object\n");