auto.h 1.7 KB

12345678910111213141516171819202122
  1. #pragma once
  2. #include <util/generic/va_args.h>
  3. // int a = 1, b = 2; Cout << LabeledDump(a, b, 1 + 2); yields {"a": 1, "b": 2, "1 + 2": 3}
  4. #define LabeledDump(...) \
  5. '{' Y_PASS_VA_ARGS(Y_MAP_ARGS_WITH_LAST(__LABELED_DUMP_NONLAST__, __LABELED_DUMP_IMPL__, __VA_ARGS__)) << '}'
  6. #define __LABELED_DUMP_IMPL__(x) << "\"" #x "\": " << DbgDump(x)
  7. #define __LABELED_DUMP_NONLAST__(x) __LABELED_DUMP_IMPL__(x) << ", "
  8. // Usage: struct TMyStruct { int A, B; }; DEFINE_DUMPER(TMyStruct, A, B); Cout << TMyStruct{3, 4};
  9. // yields {"A": 3, "B": 4}
  10. #define DEFINE_DUMPER(C, ...) \
  11. template <> \
  12. struct TDumper<C> { \
  13. template <class S> \
  14. static inline void Dump(S& s, const C& v) { \
  15. s << DumpRaw("{") Y_PASS_VA_ARGS(Y_MAP_ARGS_WITH_LAST(__DEFINE_DUMPER_NONLAST__, __DEFINE_DUMPER_IMPL__, __VA_ARGS__)) << DumpRaw("}"); \
  16. } \
  17. };
  18. #define __DEFINE_DUMPER_IMPL__(x) << DumpRaw("\"" #x "\": ") << v.x
  19. #define __DEFINE_DUMPER_NONLAST__(x) __DEFINE_DUMPER_IMPL__(x) << DumpRaw(", ")