Commit 4851be6
authored
Add ToString function for array and tuple (#4584)
## Summary
This PR adds a `ToString` function that can be used with scalars, arrays
and tuples to write error messages or when debugging.
TODO: write a doc comment for `ToString`
It might be useful to add compatibility for BaseFab and PODVector with
automatic dtoh memcpy.
Example (https://godbolt.org/z/vsn6Krbsj)
```C++
std::vector v{4., 7., 23., 0.000001, 7.123456789123456789, 8., 3., 0.0000011};
std::tuple t{443, 0.234423, 244.4f, "Test tuple"};
std::array a{4., 7., 23., 0.000001, 7.12345, 8., 3., 0.0000011};
std::map<int, std::vector<double>> m{{0, {32.42, 1.000324}},
{4, {12.3, 2.65775, 3.24}}};
int b[]{2, 3, 65, 3, 6, 54};
const char* c = "Test char *";
char e[] = "Test char array";
std::cout << amrex::ToString(v) << '\n';
std::cout << amrex::ToString(t, "[", ",", "]", "'", 100,std::ostringstream{} << std::setprecision(4)) << '\n';
std::cout << amrex::ToString(a, "{", " ; ", "}", "\"", 3, std::ostringstream{} << std::setprecision(10)) << '\n';
std::cout << amrex::ToString(m, "<", " | ", ">", "\"", 100, std::ostringstream{} << std::setprecision(10))<< '\n';
std::cout << amrex::ToString(b) << '\n';
std::cout << amrex::ToString(c) << '\n';
std::cout << amrex::ToString(e) << '\n';
std::cout << amrex::ToString("Test direct string") << '\n';
std::cout << amrex::ToString(std::string("Test string")) << '\n';
std::cout << amrex::ToString(std::array{"awdawd", "43tf4", "awd4t45"}) << '\n';
std::cout << amrex::ToString(std::array{'t', 'e', 's', 't'}) << '\n';
std::cout << '\n';
amrex::ToString(std::cout, v) << '\n';
amrex::ToString(std::cout << std::setprecision(4), t, "[", ",", "]", "'", 100) << '\n';
amrex::ToString(std::cout << std::setprecision(10), a, "{", " ; ", "}", "\"", 3) << '\n';
amrex::ToString(std::cout << std::setprecision(10), m, "<", " | ", ">", "\"", 100) << '\n';
amrex::ToString(std::cout, b) << '\n';
amrex::ToString(std::cout, c) << '\n';
amrex::ToString(std::cout, e) << '\n';
amrex::ToString(std::cout, "Test direct string") << '\n';
amrex::ToString(std::cout, std::string("Test string")) << '\n';
amrex::ToString(std::cout, std::array{"awdawd", "43tf4", "awd4t45"}) << '\n';
amrex::ToString(std::cout, std::array{'t', 'e', 's', 't'}) << '\n';
```
Prints
```python
[4, 7, 23, 1e-06, 7.12345, 8, 3, 1.1e-06]
[443,0.2344,244.4,'Test tuple']
{4 ; 7 ; 23 ; ...}
<<0 | <32.42 | 1.000324>> | <4 | <12.3 | 2.65775 | 3.24>>>
[2, 3, 65, 3, 6, 54]
"Test char *"
"Test char array"
"Test direct string"
"Test string"
["awdawd", "43tf4", "awd4t45"]
[t, e, s, t]
[4, 7, 23, 1e-06, 7.12345, 8, 3, 1.1e-06]
[443,0.2344,244.4,'Test tuple']
{4 ; 7 ; 23 ; ...}
<<0 | <32.42 | 1.000324>> | <4 | <12.3 | 2.65775 | 3.24>>>
[2, 3, 65, 3, 6, 54]
"Test char *"
"Test char array"
"Test direct string"
"Test string"
["awdawd", "43tf4", "awd4t45"]
[t, e, s, t]
```1 parent 8cfb8b6 commit 4851be6
1 file changed
+127
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
| 30 | + | |
| 31 | + | |
29 | 32 | | |
30 | 33 | | |
| 34 | + | |
31 | 35 | | |
32 | 36 | | |
33 | 37 | | |
| |||
220 | 224 | | |
221 | 225 | | |
222 | 226 | | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
223 | 245 | | |
224 | 246 | | |
225 | 247 | | |
| |||
380 | 402 | | |
381 | 403 | | |
382 | 404 | | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
383 | 510 | | |
0 commit comments