resolve.hpp 8.5 KB

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