Chris@16
|
1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
|
Chris@16
|
2 // (C) Copyright 2003-2007 Jonathan Turkanis
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
|
Chris@16
|
5
|
Chris@16
|
6 // See http://www.boost.org/libs/iostreams for documentation.
|
Chris@16
|
7
|
Chris@16
|
8 #ifndef BOOST_IOSTREAMS_INVERT_HPP_INCLUDED
|
Chris@16
|
9 #define BOOST_IOSTREAMS_INVERT_HPP_INCLUDED
|
Chris@16
|
10
|
Chris@16
|
11 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
Chris@16
|
12 # pragma once
|
Chris@16
|
13 #endif
|
Chris@16
|
14
|
Chris@16
|
15 #include <algorithm> // copy, min.
|
Chris@16
|
16 #include <boost/assert.hpp>
|
Chris@16
|
17 #include <boost/config.hpp> // BOOST_DEDUCED_TYPENAME.
|
Chris@16
|
18 #include <boost/detail/workaround.hpp> // default_filter_buffer_size.
|
Chris@16
|
19 #include <boost/iostreams/char_traits.hpp>
|
Chris@16
|
20 #include <boost/iostreams/compose.hpp>
|
Chris@16
|
21 #include <boost/iostreams/constants.hpp>
|
Chris@16
|
22 #include <boost/iostreams/device/array.hpp>
|
Chris@16
|
23 #include <boost/iostreams/detail/buffer.hpp>
|
Chris@16
|
24 #include <boost/iostreams/detail/counted_array.hpp>
|
Chris@16
|
25 #include <boost/iostreams/detail/execute.hpp>
|
Chris@16
|
26 #include <boost/iostreams/detail/functional.hpp> // clear_flags, call_reset
|
Chris@16
|
27 #include <boost/mpl/if.hpp>
|
Chris@16
|
28 #include <boost/ref.hpp>
|
Chris@16
|
29 #include <boost/shared_ptr.hpp>
|
Chris@16
|
30 #include <boost/type_traits/is_convertible.hpp>
|
Chris@16
|
31
|
Chris@16
|
32 // Must come last.
|
Chris@16
|
33 #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
|
Chris@16
|
34
|
Chris@16
|
35 namespace boost { namespace iostreams {
|
Chris@16
|
36
|
Chris@16
|
37 //
|
Chris@16
|
38 // Template name: inverse.
|
Chris@16
|
39 // Template parameters:
|
Chris@16
|
40 // Filter - A model of InputFilter or OutputFilter.
|
Chris@16
|
41 // Description: Generates an InputFilter from an OutputFilter or
|
Chris@16
|
42 // vice versa.
|
Chris@16
|
43 //
|
Chris@16
|
44 template<typename Filter>
|
Chris@16
|
45 class inverse {
|
Chris@16
|
46 private:
|
Chris@16
|
47 BOOST_STATIC_ASSERT(is_filter<Filter>::value);
|
Chris@16
|
48 typedef typename category_of<Filter>::type base_category;
|
Chris@16
|
49 typedef reference_wrapper<Filter> filter_ref;
|
Chris@16
|
50 public:
|
Chris@16
|
51 typedef typename char_type_of<Filter>::type char_type;
|
Chris@16
|
52 typedef typename int_type_of<Filter>::type int_type;
|
Chris@16
|
53 typedef char_traits<char_type> traits_type;
|
Chris@16
|
54 typedef typename
|
Chris@16
|
55 mpl::if_<
|
Chris@16
|
56 is_convertible<
|
Chris@16
|
57 base_category,
|
Chris@16
|
58 input
|
Chris@16
|
59 >,
|
Chris@16
|
60 output,
|
Chris@16
|
61 input
|
Chris@16
|
62 >::type mode;
|
Chris@16
|
63 struct category
|
Chris@16
|
64 : mode,
|
Chris@16
|
65 filter_tag,
|
Chris@16
|
66 multichar_tag,
|
Chris@16
|
67 closable_tag
|
Chris@16
|
68 { };
|
Chris@16
|
69 explicit inverse( const Filter& filter,
|
Chris@16
|
70 std::streamsize buffer_size =
|
Chris@16
|
71 default_filter_buffer_size)
|
Chris@16
|
72 : pimpl_(new impl(filter, buffer_size))
|
Chris@16
|
73 { }
|
Chris@16
|
74
|
Chris@16
|
75 template<typename Source>
|
Chris@16
|
76 std::streamsize read(Source& src, char* s, std::streamsize n)
|
Chris@16
|
77 {
|
Chris@16
|
78 typedef detail::counted_array_sink<char_type> array_sink;
|
Chris@16
|
79 typedef composite<filter_ref, array_sink> filtered_array_sink;
|
Chris@16
|
80
|
Chris@16
|
81 BOOST_ASSERT((flags() & f_write) == 0);
|
Chris@16
|
82 if (flags() == 0) {
|
Chris@16
|
83 flags() = f_read;
|
Chris@16
|
84 buf().set(0, 0);
|
Chris@16
|
85 }
|
Chris@16
|
86
|
Chris@16
|
87 filtered_array_sink snk(filter(), array_sink(s, n));
|
Chris@16
|
88 int_type status;
|
Chris@16
|
89 for ( status = traits_type::good();
|
Chris@16
|
90 snk.second().count() < n && status == traits_type::good(); )
|
Chris@16
|
91 {
|
Chris@16
|
92 status = buf().fill(src);
|
Chris@16
|
93 buf().flush(snk);
|
Chris@16
|
94 }
|
Chris@16
|
95 return snk.second().count() == 0 &&
|
Chris@16
|
96 status == traits_type::eof()
|
Chris@16
|
97 ?
|
Chris@16
|
98 -1
|
Chris@16
|
99 :
|
Chris@16
|
100 snk.second().count();
|
Chris@16
|
101 }
|
Chris@16
|
102
|
Chris@16
|
103 template<typename Sink>
|
Chris@16
|
104 std::streamsize write(Sink& dest, const char* s, std::streamsize n)
|
Chris@16
|
105 {
|
Chris@16
|
106 typedef detail::counted_array_source<char_type> array_source;
|
Chris@16
|
107 typedef composite<filter_ref, array_source> filtered_array_source;
|
Chris@16
|
108
|
Chris@16
|
109 BOOST_ASSERT((flags() & f_read) == 0);
|
Chris@16
|
110 if (flags() == 0) {
|
Chris@16
|
111 flags() = f_write;
|
Chris@16
|
112 buf().set(0, 0);
|
Chris@16
|
113 }
|
Chris@16
|
114
|
Chris@16
|
115 filtered_array_source src(filter(), array_source(s, n));
|
Chris@16
|
116 for (bool good = true; src.second().count() < n && good; ) {
|
Chris@16
|
117 buf().fill(src);
|
Chris@16
|
118 good = buf().flush(dest);
|
Chris@16
|
119 }
|
Chris@16
|
120 return src.second().count();
|
Chris@16
|
121 }
|
Chris@16
|
122
|
Chris@16
|
123 template<typename Device>
|
Chris@16
|
124 void close(Device& dev)
|
Chris@16
|
125 {
|
Chris@16
|
126 detail::execute_all(
|
Chris@16
|
127 detail::flush_buffer(buf(), dev, (flags() & f_write) != 0),
|
Chris@16
|
128 detail::call_close_all(pimpl_->filter_, dev),
|
Chris@16
|
129 detail::clear_flags(flags())
|
Chris@16
|
130 );
|
Chris@16
|
131 }
|
Chris@16
|
132 private:
|
Chris@16
|
133 filter_ref filter() { return boost::ref(pimpl_->filter_); }
|
Chris@16
|
134 detail::buffer<char_type>& buf() { return pimpl_->buf_; }
|
Chris@16
|
135 int& flags() { return pimpl_->flags_; }
|
Chris@16
|
136
|
Chris@16
|
137 enum flags_ {
|
Chris@16
|
138 f_read = 1, f_write = 2
|
Chris@16
|
139 };
|
Chris@16
|
140
|
Chris@16
|
141 struct impl {
|
Chris@16
|
142 impl(const Filter& filter, std::streamsize n)
|
Chris@16
|
143 : filter_(filter), buf_(n), flags_(0)
|
Chris@16
|
144 { buf_.set(0, 0); }
|
Chris@16
|
145 Filter filter_;
|
Chris@16
|
146 detail::buffer<char_type> buf_;
|
Chris@16
|
147 int flags_;
|
Chris@16
|
148 };
|
Chris@16
|
149 shared_ptr<impl> pimpl_;
|
Chris@16
|
150 };
|
Chris@16
|
151
|
Chris@16
|
152 //
|
Chris@16
|
153 // Template name: invert.
|
Chris@16
|
154 // Template parameters:
|
Chris@16
|
155 // Filter - A model of InputFilter or OutputFilter.
|
Chris@16
|
156 // Description: Returns an instance of an appropriate specialization of inverse.
|
Chris@16
|
157 //
|
Chris@16
|
158 template<typename Filter>
|
Chris@16
|
159 inverse<Filter> invert(const Filter& f) { return inverse<Filter>(f); }
|
Chris@16
|
160
|
Chris@16
|
161 //----------------------------------------------------------------------------//
|
Chris@16
|
162
|
Chris@16
|
163 } } // End namespaces iostreams, boost.
|
Chris@16
|
164
|
Chris@16
|
165 #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC.
|
Chris@16
|
166
|
Chris@16
|
167 #endif // #ifndef BOOST_IOSTREAMS_INVERT_HPP_INCLUDED
|