]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/coroutine/example/asymmetric/layout.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / coroutine / example / asymmetric / layout.cpp
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/assign/list_of.hpp>
14 #include <boost/bind.hpp>
15 #include <boost/coroutine/all.hpp>
16 #include <boost/range.hpp>
17
18 struct FinalEOL
19 {
20 ~FinalEOL() { std::cout << std::endl; }
21 };
22
23 void layout(boost::coroutines::asymmetric_coroutine<std::string>::pull_type& in, int num, int width)
24 {
25 // Finish the last line when we leave by whatever means
26 FinalEOL eol;
27
28 // Pull values from upstream, lay them out 'num' to a line
29 for (;;)
30 {
31 for (int i = 0; i < num; ++i)
32 {
33 // when we exhaust the input, stop
34 if (! in)
35 return;
36
37 std::cout << std::setw(width) << in.get();
38 // now that we've handled this item, advance to next
39 in();
40 }
41 // after 'num' items, line break
42 std::cout << std::endl;
43 }
44 }
45
46 int main(int argc, char *argv[])
47 {
48 std::vector<std::string> words = boost::assign::list_of
49 ("peas")
50 ("porridge")
51 ("hot")
52 ("peas")
53 ("porridge")
54 ("cold")
55 ("peas")
56 ("porridge")
57 ("in")
58 ("the")
59 ("pot")
60 ("nine")
61 ("days")
62 ("old")
63 ;
64
65 boost::coroutines::asymmetric_coroutine<std::string>::push_type writer(
66 boost::bind(layout, _1, 5, 15));
67
68 std::copy(boost::begin(words), boost::end(words), boost::begin(writer));
69
70 std::cout << "\nDone" << std::endl;
71
72 return EXIT_SUCCESS;
73 }