out.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*=============================================================================
  2. Copyright (c) 1999-2003 Jaakko Jarvi
  3. Copyright (c) 1999-2003 Jeremiah Willcock
  4. Copyright (c) 2001-2011 Joel de Guzman
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #if !defined(FUSION_OUT_05052005_0121)
  9. #define FUSION_OUT_05052005_0121
  10. #include <boost/fusion/support/config.hpp>
  11. #include <ostream>
  12. #include <boost/fusion/sequence/io/detail/manip.hpp>
  13. #include <boost/mpl/bool.hpp>
  14. #include <boost/fusion/sequence/intrinsic/begin.hpp>
  15. #include <boost/fusion/sequence/intrinsic/end.hpp>
  16. #include <boost/fusion/iterator/deref.hpp>
  17. #include <boost/fusion/iterator/next.hpp>
  18. #include <boost/fusion/iterator/equal_to.hpp>
  19. namespace boost { namespace fusion { namespace detail
  20. {
  21. template <typename Tag>
  22. struct delimiter_out
  23. {
  24. // print a delimiter
  25. template <typename OS>
  26. static void
  27. print(OS& os, char const* delim, mpl::false_ = mpl::false_())
  28. {
  29. detail::string_ios_manip<Tag, OS> manip(os);
  30. manip.print(delim);
  31. }
  32. template <typename OS>
  33. static void
  34. print(OS&, char const*, mpl::true_)
  35. {
  36. }
  37. };
  38. struct print_sequence_loop
  39. {
  40. template <typename OS, typename First, typename Last>
  41. static void
  42. call(OS&, First const&, Last const&, mpl::true_)
  43. {
  44. }
  45. template <typename OS, typename First, typename Last>
  46. static void
  47. call(OS& os, First const& first, Last const& last, mpl::false_)
  48. {
  49. result_of::equal_to<
  50. typename result_of::next<First>::type
  51. , Last
  52. >
  53. is_last;
  54. os << *first;
  55. delimiter_out<tuple_delimiter_tag>::print(os, " ", is_last);
  56. call(os, fusion::next(first), last, is_last);
  57. }
  58. template <typename OS, typename First, typename Last>
  59. static void
  60. call(OS& os, First const& first, Last const& last)
  61. {
  62. result_of::equal_to<First, Last> eq;
  63. call(os, first, last, eq);
  64. }
  65. };
  66. template <typename OS, typename Sequence>
  67. inline void
  68. print_sequence(OS& os, Sequence const& seq)
  69. {
  70. delimiter_out<tuple_open_tag>::print(os, "(");
  71. print_sequence_loop::call(os, fusion::begin(seq), fusion::end(seq));
  72. delimiter_out<tuple_close_tag>::print(os, ")");
  73. }
  74. }}}
  75. #endif