format_pointer_container.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (c) 2009 Matthias Vallentin
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/config/warning_disable.hpp>
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/iterator/indirect_iterator.hpp>
  8. #include <boost/make_shared.hpp>
  9. #include <boost/noncopyable.hpp>
  10. #include <boost/spirit/include/karma.hpp>
  11. #include <boost/spirit/include/karma_format.hpp>
  12. #include <sstream>
  13. #include <string>
  14. #include <vector>
  15. struct foo : boost::noncopyable
  16. {
  17. foo()
  18. : str("foo")
  19. {
  20. }
  21. std::string str;
  22. };
  23. template <typename Stream>
  24. Stream& operator<<(Stream& out, const foo& f)
  25. {
  26. out << f.str;
  27. return out;
  28. }
  29. int main()
  30. {
  31. using namespace boost::spirit;
  32. typedef boost::shared_ptr<foo> foo_ptr;
  33. std::vector<foo_ptr> v;
  34. std::size_t i = 10;
  35. while (i--)
  36. v.push_back(boost::make_shared<foo>());
  37. typedef boost::indirect_iterator<std::vector<foo_ptr>::const_iterator>
  38. iterator_type;
  39. std::stringstream strm;
  40. strm
  41. << karma::format(stream % ',',
  42. boost::iterator_range<iterator_type>(
  43. iterator_type(v.begin()), iterator_type(v.end())
  44. )
  45. );
  46. BOOST_TEST(strm.str() == "foo,foo,foo,foo,foo,foo,foo,foo,foo,foo");
  47. return boost::report_errors();
  48. }