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.)
5
6// See http://www.boost.org/libs/iostreams for documentation.
7
8#ifndef BOOST_IOSTREAMS_FILTER_STREAM_HPP_INCLUDED
9#define BOOST_IOSTREAMS_FILTER_STREAM_HPP_INCLUDED
10
11#if defined(_MSC_VER) && (_MSC_VER >= 1020)
12# pragma once
13#endif
14
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>
27
28// Must come last.
29#include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
30
31namespace boost { namespace iostreams {
32
33//--------------Definition of filtered_istream--------------------------------//
34
35namespace detail {
36
37template<typename Mode, typename Ch, typename Tr>
38struct filtering_stream_traits {
39 typedef typename
40 iostreams::select< // Disambiguation for Tru64
41 mpl::and_<
42 is_convertible<Mode, input>,
43 is_convertible<Mode, output>
44 >,
45 BOOST_IOSTREAMS_BASIC_IOSTREAM(Ch, Tr),
46 is_convertible<Mode, input>,
47 BOOST_IOSTREAMS_BASIC_ISTREAM(Ch, Tr),
48 else_,
49 BOOST_IOSTREAMS_BASIC_OSTREAM(Ch, Tr)
50 >::type stream_type;
51 typedef typename
52 iostreams::select< // Dismbiguation required for Tru64.
53 mpl::and_<
54 is_convertible<Mode, input>,
55 is_convertible<Mode, output>
56 >,
57 iostream_tag,
58 is_convertible<Mode, input>,
59 istream_tag,
60 else_,
61 ostream_tag
62 >::type stream_tag;
63};
64
65template<typename Chain, typename Access>
66class filtering_stream_base
67 : public access_control<
68 boost::iostreams::detail::chain_client<Chain>,
69 Access
70 >,
71 public filtering_stream_traits<
72 typename Chain::mode,
73 typename Chain::char_type,
74 typename Chain::traits_type
75 >::stream_type
76{
77public:
78 typedef Chain chain_type;
79 typedef access_control<
80 boost::iostreams::detail::chain_client<Chain>,
81 Access
82 > client_type;
83protected:
84 typedef typename
85 filtering_stream_traits<
86 typename Chain::mode,
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_); }
91private:
92 void notify() { this->rdbuf(chain_.empty() ? 0 : &chain_.front()); }
93 Chain chain_;
94};
95
96} // End namespace detail.
97
98//
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_.
108// Macro parameters:
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.
112//
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_ > \
119 class name_ \
120 : public boost::iostreams::detail::filtering_stream_base< \
121 chain_type_<Mode, Ch, Tr, Alloc>, Access \
122 > \
123 { \
124 public: \
125 typedef Ch char_type; \
126 struct category \
127 : Mode, \
128 closable_tag, \
129 detail::filtering_stream_traits<Mode, Ch, Tr>::stream_tag \
130 { }; \
131 BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr) \
132 typedef Mode mode; \
133 typedef chain_type_<Mode, Ch, Tr, Alloc> chain_type; \
134 name_() { } \
135 BOOST_IOSTREAMS_DEFINE_PUSH_CONSTRUCTOR(name_, mode, Ch, push_impl) \
136 ~name_() { \
137 if (this->is_complete()) \
138 this->rdbuf()->BOOST_IOSTREAMS_PUBSYNC(); \
139 } \
140 private: \
141 typedef access_control< \
142 boost::iostreams::detail::chain_client< \
143 chain_type_<Mode, Ch, Tr, Alloc> \
144 >, \
145 Access \
146 > client_type; \
147 template<typename T> \
148 void push_impl(const T& t BOOST_IOSTREAMS_PUSH_PARAMS()) \
149 { client_type::push(t BOOST_IOSTREAMS_PUSH_ARGS()); } \
150 }; \
151 /**/
152BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(filtering_stream, boost::iostreams::chain, char)
153BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(wfiltering_stream, boost::iostreams::chain, wchar_t)
154
155typedef filtering_stream<input> filtering_istream;
156typedef filtering_stream<output> filtering_ostream;
157typedef wfiltering_stream<input> filtering_wistream;
158typedef wfiltering_stream<output> filtering_wostream;
159
160//----------------------------------------------------------------------------//
161
162} } // End namespace iostreams, boost
163
164#include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC
165
166#endif // #ifndef BOOST_IOSTREAMS_FILTER_STREAM_HPP_INCLUDED
167

source code of boost/boost/iostreams/filtering_stream.hpp