OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file contains a template for a Most Recently Used cache that allows | 5 // This file contains a template for a Most Recently Used cache that allows |
6 // constant-time access to items using a key, but easy identification of the | 6 // constant-time access to items using a key, but easy identification of the |
7 // least-recently-used items for removal. Each key can only be associated with | 7 // least-recently-used items for removal. Each key can only be associated with |
8 // one payload item at a time. | 8 // one payload item at a time. |
9 // | 9 // |
10 // The key object will be stored twice, so it should support efficient copying. | 10 // The key object will be stored twice, so it should support efficient copying. |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 | 206 |
207 DISALLOW_COPY_AND_ASSIGN(MRUCacheBase); | 207 DISALLOW_COPY_AND_ASSIGN(MRUCacheBase); |
208 }; | 208 }; |
209 | 209 |
210 // MRUCache -------------------------------------------------------------------- | 210 // MRUCache -------------------------------------------------------------------- |
211 | 211 |
212 // A functor that does nothing. Used by the MRUCache. | 212 // A functor that does nothing. Used by the MRUCache. |
213 template<class PayloadType> | 213 template<class PayloadType> |
214 class MRUCacheNullDeletor { | 214 class MRUCacheNullDeletor { |
215 public: | 215 public: |
216 void operator()(PayloadType& payload) { | 216 void operator()(const PayloadType& payload) {} |
217 } | |
218 }; | 217 }; |
219 | 218 |
220 // A container that does not do anything to free its data. Use this when storing | 219 // A container that does not do anything to free its data. Use this when storing |
221 // value types (as opposed to pointers) in the list. | 220 // value types (as opposed to pointers) in the list. |
222 template <class KeyType, class PayloadType> | 221 template <class KeyType, class PayloadType> |
223 class MRUCache : public MRUCacheBase<KeyType, | 222 class MRUCache : public MRUCacheBase<KeyType, |
224 PayloadType, | 223 PayloadType, |
225 MRUCacheNullDeletor<PayloadType> > { | 224 MRUCacheNullDeletor<PayloadType> > { |
226 private: | 225 private: |
227 typedef MRUCacheBase<KeyType, PayloadType, | 226 typedef MRUCacheBase<KeyType, PayloadType, |
228 MRUCacheNullDeletor<PayloadType> > ParentType; | 227 MRUCacheNullDeletor<PayloadType> > ParentType; |
229 | 228 |
230 public: | 229 public: |
231 // See MRUCacheBase, noting the possibility of using NO_AUTO_EVICT. | 230 // See MRUCacheBase, noting the possibility of using NO_AUTO_EVICT. |
232 explicit MRUCache(typename ParentType::size_type max_size) | 231 explicit MRUCache(typename ParentType::size_type max_size) |
233 : ParentType(max_size) { | 232 : ParentType(max_size) { |
234 } | 233 } |
235 virtual ~MRUCache() { | 234 virtual ~MRUCache() { |
236 } | 235 } |
237 | 236 |
238 private: | 237 private: |
239 DISALLOW_COPY_AND_ASSIGN(MRUCache); | 238 DISALLOW_COPY_AND_ASSIGN(MRUCache); |
240 }; | 239 }; |
241 | 240 |
242 // OwningMRUCache -------------------------------------------------------------- | 241 // OwningMRUCache -------------------------------------------------------------- |
243 | 242 |
244 template<class PayloadType> | 243 template<class PayloadType> |
245 class MRUCachePointerDeletor { | 244 class MRUCachePointerDeletor { |
246 public: | 245 public: |
247 void operator()(PayloadType& payload) { | 246 void operator()(const PayloadType& payload) { delete payload; } |
248 delete payload; | |
249 } | |
250 }; | 247 }; |
251 | 248 |
252 // A cache that owns the payload type, which must be a non-const pointer type. | 249 // A cache that owns the payload type, which must be a non-const pointer type. |
253 // The pointers will be deleted when they are removed, replaced, or when the | 250 // The pointers will be deleted when they are removed, replaced, or when the |
254 // cache is destroyed. | 251 // cache is destroyed. |
255 template <class KeyType, class PayloadType> | 252 template <class KeyType, class PayloadType> |
256 class OwningMRUCache | 253 class OwningMRUCache |
257 : public MRUCacheBase<KeyType, | 254 : public MRUCacheBase<KeyType, |
258 PayloadType, | 255 PayloadType, |
259 MRUCachePointerDeletor<PayloadType> > { | 256 MRUCachePointerDeletor<PayloadType> > { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 virtual ~HashingMRUCache() { | 298 virtual ~HashingMRUCache() { |
302 } | 299 } |
303 | 300 |
304 private: | 301 private: |
305 DISALLOW_COPY_AND_ASSIGN(HashingMRUCache); | 302 DISALLOW_COPY_AND_ASSIGN(HashingMRUCache); |
306 }; | 303 }; |
307 | 304 |
308 } // namespace base | 305 } // namespace base |
309 | 306 |
310 #endif // BASE_CONTAINERS_MRU_CACHE_H_ | 307 #endif // BASE_CONTAINERS_MRU_CACHE_H_ |
OLD | NEW |