James Ward | 22a4e15 | 2020-09-25 11:43:21 +0100 | [diff] [blame] | 1 | #ifndef MAPBOX_UTIL_VARIANT_IO_HPP |
| 2 | #define MAPBOX_UTIL_VARIANT_IO_HPP |
| 3 | |
| 4 | #include <iosfwd> |
| 5 | |
| 6 | #include <mapbox/variant.hpp> |
| 7 | |
| 8 | namespace mapbox { |
| 9 | namespace util { |
| 10 | |
| 11 | namespace detail { |
| 12 | // operator<< helper |
| 13 | template <typename Out> |
| 14 | class printer |
| 15 | { |
| 16 | public: |
| 17 | explicit printer(Out& out) |
| 18 | : out_(out) {} |
| 19 | printer& operator=(printer const&) = delete; |
| 20 | |
| 21 | // visitor |
| 22 | template <typename T> |
| 23 | void operator()(T const& operand) const |
| 24 | { |
| 25 | out_ << operand; |
| 26 | } |
| 27 | |
| 28 | private: |
| 29 | Out& out_; |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | // operator<< |
| 34 | template <typename CharT, typename Traits, typename... Types> |
| 35 | VARIANT_INLINE std::basic_ostream<CharT, Traits>& |
| 36 | operator<<(std::basic_ostream<CharT, Traits>& out, variant<Types...> const& rhs) |
| 37 | { |
| 38 | detail::printer<std::basic_ostream<CharT, Traits>> visitor(out); |
| 39 | apply_visitor(visitor, rhs); |
| 40 | return out; |
| 41 | } |
| 42 | } // namespace util |
| 43 | } // namespace mapbox |
| 44 | |
| 45 | #endif // MAPBOX_UTIL_VARIANT_IO_HPP |