Debian Codegraph @

src/3rdParty/lru-cache/include/lru/internal/statistics-mutator.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_STATISTICS_MUTATOR_HPP
23 #define LRU_STATISTICS_MUTATOR_HPP
24
25 #include <cassert>
26 #include <cstddef>
27 #include <memory>
28 #include <utility>
29
30 #include <lru/internal/optional.hpp>
31 #include <lru/statistics.hpp>
32
33 namespace LRU {
34 namespace Internal {
35
36 /// A mutable proxy interface to a statistics object.
37 ///
38 /// The `StatisticsMutator` allows modification of the members of a statistics
39 /// object via a narrow interface, available only to internal classes. The point
40 /// of this is that while we don't want the user to be able to modify the hit or
41 /// miss count on a statistics object (it is "getter-only" in that sense), it's
42 /// also not ideal, from an encapsulation standpoint, to make the cache classes
43 /// (which do need to access and modify the hit and miss counts) friends of the
44 /// statistics. This is especially true since the caches should only need to
45 /// register hits or misses and not have to increment the count of total
46 /// accesses. As such, we really require a "package-level" interface that is not
47 /// visible to the end user, while at the same time providing an interface to
48 /// internal classes. The `StatisticsMutator` is a proxy/adapter class that
49 /// serves exactly this purpose. It is friends with the `Statistics` and can
50 /// thus access its members. At the same time the interface it defines is narrow
51 /// and provides only the necessary interface for the cache classes to register
52 /// hits and misses.
53 template <typename Key>
54 class StatisticsMutator {
55 public:
56 using StatisticsPointer = std::shared_ptr<Statistics<Key>>;
57
58 /// Constructor.
59 StatisticsMutator() noexcept = default;
60
61 /// Constructor.
62 ///
63 /// \param stats A shared pointer lvalue reference.
64 StatisticsMutator(const StatisticsPointer& stats) // NOLINT(runtime/explicit)
65 : _stats(stats) {
66 }
67
68 /// Constructor.
69 ///
70 /// \param stats A shared pointer rvalue reference to move into the
71 /// mutator.
72 StatisticsMutator(StatisticsPointer&& stats) // NOLINT(runtime/explicit)
73 : _stats(std::move(stats)) {
74 }
75
76 /// Registers a hit for the given key with the internal statistics.
77 ///
78 /// \param key The key to register a hit for.
79 void register_hit(const Key& key) {
80 assert(has_stats());
81
82 _stats->_total_accesses += 1;
83 _stats->_total_hits += 1;
84
85 auto iterator = _stats->_key_map.find(key);
86 if (iterator != _stats->_key_map.end()) {
87 iterator->second.hits += 1;
88 }
89 }
90
91 /// Registers a miss for the given key with the internal statistics.
92 ///
93 /// \param key The key to register a miss for.
94 void register_miss(const Key& key) {
95 assert(has_stats());
96
97 _stats->_total_accesses += 1;
98
99 auto iterator = _stats->_key_map.find(key);
100 if (iterator != _stats->_key_map.end()) {
101 iterator->second.misses += 1;
102 }
103 }
104
105 /// \returns A reference to the statistics object.
106 Statistics<Key>& get() noexcept {
107 assert(has_stats());
108 return *_stats;
109 }
110
111 /// \returns A const reference to the statistics object.
112 const Statistics<Key>& get() const noexcept {
113 assert(has_stats());
114 return *_stats;
115 }
116
117 /// \returns A `shared_ptr` to the statistics object.
118 StatisticsPointer& shared() noexcept {
119 return _stats;
120 }
121
122 /// \returns A const `shared_ptr` to the statistics object.
123 const StatisticsPointer& shared() const noexcept {
124 return _stats;
125 }
126
127 /// \returns True if the mutator has a statistics object, else false.
128 bool has_stats() const noexcept {
129 return _stats != nullptr;
130 }
131
132 /// \copydoc has_stats()
133 explicit operator bool() const noexcept {
134 return has_stats();
135 }
136
137 /// Resets the internal statistics pointer.
138 void reset() {
139 _stats.reset();
140 }
141
142 private:
143 /// A shared pointer to a statistics object.
144 std::shared_ptr<Statistics<Key>> _stats;
145 };
146
147 } // namespace Internal
148 } // namespace LRU
149
150 #endif // LRU_STATISTICS_MUTATOR_HPP