src/generics/functions.h

Sat, 24 Mar 2018 15:54:41 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 24 Mar 2018 15:54:41 +0200
changeset 1335
5530161fcd47
parent 1322
d8935cdb24c0
child 1353
48374309f3d1
permissions
-rw-r--r--

MainWindow now stores its state in the config file so its state and geometry is preserved across instances

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

mercurial