Debian Codegraph @

src/3rdParty/lru-cache/include/lru/internal/callback-manager.hpp

raw
1 /// The MIT License (MIT)
2 /// Copyright (c) 2016 Peter Goldsborough
3 ///
4 /// Permission is hereby granted, free of charge, to any person obtaining a copy
5 /// of this software and associated documentation files (the "Software"), to
6 /// deal in the Software without restriction, including without limitation the
7 /// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 /// sell copies of the Software, and to permit persons to whom the Software is
9 /// furnished to do so, subject to the following conditions:
10 ///
11 /// The above copyright notice and this permission notice shall be included in
12 /// all copies or substantial portions of the Software.
13 ///
14 /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 /// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 /// IN THE SOFTWARE.
21
22 #ifndef LRU_INTERNAL_CALLBACK_MANAGER_HPP
23 #define LRU_INTERNAL_CALLBACK_MANAGER_HPP
24
25 #include <functional>
26 #include <vector>
27
28 #include <lru/entry.hpp>
29 #include <lru/internal/optional.hpp>
30
31 namespace LRU {
32 namespace Internal {
33
34 /// Manages hit, miss and access callbacks for a cache.
35 ///
36 /// The callback manager implements the "publisher" of the observer pattern we
37 /// implement. It stores and calls three kinds of callbacks:
38 /// 1. Hit callbacks, taking a key and value after a cache hit.
39 /// 2. Miss callbacks, taking only a key, that was not found in a cache.
40 /// 3. Access callbacks, taking a key and a boolean indicating a hit or a miss.
41 ///
42 /// Callbacks can be added, accessed and cleared.
43 template <typename Key, typename Value>
44 class CallbackManager {
45 public:
46 using HitCallback = std::function<void(const Key&, const Value&)>;
47 using MissCallback = std::function<void(const Key&)>;
48 using AccessCallback = std::function<void(const Key&, bool)>;
49
50 using HitCallbackContainer = std::vector<HitCallback>;
51 using MissCallbackContainer = std::vector<MissCallback>;
52 using AccessCallbackContainer = std::vector<AccessCallback>;
53
54 /// Calls all callbacks registered for a hit, with the given key and value.
55 ///
56 /// \param key The key for which a cache hit ocurred.
57 /// \param value The value that was found for the key.
58 void hit(const Key& key, const Value& value) {
59 _call_each(_hit_callbacks, key, value);
60 _call_each(_access_callbacks, key, true);
61 }
62
63 /// Calls all callbacks registered for a miss, with the given key.
64 ///
65 /// \param key The key for which a cache miss ocurred.
66 void miss(const Key& key) {
67 _call_each(_miss_callbacks, key);
68 _call_each(_access_callbacks, key, false);
69 }
70
71 /// Registers a new hit callback.
72 ///
73 /// \param hit_callback The hit callback function to register with the
74 /// manager.
75 template <typename Callback>
76 void hit_callback(Callback&& hit_callback) {
77 _hit_callbacks.emplace_back(std::forward<Callback>(hit_callback));
78 }
79
80 /// Registers a new miss callback.
81 ///
82 /// \param miss_callback The miss callback function to register with the
83 /// manager.
84 template <typename Callback>
85 void miss_callback(Callback&& miss_callback) {
86 _miss_callbacks.emplace_back(std::forward<Callback>(miss_callback));
87 }
88
89 /// Registers a new access callback.
90 ///
91 /// \param access_callback The access callback function to register with the
92 /// manager.
93 template <typename Callback>
94 void access_callback(Callback&& access_callback) {
95 _access_callbacks.emplace_back(std::forward<Callback>(access_callback));
96 }
97
98 /// Clears all hit callbacks.
99 void clear_hit_callbacks() {
100 _hit_callbacks.clear();
101 }
102
103 /// Clears all miss callbacks.
104 void clear_miss_callbacks() {
105 _miss_callbacks.clear();
106 }
107
108 /// Clears all access callbacks.
109 void clear_access_callbacks() {
110 _access_callbacks.clear();
111 }
112
113 /// Clears all callbacks.
114 void clear() {
115 clear_hit_callbacks();
116 clear_miss_callbacks();
117 clear_access_callbacks();
118 }
119
120 /// \returns All hit callbacks.
121 const HitCallbackContainer& hit_callbacks() const noexcept {
122 return _hit_callbacks;
123 }
124
125 /// \returns All miss callbacks.
126 const MissCallbackContainer& miss_callbacks() const noexcept {
127 return _miss_callbacks;
128 }
129
130 /// \returns All access callbacks.
131 const AccessCallbackContainer& access_callbacks() const noexcept {
132 return _access_callbacks;
133 }
134
135 private:
136 /// Calls each function in the given container with the given arguments.
137 ///
138 /// \param callbacks The container of callbacks to call.
139 /// \param args The arguments to call the callbacks with.
140 template <typename CallbackContainer, typename... Args>
141 void _call_each(const CallbackContainer& callbacks, Args&&... args) {
142 for (const auto& callback : callbacks) {
143 callback(std::forward<Args>(args)...);
144 }
145 }
146
147 /// The container of hit callbacks registered.
148 HitCallbackContainer _hit_callbacks;
149
150 /// The container of miss callbacks registered.
151 MissCallbackContainer _miss_callbacks;
152
153 /// The container of access callbacks registered.
154 AccessCallbackContainer _access_callbacks;
155 };
156 } // namespace Internal
157 } // namespace LRU
158
159 #endif // LRU_INTERNAL_CALLBACK_MANAGER_HPP