resolve.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // See http://www.boost.org/libs/iostreams for documentation.
  6. #ifndef BOOST_IOSTREAMS_DETAIL_RESOLVE_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_DETAIL_RESOLVE_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <boost/config.hpp> // partial spec, put size_t in std.
  12. #include <cstddef> // std::size_t.
  13. #include <boost/detail/is_incrementable.hpp>
  14. #include <boost/detail/workaround.hpp>
  15. #include <boost/iostreams/detail/adapter/mode_adapter.hpp>
  16. #include <boost/iostreams/detail/adapter/output_iterator_adapter.hpp>
  17. #include <boost/iostreams/detail/adapter/range_adapter.hpp>
  18. #include <boost/iostreams/detail/config/gcc.hpp>
  19. #include <boost/iostreams/detail/config/overload_resolution.hpp>
  20. #include <boost/iostreams/detail/config/wide_streams.hpp>
  21. #include <boost/iostreams/detail/enable_if_stream.hpp>
  22. #include <boost/iostreams/detail/is_dereferenceable.hpp>
  23. #include <boost/iostreams/detail/is_iterator_range.hpp>
  24. #include <boost/iostreams/detail/select.hpp>
  25. #include <boost/iostreams/detail/wrap_unwrap.hpp>
  26. #include <boost/iostreams/device/array.hpp>
  27. #include <boost/iostreams/traits.hpp>
  28. #include <boost/mpl/and.hpp>
  29. #include <boost/mpl/bool.hpp> // true_.
  30. #include <boost/mpl/if.hpp>
  31. #include <boost/range/iterator_range.hpp>
  32. #include <boost/type_traits/is_array.hpp>
  33. // Must come last.
  34. #include <boost/iostreams/detail/config/disable_warnings.hpp> // VC7.1 C4224.
  35. namespace boost { namespace iostreams { namespace detail {
  36. //------------------Definition of resolve-------------------------------------//
  37. #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //-------------------------//
  38. template<typename Mode, typename Ch, typename T>
  39. struct resolve_traits {
  40. typedef typename
  41. mpl::if_<
  42. boost::detail::is_incrementable<T>,
  43. output_iterator_adapter<Mode, Ch, T>,
  44. const T&
  45. >::type type;
  46. };
  47. # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-------------------------------//
  48. template<typename Mode, typename Ch, typename T>
  49. typename resolve_traits<Mode, Ch, T>::type
  50. resolve( const T& t
  51. BOOST_IOSTREAMS_DISABLE_IF_STREAM(T)
  52. // I suspect that the compilers which require this workaround may
  53. // be correct, but I'm not sure why :(
  54. #if BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, BOOST_TESTED_AT(810)) ||\
  55. BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205)) || \
  56. BOOST_WORKAROUND(BOOST_IOSTREAMS_GCC, BOOST_TESTED_AT(400)) ||\
  57. BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(1110))
  58. /**/
  59. , typename disable_if< is_iterator_range<T> >::type* = 0
  60. #endif
  61. )
  62. {
  63. typedef typename resolve_traits<Mode, Ch, T>::type return_type;
  64. return return_type(t);
  65. }
  66. template<typename Mode, typename Ch, typename Tr>
  67. mode_adapter< Mode, std::basic_streambuf<Ch, Tr> >
  68. resolve(std::basic_streambuf<Ch, Tr>& sb)
  69. { return mode_adapter< Mode, std::basic_streambuf<Ch, Tr> >(wrap(sb)); }
  70. template<typename Mode, typename Ch, typename Tr>
  71. mode_adapter< Mode, std::basic_istream<Ch, Tr> >
  72. resolve(std::basic_istream<Ch, Tr>& is)
  73. { return mode_adapter< Mode, std::basic_istream<Ch, Tr> >(wrap(is)); }
  74. template<typename Mode, typename Ch, typename Tr>
  75. mode_adapter< Mode, std::basic_ostream<Ch, Tr> >
  76. resolve(std::basic_ostream<Ch, Tr>& os)
  77. { return mode_adapter< Mode, std::basic_ostream<Ch, Tr> >(wrap(os)); }
  78. template<typename Mode, typename Ch, typename Tr>
  79. mode_adapter< Mode, std::basic_iostream<Ch, Tr> >
  80. resolve(std::basic_iostream<Ch, Tr>& io)
  81. { return mode_adapter< Mode, std::basic_iostream<Ch, Tr> >(wrap(io)); }
  82. template<typename Mode, typename Ch, std::size_t N>
  83. array_adapter<Mode, Ch> resolve(Ch (&array)[N])
  84. { return array_adapter<Mode, Ch>(array); }
  85. template<typename Mode, typename Ch, typename Iter>
  86. range_adapter< Mode, boost::iterator_range<Iter> >
  87. resolve(const boost::iterator_range<Iter>& rng)
  88. { return range_adapter< Mode, boost::iterator_range<Iter> >(rng); }
  89. # else // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //---------------------//
  90. template<typename Mode, typename Ch, typename T>
  91. typename resolve_traits<Mode, Ch, T>::type
  92. resolve( const T& t
  93. BOOST_IOSTREAMS_DISABLE_IF_STREAM(T)
  94. #if defined(__GNUC__)
  95. , typename disable_if< is_iterator_range<T> >::type* = 0
  96. #endif
  97. )
  98. {
  99. typedef typename resolve_traits<Mode, Ch, T>::type return_type;
  100. return return_type(t);
  101. }
  102. template<typename Mode, typename Ch>
  103. mode_adapter<Mode, std::streambuf>
  104. resolve(std::streambuf& sb)
  105. { return mode_adapter<Mode, std::streambuf>(wrap(sb)); }
  106. template<typename Mode, typename Ch>
  107. mode_adapter<Mode, std::istream>
  108. resolve(std::istream& is)
  109. { return mode_adapter<Mode, std::istream>(wrap(is)); }
  110. template<typename Mode, typename Ch>
  111. mode_adapter<Mode, std::ostream>
  112. resolve(std::ostream& os)
  113. { return mode_adapter<Mode, std::ostream>(wrap(os)); }
  114. template<typename Mode, typename Ch>
  115. mode_adapter<Mode, std::iostream>
  116. resolve(std::iostream& io)
  117. { return mode_adapter<Mode, std::iostream>(wrap(io)); }
  118. template<typename Mode, typename Ch, std::size_t N>
  119. array_adapter<Mode, Ch> resolve(Ch (&array)[N])
  120. { return array_adapter<Mode, Ch>(array); }
  121. template<typename Mode, typename Ch, typename Iter>
  122. range_adapter< Mode, boost::iterator_range<Iter> >
  123. resolve(const boost::iterator_range<Iter>& rng)
  124. { return range_adapter< Mode, boost::iterator_range<Iter> >(rng); }
  125. # endif // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------//
  126. #else // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //----------------//
  127. template<typename Mode, typename Ch, typename T>
  128. struct resolve_traits {
  129. // Note: test for is_iterator_range must come before test for output
  130. // iterator.
  131. typedef typename
  132. iostreams::select< // Disambiguation for Tru64.
  133. is_std_io<T>,
  134. mode_adapter<Mode, T>,
  135. is_iterator_range<T>,
  136. range_adapter<Mode, T>,
  137. is_dereferenceable<T>,
  138. output_iterator_adapter<Mode, Ch, T>,
  139. is_array<T>,
  140. array_adapter<Mode, T>,
  141. else_,
  142. #if !BOOST_WORKAROUND(BOOST_BORLANDC, < 0x600)
  143. const T&
  144. #else
  145. T
  146. #endif
  147. >::type type;
  148. };
  149. template<typename Mode, typename Ch, typename T>
  150. typename resolve_traits<Mode, Ch, T>::type
  151. resolve(const T& t, mpl::true_)
  152. { // Bad overload resolution.
  153. typedef typename resolve_traits<Mode, Ch, T>::type return_type;
  154. return return_type(wrap(const_cast<T&>(t)));
  155. }
  156. template<typename Mode, typename Ch, typename T>
  157. typename resolve_traits<Mode, Ch, T>::type
  158. resolve(const T& t, mpl::false_)
  159. {
  160. typedef typename resolve_traits<Mode, Ch, T>::type return_type;
  161. return return_type(t);
  162. }
  163. template<typename Mode, typename Ch, typename T>
  164. typename resolve_traits<Mode, Ch, T>::type
  165. resolve(const T& t BOOST_IOSTREAMS_DISABLE_IF_STREAM(T))
  166. { return resolve<Mode, Ch>(t, is_std_io<T>()); }
  167. # if !BOOST_WORKAROUND(BOOST_BORLANDC, < 0x600) && \
  168. !defined(__GNUC__) // ---------------------------------------------------//
  169. template<typename Mode, typename Ch, typename T>
  170. typename resolve_traits<Mode, Ch, T>::type
  171. resolve(T& t, mpl::true_)
  172. {
  173. typedef typename resolve_traits<Mode, Ch, T>::type return_type;
  174. return return_type(wrap(t));
  175. }
  176. template<typename Mode, typename Ch, typename T>
  177. typename resolve_traits<Mode, Ch, T>::type
  178. resolve(T& t, mpl::false_)
  179. {
  180. typedef typename resolve_traits<Mode, Ch, T>::type return_type;
  181. return return_type(t);
  182. }
  183. template<typename Mode, typename Ch, typename T>
  184. typename resolve_traits<Mode, Ch, T>::type
  185. resolve(T& t BOOST_IOSTREAMS_ENABLE_IF_STREAM(T))
  186. { return resolve<Mode, Ch>(t, is_std_io<T>()); }
  187. # endif // Borland 5.x or GCC //--------------------------------//
  188. #endif // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //---------------//
  189. } } } // End namespaces detail, iostreams, boost.
  190. #include <boost/iostreams/detail/config/enable_warnings.hpp> // VC7.1 4224.
  191. #endif // BOOST_IOSTREAMS_DETAIL_RESOLVE_HPP_INCLUDED