src/basics.h

changeset 8
44679e468ba9
parent 7
68443f5be176
child 17
a5111f4e6412
equal deleted inserted replaced
7:68443f5be176 8:44679e468ba9
15 * You should have received a copy of the GNU General Public License 15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */ 17 */
18 18
19 #pragma once 19 #pragma once
20 #include <algorithm>
20 #include <cstdio> 21 #include <cstdio>
21 #include <cstdlib> 22 #include <cstdlib>
23 #include <cstring>
22 #include <cmath> 24 #include <cmath>
23 #include <QMatrix4x4> 25 #include <QMatrix4x4>
24 #include <QObject> 26 #include <QObject>
25 #include <QPointF> 27 #include <QPointF>
26 #include <QSet> 28 #include <QSet>
40 }; 42 };
41 43
42 enum Winding 44 enum Winding
43 { 45 {
44 NoWinding, 46 NoWinding,
45 CounterClockwise, 47 Anticlockwise,
46 Clockwise, 48 Clockwise,
47 }; 49 };
48 50
49 /* 51 /*
50 * Special operator definition that implements the XOR operator for windings. 52 * Special operator definition that implements the XOR operator for windings.
62 { 64 {
63 one = one ^ other; 65 one = one ^ other;
64 return one; 66 return one;
65 } 67 }
66 68
67 template<typename T, std::size_t N>
68 constexpr std::size_t countof(T(&)[N])
69 {
70 return N;
71 }
72
73 static constexpr long double pi = M_PIl; 69 static constexpr long double pi = M_PIl;
74
75 // http://stackoverflow.com/a/18204188/3629665
76 template<typename T>
77 inline auto rotl10(T x)
78 -> std::enable_if_t<std::is_arithmetic_v<T>, T>
79 {
80 return (x << 10) | ((x >> 22) & 0x000000ff);
81 }
82
83 template<typename T>
84 inline auto rotl20(T x)
85 -> std::enable_if_t<std::is_arithmetic_v<T>, T>
86 {
87 return (x << 20) | ((x >> 12) & 0x000000ff);
88 }
89
90 template<typename T, typename... Rest>
91 QString format(const QString& format_string, T&& arg, Rest&&... rest)
92 {
93 return format(format_string.arg(arg), std::forward<Rest>(rest)...);
94 }
95
96 template<typename T>
97 QString format(const QString& format_string, T&& arg)
98 {
99 return format_string.arg(arg);
100 }
101
102 inline QString quoted(QString string)
103 {
104 if (string.contains("'"))
105 {
106 string.replace("\"", "\\\"");
107 string = "\"" + string + "\"";
108 }
109 else
110 {
111 string = "'" + string + "'";
112 }
113 return string;
114 }

mercurial