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
46namespace boost { namespace iostreams {
47
48//----------Definitions of predicates for streams and stream buffers----------//
49
50#ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------------------//
51
52BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_istream, std::basic_istream, 2)
53BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ostream, std::basic_ostream, 2)
54BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_iostream, std::basic_iostream, 2)
55BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_streambuf, std::basic_streambuf, 2)
56BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ifstream, std::basic_ifstream, 2)
57BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ofstream, std::basic_ofstream, 2)
58BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_fstream, std::basic_fstream, 2)
59BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_filebuf, std::basic_filebuf, 2)
60BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_istringstream, std::basic_istringstream, 3)
61BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ostringstream, std::basic_ostringstream, 3)
62BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_stringstream, std::basic_stringstream, 3)
63BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_stringbuf, std::basic_stringbuf, 3)
64
65#else // #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-----------------------//
66
67BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_istream, std::istream, 0)
68BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ostream, std::ostream, 0)
69BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_iostream, std::iostream, 0)
70BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_streambuf, std::streambuf, 0)
71
72#endif // #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //----------------------//
73
74template<typename T>
75struct is_std_io
76 : mpl::or_< is_istream<T>, is_ostream<T>, is_streambuf<T> >
77 { };
78
79template<typename T>
80struct 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
89template<typename T>
90struct 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
99template<typename Device, typename Tr, typename Alloc>
100struct stream;
101
102template<typename T, typename Tr, typename Alloc, typename Mode>
103class stream_buffer;
104
105template< typename Mode, typename Ch, typename Tr,
106 typename Alloc, typename Access >
107class filtering_stream;
108
109template< typename Mode, typename Ch, typename Tr,
110 typename Alloc, typename Access >
111class wfiltering_stream;
112
113template< typename Mode, typename Ch, typename Tr,
114 typename Alloc, typename Access >
115class filtering_streambuf;
116
117template< typename Mode, typename Ch, typename Tr,
118 typename Alloc, typename Access >
119class filtering_wstreambuf;
120
121namespace detail {
122
123template<typename T, typename Tr>
124class linked_streambuf;
125
126BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_boost_stream,
127 boost::iostreams::stream,
128 3 )
129BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_boost_stream_buffer,
130 boost::iostreams::stream_buffer,
131 4 )
132BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_stream_impl,
133 boost::iostreams::filtering_stream,
134 5 )
135BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_wstream_impl,
136 boost::iostreams::wfiltering_stream,
137 5 )
138BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_streambuf_impl,
139 boost::iostreams::filtering_streambuf,
140 5 )
141BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_wstreambuf_impl,
142 boost::iostreams::filtering_wstreambuf,
143 5 )
144BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_linked, linked_streambuf, 2)
145
146template<typename T>
147struct is_filtering_stream
148 : mpl::or_<
149 is_filtering_stream_impl<T>,
150 is_filtering_wstream_impl<T>
151 >
152 { };
153
154template<typename T>
155struct is_filtering_streambuf
156 : mpl::or_<
157 is_filtering_streambuf_impl<T>,
158 is_filtering_wstreambuf_impl<T>
159 >
160 { };
161
162template<typename T>
163struct 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
176namespace detail {
177
178template<typename T>
179struct member_char_type { typedef typename T::char_type type; };
180
181} // End namespace detail.
182
183# ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-------------------------------//
184
185template<typename T>
186struct 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
194template<typename T>
195struct 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
207template<typename Iter>
208struct char_type_of< iterator_range<Iter> > {
209 typedef typename iterator_value<Iter>::type type;
210};
211
212
213//------------------Definitions of category_of--------------------------------//
214
215namespace detail {
216
217template<typename T>
218struct member_category { typedef typename T::category type; };
219
220} // End namespace detail.
221
222template<typename T>
223struct 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
255template<typename T>
256struct 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//
266template<typename T>
267inline 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
272template<typename T>
273struct 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
286namespace detail {
287
288template<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 /**/
294BOOST_IOSTREAMS_MODE_HELPER(input, 1)
295BOOST_IOSTREAMS_MODE_HELPER(output, 2)
296BOOST_IOSTREAMS_MODE_HELPER(bidirectional, 3)
297BOOST_IOSTREAMS_MODE_HELPER(input_seekable, 4)
298BOOST_IOSTREAMS_MODE_HELPER(output_seekable, 5)
299BOOST_IOSTREAMS_MODE_HELPER(seekable, 6)
300BOOST_IOSTREAMS_MODE_HELPER(dual_seekable, 7)
301BOOST_IOSTREAMS_MODE_HELPER(bidirectional_seekable, 8)
302BOOST_IOSTREAMS_MODE_HELPER(dual_use, 9)
303#undef BOOST_IOSTREAMS_MODE_HELPER
304
305template<typename T>
306struct 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
313template<typename T> // Borland 5.6.4 requires this circumlocution.
314struct mode_of : detail::io_mode_impl< detail::io_mode_id<T>::value > { };
315
316// Partial specialization for reference wrappers
317
318template<typename T>
319struct mode_of< reference_wrapper<T> >
320 : mode_of<T>
321 { };
322
323
324//------------------Definition of is_device, is_filter and is_direct----------//
325
326namespace detail {
327
328template<typename T, typename Tag>
329struct has_trait_impl {
330 typedef typename category_of<T>::type category;
331 BOOST_STATIC_CONSTANT(bool, value = (is_convertible<category, Tag>::value));
332};
333
334template<typename T, typename Tag>
335struct has_trait
336 : mpl::bool_<has_trait_impl<T, Tag>::value>
337 { };
338
339} // End namespace detail.
340
341template<typename T>
342struct is_device : detail::has_trait<T, device_tag> { };
343
344template<typename T>
345struct is_filter : detail::has_trait<T, filter_tag> { };
346
347template<typename T>
348struct 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

source code of boost/libs/iostreams/include/boost/iostreams/traits.hpp