gtest_variant_printers.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. template <std::size_t I, typename... Ts>
  22. void PrintTo(variant<Ts...> const & value, std::ostream * os) {
  23. if (value.index() == I) { (*os) << value.template get<I>(); }
  24. }
  25. template <typename... Ts, std::size_t... Is>
  26. void PrintTo(variant<Ts...> const & value, std::ostream * os,
  27. std::index_sequence<Is...>) {
  28. [[maybe_unused]] auto l = {(PrintTo<Is>(value, os), 0)...};
  29. }
  30. template <typename... Ts>
  31. void PrintTo(variant<Ts...> const & value, std::ostream * os) {
  32. if (value.valid()) {
  33. PrintTo(value, os, std::make_index_sequence<sizeof...(Ts)>());
  34. } else {
  35. (*os) << "<invalid>";
  36. }
  37. }