| 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 BOOST_IOSTREAMS_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED |
| 9 | #define BOOST_IOSTREAMS_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED |
| 10 | |
| 11 | #if defined(_MSC_VER) |
| 12 | # pragma once |
| 13 | #endif |
| 14 | |
| 15 | #include <boost/config.hpp> // SFINAE, MSVC, put ptrdiff_t in std. |
| 16 | #include <algorithm> // copy, min. |
| 17 | #include <cstddef> // ptrdiff_t. |
| 18 | #include <boost/detail/workaround.hpp> |
| 19 | #include <boost/iostreams/categories.hpp> |
| 20 | #include <boost/iostreams/detail/config/limits.hpp> // forwarding. |
| 21 | #include <boost/iostreams/detail/config/wide_streams.hpp> // locale. |
| 22 | #include <boost/iostreams/detail/double_object.hpp> |
| 23 | #include <boost/iostreams/detail/error.hpp> |
| 24 | #include <boost/iostreams/detail/ios.hpp> // openmode, seekdir, int types. |
| 25 | #include <boost/iostreams/traits.hpp> // mode_of, is_direct. |
| 26 | #include <boost/iostreams/operations.hpp> |
| 27 | #include <boost/mpl/bool.hpp> |
| 28 | #include <boost/mpl/or.hpp> |
| 29 | #include <boost/preprocessor/iteration/local.hpp> |
| 30 | #include <boost/preprocessor/repetition/enum_binary_params.hpp> |
| 31 | #include <boost/preprocessor/repetition/enum_params.hpp> |
| 32 | #include <boost/static_assert.hpp> |
| 33 | #include <boost/throw_exception.hpp> |
| 34 | #include <boost/type_traits/is_convertible.hpp> |
| 35 | |
| 36 | // Must come last. |
| 37 | #include <boost/iostreams/detail/config/disable_warnings.hpp> // VC7.1 |
| 38 | |
| 39 | namespace boost { namespace iostreams { namespace detail { |
| 40 | |
| 41 | //------------------Definition of direct_adapter_base-------------------------// |
| 42 | |
| 43 | // Put all initialization in base class to faciliate forwarding. |
| 44 | template<typename Direct> |
| 45 | class direct_adapter_base { |
| 46 | public: |
| 47 | typedef typename char_type_of<Direct>::type char_type; |
| 48 | typedef typename mode_of<Direct>::type mode_type; |
| 49 | struct category |
| 50 | : mode_type, |
| 51 | device_tag, |
| 52 | closable_tag |
| 53 | #ifndef BOOST_IOSTREAMS_NO_LOCALE |
| 54 | , localizable_tag |
| 55 | #endif |
| 56 | { }; |
| 57 | protected: |
| 58 | explicit direct_adapter_base(const Direct& d); |
| 59 | typedef is_convertible<category, two_sequence> is_double; |
| 60 | struct pointers { |
| 61 | pointers() : beg(0), ptr(0), end(0) { } |
| 62 | char_type *beg, *ptr, *end; |
| 63 | }; |
| 64 | void init_input(mpl::true_); |
| 65 | void init_input(mpl::false_) { } |
| 66 | void init_output(mpl::true_); |
| 67 | void init_output(mpl::false_) { } |
| 68 | double_object<pointers, is_double> ptrs_; |
| 69 | Direct d_; |
| 70 | }; |
| 71 | |
| 72 | template<typename Direct> |
| 73 | class direct_adapter : private direct_adapter_base<Direct> { |
| 74 | private: |
| 75 | typedef direct_adapter_base<Direct> base_type; |
| 76 | typedef typename base_type::pointers pointers; |
| 77 | typedef typename base_type::is_double is_double; |
| 78 | using base_type::ptrs_; |
| 79 | using base_type::d_; |
| 80 | public: |
| 81 | typedef typename base_type::char_type char_type; |
| 82 | typedef typename base_type::category category; |
| 83 | |
| 84 | // Constructors |
| 85 | |
| 86 | #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1310) |
| 87 | direct_adapter(const Direct& d) : base_type(d) { } |
| 88 | direct_adapter(const direct_adapter& d) : base_type(d) { } |
| 89 | # define BOOST_PP_LOCAL_LIMITS (1, BOOST_IOSTREAMS_MAX_FORWARDING_ARITY) |
| 90 | #else |
| 91 | template<typename U> |
| 92 | struct is_direct |
| 93 | : mpl::or_< |
| 94 | is_same<U, direct_adapter<Direct> >, |
| 95 | is_same<U, Direct> |
| 96 | > |
| 97 | { }; |
| 98 | template<typename U> |
| 99 | direct_adapter(const U& u) |
| 100 | : base_type(forward(u, is_direct<U>())) |
| 101 | { } |
| 102 | # define BOOST_PP_LOCAL_LIMITS (2, BOOST_IOSTREAMS_MAX_FORWARDING_ARITY) |
| 103 | #endif |
| 104 | |
| 105 | #define BOOST_PP_LOCAL_MACRO(n) \ |
| 106 | template<BOOST_PP_ENUM_PARAMS(n, typename P)> \ |
| 107 | direct_adapter(BOOST_PP_ENUM_BINARY_PARAMS(n, const P, &p)) \ |
| 108 | : base_type(Direct(BOOST_PP_ENUM_PARAMS(n, p))) \ |
| 109 | { } \ |
| 110 | /**/ |
| 111 | #include BOOST_PP_LOCAL_ITERATE() |
| 112 | #undef BOOST_PP_LOCAL_MACRO |
| 113 | |
| 114 | // Device interface. |
| 115 | |
| 116 | std::streamsize read(char_type* s, std::streamsize n); |
| 117 | std::streamsize write(const char_type* s, std::streamsize n); |
| 118 | std::streampos seek( stream_offset, BOOST_IOS::seekdir, |
| 119 | BOOST_IOS::openmode = BOOST_IOS::in | BOOST_IOS::out ); |
| 120 | void close(); |
| 121 | void close(BOOST_IOS::openmode which); |
| 122 | #ifndef BOOST_IOSTREAMS_NO_LOCALE |
| 123 | void imbue(const std::locale&); |
| 124 | #endif |
| 125 | |
| 126 | // Direct device access. |
| 127 | |
| 128 | Direct& operator*() { return d_; } |
| 129 | Direct* operator->() { return &d_; } |
| 130 | #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310) |
| 131 | private: |
| 132 | template<typename U> |
| 133 | static Direct forward(const U& u, mpl::true_) { return u; } |
| 134 | template<typename U> |
| 135 | static Direct forward(const U& u, mpl::false_) { return Direct(u); } |
| 136 | #endif |
| 137 | }; |
| 138 | |
| 139 | //--------------Definition of wrap_direct and unwrap_direct-------------------// |
| 140 | |
| 141 | template<typename Device> |
| 142 | struct wrap_direct_traits |
| 143 | : mpl::if_< |
| 144 | is_direct<Device>, |
| 145 | direct_adapter<Device>, |
| 146 | Device |
| 147 | > |
| 148 | { }; |
| 149 | |
| 150 | template<typename Device> |
| 151 | typename wrap_direct_traits<Device>::type |
| 152 | inline wrap_direct(Device dev) |
| 153 | { |
| 154 | typedef typename wrap_direct_traits<Device>::type type; |
| 155 | return type(dev); |
| 156 | } |
| 157 | |
| 158 | template<typename Device> |
| 159 | inline Device& unwrap_direct(Device& d) { return d; } |
| 160 | |
| 161 | template<typename Device> |
| 162 | inline Device& unwrap_direct(direct_adapter<Device>& d) { return *d; } |
| 163 | |
| 164 | //--------------Implementation of direct_adapter_base-------------------------// |
| 165 | |
| 166 | template<typename Direct> |
| 167 | direct_adapter_base<Direct>::direct_adapter_base(const Direct& d) : d_(d) |
| 168 | { |
| 169 | init_input(is_convertible<category, input>()); |
| 170 | init_output(is_convertible<category, output>()); |
| 171 | } |
| 172 | |
| 173 | template<typename Direct> |
| 174 | void direct_adapter_base<Direct>::init_input(mpl::true_) |
| 175 | { |
| 176 | std::pair<char_type*, char_type*> seq = iostreams::input_sequence(d_); |
| 177 | ptrs_.first().beg = seq.first; |
| 178 | ptrs_.first().ptr = seq.first; |
| 179 | ptrs_.first().end = seq.second; |
| 180 | } |
| 181 | |
| 182 | template<typename Direct> |
| 183 | void direct_adapter_base<Direct>::init_output(mpl::true_) |
| 184 | { |
| 185 | std::pair<char_type*, char_type*> seq = iostreams::output_sequence(d_); |
| 186 | ptrs_.second().beg = seq.first; |
| 187 | ptrs_.second().ptr = seq.first; |
| 188 | ptrs_.second().end = seq.second; |
| 189 | } |
| 190 | |
| 191 | //--------------Implementation of direct_adapter------------------------------// |
| 192 | |
| 193 | template<typename Direct> |
| 194 | inline std::streamsize direct_adapter<Direct>::read |
| 195 | (char_type* s, std::streamsize n) |
| 196 | { |
| 197 | using namespace std; |
| 198 | pointers& get = ptrs_.first(); |
| 199 | std::streamsize avail = |
| 200 | static_cast<std::streamsize>(get.end - get.ptr); |
| 201 | std::streamsize result = (std::min)(a: n, b: avail); |
| 202 | std::copy(get.ptr, get.ptr + result, s); |
| 203 | get.ptr += result; |
| 204 | return result != 0 ? result : -1; |
| 205 | } |
| 206 | |
| 207 | template<typename Direct> |
| 208 | inline std::streamsize direct_adapter<Direct>::write |
| 209 | (const char_type* s, std::streamsize n) |
| 210 | { |
| 211 | using namespace std; |
| 212 | pointers& put = ptrs_.second(); |
| 213 | if (n > static_cast<std::streamsize>(put.end - put.ptr)) |
| 214 | boost::throw_exception(e: write_area_exhausted()); |
| 215 | std::copy(s, s + n, put.ptr); |
| 216 | put.ptr += n; |
| 217 | return n; |
| 218 | } |
| 219 | |
| 220 | template<typename Direct> |
| 221 | inline std::streampos direct_adapter<Direct>::seek |
| 222 | ( stream_offset off, BOOST_IOS::seekdir way, |
| 223 | BOOST_IOS::openmode which ) |
| 224 | { |
| 225 | using namespace std; |
| 226 | pointers& get = ptrs_.first(); |
| 227 | pointers& put = ptrs_.second(); |
| 228 | if (way == BOOST_IOS::cur && get.ptr != put.ptr) |
| 229 | boost::throw_exception(e: bad_seek()); |
| 230 | ptrdiff_t next = 0; |
| 231 | if ((which & BOOST_IOS::in) || !is_double::value) { |
| 232 | if (way == BOOST_IOS::beg) |
| 233 | next = off; |
| 234 | else if (way == BOOST_IOS::cur) |
| 235 | next = get.ptr - get.beg + off; |
| 236 | else |
| 237 | next = get.end - get.beg + off; |
| 238 | if (next >= 0 && next <= get.end - get.beg) |
| 239 | get.ptr = get.beg + next; |
| 240 | else |
| 241 | boost::throw_exception(e: bad_seek()); |
| 242 | } |
| 243 | if ((which & BOOST_IOS::out) && is_double::value) { |
| 244 | if (way == BOOST_IOS::beg) |
| 245 | next = off; |
| 246 | else if (way == BOOST_IOS::cur) |
| 247 | next = put.ptr - put.beg + off; |
| 248 | else |
| 249 | next = put.end - put.beg + off; |
| 250 | if (next >= 0 && next <= put.end - put.beg) |
| 251 | put.ptr = put.beg + next; |
| 252 | else |
| 253 | boost::throw_exception(e: bad_seek()); |
| 254 | } |
| 255 | return offset_to_position(off: next); |
| 256 | } |
| 257 | |
| 258 | template<typename Direct> |
| 259 | void direct_adapter<Direct>::close() |
| 260 | { |
| 261 | BOOST_STATIC_ASSERT((!is_convertible<category, two_sequence>::value)); |
| 262 | detail::close_all(d_); |
| 263 | } |
| 264 | |
| 265 | template<typename Direct> |
| 266 | void direct_adapter<Direct>::close(BOOST_IOS::openmode which) |
| 267 | { |
| 268 | BOOST_STATIC_ASSERT((is_convertible<category, two_sequence>::value)); |
| 269 | boost::iostreams::close(d_, which); |
| 270 | } |
| 271 | |
| 272 | #ifndef BOOST_IOSTREAMS_NO_LOCALE |
| 273 | template<typename Direct> |
| 274 | void direct_adapter<Direct>::imbue(const std::locale& loc) |
| 275 | { boost::iostreams::imbue(d_, loc); } |
| 276 | #endif |
| 277 | |
| 278 | } } } // End namespaces detail, iostreams, boost. |
| 279 | |
| 280 | #include <boost/iostreams/detail/config/enable_warnings.hpp> |
| 281 | |
| 282 | #endif // #ifndef BOOST_IOSTREAMS_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED |
| 283 | |