1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2003-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 //
9 // Contains metafunctions char_type_of, category_of and mode_of used for
10 // deducing the i/o category and i/o mode of a model of Filter or Device.
11 //
12 // Also contains several utility metafunctions, functions and macros.
13 //
14
15 #ifndef BOOST_IOSTREAMS_IO_TRAITS_HPP_INCLUDED
16 #define BOOST_IOSTREAMS_IO_TRAITS_HPP_INCLUDED
17
18 #if defined(_MSC_VER)
19 # pragma once
20 #endif
21
22 #include <iosfwd> // stream types, char_traits.
23 #include <boost/config.hpp> // partial spec, deduced typename.
24 #include <boost/detail/workaround.hpp>
25 #include <boost/iostreams/categories.hpp>
26 #include <boost/iostreams/detail/bool_trait_def.hpp>
27 #include <boost/iostreams/detail/config/wide_streams.hpp>
28 #include <boost/iostreams/detail/is_iterator_range.hpp>
29 #include <boost/iostreams/detail/select.hpp>
30 #include <boost/iostreams/detail/select_by_size.hpp>
31 #include <boost/iostreams/detail/wrap_unwrap.hpp>
32 #include <boost/iostreams/traits_fwd.hpp>
33 #include <boost/mpl/bool.hpp>
34 #include <boost/mpl/eval_if.hpp>
35 #include <boost/mpl/identity.hpp>
36 #include <boost/mpl/int.hpp>
37 #include <boost/mpl/or.hpp>
38 #include <boost/range/iterator_range.hpp>
39 #include <boost/range/value_type.hpp>
40 #include <boost/ref.hpp>
41 #include <boost/type_traits/is_convertible.hpp>
42
43 // Must come last.
44 #include <boost/iostreams/detail/config/disable_warnings.hpp>
45
46 namespace boost { namespace iostreams {
47
48 //----------Definitions of predicates for streams and stream buffers----------//
49
50 #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------------------//
51
52 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_istream, std::basic_istream, 2)
53 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ostream, std::basic_ostream, 2)
54 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_iostream, std::basic_iostream, 2)
55 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_streambuf, std::basic_streambuf, 2)
56 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ifstream, std::basic_ifstream, 2)
57 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ofstream, std::basic_ofstream, 2)
58 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_fstream, std::basic_fstream, 2)
59 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_filebuf, std::basic_filebuf, 2)
60 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_istringstream, std::basic_istringstream, 3)
61 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ostringstream, std::basic_ostringstream, 3)
62 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_stringstream, std::basic_stringstream, 3)
63 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_stringbuf, std::basic_stringbuf, 3)
64
65 #else // #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-----------------------//
66
67 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_istream, std::istream, 0)
68 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ostream, std::ostream, 0)
69 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_iostream, std::iostream, 0)
70 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_streambuf, std::streambuf, 0)
71
72 #endif // #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //----------------------//
73
74 template<typename T>
75 struct is_std_io
76 : mpl::or_< is_istream<T>, is_ostream<T>, is_streambuf<T> >
77 { };
78
79 template<typename T>
80 struct is_std_file_device
81 : mpl::or_<
82 is_ifstream<T>,
83 is_ofstream<T>,
84 is_fstream<T>,
85 is_filebuf<T>
86 >
87 { };
88
89 template<typename T>
90 struct is_std_string_device
91 : mpl::or_<
92 is_istringstream<T>,
93 is_ostringstream<T>,
94 is_stringstream<T>,
95 is_stringbuf<T>
96 >
97 { };
98
99 template<typename Device, typename Tr, typename Alloc>
100 struct stream;
101
102 template<typename T, typename Tr, typename Alloc, typename Mode>
103 class stream_buffer;
104
105 template< typename Mode, typename Ch, typename Tr,
106 typename Alloc, typename Access >
107 class filtering_stream;
108
109 template< typename Mode, typename Ch, typename Tr,
110 typename Alloc, typename Access >
111 class wfiltering_stream;
112
113 template< typename Mode, typename Ch, typename Tr,
114 typename Alloc, typename Access >
115 class filtering_streambuf;
116
117 template< typename Mode, typename Ch, typename Tr,
118 typename Alloc, typename Access >
119 class filtering_wstreambuf;
120
121 namespace detail {
122
123 template<typename T, typename Tr>
124 class linked_streambuf;
125
126 BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_boost_stream,
127 boost::iostreams::stream,
128 3 )
129 BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_boost_stream_buffer,
130 boost::iostreams::stream_buffer,
131 4 )
132 BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_stream_impl,
133 boost::iostreams::filtering_stream,
134 5 )
135 BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_wstream_impl,
136 boost::iostreams::wfiltering_stream,
137 5 )
138 BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_streambuf_impl,
139 boost::iostreams::filtering_streambuf,
140 5 )
141 BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_wstreambuf_impl,
142 boost::iostreams::filtering_wstreambuf,
143 5 )
144 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_linked, linked_streambuf, 2)
145
146 template<typename T>
147 struct is_filtering_stream
148 : mpl::or_<
149 is_filtering_stream_impl<T>,
150 is_filtering_wstream_impl<T>
151 >
152 { };
153
154 template<typename T>
155 struct is_filtering_streambuf
156 : mpl::or_<
157 is_filtering_streambuf_impl<T>,
158 is_filtering_wstreambuf_impl<T>
159 >
160 { };
161
162 template<typename T>
163 struct is_boost
164 : mpl::or_<
165 is_boost_stream<T>,
166 is_boost_stream_buffer<T>,
167 is_filtering_stream<T>,
168 is_filtering_streambuf<T>
169 >
170 { };
171
172 } // End namespace detail.
173
174 //------------------Definitions of char_type_of-------------------------------//
175
176 namespace detail {
177
178 template<typename T>
179 struct member_char_type { typedef typename T::char_type type; };
180
181 } // End namespace detail.
182
183 # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-------------------------------//
184
185 template<typename T>
186 struct char_type_of
187 : detail::member_char_type<
188 typename detail::unwrapped_type<T>::type
189 >
190 { };
191
192 # else // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //---------------------//
193
194 template<typename T>
195 struct char_type_of {
196 typedef typename detail::unwrapped_type<T>::type U;
197 typedef typename
198 mpl::eval_if<
199 is_std_io<U>,
200 mpl::identity<char>,
201 detail::member_char_type<U>
202 >::type type;
203 };
204
205 # endif // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------//
206
207 template<typename Iter>
208 struct char_type_of< iterator_range<Iter> > {
209 typedef typename iterator_value<Iter>::type type;
210 };
211
212
213 //------------------Definitions of category_of--------------------------------//
214
215 namespace detail {
216
217 template<typename T>
218 struct member_category { typedef typename T::category type; };
219
220 } // End namespace detail.
221
222 template<typename T>
223 struct category_of {
224 template<typename U>
225 struct member_category {
226 typedef typename U::category type;
227 };
228 typedef typename detail::unwrapped_type<T>::type U;
229 typedef typename
230 mpl::eval_if<
231 mpl::and_<
232 is_std_io<U>,
233 mpl::not_< detail::is_boost<U> >
234 >,
235 iostreams::select< // Disambiguation for Tru64
236 is_filebuf<U>, filebuf_tag,
237 is_ifstream<U>, ifstream_tag,
238 is_ofstream<U>, ofstream_tag,
239 is_fstream<U>, fstream_tag,
240 is_stringbuf<U>, stringbuf_tag,
241 is_istringstream<U>, istringstream_tag,
242 is_ostringstream<U>, ostringstream_tag,
243 is_stringstream<U>, stringstream_tag,
244 is_streambuf<U>, generic_streambuf_tag,
245 is_iostream<U>, generic_iostream_tag,
246 is_istream<U>, generic_istream_tag,
247 is_ostream<U>, generic_ostream_tag
248 >,
249 detail::member_category<U>
250 >::type type;
251 };
252
253 // Partial specialization for reference wrappers
254
255 template<typename T>
256 struct category_of< reference_wrapper<T> >
257 : category_of<T>
258 { };
259
260
261 //------------------Definition of get_category--------------------------------//
262
263 //
264 // Returns an object of type category_of<T>::type.
265 //
266 template<typename T>
get_category(const T &)267 inline typename category_of<T>::type get_category(const T&)
268 { typedef typename category_of<T>::type category; return category(); }
269
270 //------------------Definition of int_type_of---------------------------------//
271
272 template<typename T>
273 struct int_type_of {
274 #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
275 typedef std::char_traits<
276 BOOST_DEDUCED_TYPENAME char_type_of<T>::type
277 > traits_type;
278 typedef typename traits_type::int_type type;
279 #else
280 typedef int type;
281 #endif
282 };
283
284 //------------------Definition of mode_of-------------------------------------//
285
286 namespace detail {
287
288 template<int N> struct io_mode_impl;
289
290 #define BOOST_IOSTREAMS_MODE_HELPER(tag_, id_) \
291 case_<id_> io_mode_impl_helper(tag_); \
292 template<> struct io_mode_impl<id_> { typedef tag_ type; }; \
293 /**/
294 BOOST_IOSTREAMS_MODE_HELPER(input, 1)
295 BOOST_IOSTREAMS_MODE_HELPER(output, 2)
296 BOOST_IOSTREAMS_MODE_HELPER(bidirectional, 3)
297 BOOST_IOSTREAMS_MODE_HELPER(input_seekable, 4)
298 BOOST_IOSTREAMS_MODE_HELPER(output_seekable, 5)
299 BOOST_IOSTREAMS_MODE_HELPER(seekable, 6)
300 BOOST_IOSTREAMS_MODE_HELPER(dual_seekable, 7)
301 BOOST_IOSTREAMS_MODE_HELPER(bidirectional_seekable, 8)
302 BOOST_IOSTREAMS_MODE_HELPER(dual_use, 9)
303 #undef BOOST_IOSTREAMS_MODE_HELPER
304
305 template<typename T>
306 struct io_mode_id {
307 typedef typename category_of<T>::type category;
308 BOOST_SELECT_BY_SIZE(int, value, detail::io_mode_impl_helper(category()));
309 };
310
311 } // End namespace detail.
312
313 template<typename T> // Borland 5.6.4 requires this circumlocution.
314 struct mode_of : detail::io_mode_impl< detail::io_mode_id<T>::value > { };
315
316 // Partial specialization for reference wrappers
317
318 template<typename T>
319 struct mode_of< reference_wrapper<T> >
320 : mode_of<T>
321 { };
322
323
324 //------------------Definition of is_device, is_filter and is_direct----------//
325
326 namespace detail {
327
328 template<typename T, typename Tag>
329 struct has_trait_impl {
330 typedef typename category_of<T>::type category;
331 BOOST_STATIC_CONSTANT(bool, value = (is_convertible<category, Tag>::value));
332 };
333
334 template<typename T, typename Tag>
335 struct has_trait
336 : mpl::bool_<has_trait_impl<T, Tag>::value>
337 { };
338
339 } // End namespace detail.
340
341 template<typename T>
342 struct is_device : detail::has_trait<T, device_tag> { };
343
344 template<typename T>
345 struct is_filter : detail::has_trait<T, filter_tag> { };
346
347 template<typename T>
348 struct is_direct : detail::has_trait<T, direct_tag> { };
349
350 //------------------Definition of BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS----------//
351
352 #define BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr) \
353 typedef Tr traits_type; \
354 typedef typename traits_type::int_type int_type; \
355 typedef typename traits_type::off_type off_type; \
356 typedef typename traits_type::pos_type pos_type; \
357 /**/
358
359 } } // End namespaces iostreams, boost.
360
361 #include <boost/iostreams/detail/config/enable_warnings.hpp>
362
363 #endif // #ifndef BOOST_IOSTREAMS_IO_TRAITS_HPP_INCLUDED
364