layout.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright Nat Goodspeed 2013.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <vector>
  8. #include <string>
  9. #include <utility>
  10. #include <boost/coroutine2/all.hpp>
  11. struct FinalEOL{
  12. ~FinalEOL(){
  13. std::cout << std::endl;
  14. }
  15. };
  16. int main(int argc,char* argv[]){
  17. using std::begin;
  18. using std::end;
  19. std::vector<std::string> words{
  20. "peas", "porridge", "hot", "peas",
  21. "porridge", "cold", "peas", "porridge",
  22. "in", "the", "pot", "nine",
  23. "days", "old" };
  24. int num=5,width=15;
  25. boost::coroutines2::coroutine<std::string>::push_type writer(
  26. [&](boost::coroutines2::coroutine<std::string>::pull_type& in){
  27. // finish the last line when we leave by whatever means
  28. FinalEOL eol;
  29. // pull values from upstream, lay them out 'num' to a line
  30. for (;;){
  31. for(int i=0;i<num;++i){
  32. // when we exhaust the input, stop
  33. if(!in) return;
  34. std::cout << std::setw(width) << in.get();
  35. // now that we've handled this item, advance to next
  36. in();
  37. }
  38. // after 'num' items, line break
  39. std::cout << std::endl;
  40. }
  41. });
  42. std::copy(begin(words),end(words),begin(writer));
  43. std::cout << "\nDone";
  44. return EXIT_SUCCESS;
  45. }