gtest_variant_printers.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // gtest_variant_printers.h
  3. // variant
  4. //
  5. // Created by Sam Jaffe on 8/14/20.
  6. // Copyright © 2020 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. template <typename K, typename V>
  10. std::ostream & operator<<(std::ostream & os, std::map<K, V> const & value) {
  11. os << "{";
  12. if (value.size()) {
  13. auto it = value.cbegin();
  14. os << " " << it->first << ":" << it->second;
  15. for (++it; it != value.cend(); ++it) {
  16. os << ", " << it->first << ":" << it->second;
  17. }
  18. }
  19. return os << " }";
  20. }
  21. namespace variant {
  22. template <std::size_t I, typename... Ts>
  23. void PrintTo(variant<Ts...> const & value, std::ostream * os) {
  24. if (value.index() == I) { (*os) << value.template get<I>(); }
  25. }
  26. template <typename... Ts, std::size_t... Is>
  27. void PrintTo(variant<Ts...> const & value, std::ostream * os,
  28. std::index_sequence<Is...>) {
  29. [[maybe_unused]] auto l = {(PrintTo<Is>(value, os), 0)...};
  30. }
  31. template <typename... Ts>
  32. void PrintTo(variant<Ts...> const & value, std::ostream * os) {
  33. if (value.valid()) {
  34. PrintTo(value, os, std::make_index_sequence<sizeof...(Ts)>());
  35. } else {
  36. (*os) << "<invalid>";
  37. }
  38. }
  39. }