Skip to content

Commit

Permalink
move operator<< into separate header <variant_io.hpp>
Browse files Browse the repository at this point in the history
  • Loading branch information
artemp committed Jan 27, 2015
1 parent f87a1cf commit 24dcab2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
1 change: 1 addition & 0 deletions test/binary_visitor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <boost/variant.hpp>
#include <boost/timer/timer.hpp>
#include "variant.hpp"
#include "variant_io.hpp"

using namespace mapbox;

Expand Down
2 changes: 1 addition & 1 deletion test/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "catch.hpp"

#include "variant.hpp"

#include "variant_io.hpp"
#include <algorithm>
#include <cstdint>
#include <iterator>
Expand Down
28 changes: 0 additions & 28 deletions variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,24 +497,6 @@ class comparer
Variant const& lhs_;
};

// operator<< helper
template <typename Out>
class printer
{
public:
explicit printer(Out & out)
: out_(out) {}
printer& operator=(printer const&) = delete;

// visitor
template <typename T>
void operator()(T const& operand) const
{
out_ << operand;
}
private:
Out & out_;
};

} // namespace detail

Expand Down Expand Up @@ -768,16 +750,6 @@ ResultType const& get(T const& var)
}


// operator<<
template <typename charT, typename traits, typename... Types>
VARIANT_INLINE std::basic_ostream<charT, traits>&
operator<< (std::basic_ostream<charT, traits>& out, variant<Types...> const& rhs)
{
detail::printer<std::basic_ostream<charT, traits>> visitor(out);
apply_visitor(visitor, rhs);
return out;
}

}}

#endif // MAPBOX_UTIL_VARIANT_HPP
39 changes: 39 additions & 0 deletions variant_io.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef MAPBOX_UTIL_VARIANT_IO_HPP
#define MAPBOX_UTIL_VARIANT_IO_HPP

namespace mapbox { namespace util {

namespace detail {
// operator<< helper
template <typename Out>
class printer
{
public:
explicit printer(Out & out)
: out_(out) {}
printer& operator=(printer const&) = delete;

// visitor
template <typename T>
void operator()(T const& operand) const
{
out_ << operand;
}
private:
Out & out_;
};
}

// operator<<
template <typename charT, typename traits, typename... Types>
VARIANT_INLINE std::basic_ostream<charT, traits>&
operator<< (std::basic_ostream<charT, traits>& out, variant<Types...> const& rhs)
{
detail::printer<std::basic_ostream<charT, traits>> visitor(out);
apply_visitor(visitor, rhs);
return out;
}

}}

#endif //MAPBOX_UTIL_VARIANT_IO_HPP

0 comments on commit 24dcab2

Please sign in to comment.