mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2025-08-03 12:49:50 +00:00
add variant/io
This commit is contained in:
parent
410a8af026
commit
5cb070b3b3
10 changed files with 403 additions and 244 deletions
40
sprout/variant/io.hpp
Normal file
40
sprout/variant/io.hpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
#ifndef SPROUT_VARIANT_IO_HPP
|
||||
#define SPROUT_VARIANT_IO_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/variant/variant.hpp>
|
||||
#include <sprout/variant/static_visitor.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<typename OStream>
|
||||
class variant_output_visitor
|
||||
: public sprout::static_visitor<>
|
||||
{
|
||||
public:
|
||||
typedef OStream ostream_type;
|
||||
private:
|
||||
ostream_type& out_;
|
||||
public:
|
||||
explicit variant_output_visitor(ostream_type& out)
|
||||
: out_(out)
|
||||
{}
|
||||
template<typename T>
|
||||
void operator()(T const& operand) const {
|
||||
out_ << operand;
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
//
|
||||
// operator<<
|
||||
//
|
||||
template<typename Elem, typename Traits, typename... Types>
|
||||
inline std::basic_ostream<Elem, Traits>&
|
||||
operator<<(std::basic_ostream<Elem, Traits>& lhs, sprout::variant<Types...> const& rhs) {
|
||||
sprout::detail::variant_output_visitor<std::basic_ostream<Elem, Traits> > visitor(lhs);
|
||||
rhs.apply_visitor(visitor);
|
||||
return lhs;
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_VARIANT_IO_HPP
|
Loading…
Add table
Add a link
Reference in a new issue