fix doc example typo
[boost.git] / boost / iostreams / detail / resolve.hpp
blob2d540e81eab7f70c0d784d323d8bd2310a91b2fc
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.
8 #ifndef BOOST_IOSTREAMS_DETAIL_RESOLVE_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_DETAIL_RESOLVE_HPP_INCLUDED
11 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
12 # pragma once
13 #endif
15 #include <boost/config.hpp> // partial spec, put size_t in std.
16 #include <cstddef> // std::size_t.
17 #include <boost/detail/is_incrementable.hpp>
18 #include <boost/detail/workaround.hpp>
19 #include <boost/iostreams/detail/adapter/mode_adapter.hpp>
20 #include <boost/iostreams/detail/adapter/output_iterator_adapter.hpp>
21 #include <boost/iostreams/detail/adapter/range_adapter.hpp>
22 #include <boost/iostreams/detail/config/gcc.hpp>
23 #include <boost/iostreams/detail/config/overload_resolution.hpp>
24 #include <boost/iostreams/detail/config/wide_streams.hpp>
25 #include <boost/iostreams/detail/enable_if_stream.hpp>
26 #include <boost/iostreams/detail/is_dereferenceable.hpp>
27 #include <boost/iostreams/detail/is_iterator_range.hpp>
28 #include <boost/iostreams/detail/select.hpp>
29 #include <boost/iostreams/detail/wrap_unwrap.hpp>
30 #include <boost/iostreams/device/array.hpp>
31 #include <boost/iostreams/traits.hpp>
32 #include <boost/mpl/and.hpp>
33 #include <boost/mpl/bool.hpp> // true_.
34 #include <boost/mpl/if.hpp>
35 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
36 # include <boost/range/iterator_range.hpp>
37 #endif // #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
38 #include <boost/type_traits/is_array.hpp>
40 // Must come last.
41 #include <boost/iostreams/detail/config/disable_warnings.hpp> // VC7.1 C4224.
43 namespace boost { namespace iostreams { namespace detail {
45 //------------------Definition of resolve-------------------------------------//
47 #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //-------------------------//
49 template<typename Mode, typename Ch, typename T>
50 struct resolve_traits {
51 typedef typename
52 mpl::if_<
53 boost::detail::is_incrementable<T>,
54 output_iterator_adapter<Mode, Ch, T>,
55 const T&
56 >::type type;
59 # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-------------------------------//
61 template<typename Mode, typename Ch, typename T>
62 typename resolve_traits<Mode, Ch, T>::type
63 resolve( const T& t
64 BOOST_IOSTREAMS_DISABLE_IF_STREAM(T)
66 // I suspect that the compilers which require this workaround may
67 // be correct, but I'm not sure why :(
68 #if BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, BOOST_TESTED_AT(810)) ||\
69 BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205)) || \
70 BOOST_WORKAROUND(BOOST_IOSTREAMS_GCC, BOOST_TESTED_AT(400)) \
71 /**/
72 , typename disable_if< is_iterator_range<T> >::type* = 0
73 #endif
76 typedef typename resolve_traits<Mode, Ch, T>::type return_type;
77 return return_type(t);
80 template<typename Mode, typename Ch, typename Tr>
81 mode_adapter< Mode, std::basic_streambuf<Ch, Tr> >
82 resolve(std::basic_streambuf<Ch, Tr>& sb)
83 { return mode_adapter< Mode, std::basic_streambuf<Ch, Tr> >(wrap(sb)); }
85 template<typename Mode, typename Ch, typename Tr>
86 mode_adapter< Mode, std::basic_istream<Ch, Tr> >
87 resolve(std::basic_istream<Ch, Tr>& is)
88 { return mode_adapter< Mode, std::basic_istream<Ch, Tr> >(wrap(is)); }
90 template<typename Mode, typename Ch, typename Tr>
91 mode_adapter< Mode, std::basic_ostream<Ch, Tr> >
92 resolve(std::basic_ostream<Ch, Tr>& os)
93 { return mode_adapter< Mode, std::basic_ostream<Ch, Tr> >(wrap(os)); }
95 template<typename Mode, typename Ch, typename Tr>
96 mode_adapter< Mode, std::basic_iostream<Ch, Tr> >
97 resolve(std::basic_iostream<Ch, Tr>& io)
98 { return mode_adapter< Mode, std::basic_iostream<Ch, Tr> >(wrap(io)); }
100 template<typename Mode, typename Ch, std::size_t N>
101 array_adapter<Mode, Ch> resolve(Ch (&array)[N])
102 { return array_adapter<Mode, Ch>(array); }
104 # if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
105 template<typename Mode, typename Ch, typename Iter>
106 range_adapter< Mode, boost::iterator_range<Iter> >
107 resolve(const boost::iterator_range<Iter>& rng)
108 { return range_adapter< Mode, boost::iterator_range<Iter> >(rng); }
109 # endif // #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
111 # else // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //---------------------//
113 template<typename Mode, typename Ch, typename T>
114 typename resolve_traits<Mode, Ch, T>::type
115 resolve( const T& t
116 BOOST_IOSTREAMS_DISABLE_IF_STREAM(T)
117 #if defined(__GNUC__)
118 , typename disable_if< is_iterator_range<T> >::type* = 0
119 #endif
122 typedef typename resolve_traits<Mode, Ch, T>::type return_type;
123 return return_type(t);
126 template<typename Mode, typename Ch>
127 mode_adapter<Mode, std::streambuf>
128 resolve(std::streambuf& sb)
129 { return mode_adapter<Mode, std::streambuf>(wrap(sb)); }
131 template<typename Mode, typename Ch>
132 mode_adapter<Mode, std::istream>
133 resolve(std::istream& is)
134 { return mode_adapter<Mode, std::istream>(wrap(is)); }
136 template<typename Mode, typename Ch>
137 mode_adapter<Mode, std::ostream>
138 resolve(std::ostream& os)
139 { return mode_adapter<Mode, std::ostream>(wrap(os)); }
141 template<typename Mode, typename Ch>
142 mode_adapter<Mode, std::iostream>
143 resolve(std::iostream& io)
144 { return mode_adapter<Mode, std::iostream>(wrap(io)); }
146 template<typename Mode, typename Ch, std::size_t N>
147 array_adapter<Mode, Ch> resolve(Ch (&array)[N])
148 { return array_adapter<Mode, Ch>(array); }
150 template<typename Mode, typename Ch, typename Iter>
151 range_adapter< Mode, boost::iterator_range<Iter> >
152 resolve(const boost::iterator_range<Iter>& rng)
153 { return range_adapter< Mode, boost::iterator_range<Iter> >(rng); }
155 # endif // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------//
156 #else // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //----------------//
158 template<typename Mode, typename Ch, typename T>
159 struct resolve_traits {
160 // Note: test for is_iterator_range must come before test for output
161 // iterator.
162 typedef typename
163 iostreams::select< // Disambiguation for Tru64.
164 is_std_io<T>,
165 mode_adapter<Mode, T>,
166 is_iterator_range<T>,
167 range_adapter<Mode, T>,
168 is_dereferenceable<T>,
169 output_iterator_adapter<Mode, Ch, T>,
170 is_array<T>,
171 array_adapter<Mode, T>,
172 else_,
173 #if !BOOST_WORKAROUND(__BORLANDC__, < 0x600)
174 const T&
175 #else
177 #endif
178 >::type type;
181 template<typename Mode, typename Ch, typename T>
182 typename resolve_traits<Mode, Ch, T>::type
183 resolve(const T& t, mpl::true_)
184 { // Bad overload resolution.
185 typedef typename resolve_traits<Mode, Ch, T>::type return_type;
186 return return_type(wrap(const_cast<T&>(t)));
189 template<typename Mode, typename Ch, typename T>
190 typename resolve_traits<Mode, Ch, T>::type
191 resolve(const T& t, mpl::false_)
193 typedef typename resolve_traits<Mode, Ch, T>::type return_type;
194 return return_type(t);
197 template<typename Mode, typename Ch, typename T>
198 typename resolve_traits<Mode, Ch, T>::type
199 resolve(const T& t BOOST_IOSTREAMS_DISABLE_IF_STREAM(T))
200 { return resolve<Mode, Ch>(t, is_std_io<T>()); }
202 # if !BOOST_WORKAROUND(__BORLANDC__, < 0x600) && \
203 !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) && \
204 !defined(__GNUC__) // ---------------------------------------------------//
206 template<typename Mode, typename Ch, typename T>
207 typename resolve_traits<Mode, Ch, T>::type
208 resolve(T& t, mpl::true_)
210 typedef typename resolve_traits<Mode, Ch, T>::type return_type;
211 return return_type(wrap(t));
214 template<typename Mode, typename Ch, typename T>
215 typename resolve_traits<Mode, Ch, T>::type
216 resolve(T& t, mpl::false_)
218 typedef typename resolve_traits<Mode, Ch, T>::type return_type;
219 return return_type(t);
222 template<typename Mode, typename Ch, typename T>
223 typename resolve_traits<Mode, Ch, T>::type
224 resolve(T& t BOOST_IOSTREAMS_ENABLE_IF_STREAM(T))
225 { return resolve<Mode, Ch>(t, is_std_io<T>()); }
227 # endif // Borland 5.x, VC6-7.0 or GCC 2.9x //--------------------------------//
228 #endif // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //---------------//
230 } } } // End namespaces detail, iostreams, boost.
232 #include <boost/iostreams/detail/config/enable_warnings.hpp> // VC7.1 4224.
234 #endif // BOOST_IOSTREAMS_DETAIL_RESOLVE_HPP_INCLUDED