blob: 7fdb8183e7cf58f6e89276e999fb87b202c6f05a [file] [log] [blame]
Jim Flynn6217c3d2022-06-14 10:58:23 +01001//
2// Copyright (c) MapBox All rights reserved.
3// SPDX-License-Identifier: BSD-3-Clause
4//
5
James Ward22a4e152020-09-25 11:43:21 +01006#ifndef MAPBOX_UTIL_VARIANT_IO_HPP
7#define MAPBOX_UTIL_VARIANT_IO_HPP
8
9#include <iosfwd>
10
11#include <mapbox/variant.hpp>
12
13namespace mapbox {
14namespace util {
15
16namespace detail {
17// operator<< helper
18template <typename Out>
19class printer
20{
21public:
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
33private:
34 Out& out_;
35};
36}
37
38// operator<<
39template <typename CharT, typename Traits, typename... Types>
40VARIANT_INLINE std::basic_ostream<CharT, Traits>&
41operator<<(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