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.)
6 // See http://www.boost.org/libs/iostreams for documentation.
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.
12 // Also contains several utility metafunctions, functions and macros.
15 #ifndef BOOST_IOSTREAMS_IO_TRAITS_HPP_INCLUDED
16 #define BOOST_IOSTREAMS_IO_TRAITS_HPP_INCLUDED
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>
44 #include <boost/iostreams/detail/config/disable_warnings.hpp>
46 namespace boost { namespace iostreams {
48 //----------Definitions of predicates for streams and stream buffers----------//
50 #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------------------//
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)
65 #else // #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-----------------------//
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)
72 #endif // #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //----------------------//
76 : mpl::or_< is_istream<T>, is_ostream<T>, is_streambuf<T> >
80 struct is_std_file_device
90 struct is_std_string_device
99 template<typename Device, typename Tr, typename Alloc>
102 template<typename T, typename Tr, typename Alloc, typename Mode>
105 template< typename Mode, typename Ch, typename Tr,
106 typename Alloc, typename Access >
107 class filtering_stream;
109 template< typename Mode, typename Ch, typename Tr,
110 typename Alloc, typename Access >
111 class wfiltering_stream;
113 template< typename Mode, typename Ch, typename Tr,
114 typename Alloc, typename Access >
115 class filtering_streambuf;
117 template< typename Mode, typename Ch, typename Tr,
118 typename Alloc, typename Access >
119 class filtering_wstreambuf;
123 template<typename T, typename Tr>
124 class linked_streambuf;
126 BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_boost_stream,
127 boost::iostreams::stream,
129 BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_boost_stream_buffer,
130 boost::iostreams::stream_buffer,
132 BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_stream_impl,
133 boost::iostreams::filtering_stream,
135 BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_wstream_impl,
136 boost::iostreams::wfiltering_stream,
138 BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_streambuf_impl,
139 boost::iostreams::filtering_streambuf,
141 BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_wstreambuf_impl,
142 boost::iostreams::filtering_wstreambuf,
144 BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_linked, linked_streambuf, 2)
147 struct is_filtering_stream
149 is_filtering_stream_impl<T>,
150 is_filtering_wstream_impl<T>
155 struct is_filtering_streambuf
157 is_filtering_streambuf_impl<T>,
158 is_filtering_wstreambuf_impl<T>
166 is_boost_stream_buffer<T>,
167 is_filtering_stream<T>,
168 is_filtering_streambuf<T>
172 } // End namespace detail.
174 //------------------Definitions of char_type_of-------------------------------//
179 struct member_char_type { typedef typename T::char_type type; };
181 } // End namespace detail.
183 # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-------------------------------//
187 : detail::member_char_type<
188 typename detail::unwrapped_type<T>::type
192 # else // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //---------------------//
195 struct char_type_of {
196 typedef typename detail::unwrapped_type<T>::type U;
201 detail::member_char_type<U>
205 # endif // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------//
207 template<typename Iter>
208 struct char_type_of< iterator_range<Iter> > {
209 typedef typename iterator_value<Iter>::type type;
213 //------------------Definitions of category_of--------------------------------//
218 struct member_category { typedef typename T::category type; };
220 } // End namespace detail.
225 struct member_category {
226 typedef typename U::category type;
228 typedef typename detail::unwrapped_type<T>::type U;
233 mpl::not_< detail::is_boost<U> >
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
249 detail::member_category<U>
253 // Partial specialization for reference wrappers
256 struct category_of< reference_wrapper<T> >
261 //------------------Definition of get_category--------------------------------//
264 // Returns an object of type category_of<T>::type.
267 inline typename category_of<T>::type get_category(const T&)
268 { typedef typename category_of<T>::type category; return category(); }
270 //------------------Definition of int_type_of---------------------------------//
274 #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
275 typedef std::char_traits<
276 BOOST_DEDUCED_TYPENAME char_type_of<T>::type
278 typedef typename traits_type::int_type type;
284 //------------------Definition of mode_of-------------------------------------//
288 template<int N> struct io_mode_impl;
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; }; \
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
307 typedef typename category_of<T>::type category;
308 BOOST_SELECT_BY_SIZE(int, value, detail::io_mode_impl_helper(category()));
311 } // End namespace detail.
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 > { };
316 // Partial specialization for reference wrappers
319 struct mode_of< reference_wrapper<T> >
324 //------------------Definition of is_device, is_filter and is_direct----------//
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));
334 template<typename T, typename Tag>
336 : mpl::bool_<has_trait_impl<T, Tag>::value>
339 } // End namespace detail.
342 struct is_device : detail::has_trait<T, device_tag> { };
345 struct is_filter : detail::has_trait<T, filter_tag> { };
348 struct is_direct : detail::has_trait<T, direct_tag> { };
350 //------------------Definition of BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS----------//
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; \
359 } } // End namespaces iostreams, boost.
361 #include <boost/iostreams/detail/config/enable_warnings.hpp>
363 #endif // #ifndef BOOST_IOSTREAMS_IO_TRAITS_HPP_INCLUDED