Debian Codegraph @

src/3rdParty/lru-cache/include/lru/entry.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_PAIR_HPP
23 #define LRU_PAIR_HPP
24
25 #include <algorithm>
26 #include <type_traits>
27 #include <utility>
28
29 namespace LRU {
30
31 /// A entry of references to the key and value of an entry in a cache.
32 ///
33 /// Instances of this class are usually the result of dereferencing an iterator.
34 ///
35 /// \tparam Key The key type of the pair.
36 /// \tparam Value The value type of the pair.
37 template <typename Key, typename Value>
38 struct Entry final {
39 using KeyType = Key;
40 using ValueType = Value;
41 using first_type = Key;
42 using second_type = Value;
43
44 /// Constructor.
45 ///
46 /// \param key The key of the entry.
47 /// \param value The value of the entry.
48 Entry(const Key& key, Value& value) : first(key), second(value) {
49 }
50
51 /// Generalized copy constructor.
52 ///
53 /// Mainly for conversion from non-const values to const values.
54 ///
55 /// \param other The entry to construct from.
56 template <typename AnyKey,
57 typename AnyValue,
58 typename =
59 std::enable_if_t<(std::is_convertible<AnyKey, Key>::value &&
60 std::is_convertible<AnyValue, Value>::value)>>
61 Entry(const Entry<AnyKey, AnyValue>& other)
62 : first(other.first), second(other.second) {
63 }
64
65 /// Compares two entrys for equality.
66 ///
67 /// \param first The first entry to compare.
68 /// \param second The second entry to compare.
69 /// \returns True if the firest entry equals the second, else false.
70 template <typename Pair, typename = typename Pair::first_type>
71 friend bool operator==(const Entry& first, const Pair& second) noexcept {
72 return first.first == second.first && first.second == second.second;
73 }
74
75 /// Compares two entrys for equality.
76 ///
77 /// \param first The first entry to compare.
78 /// \param second The second entry to compare.
79 /// \returns True if the first entry equals the second, else false.
80 template <typename Pair, typename = typename Pair::first_type>
81 friend bool operator==(const Pair& first, const Entry& second) noexcept {
82 return second == first;
83 }
84
85 /// Compares two entrys for inequality.
86 ///
87 /// \param first The first entry to compare.
88 /// \param second The second entry to compare.
89 /// \returns True if the first entry does not equal the second, else false.
90 template <typename Pair, typename = typename Pair::first_type>
91 friend bool operator!=(const Entry& first, const Pair& second) noexcept {
92 return !(first == second);
93 }
94
95 /// Compares two entrys for inequality.
96 ///
97 /// \param first The first entry to compare.
98 /// \param second The second entry to compare.fdas
99 /// \returns True if the first entry does not equal the second, else false.
100 template <typename Pair, typename = typename Pair::first_type>
101 friend bool operator!=(const Pair& first, const Entry& second) noexcept {
102 return second != first;
103 }
104
105 /// \returns A `std::pair` instance with the key and value of this entry.
106 operator std::pair<const Key&, Value&>() noexcept {
107 return {first, second};
108 }
109
110 /// \returns The key of the entry (`first`).
111 const Key& key() const noexcept {
112 return first;
113 }
114
115 /// \returns The value of the entry (`second`).
116 Value& value() noexcept {
117 return second;
118 }
119
120 /// \returns The value of the entry (`second`).
121 const Value& value() const noexcept {
122 return second;
123 }
124
125 /// The key of the entry.
126 const Key& first;
127
128 /// The value of the entry.
129 Value& second;
130 };
131 } // namespace LRU
132
133
134 #endif // LRU_PAIR_HPP