1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #ifndef SYZYGY_REFINERY_SYMBOLS_SIMPLE_CACHE_H_
16 : #define SYZYGY_REFINERY_SYMBOLS_SIMPLE_CACHE_H_
17 :
18 : #include "base/callback.h"
19 : #include "base/logging.h"
20 : #include "base/macros.h"
21 : #include "base/containers/hash_tables.h"
22 : #include "base/memory/ref_counted.h"
23 : #include "base/strings/string16.h"
24 :
25 : namespace refinery {
26 :
27 : // A simple cache which uses negative entries in the form of null pointers.
28 : template <typename EntryType>
29 : class SimpleCache {
30 : public:
31 : typedef base::Callback<bool(scoped_refptr<EntryType>*)> LoadingCallback;
32 :
33 E : SimpleCache() {}
34 E : ~SimpleCache() {}
35 :
36 : // Retrieves a cache entry.
37 : // @param key the desired entry's cache key.
38 : // @param entry on success, returns the desired entry or nullptr to indicate a
39 : // negative entry.
40 : // @returns true if the cache contains an entry for @p key, false otherwise.
41 : bool Get(const base::string16& key, scoped_refptr<EntryType>* entry) const;
42 :
43 : // Retrieves a cache entry, loading it if required.
44 : // @param key the desired entry's cache key.
45 : // @param load_cb a LoadingCallback for use if the entry is not in cache.
46 : // @param entry on success, returns the desired entry or nullptr to indicate a
47 : // failure to load and the insertion of a negative entry.
48 : void GetOrLoad(const base::string16& key,
49 : const LoadingCallback& load_cb,
50 : scoped_refptr<EntryType>* entry);
51 :
52 : // Stores a cache entry.
53 : // @note replaces any previous entry at @p key.
54 : // @param key the key to store at.
55 : // @param entry the entry to store at @p key.
56 : void Store(const base::string16& key, scoped_refptr<EntryType> entry);
57 :
58 : private:
59 : base::hash_map<base::string16, scoped_refptr<EntryType>> entries_;
60 :
61 : DISALLOW_COPY_AND_ASSIGN(SimpleCache);
62 : };
63 :
64 : template <typename EntryType>
65 : bool SimpleCache<EntryType>::Get(const base::string16& key,
66 E : scoped_refptr<EntryType>* entry) const {
67 E : DCHECK(entry);
68 E : *entry = nullptr;
69 :
70 E : auto it = entries_.find(key);
71 E : if (it == entries_.end())
72 E : return false; // Not present in the cache.
73 :
74 E : *entry = it->second;
75 E : return true;
76 E : }
77 :
78 : template <typename EntryType>
79 : void SimpleCache<EntryType>::GetOrLoad(const base::string16& key,
80 : const LoadingCallback& load_cb,
81 E : scoped_refptr<EntryType>* entry) {
82 E : DCHECK(entry);
83 E : *entry = nullptr;
84 :
85 E : if (Get(key, entry))
86 E : return; // There's a pre-existing entry.
87 :
88 : // No entry in the cache. Create a negative cache entry, which will be
89 : // replaced on success.
90 E : Store(key, *entry);
91 :
92 E : if (!load_cb.Run(entry))
93 E : return; // Load failed. Keep the negative entry.
94 :
95 E : Store(key, *entry);
96 E : }
97 :
98 : template <typename EntryType>
99 : void SimpleCache<EntryType>::Store(const base::string16& key,
100 E : scoped_refptr<EntryType> entry) {
101 E : entries_[key] = entry;
102 E : }
103 :
104 : } // namespace refinery
105 :
106 : #endif // SYZYGY_REFINERY_SYMBOLS_SIMPLE_CACHE_H_
|