variant_io.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //-----------------------------------------------------------------------------
  2. // boost variant/detail/variant_io.hpp header file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2002-2003
  7. // Eric Friedman, Itay Maman
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_VARIANT_DETAIL_VARIANT_IO_HPP
  13. #define BOOST_VARIANT_DETAIL_VARIANT_IO_HPP
  14. #include <iosfwd> // for std::basic_ostream forward declare
  15. #include <boost/variant/variant_fwd.hpp>
  16. #include <boost/variant/static_visitor.hpp>
  17. namespace boost {
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // function template operator<<
  20. //
  21. // Outputs the content of the given variant to the given ostream.
  22. //
  23. // forward declare (allows output of embedded variant< variant< ... >, ... >)
  24. template <class CharT, class Trait, typename... U>
  25. inline std::basic_ostream<CharT, Trait>& operator<<(
  26. std::basic_ostream<CharT, Trait>& out, const variant<U...>& rhs
  27. );
  28. namespace detail { namespace variant {
  29. template <typename OStream>
  30. class printer
  31. : public boost::static_visitor<>
  32. {
  33. private: // representation
  34. OStream& out_;
  35. public: // structors
  36. explicit printer(OStream& out)
  37. : out_( out )
  38. {
  39. }
  40. public: // visitor interface
  41. template <typename T>
  42. void operator()(const T& operand) const
  43. {
  44. out_ << operand;
  45. }
  46. private:
  47. printer& operator=(const printer&);
  48. };
  49. }} // namespace detail::variant
  50. template <class CharT, class Trait, typename... U>
  51. inline std::basic_ostream<CharT, Trait>& operator<<(
  52. std::basic_ostream<CharT, Trait>& out, const variant<U...>& rhs
  53. )
  54. {
  55. detail::variant::printer<
  56. std::basic_ostream<CharT, Trait>
  57. > visitor(out);
  58. rhs.apply_visitor(visitor);
  59. return out;
  60. }
  61. } // namespace boost
  62. #endif // BOOST_VARIANT_DETAIL_VARIANT_IO_HPP