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