Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / android / thumbnail / scoped_ptr_expiring_cache.h
blob86298292c9966707e2f12e11522300111842d461
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 {
13 private:
14 typedef linked_hash_map<Key, Value*> LinkedHashMap;
16 public:
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) {
25 Remove(key);
26 map_[key] = value.release();
27 EvictIfFull();
30 Value* Get(const Key& key) {
31 iterator iter = map_.find(key);
32 if (iter != map_.end())
33 return iter->second;
34 return NULL;
37 void Remove(const Key& key) {
38 iterator iter = map_.find(key);
39 if (iter != map_.end()) {
40 delete iter->second;
41 map_.erase(key);
45 void Clear() {
46 for (iterator iter = map_.begin(); iter != map_.end(); iter++) {
47 delete iter->second;
49 map_.clear();
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(); }
57 private:
58 void EvictIfFull() {
59 while (map_.size() > max_cache_size_) {
60 iterator it = map_.begin();
61 delete it->second;
62 map_.erase(it);
66 size_t max_cache_size_;
67 LinkedHashMap map_;
69 DISALLOW_COPY_AND_ASSIGN(ScopedPtrExpiringCache);
72 #endif // CHROME_BROWSER_ANDROID_THUMBNAIL_SCOPED_PTR_EXPIRING_CACHE_H_