Jeff Thompson | 86b6d64 | 2013-10-17 15:01:56 -0700 | [diff] [blame^] | 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 | |
| 6 | // See http://www.boost.org/libs/iostreams for documentation. |
| 7 | |
| 8 | #ifndef NDNBOOST_IOSTREAMS_DETAIL_RESOLVE_HPP_INCLUDED |
| 9 | #define NDNBOOST_IOSTREAMS_DETAIL_RESOLVE_HPP_INCLUDED |
| 10 | |
| 11 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
| 12 | # pragma once |
| 13 | #endif |
| 14 | |
| 15 | #include <ndnboost/config.hpp> // partial spec, put size_t in std. |
| 16 | #include <cstddef> // std::size_t. |
| 17 | #include <ndnboost/detail/is_incrementable.hpp> |
| 18 | #include <ndnboost/detail/workaround.hpp> |
| 19 | #include <ndnboost/iostreams/detail/adapter/mode_adapter.hpp> |
| 20 | #include <ndnboost/iostreams/detail/adapter/output_iterator_adapter.hpp> |
| 21 | #include <ndnboost/iostreams/detail/adapter/range_adapter.hpp> |
| 22 | #include <ndnboost/iostreams/detail/config/gcc.hpp> |
| 23 | #include <ndnboost/iostreams/detail/config/overload_resolution.hpp> |
| 24 | #include <ndnboost/iostreams/detail/config/wide_streams.hpp> |
| 25 | #include <ndnboost/iostreams/detail/enable_if_stream.hpp> |
| 26 | #include <ndnboost/iostreams/detail/is_dereferenceable.hpp> |
| 27 | #include <ndnboost/iostreams/detail/is_iterator_range.hpp> |
| 28 | #include <ndnboost/iostreams/detail/select.hpp> |
| 29 | #include <ndnboost/iostreams/detail/wrap_unwrap.hpp> |
| 30 | #include <ndnboost/iostreams/device/array.hpp> |
| 31 | #include <ndnboost/iostreams/traits.hpp> |
| 32 | #include <ndnboost/mpl/and.hpp> |
| 33 | #include <ndnboost/mpl/bool.hpp> // true_. |
| 34 | #include <ndnboost/mpl/if.hpp> |
| 35 | #if !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, <= 1300) |
| 36 | # include <ndnboost/range/iterator_range.hpp> |
| 37 | #endif // #if NDNBOOST_WORKAROUND(NDNBOOST_MSVC, <= 1300) |
| 38 | #include <ndnboost/type_traits/is_array.hpp> |
| 39 | |
| 40 | // Must come last. |
| 41 | #include <ndnboost/iostreams/detail/config/disable_warnings.hpp> // VC7.1 C4224. |
| 42 | |
| 43 | namespace ndnboost { namespace iostreams { namespace detail { |
| 44 | |
| 45 | //------------------Definition of resolve-------------------------------------// |
| 46 | |
| 47 | #ifndef NDNBOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //-------------------------// |
| 48 | |
| 49 | template<typename Mode, typename Ch, typename T> |
| 50 | struct resolve_traits { |
| 51 | typedef typename |
| 52 | mpl::if_< |
| 53 | ndnboost::detail::is_incrementable<T>, |
| 54 | output_iterator_adapter<Mode, Ch, T>, |
| 55 | const T& |
| 56 | >::type type; |
| 57 | }; |
| 58 | |
| 59 | # ifndef NDNBOOST_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 | NDNBOOST_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 NDNBOOST_WORKAROUND(NDNBOOST_INTEL_CXX_VERSION, NDNBOOST_TESTED_AT(810)) ||\ |
| 69 | NDNBOOST_WORKAROUND(__MWERKS__, NDNBOOST_TESTED_AT(0x3205)) || \ |
| 70 | NDNBOOST_WORKAROUND(NDNBOOST_IOSTREAMS_GCC, NDNBOOST_TESTED_AT(400)) ||\ |
| 71 | NDNBOOST_WORKAROUND(__IBMCPP__, NDNBOOST_TESTED_AT(1110)) |
| 72 | /**/ |
| 73 | , typename disable_if< is_iterator_range<T> >::type* = 0 |
| 74 | #endif |
| 75 | ) |
| 76 | { |
| 77 | typedef typename resolve_traits<Mode, Ch, T>::type return_type; |
| 78 | return return_type(t); |
| 79 | } |
| 80 | |
| 81 | template<typename Mode, typename Ch, typename Tr> |
| 82 | mode_adapter< Mode, std::basic_streambuf<Ch, Tr> > |
| 83 | resolve(std::basic_streambuf<Ch, Tr>& sb) |
| 84 | { return mode_adapter< Mode, std::basic_streambuf<Ch, Tr> >(wrap(sb)); } |
| 85 | |
| 86 | template<typename Mode, typename Ch, typename Tr> |
| 87 | mode_adapter< Mode, std::basic_istream<Ch, Tr> > |
| 88 | resolve(std::basic_istream<Ch, Tr>& is) |
| 89 | { return mode_adapter< Mode, std::basic_istream<Ch, Tr> >(wrap(is)); } |
| 90 | |
| 91 | template<typename Mode, typename Ch, typename Tr> |
| 92 | mode_adapter< Mode, std::basic_ostream<Ch, Tr> > |
| 93 | resolve(std::basic_ostream<Ch, Tr>& os) |
| 94 | { return mode_adapter< Mode, std::basic_ostream<Ch, Tr> >(wrap(os)); } |
| 95 | |
| 96 | template<typename Mode, typename Ch, typename Tr> |
| 97 | mode_adapter< Mode, std::basic_iostream<Ch, Tr> > |
| 98 | resolve(std::basic_iostream<Ch, Tr>& io) |
| 99 | { return mode_adapter< Mode, std::basic_iostream<Ch, Tr> >(wrap(io)); } |
| 100 | |
| 101 | template<typename Mode, typename Ch, std::size_t N> |
| 102 | array_adapter<Mode, Ch> resolve(Ch (&array)[N]) |
| 103 | { return array_adapter<Mode, Ch>(array); } |
| 104 | |
| 105 | # if !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, <= 1300) |
| 106 | template<typename Mode, typename Ch, typename Iter> |
| 107 | range_adapter< Mode, ndnboost::iterator_range<Iter> > |
| 108 | resolve(const ndnboost::iterator_range<Iter>& rng) |
| 109 | { return range_adapter< Mode, ndnboost::iterator_range<Iter> >(rng); } |
| 110 | # endif // #if NDNBOOST_WORKAROUND(NDNBOOST_MSVC, <= 1300) |
| 111 | |
| 112 | # else // # ifndef NDNBOOST_IOSTREAMS_NO_STREAM_TEMPLATES //---------------------// |
| 113 | |
| 114 | template<typename Mode, typename Ch, typename T> |
| 115 | typename resolve_traits<Mode, Ch, T>::type |
| 116 | resolve( const T& t |
| 117 | NDNBOOST_IOSTREAMS_DISABLE_IF_STREAM(T) |
| 118 | #if defined(__GNUC__) |
| 119 | , typename disable_if< is_iterator_range<T> >::type* = 0 |
| 120 | #endif |
| 121 | ) |
| 122 | { |
| 123 | typedef typename resolve_traits<Mode, Ch, T>::type return_type; |
| 124 | return return_type(t); |
| 125 | } |
| 126 | |
| 127 | template<typename Mode, typename Ch> |
| 128 | mode_adapter<Mode, std::streambuf> |
| 129 | resolve(std::streambuf& sb) |
| 130 | { return mode_adapter<Mode, std::streambuf>(wrap(sb)); } |
| 131 | |
| 132 | template<typename Mode, typename Ch> |
| 133 | mode_adapter<Mode, std::istream> |
| 134 | resolve(std::istream& is) |
| 135 | { return mode_adapter<Mode, std::istream>(wrap(is)); } |
| 136 | |
| 137 | template<typename Mode, typename Ch> |
| 138 | mode_adapter<Mode, std::ostream> |
| 139 | resolve(std::ostream& os) |
| 140 | { return mode_adapter<Mode, std::ostream>(wrap(os)); } |
| 141 | |
| 142 | template<typename Mode, typename Ch> |
| 143 | mode_adapter<Mode, std::iostream> |
| 144 | resolve(std::iostream& io) |
| 145 | { return mode_adapter<Mode, std::iostream>(wrap(io)); } |
| 146 | |
| 147 | template<typename Mode, typename Ch, std::size_t N> |
| 148 | array_adapter<Mode, Ch> resolve(Ch (&array)[N]) |
| 149 | { return array_adapter<Mode, Ch>(array); } |
| 150 | |
| 151 | template<typename Mode, typename Ch, typename Iter> |
| 152 | range_adapter< Mode, ndnboost::iterator_range<Iter> > |
| 153 | resolve(const ndnboost::iterator_range<Iter>& rng) |
| 154 | { return range_adapter< Mode, ndnboost::iterator_range<Iter> >(rng); } |
| 155 | |
| 156 | # endif // # ifndef NDNBOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------// |
| 157 | #else // #ifndef NDNBOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //----------------// |
| 158 | |
| 159 | template<typename Mode, typename Ch, typename T> |
| 160 | struct resolve_traits { |
| 161 | // Note: test for is_iterator_range must come before test for output |
| 162 | // iterator. |
| 163 | typedef typename |
| 164 | iostreams::select< // Disambiguation for Tru64. |
| 165 | is_std_io<T>, |
| 166 | mode_adapter<Mode, T>, |
| 167 | is_iterator_range<T>, |
| 168 | range_adapter<Mode, T>, |
| 169 | is_dereferenceable<T>, |
| 170 | output_iterator_adapter<Mode, Ch, T>, |
| 171 | is_array<T>, |
| 172 | array_adapter<Mode, T>, |
| 173 | else_, |
| 174 | #if !NDNBOOST_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 NDNBOOST_IOSTREAMS_DISABLE_IF_STREAM(T)) |
| 201 | { return resolve<Mode, Ch>(t, is_std_io<T>()); } |
| 202 | |
| 203 | # if !NDNBOOST_WORKAROUND(__BORLANDC__, < 0x600) && \ |
| 204 | !NDNBOOST_WORKAROUND(NDNBOOST_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 NDNBOOST_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 NDNBOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //---------------// |
| 230 | |
| 231 | } } } // End namespaces detail, iostreams, boost. |
| 232 | |
| 233 | #include <ndnboost/iostreams/detail/config/enable_warnings.hpp> // VC7.1 4224. |
| 234 | |
| 235 | #endif // NDNBOOST_IOSTREAMS_DETAIL_RESOLVE_HPP_INCLUDED |