variant-inl.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef VARIANT_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include variant.h"
  3. // For the sake of sane code completion.
  4. #include "variant.h"
  5. #endif
  6. #include <type_traits>
  7. namespace NYT {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. namespace NDetail {
  10. template <size_t Index, class... Ts>
  11. struct TVariantFormatter;
  12. template <size_t Index>
  13. struct TVariantFormatter<Index>
  14. {
  15. template <class TVariant>
  16. static void Do(TStringBuilderBase* /*builder*/, const TVariant& /*variant*/, TStringBuf /*spec*/)
  17. { }
  18. };
  19. template <size_t Index, class T, class... Ts>
  20. struct TVariantFormatter<Index, T, Ts...>
  21. {
  22. template <class TVariant>
  23. static void Do(TStringBuilderBase* builder, const TVariant& variant, TStringBuf spec)
  24. {
  25. if (variant.index() == Index) {
  26. FormatValue(builder, std::get<Index>(variant), spec);
  27. } else {
  28. TVariantFormatter<Index + 1, Ts...>::Do(builder, variant, spec);
  29. }
  30. }
  31. };
  32. } // namespace NDetail
  33. template <class... Ts>
  34. void FormatValue(TStringBuilderBase* builder, const std::variant<Ts...>& variant, TStringBuf spec)
  35. {
  36. NDetail::TVariantFormatter<0, Ts...>::Do(builder, variant, spec);
  37. }
  38. template <class... Ts>
  39. TString ToString(const std::variant<Ts...>& variant)
  40. {
  41. return ToStringViaBuilder(variant);
  42. }
  43. ////////////////////////////////////////////////////////////////////////////////
  44. } // namespace NYT