Sat, 24 Mar 2018 11:57:24 +0200
reorganized headers
1319 | 1 | #pragma once |
2 | #include <functional> | |
3 | #include <cmath> | |
4 | #include "../basics.h" | |
5 | ||
6 | using std::abs; | |
7 | using std::ceil; | |
8 | using std::cos; | |
9 | using std::floor; | |
10 | using std::hypot; | |
11 | using std::sin; | |
12 | using std::sqrt; | |
13 | ||
14 | /* | |
15 | * Returns whether the argument is reasonably close to zero. | |
16 | */ | |
17 | template<typename T> | |
18 | bool isZero(T a) | |
19 | { | |
20 | return qFuzzyCompare(a + 1.0, 1.0); | |
21 | } | |
22 | ||
23 | template<typename T> | |
24 | bool isInteger(T a) | |
25 | { | |
26 | return (::abs(a - ::floor(a)) < 0.00001) or (::abs(a - ::ceil(a)) < 0.00001); | |
27 | } | |
28 | ||
29 | // | |
30 | // Returns true if first arg is equal to any of the other args | |
31 | // | |
32 | template<typename T, typename Arg, typename... Args> | |
33 | bool isOneOf(const T& needle, const Arg& arg, const Args&... args) | |
34 | { | |
35 | if (needle == arg) | |
36 | return true; | |
37 | else | |
38 | return isOneOf(needle, args...); | |
39 | } | |
40 | ||
41 | template<typename T> | |
42 | bool isOneOf(const T&) | |
43 | { | |
44 | return false; | |
45 | } | |
46 | ||
47 | // http://stackoverflow.com/a/18204188/3629665 | |
48 | template<typename T> | |
49 | inline int rotl10(T x) | |
50 | { | |
51 | return (((x) << 10) | (((x) >> 22) & 0x000000ff)); | |
52 | } | |
53 | ||
54 | template<typename T> | |
55 | inline int rotl20(T x) | |
56 | { | |
57 | return (((x) << 20) | (((x) >> 12) & 0x000000ff)); | |
58 | } | |
59 | ||
60 | // | |
61 | // Get the amount of elements in something. | |
62 | // | |
63 | template<typename T, size_t N> | |
64 | int countof(T(&)[N]) | |
65 | { | |
66 | return N; | |
67 | } | |
68 | ||
69 | static inline int countof(const QString& string) | |
70 | { | |
71 | return string.length(); | |
72 | } | |
73 | ||
74 | template<typename T> | |
75 | int countof(const QVector<T>& vector) | |
76 | { | |
77 | return vector.size(); | |
78 | } | |
79 | ||
80 | template<typename T> | |
81 | int countof(const QList<T>& vector) | |
82 | { | |
83 | return vector.size(); | |
84 | } | |
85 | ||
86 | template<typename T> | |
87 | int countof(const QSet<T>& set) | |
88 | { | |
89 | return set.size(); | |
90 | } | |
91 | ||
92 | template<typename T> | |
93 | int countof(const std::initializer_list<T>& vector) | |
94 | { | |
95 | return vector.size(); | |
96 | } | |
97 | ||
98 | /* | |
99 | * Extracts the sign of x. | |
100 | */ | |
101 | template<typename T> | |
102 | T sign(T x) | |
103 | { | |
104 | if (isZero(x)) | |
105 | return {}; | |
106 | else | |
107 | return x / qAbs(x); | |
108 | } | |
109 | ||
110 | template<> | |
111 | inline int sign(int x) | |
112 | { | |
113 | if (x == 0) | |
114 | return 0; | |
115 | else | |
116 | return x / qAbs(x); | |
117 | } | |
118 | ||
119 | /* | |
120 | * Returns the maximum of a single parameter (the parameter itself). | |
121 | */ | |
122 | template <typename T> | |
123 | T max(T a) | |
124 | { | |
125 | return a; | |
126 | } | |
127 | ||
128 | /* | |
129 | * Returns the maximum of two parameters. | |
130 | */ | |
131 | template <typename T> | |
132 | T max(T a, T b) | |
133 | { | |
134 | return a > b ? a : b; | |
135 | } | |
136 | ||
137 | /* | |
138 | * Returns the maximum of n parameters. | |
139 | */ | |
140 | template <typename T, typename... Rest> | |
141 | T max(T a, Rest&&... rest) | |
142 | { | |
143 | return max(a, max(rest...)); | |
144 | } | |
145 | ||
146 | /* | |
147 | * Returns the minimum of a single parameter (the parameter itself). | |
148 | */ | |
149 | template <typename T> | |
150 | T min(T a) | |
151 | { | |
152 | return a; | |
153 | } | |
154 | ||
155 | /* | |
156 | * Returns the minimum of two parameters. | |
157 | */ | |
158 | template <typename T> | |
159 | T min(T a, T b) | |
160 | { | |
161 | return a < b ? a : b; | |
162 | } | |
163 | ||
164 | /* | |
165 | * Returns the minimum of n parameters. | |
166 | */ | |
167 | template <typename T, typename... Rest> | |
168 | T min(T a, Rest&&... rest) | |
169 | { | |
170 | return min(a, min(rest...)); | |
171 | } | |
172 | ||
173 | /* | |
174 | * Assigns the value of a single flag in a flagset | |
175 | */ | |
176 | template<int Flag, typename T> | |
177 | void assignFlag(QFlags<T>& flagset, bool value) | |
178 | { | |
179 | if (value) | |
180 | flagset |= static_cast<T>(Flag); | |
181 | else | |
182 | flagset &= ~static_cast<T>(Flag); | |
183 | } | |
184 | ||
185 | /* | |
186 | * Returns a singleton of type T, useful for providing a valid but unused | |
187 | * pointer. | |
188 | */ | |
189 | template<typename T> | |
190 | inline T& sink() | |
191 | { | |
192 | static T result; | |
193 | return result; | |
194 | } | |
195 | ||
196 | /* | |
197 | * Rounds the input value to the nearest multiple of the provided interval. | |
198 | */ | |
199 | template<typename T> | |
200 | T roundToInterval(T value, double interval) | |
201 | { | |
202 | return static_cast<T>(round(value / interval) * interval); | |
203 | } | |
204 | ||
205 | // Copy of qOverload so as to drop Qt version requirement from 5.7 to 5.5. | |
206 | #if (QT_VERSION < QT_VERSION_CHECK(5, 7, 0)) | |
207 | template <typename... Args> | |
208 | struct QNonConstOverload | |
209 | { | |
210 | template <typename R, typename T> | |
211 | Q_DECL_CONSTEXPR auto operator()(R (T::*ptr)(Args...)) const Q_DECL_NOTHROW -> decltype(ptr) | |
212 | { return ptr; } | |
213 | template <typename R, typename T> | |
214 | static Q_DECL_CONSTEXPR auto of(R (T::*ptr)(Args...)) Q_DECL_NOTHROW -> decltype(ptr) | |
215 | { return ptr; } | |
216 | }; | |
217 | template <typename... Args> | |
218 | struct QConstOverload | |
219 | { | |
220 | template <typename R, typename T> | |
221 | Q_DECL_CONSTEXPR auto operator()(R (T::*ptr)(Args...) const) const Q_DECL_NOTHROW -> decltype(ptr) | |
222 | { return ptr; } | |
223 | template <typename R, typename T> | |
224 | static Q_DECL_CONSTEXPR auto of(R (T::*ptr)(Args...) const) Q_DECL_NOTHROW -> decltype(ptr) | |
225 | { return ptr; } | |
226 | }; | |
227 | template <typename... Args> | |
228 | struct QOverload : QConstOverload<Args...>, QNonConstOverload<Args...> | |
229 | { | |
230 | using QConstOverload<Args...>::of; | |
231 | using QConstOverload<Args...>::operator(); | |
232 | using QNonConstOverload<Args...>::of; | |
233 | using QNonConstOverload<Args...>::operator(); | |
234 | template <typename R> | |
235 | Q_DECL_CONSTEXPR auto operator()(R (*ptr)(Args...)) const Q_DECL_NOTHROW -> decltype(ptr) | |
236 | { return ptr; } | |
237 | template <typename R> | |
238 | static Q_DECL_CONSTEXPR auto of(R (*ptr)(Args...)) Q_DECL_NOTHROW -> decltype(ptr) | |
239 | { return ptr; } | |
240 | }; | |
241 | template <typename... Args> Q_CONSTEXPR Q_DECL_UNUSED QOverload<Args...> qOverload = {}; | |
242 | template <typename... Args> Q_CONSTEXPR Q_DECL_UNUSED QConstOverload<Args...> qConstOverload = {}; | |
243 | template <typename... Args> Q_CONSTEXPR Q_DECL_UNUSED QNonConstOverload<Args...> qNonConstOverload = {}; | |
244 | #endif |