os/ossrv/ossrv_pub/boost_apis/boost/iostreams/detail/resolve.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // (C) Copyright Jonathan Turkanis 2003.
     2 // Distributed under the Boost Software License, Version 1.0. (See accompanying
     3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
     4 
     5 // See http://www.boost.org/libs/iostreams for documentation.
     6 
     7 #ifndef BOOST_IOSTREAMS_DETAIL_RESOLVE_HPP_INCLUDED
     8 #define BOOST_IOSTREAMS_DETAIL_RESOLVE_HPP_INCLUDED
     9 
    10 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
    11 # pragma once
    12 #endif              
    13 
    14 #include <boost/config.hpp> // partial spec, put size_t in std.
    15 #include <cstddef>          // std::size_t.
    16 #include <boost/detail/is_incrementable.hpp>
    17 #include <boost/detail/workaround.hpp>
    18 #include <boost/iostreams/detail/adapter/mode_adapter.hpp>
    19 #include <boost/iostreams/detail/adapter/output_iterator_adapter.hpp>
    20 #include <boost/iostreams/detail/adapter/range_adapter.hpp>
    21 #include <boost/iostreams/detail/config/gcc.hpp>
    22 #include <boost/iostreams/detail/config/overload_resolution.hpp>
    23 #include <boost/iostreams/detail/config/wide_streams.hpp>
    24 #include <boost/iostreams/detail/enable_if_stream.hpp>
    25 #include <boost/iostreams/detail/is_dereferenceable.hpp>
    26 #include <boost/iostreams/detail/is_iterator_range.hpp>
    27 #include <boost/iostreams/detail/select.hpp>
    28 #include <boost/iostreams/detail/wrap_unwrap.hpp>
    29 #include <boost/iostreams/device/array.hpp>
    30 #include <boost/iostreams/traits.hpp>
    31 #include <boost/mpl/and.hpp>
    32 #include <boost/mpl/bool.hpp> // true_.
    33 #include <boost/mpl/if.hpp>
    34 #include <boost/range/iterator_range.hpp>
    35 #include <boost/type_traits/is_array.hpp>
    36 
    37 // Must come last.
    38 #include <boost/iostreams/detail/config/disable_warnings.hpp> // VC7.1 C4224.
    39 
    40 namespace boost { namespace iostreams { namespace detail {
    41 
    42 //------------------Definition of resolve-------------------------------------//
    43 
    44 #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //-------------------------//
    45 
    46 template<typename Mode, typename Ch, typename T>
    47 struct resolve_traits {
    48     typedef typename 
    49             mpl::if_<
    50                 mpl::and_<
    51                     boost::detail::is_incrementable<T>, // Must come first
    52                     is_dereferenceable<T>               // for CW 9.[0-4]
    53                 >,
    54                 output_iterator_adapter<Mode, Ch, T>,
    55                 const T&
    56             >::type type;
    57 };
    58 
    59 # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-------------------------------//
    60 
    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)
    65 
    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
    74          )
    75 {
    76     typedef typename resolve_traits<Mode, Ch, T>::type return_type;
    77     return return_type(t);
    78 }
    79 
    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)); }
    84 
    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)); }
    89 
    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)); }
    94 
    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)); }
    99 
   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); }
   103 
   104 template<typename Mode, typename Ch, typename Iter>
   105 range_adapter< Mode, boost::iterator_range<Iter> > 
   106 resolve(const boost::iterator_range<Iter>& rng)
   107 { return range_adapter< Mode, boost::iterator_range<Iter> >(rng); }
   108 
   109 # else // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //---------------------//
   110 
   111 template<typename Mode, typename Ch, typename T>
   112 typename resolve_traits<Mode, Ch, T>::type
   113 resolve( const T& t 
   114          BOOST_IOSTREAMS_DISABLE_IF_STREAM(T)
   115          #if defined(__GNUC__)
   116          , typename disable_if< is_iterator_range<T> >::type* = 0
   117          #endif
   118          )
   119 {
   120     typedef typename resolve_traits<Mode, Ch, T>::type return_type;
   121     return return_type(t);
   122 }
   123 
   124 template<typename Mode, typename Ch>
   125 mode_adapter<Mode, std::streambuf> 
   126 resolve(std::streambuf& sb) 
   127 { return mode_adapter<Mode, std::streambuf>(wrap(sb)); }
   128 
   129 template<typename Mode, typename Ch>
   130 mode_adapter<Mode, std::istream> 
   131 resolve(std::istream& is)
   132 { return mode_adapter<Mode, std::istream>(wrap(is)); }
   133 
   134 template<typename Mode, typename Ch>
   135 mode_adapter<Mode, std::ostream> 
   136 resolve(std::ostream& os)
   137 { return mode_adapter<Mode, std::ostream>(wrap(os)); }
   138 
   139 template<typename Mode, typename Ch>
   140 mode_adapter<Mode, std::iostream> 
   141 resolve(std::iostream& io)
   142 { return mode_adapter<Mode, std::iostream>(wrap(io)); }
   143 
   144 template<typename Mode, typename Ch, std::size_t N>
   145 array_adapter<Mode, Ch> resolve(Ch (&array)[N])
   146 { return array_adapter<Mode, Ch>(array); }
   147 
   148 template<typename Mode, typename Ch, typename Iter>
   149 range_adapter< Mode, boost::iterator_range<Iter> > 
   150 resolve(const boost::iterator_range<Iter>& rng)
   151 { return range_adapter< Mode, boost::iterator_range<Iter> >(rng); }
   152 
   153 # endif // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------//
   154 #else // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //----------------//
   155 
   156 template<typename Mode, typename Ch, typename T>
   157 struct resolve_traits {
   158     // Note: test for is_iterator_range must come before test for output
   159     // iterator.
   160     typedef typename 
   161             iostreams::select<  // Disambiguation for Tru64.
   162                 is_std_io<T>,
   163                 mode_adapter<Mode, T>,
   164                 is_iterator_range<T>,
   165                 range_adapter<Mode, T>,
   166                 mpl::and_<
   167                     is_dereferenceable<T>,
   168                     boost::detail::is_incrementable<T>
   169                 >,
   170                 output_iterator_adapter<Mode, Ch, T>,
   171                 is_array<T>,
   172                 array_adapter<Mode, T>,
   173                 else_,
   174                 #if !BOOST_WORKAROUND(__BORLANDC__, < 0x600)
   175                     const T&
   176                 #else
   177                     T
   178                 #endif
   179             >::type type;
   180 };
   181 
   182 template<typename Mode, typename Ch, typename T>
   183 typename resolve_traits<Mode, Ch, T>::type 
   184 resolve(const T& t, mpl::true_)
   185 {   // Bad overload resolution.
   186     typedef typename resolve_traits<Mode, Ch, T>::type return_type;
   187     return return_type(wrap(const_cast<T&>(t)));
   188 }
   189 
   190 template<typename Mode, typename Ch, typename T>
   191 typename resolve_traits<Mode, Ch, T>::type 
   192 resolve(const T& t, mpl::false_)
   193 { 
   194     typedef typename resolve_traits<Mode, Ch, T>::type return_type;
   195     return return_type(t);
   196 }
   197 
   198 template<typename Mode, typename Ch, typename T>
   199 typename resolve_traits<Mode, Ch, T>::type 
   200 resolve(const T& t BOOST_IOSTREAMS_DISABLE_IF_STREAM(T))
   201 { return resolve<Mode, Ch>(t, is_std_io<T>()); }
   202 
   203 # if !BOOST_WORKAROUND(__BORLANDC__, < 0x600) && \
   204      !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) && \
   205      !defined(__GNUC__) // ---------------------------------------------------//
   206 
   207 template<typename Mode, typename Ch, typename T>
   208 typename resolve_traits<Mode, Ch, T>::type 
   209 resolve(T& t, mpl::true_)
   210 { 
   211     typedef typename resolve_traits<Mode, Ch, T>::type return_type;
   212     return return_type(wrap(t));
   213 }
   214 
   215 template<typename Mode, typename Ch, typename T>
   216 typename resolve_traits<Mode, Ch, T>::type 
   217 resolve(T& t, mpl::false_)
   218 { 
   219     typedef typename resolve_traits<Mode, Ch, T>::type return_type;
   220     return return_type(t);
   221 }
   222 
   223 template<typename Mode, typename Ch, typename T>
   224 typename resolve_traits<Mode, Ch, T>::type 
   225 resolve(T& t BOOST_IOSTREAMS_ENABLE_IF_STREAM(T))
   226 { return resolve<Mode, Ch>(t, is_std_io<T>()); }
   227 
   228 # endif // Borland 5.x, VC6-7.0 or GCC 2.9x //--------------------------------//
   229 #endif // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //---------------//
   230 
   231 } } } // End namespaces detail, iostreams, boost.
   232 
   233 #include <boost/iostreams/detail/config/enable_warnings.hpp> // VC7.1 4224.
   234 
   235 #endif // BOOST_IOSTREAMS_DETAIL_RESOLVE_HPP_INCLUDED