gz-cpp-util 1.3
A c++20 library containing various utilities
to_string.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../concepts.hpp"
4
5#include <string>
6#include <string_view>
7#include <ranges>
8
9#define GZ_UTIL_STRING_CONCEPTS
10
14template<gz::util::False T>
15std::string toString(const T& s);
16
17namespace gz::util {
25 template<typename T>
27
29 template<typename T>
31 !Stringy<T> &&
32 requires(const T& t) {
33 { std::string(t) } -> std::same_as<std::string>;
34 };
35
37 template<typename T>
39 !Stringy<T> &&
41 requires(const T& t) { { t.toString() }-> Stringy; };
42
44 template<typename T>
46 !Stringy<T> &&
49 requires(const T& t) { { t.to_string() }-> Stringy; };
50
52 template<typename T>
54 !Stringy<T> &&
58 requires(const T& t) { { std::to_string(t) } -> Stringy; };
59
61 template<typename T>
63 !Stringy<T> &&
68 requires(const T& t) {
69 { ::toString(t) } -> std::same_as<std::string>;
70 };
71
72 template<typename T>
73 concept _ToStringBasicNoPtr =
74 Stringy<T> ||
80
81#ifndef GZ_TO_STRING_NO_VECTORS
82 // VECTOR
84 template<typename T>
85 concept Vector2ConvertibleToString = !_ToStringBasicNoPtr<T> &&
86 requires(T t) {
87 { t.x } -> _ToStringBasicNoPtr;
88 { t.y } -> _ToStringBasicNoPtr;
89 requires sizeof(t.x) * 2 == sizeof(T);
90 };
92 template<typename T>
93 concept Vector3ConvertibleToString = !_ToStringBasicNoPtr<T> &&
94 requires(T t) {
95 { t.x } -> _ToStringBasicNoPtr;
96 { t.y } -> _ToStringBasicNoPtr;
97 { t.z } -> _ToStringBasicNoPtr;
98 requires sizeof(t.x) * 3 == sizeof(T);
99 };
101 template<typename T>
102 concept Vector4ConvertibleToString = !_ToStringBasicNoPtr<T> &&
103 requires(T t) {
104 { t.x } -> _ToStringBasicNoPtr;
105 { t.y } -> _ToStringBasicNoPtr;
106 { t.z } -> _ToStringBasicNoPtr;
107 { t.w } -> _ToStringBasicNoPtr;
108 requires sizeof(t.x) * 4 == sizeof(T);
109 };
110
112 template<typename T>
113 concept Extent2DConvertibleToString = !_ToStringBasicNoPtr<T> &&
114 requires(T t) {
115 { t.width } -> _ToStringBasicNoPtr;
116 { t.height } -> _ToStringBasicNoPtr;
117 requires sizeof(t.width) * 2 == sizeof(T);
118 };
120 template<typename T>
121 concept Extent3DConvertibleToString = !_ToStringBasicNoPtr<T> &&
122 requires(T t) {
123 { t.width } -> _ToStringBasicNoPtr;
124 { t.height } -> _ToStringBasicNoPtr;
125 { t.depth } -> _ToStringBasicNoPtr;
126 requires sizeof(t.width) * 3 == sizeof(T);
127 };
128#endif
129
130 template<typename T>
131 concept _ToStringBasicOrVectorNoPtr =
132 _ToStringBasicNoPtr<T>
133#ifndef GZ_TO_STRING_NO_VECTORS
139#endif
140 ;
141
142 template<typename T>
143 concept PtrToToStringBasicOrVector =
144 !_ToStringBasicOrVectorNoPtr<T> &&
145 _ToStringBasicOrVectorNoPtr<std::remove_pointer_t<T>>;
147} // namespace gz::util
148
149
150namespace gz {
161 template<util::Stringy T>
162 inline std::string toString(const T& t) {
163 return static_cast<std::string>(t);
164 }
165
170 template<util::CanConstructString T>
171 inline std::string toString(const T& t) {
172 return std::string(t);
173 }
174
180 template<util::HasToStringMember T>
181 inline std::string toString(const T& t) {
182 return t.toString();
183 }
184
190 template<util::HasToStringMember2 T>
191 inline std::string toString(const T& t) {
192 return t.to_string();
193 }
194
200 template<util::WorksWithStdToString T>
201 inline std::string toString(const T& t) {
202 return std::to_string(t);
203 }
204
211 template<std::same_as<bool> T>
212 inline std::string toString(const T& b) {
213 return b ? "true" : "false";
214 }
215
220 template<util::ConvertibleToStringGlobal T>
221 inline std::string toString(const T& t) {
223 }
224
225#ifndef GZ_TO_STRING_NO_VECTORS
231 template<util::Vector2ConvertibleToString T>
232 inline std::string toString(const T& t) {
233 std::string s = "( ";
234 s += toString(t.x) + ", ";
235 s += toString(t.y) + " )";
236 return s;
237 }
238
244 template<util::Vector3ConvertibleToString T>
245 inline std::string toString(const T& t) {
246 std::string s = "( ";
247 s += toString(t.x) + ", ";
248 s += toString(t.y) + ", ";
249 s += toString(t.z) + " )";
250 return s;
251 }
252
258 template<util::Vector4ConvertibleToString T>
259 inline std::string toString(const T& t) {
260 std::string s = "( ";
261 s += toString(t.x) + ", ";
262 s += toString(t.y) + ", ";
263 s += toString(t.z) + ", ";
264 s += toString(t.w) + " )";
265 return s;
266 }
267
273 template<util::Extent2DConvertibleToString T>
274 inline std::string toString(const T& t) {
275 std::string s = "( ";
276 s += toString(t.width) + ", ";
277 s += toString(t.height) + " ) ";
278 return s;
279 }
280
286 template<util::Extent3DConvertibleToString T>
287 inline std::string toString(const T& t) {
288 std::string s = "( ";
289 s += toString(t.width) + ", ";
290 s += toString(t.height) + ", ";
291 s += toString(t.depth) + " ) ";
292 return s;
293 }
294#endif
295
301 template<util::PtrToToStringBasicOrVector T>
302 inline std::string toString(const T& t) {
303 return toString(*t);
304 }
305} // namespace gz
306
307
308
309namespace gz::util {
315 template<typename T>
317 requires (const T& t) {
318 { gz::toString(t) } -> std::same_as<std::string>;
319 };
320
321
322 // CONTAINER
324 template<typename T>
326
327 template<typename T>
328 concept _TypeOrForwardRangeConvertibleToString = _ConvertibleToString<T> || ForwardRangeConvertibleToString<T>;
329
331 template<typename T>
332 concept PairConvertibleToString = !ForwardRangeConvertibleToString<T> and requires(const T& p) {
333 requires _TypeOrForwardRangeConvertibleToString<decltype(p.first)>;
334 requires _TypeOrForwardRangeConvertibleToString<decltype(p.second)>;
335 };
336
338 template<typename T>
339 concept MapConvertibleToString = !PairConvertibleToString<T> and !_TypeOrForwardRangeConvertibleToString<T> and
340 std::ranges::forward_range<T> and PairConvertibleToString<std::ranges::range_reference_t<T>>;
341
342 template<typename T>
343 concept _ContainerTypeConvertibleToString = PairConvertibleToString<T> || ForwardRangeConvertibleToString<T> || MapConvertibleToString<T>;
345
346} // namespace gz::util
347
348
349
350namespace gz {
362 template<util::ForwardRangeConvertibleToString T>
363 std::string toString(const T& t) {
364 std::string s = "[ ";
365 for (auto it = t.begin(); it != t.end(); it++) {
366 s += toString(*it) + ", ";
367 }
368 if (s.size() > 2) {
369 s.erase(s.size() - 2);
370 }
371 s += " ]";
372 return s;
373 }
374
380 template<util::PairConvertibleToString T>
381 inline std::string toString(const T& t) {
382 return "( " + toString(t.first) + ", " + toString(t.second) + " )";
383 }
384
390 template<util::MapConvertibleToString T>
391 std::string toString(const T& t) {
392 std::string s = "{ ";
393 for (const auto& [k, v] : t) {
394 s += toString(k) + ": ";
395 s += toString(v) + ", ";
396 }
397 if (s.size() > 2) {
398 s.erase(s.size() - 2);
399 }
400 s += " }";
401 return s;
402 }
404
408 template<typename T>
410 requires (const T& t) {
411 { gz::toString(t) } -> std::same_as<std::string>;
412 };
413} // namespace gz
414
Any type where gz::toString(t) exists.
Definition: to_string.hpp:409
can construct std::string from T
Definition: to_string.hpp:30
toString function overload exists in global namespace and returns std::string
Definition: to_string.hpp:62
Type having string-convertible width, height members and sizeof(T) == 2 * sizeof(width)
Definition: to_string.hpp:113
Type having string-convertible width, height, depth members and sizeof(T) == 3 * sizeof(width)
Definition: to_string.hpp:121
Forward range having string-convertible elements.
Definition: to_string.hpp:325
has .to_string() const member
Definition: to_string.hpp:45
has .toString() const member
Definition: to_string.hpp:38
Satisfied when T is in PackTypes.
Definition: concepts.hpp:9
Container having string-convertible pairs.
Definition: to_string.hpp:339
Pair having string-convertible elements.
Definition: to_string.hpp:332
same as std::string, std::string_view or const char*
Definition: to_string.hpp:26
Type having string-convertible x, y members and sizeof(T) == 2 * sizeof(x)
Definition: to_string.hpp:85
Type having string-convertible x, y, z members and sizeof(T) == 3 * sizeof(x)
Definition: to_string.hpp:93
Type having string-convertible x, y, z, w members and sizeof(T) == 4 * sizeof(x)
Definition: to_string.hpp:102
works with std::to_string()
Definition: to_string.hpp:53
Everything that is convertible to string with toString at this point.
Definition: to_string.hpp:316
std::string toString(const T &s)
Declaration of toString in global namespace, so that concepts can use it.