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