File: chaining.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (206 lines) | stat: -rw-r--r-- 7,027 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

//          Copyright Nat Goodspeed 2013.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#include <boost/coroutine/all.hpp>

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <sstream>

#include <boost/bind.hpp>
#include <boost/foreach.hpp>

typedef boost::coroutines::asymmetric_coroutine<std::string> coro_t;

// deliver each line of input stream to sink as a separate string
void readlines(coro_t::push_type& sink, std::istream& in)
{
    std::string line;
    while (std::getline(in, line))
        sink(line);
}

void tokenize(coro_t::push_type& sink, coro_t::pull_type& source)
{
    // This tokenizer doesn't happen to be stateful: you could reasonably
    // implement it with a single call to push each new token downstream. But
    // I've worked with stateful tokenizers, in which the meaning of input
    // characters depends in part on their position within the input line. At
    // the time, I wished for a way to resume at the suspend point!
    BOOST_FOREACH(std::string line, source)
    {
        std::string::size_type pos = 0;
        while (pos < line.length())
        {
            if (line[pos] == '"')
            {
                std::string token;
                ++pos;              // skip open quote
                while (pos < line.length() && line[pos] != '"')
                    token += line[pos++];
                ++pos;              // skip close quote
                sink(token);        // pass token downstream
            }
            else if (std::isspace(line[pos]))
            {
                ++pos;              // outside quotes, ignore whitespace
            }
            else if (std::isalpha(line[pos]))
            {
                std::string token;
                while (pos < line.length() && std::isalpha(line[pos]))
                    token += line[pos++];
                sink(token);        // pass token downstream
            }
            else                    // punctuation
            {
                sink(std::string(1, line[pos++]));
            }
        }
    }
}

void only_words(coro_t::push_type& sink, coro_t::pull_type& source)
{
    BOOST_FOREACH(std::string token, source)
    {
        if (! token.empty() && std::isalpha(token[0]))
            sink(token);
    }
}

void trace(coro_t::push_type& sink, coro_t::pull_type& source)
{
    BOOST_FOREACH(std::string token, source)
    {
        std::cout << "trace: '" << token << "'\n";
        sink(token);
    }
}

struct FinalEOL
{
    ~FinalEOL() { std::cout << std::endl; }
};

void layout(coro_t::pull_type& source, int num, int width)
{
    // Finish the last line when we leave by whatever means
    FinalEOL eol;

    // Pull values from upstream, lay them out 'num' to a line
    for (;;)
    {
        for (int i = 0; i < num; ++i)
        {
            // when we exhaust the input, stop
            if (! source)
                return;

            std::cout << std::setw(width) << source.get();
            // now that we've handled this item, advance to next
            source();
        }
        // after 'num' items, line break
        std::cout << std::endl;
    }
}

int main(int argc, char *argv[])
{
    // For example purposes, instead of having a separate text file in the
    // local filesystem, construct an istringstream to read.
    std::string data(
        "This is the first line.\n"
        "This, the second.\n"
        "The third has \"a phrase\"!\n"
        );

    {
        std::cout << "\nreadlines:\n";
        std::istringstream infile(data);
        // Each coroutine-function has a small, specific job to do. Instead of
        // adding conditional logic to a large, complex input function, the
        // caller composes smaller functions into the desired processing
        // chain.
        coro_t::pull_type reader(boost::bind(readlines, _1, boost::ref(infile)));
        coro_t::pull_type tracer(boost::bind(trace, _1, boost::ref(reader)));
        BOOST_FOREACH(std::string line, tracer)
        {
            std::cout << "got: " << line << "\n";
        }
    }

    {
        std::cout << "\ncompose a chain:\n";
        std::istringstream infile(data);
        coro_t::pull_type reader(boost::bind(readlines, _1, boost::ref(infile)));
        coro_t::pull_type tokenizer(boost::bind(tokenize, _1, boost::ref(reader)));
        coro_t::pull_type tracer(boost::bind(trace, _1, boost::ref(tokenizer)));
        BOOST_FOREACH(std::string token, tracer)
        {
            // just iterate, we're already pulling through tracer
        }
    }

    {
        std::cout << "\nfilter:\n";
        std::istringstream infile(data);
        coro_t::pull_type reader(boost::bind(readlines, _1, boost::ref(infile)));
        coro_t::pull_type tokenizer(boost::bind(tokenize, _1, boost::ref(reader)));
        coro_t::pull_type filter(boost::bind(only_words, _1, boost::ref(tokenizer)));
        coro_t::pull_type tracer(boost::bind(trace, _1, boost::ref(filter)));
        BOOST_FOREACH(std::string token, tracer)
        {
            // just iterate, we're already pulling through tracer
        }
    }

    {
        std::cout << "\nlayout() as coroutine::push_type:\n";
        std::istringstream infile(data);
        coro_t::pull_type reader(boost::bind(readlines, _1, boost::ref(infile)));
        coro_t::pull_type tokenizer(boost::bind(tokenize, _1, boost::ref(reader)));
        coro_t::pull_type filter(boost::bind(only_words, _1, boost::ref(tokenizer)));
        coro_t::push_type writer(boost::bind(layout, _1, 5, 15));
        BOOST_FOREACH(std::string token, filter)
        {
            writer(token);
        }
    }

    {
        std::cout << "\ncalling layout() directly:\n";
        std::istringstream infile(data);
        coro_t::pull_type reader(boost::bind(readlines, _1, boost::ref(infile)));
        coro_t::pull_type tokenizer(boost::bind(tokenize, _1, boost::ref(reader)));
        coro_t::pull_type filter(boost::bind(only_words, _1, boost::ref(tokenizer)));
        // Because of the symmetry of the API, we can directly call layout()
        // instead of using it as a coroutine-function.
        layout(filter, 5, 15);
    }

    {
        std::cout << "\nfiltering output:\n";
        std::istringstream infile(data);
        coro_t::pull_type reader(boost::bind(readlines, _1, boost::ref(infile)));
        coro_t::pull_type tokenizer(boost::bind(tokenize, _1, boost::ref(reader)));
        coro_t::push_type writer(boost::bind(layout, _1, 5, 15));
        // Because of the symmetry of the API, we can use any of these
        // chaining functions in a push_type coroutine chain as well.
        coro_t::push_type filter(boost::bind(only_words, boost::ref(writer), _1));
        BOOST_FOREACH(std::string token, tokenizer)
        {
            filter(token);
        }
    }

    std::cout << "\nDone" << std::endl;

    return EXIT_SUCCESS;
}