variant_io.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/detail/templated_streams.hpp>
  17. #include <boost/variant/static_visitor.hpp>
  18. namespace boost {
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // function template operator<<
  21. //
  22. // Outputs the content of the given variant to the given ostream.
  23. //
  24. // forward declare (allows output of embedded variant< variant< ... >, ... >)
  25. template <
  26. BOOST_TEMPLATED_STREAM_ARGS(E,T)
  27. BOOST_TEMPLATED_STREAM_COMMA
  28. BOOST_VARIANT_ENUM_PARAMS(typename U)
  29. >
  30. inline BOOST_TEMPLATED_STREAM(ostream, E,T)& operator<<(
  31. BOOST_TEMPLATED_STREAM(ostream, E,T)& out
  32. , const variant< BOOST_VARIANT_ENUM_PARAMS(U) >& rhs
  33. );
  34. namespace detail { namespace variant {
  35. template <typename OStream>
  36. class printer
  37. : public boost::static_visitor<>
  38. {
  39. private: // representation
  40. OStream& out_;
  41. public: // structors
  42. explicit printer(OStream& out)
  43. : out_( out )
  44. {
  45. }
  46. public: // visitor interface
  47. template <typename T>
  48. void operator()(const T& operand) const
  49. {
  50. out_ << operand;
  51. }
  52. private:
  53. printer& operator=(const printer&);
  54. };
  55. }} // namespace detail::variant
  56. template <
  57. BOOST_TEMPLATED_STREAM_ARGS(E,T)
  58. BOOST_TEMPLATED_STREAM_COMMA
  59. BOOST_VARIANT_ENUM_PARAMS(typename U)
  60. >
  61. inline BOOST_TEMPLATED_STREAM(ostream, E,T)& operator<<(
  62. BOOST_TEMPLATED_STREAM(ostream, E,T)& out
  63. , const variant< BOOST_VARIANT_ENUM_PARAMS(U) >& rhs
  64. )
  65. {
  66. detail::variant::printer<
  67. BOOST_TEMPLATED_STREAM(ostream, E,T)
  68. > visitor(out);
  69. rhs.apply_visitor(visitor);
  70. return out;
  71. }
  72. } // namespace boost
  73. #endif // BOOST_VARIANT_DETAIL_VARIANT_IO_HPP