1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_ANDROID_THUMBNAIL_SCOPED_PTR_EXPIRING_CACHE_H_
6 #define CHROME_BROWSER_ANDROID_THUMBNAIL_SCOPED_PTR_EXPIRING_CACHE_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "net/base/linked_hash_map.h"
11 template <class Key, class Value>
12 class ScopedPtrExpiringCache {
14 typedef linked_hash_map<Key, Value*> LinkedHashMap;
17 typedef typename LinkedHashMap::iterator iterator;
19 explicit ScopedPtrExpiringCache(size_t max_cache_size)
20 : max_cache_size_(max_cache_size) {}
22 ~ScopedPtrExpiringCache() {}
24 void Put(const Key& key, scoped_ptr<Value> value) {
26 map_[key] = value.release();
30 Value* Get(const Key& key) {
31 iterator iter = map_.find(key);
32 if (iter != map_.end())
37 void Remove(const Key& key) {
38 iterator iter = map_.find(key);
39 if (iter != map_.end()) {
46 for (iterator iter = map_.begin(); iter != map_.end(); iter++) {
52 iterator begin() { return map_.begin(); }
53 iterator end() { return map_.end(); }
54 size_t MaximumCacheSize() const { return max_cache_size_; }
55 size_t size() const { return map_.size(); }
59 while (map_.size() > max_cache_size_) {
60 iterator it = map_.begin();
66 size_t max_cache_size_;
69 DISALLOW_COPY_AND_ASSIGN(ScopedPtrExpiringCache);
72 #endif // CHROME_BROWSER_ANDROID_THUMBNAIL_SCOPED_PTR_EXPIRING_CACHE_H_