blob: 1456cc5abc787c87eadd683415960d07e0120186 [file] [log] [blame]
James Ward22a4e152020-09-25 11:43:21 +01001#ifndef MAPBOX_UTIL_VARIANT_IO_HPP
2#define MAPBOX_UTIL_VARIANT_IO_HPP
3
4#include <iosfwd>
5
6#include <mapbox/variant.hpp>
7
8namespace mapbox {
9namespace util {
10
11namespace detail {
12// operator<< helper
13template <typename Out>
14class printer
15{
16public:
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
28private:
29 Out& out_;
30};
31}
32
33// operator<<
34template <typename CharT, typename Traits, typename... Types>
35VARIANT_INLINE std::basic_ostream<CharT, Traits>&
36operator<<(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