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 m : namespace refinery {
26 :
27 : // A simple cache which uses negative entries in the form of null pointers.
28 m : template <typename EntryType>
29 m : class SimpleCache {
30 m : public:
31 m : typedef base::Callback<bool(scoped_refptr<EntryType>*)> LoadingCallback;
32 :
33 m : SimpleCache() {}
34 m : ~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 m : 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 m : void GetOrLoad(const base::string16& key,
49 m : const LoadingCallback& load_cb,
50 m : 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 m : void Store(const base::string16& key, scoped_refptr<EntryType> entry);
57 :
58 m : private:
59 m : std::unordered_map<base::string16, scoped_refptr<EntryType>> entries_;
60 :
61 m : DISALLOW_COPY_AND_ASSIGN(SimpleCache);
62 m : };
63 :
64 m : template <typename EntryType>
65 m : bool SimpleCache<EntryType>::Get(const base::string16& key,
66 m : scoped_refptr<EntryType>* entry) const {
67 m : DCHECK(entry);
68 m : *entry = nullptr;
69 :
70 m : auto it = entries_.find(key);
71 m : if (it == entries_.end())
72 m : return false; // Not present in the cache.
73 :
74 m : *entry = it->second;
75 m : return true;
76 m : }
77 :
78 m : template <typename EntryType>
79 m : void SimpleCache<EntryType>::GetOrLoad(const base::string16& key,
80 m : const LoadingCallback& load_cb,
81 m : scoped_refptr<EntryType>* entry) {
82 m : DCHECK(entry);
83 m : *entry = nullptr;
84 :
85 m : if (Get(key, entry))
86 m : 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 m : Store(key, *entry);
91 :
92 m : if (!load_cb.Run(entry))
93 m : return; // Load failed. Keep the negative entry.
94 :
95 m : Store(key, *entry);
96 m : }
97 :
98 m : template <typename EntryType>
99 m : void SimpleCache<EntryType>::Store(const base::string16& key,
100 m : scoped_refptr<EntryType> entry) {
101 m : entries_[key] = entry;
102 m : }
103 :
104 m : } // namespace refinery
105 :
106 : #endif // SYZYGY_REFINERY_SYMBOLS_SIMPLE_CACHE_H_
|