Sat, 06 Jan 2018 17:57:03 +0200
simplify
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | /* |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | * LDForge: LDraw parts authoring CAD |
1222 | 3 | * Copyright (C) 2013 - 2018 Teemu Piippo |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | * |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | * This program is free software: you can redistribute it and/or modify |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | * it under the terms of the GNU General Public License as published by |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | * the Free Software Foundation, either version 3 of the License, or |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | * (at your option) any later version. |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | * |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | * This program is distributed in the hope that it will be useful, |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | * GNU General Public License for more details. |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | * |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | * You should have received a copy of the GNU General Public License |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | */ |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
19 | #pragma once |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | #include "basics.h" |
795
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
794
diff
changeset
|
21 | #include "colors.h" |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | |
1221 | 23 | extern void printToLog(const QString& msg); |
1011
4fdd74a04790
Rename LDSubfile to LDSubfileReference
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1010
diff
changeset
|
24 | |
1221 | 25 | |
26 | inline const QString& toString(const QString& text) | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
27 | { |
1221 | 28 | return text; |
29 | } | |
30 | ||
794 | 31 | |
1221 | 32 | inline QString toString(char character) |
33 | { | |
34 | return QString {character}; | |
35 | } | |
36 | ||
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | |
1221 | 38 | inline QString toString(unsigned char character) |
39 | { | |
40 | return QString {character}; | |
41 | } | |
794 | 42 | |
43 | ||
1221 | 44 | inline QString toString(QChar character) |
45 | { | |
46 | return character; | |
47 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
48 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | |
1221 | 50 | inline QString toString(int value) |
51 | { | |
52 | return QString::number(value); | |
53 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | |
1011
4fdd74a04790
Rename LDSubfile to LDSubfileReference
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1010
diff
changeset
|
55 | |
1221 | 56 | inline QString toString(short int value) |
57 | { | |
58 | return QString::number(value); | |
59 | } | |
60 | ||
61 | ||
62 | inline QString toString(long int value) | |
63 | { | |
64 | return QString::number(value); | |
65 | } | |
66 | ||
67 | ||
68 | inline QString toString(unsigned int value) | |
69 | { | |
70 | return QString::number(value); | |
71 | } | |
72 | ||
73 | ||
74 | inline QString toString(unsigned long int value) | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | { |
1221 | 76 | return QString::number(value); |
77 | } | |
78 | ||
79 | ||
80 | inline QString toString(unsigned short int value) | |
81 | { | |
82 | return QString::number(value); | |
83 | } | |
84 | ||
85 | ||
86 | inline QString toString(float value) | |
87 | { | |
88 | return QString::number(value); | |
89 | } | |
90 | ||
91 | ||
92 | inline QString toString(double value) | |
93 | { | |
94 | return QString::number(value); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
95 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | |
1011
4fdd74a04790
Rename LDSubfile to LDSubfileReference
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1010
diff
changeset
|
97 | |
1221 | 98 | inline QString toString(const Vertex& position) |
99 | { | |
100 | return position.toString(); | |
101 | } | |
102 | ||
103 | ||
104 | inline QString toString(const Matrix& transformation) | |
105 | { | |
106 | return transformation.toString(); | |
107 | } | |
108 | ||
109 | ||
110 | inline QString toString(const char* text) | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
111 | { |
1221 | 112 | return text; |
113 | } | |
114 | ||
115 | ||
116 | inline QString toString(LDColor color) | |
117 | { | |
118 | return color.indexString(); | |
119 | } | |
120 | ||
121 | ||
122 | inline QString toString(const void* pointer) | |
123 | { | |
124 | QString result; | |
125 | result.sprintf("%p", pointer); | |
126 | return result; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
127 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
128 | |
1011
4fdd74a04790
Rename LDSubfile to LDSubfileReference
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1010
diff
changeset
|
129 | |
1221 | 130 | template<typename T, int N> |
131 | QString toString(T(&array)[N]) | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
132 | { |
1221 | 133 | QString rep = "{"; |
134 | ||
135 | for (int i = 0; i < N; ++i) | |
136 | { | |
137 | if (i > 0) | |
138 | rep += ", "; | |
139 | rep += toString(array[i]); | |
140 | } | |
141 | ||
142 | rep += "}"; | |
143 | return rep; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
144 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
145 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
146 | |
1221 | 147 | template<typename T> |
148 | QString toString(const QList<T>& list) | |
149 | { | |
150 | QString result = "{"; | |
151 | ||
152 | for (const T& element : list) | |
153 | { | |
154 | if (&element != &list.first()) | |
155 | result += ", "; | |
156 | ||
157 | result += toString(element); | |
158 | } | |
159 | ||
160 | result += "}"; | |
161 | return result; | |
162 | } | |
163 | ||
164 | ||
165 | /* | |
166 | * Formats the message with the given args using QString::arg(). | |
167 | */ | |
168 | template<typename Arg1, typename... Rest> | |
169 | QString format(const QString& string, Arg1 arg1, Rest&&... rest) | |
170 | { | |
171 | return format(string.arg(toString(arg1)), rest...); | |
172 | } | |
173 | ||
174 | ||
175 | template<typename... Rest> | |
176 | const QString& format(const QString& string) | |
177 | { | |
178 | return string; | |
179 | } | |
1011
4fdd74a04790
Rename LDSubfile to LDSubfileReference
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1010
diff
changeset
|
180 | |
4fdd74a04790
Rename LDSubfile to LDSubfileReference
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1010
diff
changeset
|
181 | |
794 | 182 | // Format and print the given args to the message log. |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
183 | template<typename... Args> |
1221 | 184 | void print(const QString& formatString, Args&&... args) |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
185 | { |
1221 | 186 | printToLog(format(formatString, args...)); |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
187 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
188 | |
1221 | 189 | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
190 | template<typename... Args> |
1221 | 191 | void fprint(FILE* filePointer, const QString& formatString, Args&&... args) |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
192 | { |
1221 | 193 | fprintf(filePointer, "%s", qPrintable(format(formatString, args...))); |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
194 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
195 | |
1011
4fdd74a04790
Rename LDSubfile to LDSubfileReference
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1010
diff
changeset
|
196 | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
197 | template<typename... Args> |
1221 | 198 | void fprint(QIODevice& device, const QString& formatString, Args&&... args) |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
199 | { |
1221 | 200 | device.write(format(formatString, args...).toUtf8()); |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
201 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
202 | |
1011
4fdd74a04790
Rename LDSubfile to LDSubfileReference
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1010
diff
changeset
|
203 | |
794 | 204 | // Exactly like print() except no-op in release builds. |
1221 | 205 | #ifndef RELEASE |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
206 | template<typename... Args> |
1221 | 207 | void dprint(const QString& formatString, Args&&... args) |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
208 | { |
1221 | 209 | print(formatString, args...); |
794 | 210 | } |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
211 | #else |
1221 | 212 | template<typename... Args> |
1217 | 213 | void dprint(QString, Args...) {} |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
214 | #endif |