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)
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 
31 namespace boost { namespace iostreams {
32 
33 //--------------Definition of filtered_istream--------------------------------//
34 
35 namespace detail {
36 
37 template<typename Mode, typename Ch, typename Tr>
38 struct 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 
65 #if defined(BOOST_MSVC) && (BOOST_MSVC == 1700)
66 # pragma warning(push)
67 // https://connect.microsoft.com/VisualStudio/feedback/details/733720/
68 # pragma warning(disable: 4250)
69 #endif
70 
71 template<typename Chain, typename Access>
72 class filtering_stream_base
73     : public access_control<
74                  boost::iostreams::detail::chain_client<Chain>,
75                  Access
76              >,
77       public filtering_stream_traits<
78                  typename Chain::mode,
79                  typename Chain::char_type,
80                  typename Chain::traits_type
81              >::stream_type
82 {
83 public:
84     typedef Chain                                         chain_type;
85     typedef access_control<
86                  boost::iostreams::detail::chain_client<Chain>,
87                  Access
88              >                                            client_type;
89 protected:
90     typedef typename
91             filtering_stream_traits<
92                  typename Chain::mode,
93                  typename Chain::char_type,
94                  typename Chain::traits_type
95             >::stream_type                                stream_type;
filtering_stream_base()96     filtering_stream_base() : stream_type(0) { this->set_chain(&chain_); }
97 private:
notify()98     void notify() { this->rdbuf(chain_.empty() ? 0 : &chain_.front()); }
99     Chain chain_;
100 };
101 
102 #if defined(BOOST_MSVC) && (BOOST_MSVC == 1700)
103 # pragma warning(pop)
104 #endif
105 
106 } // End namespace detail.
107 
108 //
109 // Macro: BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(name_, chain_type_, default_char_)
110 // Description: Defines a template derived from std::basic_streambuf which uses
111 //      a chain to perform i/o. The template has the following parameters:
112 //      Mode - the i/o mode.
113 //      Ch - The character type.
114 //      Tr - The character traits type.
115 //      Alloc - The allocator type.
116 //      Access - Indicates accessibility of the chain interface; must be either
117 //          public_ or protected_; defaults to public_.
118 // Macro parameters:
119 //      name_ - The name of the template to be defined.
120 //      chain_type_ - The name of the chain template.
121 //      default_char_ - The default value for the char template parameter.
122 //
123 #define BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(name_, chain_type_, default_char_) \
124     template< typename Mode, \
125               typename Ch = default_char_, \
126               typename Tr = BOOST_IOSTREAMS_CHAR_TRAITS(Ch), \
127               typename Alloc = std::allocator<Ch>, \
128               typename Access = public_ > \
129     class name_ \
130         : public boost::iostreams::detail::filtering_stream_base< \
131                      chain_type_<Mode, Ch, Tr, Alloc>, Access \
132                  > \
133     { \
134     public: \
135         typedef Ch                                char_type; \
136         struct category \
137             : Mode, \
138               closable_tag, \
139               detail::filtering_stream_traits<Mode, Ch, Tr>::stream_tag \
140             { }; \
141         BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr) \
142         typedef Mode                              mode; \
143         typedef chain_type_<Mode, Ch, Tr, Alloc>  chain_type; \
144         name_() { } \
145         BOOST_IOSTREAMS_DEFINE_PUSH_CONSTRUCTOR(name_, mode, Ch, push_impl) \
146         ~name_() { \
147             if (this->is_complete()) \
148                  this->rdbuf()->BOOST_IOSTREAMS_PUBSYNC(); \
149         } \
150     private: \
151         typedef access_control< \
152                     boost::iostreams::detail::chain_client< \
153                         chain_type_<Mode, Ch, Tr, Alloc> \
154                     >, \
155                     Access \
156                 > client_type; \
157         template<typename T> \
158         void push_impl(const T& t BOOST_IOSTREAMS_PUSH_PARAMS()) \
159         { client_type::push(t BOOST_IOSTREAMS_PUSH_ARGS()); } \
160     }; \
161     /**/
162 
163 #if defined(BOOST_MSVC) && (BOOST_MSVC == 1700)
164 # pragma warning(push)
165 // https://connect.microsoft.com/VisualStudio/feedback/details/733720/
166 # pragma warning(disable: 4250)
167 #endif
168 
169 BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(filtering_stream, boost::iostreams::chain, char)
170 BOOST_IOSTREAMS_DEFINE_FILTER_STREAM(wfiltering_stream, boost::iostreams::chain, wchar_t)
171 
172 #if defined(BOOST_MSVC) && (BOOST_MSVC == 1700)
173 # pragma warning(pop)
174 #endif
175 
176 typedef filtering_stream<input>    filtering_istream;
177 typedef filtering_stream<output>   filtering_ostream;
178 typedef wfiltering_stream<input>   filtering_wistream;
179 typedef wfiltering_stream<output>  filtering_wostream;
180 
181 //----------------------------------------------------------------------------//
182 
183 } } // End namespace iostreams, boost
184 
185 #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC
186 
187 #endif // #ifndef BOOST_IOSTREAMS_FILTER_STREAM_HPP_INCLUDED
188