segmented_fold.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2011 Eric Niebler
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <string>
  8. #include <sstream>
  9. #include <iostream>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <boost/fusion/container/vector/vector.hpp>
  12. #include <boost/fusion/algorithm/iteration/fold.hpp>
  13. #include <boost/fusion/container/generation/make_vector.hpp>
  14. #include "../sequence/tree.hpp"
  15. struct write_string
  16. {
  17. typedef std::ostream* result_type;
  18. template<typename T>
  19. std::ostream* operator()(std::ostream* sout, T const& t) const
  20. {
  21. return &(*sout << t << " ");
  22. }
  23. };
  24. template<typename Tree>
  25. void
  26. process_tree(Tree const &tree)
  27. {
  28. using namespace boost;
  29. std::stringstream str;
  30. fusion::fold(tree, &str, write_string());
  31. std::string res = str.str();
  32. BOOST_TEST_EQ(res, "a b c 1 2 3 100 e f 0 B 1 h i 4 5 6 j k l ");
  33. }
  34. int
  35. main()
  36. {
  37. using namespace boost::fusion;
  38. process_tree(
  39. make_tree(
  40. make_vector(double(0),'B')
  41. , make_tree(
  42. make_vector(1,2,long(3))
  43. , make_tree(make_vector('a','b','c'))
  44. , make_tree(make_vector(short('d'),'e','f'))
  45. )
  46. , make_tree(
  47. make_vector(4,5,6)
  48. , make_tree(make_vector(float(1),'h','i'))
  49. , make_tree(make_vector('j','k','l'))
  50. )
  51. )
  52. );
  53. return boost::report_errors();
  54. }