1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2004-2007 Jonathan Turkanis
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
6 // See http://www.boost.org/libs/iostreams for documentation.
8 #ifndef BOOST_IOSTREAMS_FILTER_STREAM_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_FILTER_STREAM_HPP_INCLUDED
11 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
15 #include <memory> // allocator.
16 #include <boost/iostreams/detail/access_control.hpp>
17 #include <boost/iostreams/detail/char_traits.hpp>
18 #include <boost/iostreams/detail/iostream.hpp> // standard streams.
19 #include <boost/iostreams/detail/push.hpp>
20 #include <boost/iostreams/detail/select.hpp>
21 #include <boost/iostreams/detail/streambuf.hpp> // pubsync.
22 #include <boost/iostreams/filtering_streambuf.hpp>
23 #include <boost/mpl/and.hpp>
24 #include <boost/mpl/bool.hpp>
25 #include <boost/static_assert.hpp>
26 #include <boost/type_traits/is_convertible.hpp>
29 #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
31 namespace boost { namespace iostreams {
33 //--------------Definition of filtered_istream--------------------------------//
37 template<typename Mode, typename Ch, typename Tr>
38 struct filtering_stream_traits {
40 iostreams::select< // Disambiguation for Tru64
42 is_convertible<Mode, input>,
43 is_convertible<Mode, output>
45 BOOST_IOSTREAMS_BASIC_IOSTREAM(Ch, Tr),
46 is_convertible<Mode, input>,
47 BOOST_IOSTREAMS_BASIC_ISTREAM(Ch, Tr),
49 BOOST_IOSTREAMS_BASIC_OSTREAM(Ch, Tr)
52 iostreams::select< // Dismbiguation required for Tru64.
54 is_convertible<Mode, input>,
55 is_convertible<Mode, output>
58 is_convertible<Mode, input>,
65 template<typename Chain, typename Access>
66 class filtering_stream_base
67 : public access_control<
68 boost::iostreams::detail::chain_client<Chain>,
71 public filtering_stream_traits<
73 typename Chain::char_type,
74 typename Chain::traits_type
78 typedef Chain chain_type;
79 typedef access_control<
80 boost::iostreams::detail::chain_client<Chain>,
85 filtering_stream_traits<
87 typename Chain::char_type,
88 typename Chain::traits_type
89 >::stream_type stream_type;
90 filtering_stream_base() : stream_type(0) { this->set_chain(&chain_); }
92 void notify() { this->rdbuf(chain_.empty() ? 0 : &chain_.front()); }
96 } // End namespace detail.
99 // Macro: BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(name_, chain_type_, default_char_)
100 // Description: Defines a template derived from std::basic_streambuf which uses
101 // a chain to perform i/o. The template has the following parameters:
102 // Mode - the i/o mode.
103 // Ch - The character type.
104 // Tr - The character traits type.
105 // Alloc - The allocator type.
106 // Access - Indicates accessibility of the chain interface; must be either
107 // public_ or protected_; defaults to public_.
109 // name_ - The name of the template to be defined.
110 // chain_type_ - The name of the chain template.
111 // default_char_ - The default value for the char template parameter.
113 #define BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(name_, chain_type_, default_char_) \
114 template< typename Mode, \
115 typename Ch = default_char_, \
116 typename Tr = BOOST_IOSTREAMS_CHAR_TRAITS(Ch), \
117 typename Alloc = std::allocator<Ch>, \
118 typename Access = public_ > \
120 : public boost::iostreams::detail::filtering_stream_base< \
121 chain_type_<Mode, Ch, Tr, Alloc>, Access \
125 typedef Ch char_type; \
129 detail::filtering_stream_traits<Mode, Ch, Tr>::stream_tag \
131 BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr) \
133 typedef chain_type_<Mode, Ch, Tr, Alloc> chain_type; \
135 BOOST_IOSTREAMS_DEFINE_PUSH_CONSTRUCTOR(name_, mode, Ch, push_impl) \
137 if (this->is_complete()) \
138 this->rdbuf()->BOOST_IOSTREAMS_PUBSYNC(); \
141 typedef access_control< \
142 boost::iostreams::detail::chain_client< \
143 chain_type_<Mode, Ch, Tr, Alloc> \
147 template<typename T> \
148 void push_impl(const T& t BOOST_IOSTREAMS_PUSH_PARAMS()) \
149 { client_type::push(t BOOST_IOSTREAMS_PUSH_ARGS()); } \
152 BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(filtering_stream, boost::iostreams::chain, char)
153 BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(wfiltering_stream, boost::iostreams::chain, wchar_t)
155 typedef filtering_stream<input> filtering_istream;
156 typedef filtering_stream<output> filtering_ostream;
157 typedef wfiltering_stream<input> filtering_wistream;
158 typedef wfiltering_stream<output> filtering_wostream;
160 //----------------------------------------------------------------------------//
162 } } // End namespace iostreams, boost
164 #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC
166 #endif // #ifndef BOOST_IOSTREAMS_FILTER_STREAM_HPP_INCLUDED