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