|
9 | 9 | #include <tuple> |
10 | 10 | #include <type_traits> |
11 | 11 | #include <utility> |
| 12 | +#include <vector> |
12 | 13 |
|
13 | 14 | #if __has_include(<source_location>) |
14 | 15 | #include <source_location> |
@@ -347,8 +348,35 @@ namespace detail |
347 | 348 | #elif defined(_MSC_VER) |
348 | 349 | #endif |
349 | 350 |
|
| 351 | + template <typename... Ts, typename F> |
| 352 | + constexpr void enumerate_types(F&& f) |
| 353 | + { |
| 354 | + [&f]<auto... Is>(std::index_sequence<Is...>) { |
| 355 | + (f.template operator()<Ts, Is>(), ...); |
| 356 | + }(std::index_sequence_for<Ts...> {}); |
| 357 | + } |
| 358 | + |
| 359 | + template <auto... Xs, typename F> |
| 360 | + constexpr void for_values(F&& f) |
| 361 | + { |
| 362 | + (f.template operator()<Xs>(), ...); |
| 363 | + } |
| 364 | + |
| 365 | + template <typename T> |
| 366 | + constexpr bool can_be_formatted = std::is_arithmetic_v<T> || std::is_convertible_v<T, std::string>; |
| 367 | + |
350 | 368 | } // namespace detail |
351 | 369 |
|
| 370 | +template <auto B, auto E, typename F> |
| 371 | +constexpr void template_for(F&& f) |
| 372 | +{ |
| 373 | + using t = std::common_type_t<decltype(B), decltype(E)>; |
| 374 | + |
| 375 | + [&f]<auto... Xs>(std::integer_sequence<t, Xs...>) { |
| 376 | + detail::for_values<(B + Xs)...>(f); |
| 377 | + }(std::make_integer_sequence<t, E - B> {}); |
| 378 | +} |
| 379 | + |
352 | 380 | template <auto P> |
353 | 381 | requires(std::is_member_pointer_v<decltype(P)>) |
354 | 382 | consteval std::string_view GetName() |
@@ -392,30 +420,55 @@ consteval auto GetName() |
392 | 420 | #endif |
393 | 421 | } |
394 | 422 |
|
| 423 | +template <typename Object, typename Callable> |
| 424 | +decltype(auto) CallOnMembers(Object const& object, Callable&& callable) |
| 425 | +{ |
| 426 | + template_for<0, Reflection::CountMembers<Object>>( |
| 427 | + [&]<auto I>() { callable(Reflection::MemberNameOf<I, Object>, std::get<I>(Reflection::ToTuple(object))); }); |
| 428 | +} |
| 429 | + |
395 | 430 | template <typename Object> |
396 | 431 | std::string Inspect(Object const& object) |
397 | 432 | { |
398 | | - return [&]<size_t... I>(std::index_sequence<I...>) { |
399 | | - std::string str; |
400 | | - auto const onMember = [&str]<typename Name, typename Value>(Name&& name, Value&& value) { |
401 | | - auto const InspectValue = [&str]<typename T>(T&& arg) { |
402 | | - // clang-format off |
403 | | - if constexpr (std::is_convertible_v<T, std::string> |
| 433 | + std::string str; |
| 434 | + auto const onMember = [&str]<typename Name, typename Value>(Name&& name, Value&& value) { |
| 435 | + auto const InspectValue = [&str]<typename T>(T&& arg) { |
| 436 | + // clang-format off |
| 437 | + if constexpr (std::is_convertible_v<T, std::string> |
404 | 438 | || std::is_convertible_v<T, std::string_view> |
405 | 439 | || std::is_convertible_v<T, char const*>) // clang-format on |
406 | | - str += std::format("\"{}\"", arg); |
407 | | - else |
408 | | - str += std::format("{}", arg); |
409 | | - }; |
410 | | - if (!str.empty()) |
411 | | - str += ' '; |
412 | | - str += name; |
413 | | - str += '='; |
414 | | - InspectValue(value); |
| 440 | + { |
| 441 | + str += std::format("\"{}\"", arg); |
| 442 | + } |
| 443 | + else if constexpr (std::is_convertible_v<T, int>) // use std::formattable when available |
| 444 | + { |
| 445 | + str += std::format("{}", arg); |
| 446 | + } |
| 447 | + else |
| 448 | + { |
| 449 | + str += Inspect(arg); |
| 450 | + } |
415 | 451 | }; |
416 | | - (onMember(MemberNameOf<I, Object>, std::get<I>(Reflection::ToTuple(object))), ...); |
417 | | - return str; |
418 | | - }(std::make_index_sequence<Reflection::CountMembers<Object>> {}); |
| 452 | + if (!str.empty()) |
| 453 | + str += ' '; |
| 454 | + str += name; |
| 455 | + str += '='; |
| 456 | + InspectValue(value); |
| 457 | + }; |
| 458 | + |
| 459 | + CallOnMembers(object, onMember); |
| 460 | + return str; |
419 | 461 | } |
420 | 462 |
|
| 463 | +template <typename Object> |
| 464 | +std::string Inspect(std::vector<Object> const& objects) |
| 465 | +{ |
| 466 | + std::string str; |
| 467 | + for (auto const& object: objects) |
| 468 | + { |
| 469 | + str += Inspect(object); |
| 470 | + str += '\n'; |
| 471 | + } |
| 472 | + return str; |
| 473 | +} |
421 | 474 | } // namespace Reflection |
0 commit comments