1 | // Test case for flushing a filtering_ostream based on the
|
---|
2 | // back_inserter example in
|
---|
3 | // <http://www.boost.org/doc/libs/1_44_0/libs/iostreams/doc/tutorial/container_sink.html>
|
---|
4 | // augmented by a transparent output filter simplified from the
|
---|
5 | // example given in
|
---|
6 | // <http://www.boost.org/doc/libs/1_44_0/libs/iostreams/doc/concepts/output_filter.html>.
|
---|
7 | //
|
---|
8 | // Flushing a filtering_ostream using the flush() member function
|
---|
9 | // stopped working in Boost 1.44. The string the back_inserter writes
|
---|
10 | // to stays empty. A valid workaround seems to call strict_sync(), as
|
---|
11 | // it leads to the expected result. The member function sync()
|
---|
12 | // documented in
|
---|
13 | // <http://www.boost.org/doc/libs/1_44_0/libs/iostreams/doc/classes/filtering_stream.html>
|
---|
14 | // does not seem to exist.
|
---|
15 | //
|
---|
16 |
|
---|
17 | #include <cassert>
|
---|
18 | #include <string>
|
---|
19 | #include <boost/iostreams/categories.hpp>
|
---|
20 | #include <boost/iostreams/operations.hpp>
|
---|
21 | #include <boost/iostreams/device/back_inserter.hpp>
|
---|
22 | #include <boost/iostreams/filtering_stream.hpp>
|
---|
23 |
|
---|
24 | #include <iostream>
|
---|
25 |
|
---|
26 | namespace io = boost::iostreams;
|
---|
27 |
|
---|
28 | // Simple output filter forwarding just forwarding the put chars to
|
---|
29 | // the sink.
|
---|
30 | struct transparent_output_filter {
|
---|
31 | typedef char char_type;
|
---|
32 | typedef io::output_filter_tag category;
|
---|
33 |
|
---|
34 | template<typename Sink>
|
---|
35 | bool put(Sink& snk, char c)
|
---|
36 | {
|
---|
37 | return io::put(snk, c);
|
---|
38 | }
|
---|
39 | };
|
---|
40 |
|
---|
41 | int main(int argc, char *argv[])
|
---|
42 | {
|
---|
43 | using namespace std;
|
---|
44 |
|
---|
45 | string result;
|
---|
46 |
|
---|
47 | io::filtering_stream<io::output> out;
|
---|
48 |
|
---|
49 | // Transparent filter just forward the characters to the sink. If it
|
---|
50 | // is left out from the filter chain, out.flush() works correctly
|
---|
51 | // and result has the expected value.
|
---|
52 | out.push(transparent_output_filter());
|
---|
53 |
|
---|
54 | // Sink appends characters to string result.
|
---|
55 | out.push(io::back_inserter(result));
|
---|
56 |
|
---|
57 | // Write test string to filtering_ostream.
|
---|
58 | out << "Hello World!";
|
---|
59 |
|
---|
60 | // The flush only works in Boost 1.44 if there's only the sink
|
---|
61 | // device in the chain. Until Boost 1.43 this used to work.
|
---|
62 | out.flush();
|
---|
63 |
|
---|
64 | // The documented sync() member function leads to a compile error
|
---|
65 | // "‘class
|
---|
66 | // boost::iostreams::filtering_stream<boost::iostreams::output,
|
---|
67 | // char, std::char_traits<char>, std::allocator<char>,
|
---|
68 | // boost::iostreams::public_>’ has no member named ‘sync’".
|
---|
69 | //out.sync();
|
---|
70 |
|
---|
71 | // Calling strict_sync() instead of flush() or sync() results in the
|
---|
72 | // characters to be appended to the result string also in Boost 1.44.
|
---|
73 | //out.strict_sync();
|
---|
74 |
|
---|
75 | // Check result.
|
---|
76 | cout << "result = \"" << result << "\"" << endl;
|
---|
77 | assert(result == "Hello World!");
|
---|
78 |
|
---|
79 | return 0;
|
---|
80 | }
|
---|