Debian Codegraph @

src/3rdParty/lru-cache/include/lru/internal/utility.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_UTILITY_HPP
23 #define LRU_UTILITY_HPP
24
25 #include <cstddef>
26 #include <iterator>
27 #include <tuple>
28 #include <utility>
29
30 namespace LRU {
31 namespace Internal {
32
33 /// Generates an index sequence for a tuple.
34 ///
35 /// \tparam Ts The types of the tuple (to deduce the size).
36 template <typename... Ts>
37 constexpr auto tuple_indices(const std::tuple<Ts...>&) {
38 return std::make_index_sequence<sizeof...(Ts)>();
39 }
40
41 /// Applies (in the functional sense) a tuple to the constructor of a class.
42 ///
43 /// \tparam T The type to construct.
44 /// \tparam Indices The indices into the tuple (generated from an index
45 /// sequence).
46 /// \param args The tuple of arguments to construct the object with.
47 template <typename T, typename... Args, std::size_t... Indices>
48 constexpr T construct_from_tuple(const std::tuple<Args...>& arguments,
49 std::index_sequence<Indices...>) {
50 return T(std::forward<Args>(std::get<Indices>(arguments))...);
51 }
52
53 /// Applies (in the functional sense) a tuple to the constructor of a class.
54 ///
55 /// \tparam T The type to construct.
56 /// \param args The tuple of arguments to construct the object with.
57 template <typename T, typename... Args>
58 constexpr T construct_from_tuple(const std::tuple<Args...>& args) {
59 return construct_from_tuple<T>(args, tuple_indices(args));
60 }
61
62 /// Applies (in the functional sense) a tuple to the constructor of a class.
63 ///
64 /// \tparam T The type to construct.
65 /// \param args The tuple of arguments to construct the object with.
66 template <typename T, typename... Args>
67 constexpr T construct_from_tuple(std::tuple<Args...>&& args) {
68 return construct_from_tuple<T>(std::move(args), tuple_indices(args));
69 }
70
71 /// A type trait that disables a template overload if a type is not an iterator.
72 ///
73 /// \tparam T the type to check.
74 template <typename T>
75 using enable_if_iterator = typename std::iterator_traits<T>::value_type;
76
77 /// A type trait that disables a template overload if a type is not a range.
78 ///
79 /// \tparam T the type to check.
80 template <typename T>
81 using enable_if_range = std::pair<decltype(std::declval<T>().begin()),
82 decltype(std::declval<T>().end())>;
83
84 /// A type trait that disables a template overload if a type is not an iterator
85 /// over a pair.
86 ///
87 /// \tparam T the type to check.
88 template <typename T>
89 using enable_if_iterator_over_pair =
90 std::pair<typename std::iterator_traits<T>::value_type::first_type,
91 typename std::iterator_traits<T>::value_type::first_type>;
92
93
94 /// A type trait that disables a template overload if a type is not convertible
95 /// to a target type.
96 ///
97 /// \tparam Target The type one wants to check against.
98 /// \tparam T The type to check.
99 template <typename Target, typename T>
100 using enable_if_same = std::enable_if_t<std::is_convertible<T, Target>::value>;
101
102 /// Base case for `static_all_of` (the neutral element of AND is true).
103 constexpr bool static_all_of() noexcept {
104 return true;
105 }
106
107 /// Checks if all the given parameters evaluate to true.
108 ///
109 /// \param head The first expression to check.
110 /// \param tail The remaining expression to check.
111 template <typename Head, typename... Tail>
112 constexpr bool static_all_of(Head&& head, Tail&&... tail) {
113 // Replace with (ts && ...) when the time is right
114 return std::forward<Head>(head) && static_all_of(std::forward<Tail>(tail)...);
115 }
116
117 /// Base case for `static_any_of` (the neutral element of OR is false).
118 constexpr bool static_any_of() noexcept {
119 return false;
120 }
121
122 /// Checks if any the given parameters evaluate to true.
123 ///
124 /// \param head The first expression to check.
125 /// \param tail The remaining expression to check.
126 /// \returns True if any of the given parameters evaluate to true.
127 template <typename Head, typename... Tail>
128 constexpr bool static_any_of(Head&& head, Tail&&... tail) {
129 // Replace with (ts || ...) when the time is right
130 return std::forward<Head>(head) || static_any_of(std::forward<Tail>(tail)...);
131 }
132
133 /// Checks if none the given parameters evaluate to true.
134 ///
135 /// \param ts The expressions to check.
136 /// \returns True if any of the given parameters evaluate to true.
137 template <typename... Ts>
138 constexpr bool static_none_of(Ts&&... ts) {
139 // Replace with (!ts && ...) when the time is right
140 return !static_any_of(std::forward<Ts>(ts)...);
141 }
142
143 /// Checks if all the given types are convertible to the first type.
144 ///
145 /// \tparam T the first type.
146 /// \tparam Ts The types to check against the first.
147 template <typename T, typename... Ts>
148 constexpr bool
149 all_of_type = static_all_of(std::is_convertible<Ts, T>::value...);
150
151 /// Checks if none of the given types are convertible to the first type.
152 ///
153 /// \tparam T the first type.
154 /// \tparam Ts The types to check against the first.
155 template <typename T, typename... Ts>
156 constexpr bool
157 none_of_type = static_none_of(std::is_convertible<Ts, T>::value...);
158
159 /// Base case for `for_each`.
160 template <typename Function>
161 void for_each(Function) noexcept {
162 }
163
164 /// Calls a function for each of the given variadic arguments.
165 ///
166 /// \param function The function to call for each argument.
167 /// \param head The first value to call the function with.
168 /// \param tail The remaining values to call the function with.
169 template <typename Function, typename Head, typename... Tail>
170 void for_each(Function function, Head&& head, Tail&&... tail) {
171 function(std::forward<Head>(head));
172 for_each(function, std::forward<Tail>(tail)...);
173 }
174
175 } // namespace Internal
176 } // namespace LRU
177
178 #endif // LRU_UTILITY_HPP