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.)
6 // See http://www.boost.org/libs/iostreams for documentation.
8 #ifndef BOOST_IOSTREAMS_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED
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>
37 #include <boost/iostreams/detail/config/disable_warnings.hpp> // VC7.1
39 namespace boost { namespace iostreams { namespace detail {
41 //------------------Definition of direct_adapter_base-------------------------//
43 // Put all initialization in base class to faciliate forwarding.
44 template<typename Direct>
45 class direct_adapter_base {
47 typedef typename char_type_of<Direct>::type char_type;
48 typedef typename mode_of<Direct>::type mode_type;
53 #ifndef BOOST_IOSTREAMS_NO_LOCALE
58 explicit direct_adapter_base(const Direct& d);
59 typedef is_convertible<category, two_sequence> is_double;
61 char_type *beg, *ptr, *end;
63 void init_input(mpl::true_);
64 void init_input(mpl::false_) { }
65 void init_output(mpl::true_);
66 void init_output(mpl::false_) { }
67 double_object<pointers, is_double> ptrs_;
71 template<typename Direct>
72 class direct_adapter : private direct_adapter_base<Direct> {
74 typedef direct_adapter_base<Direct> base_type;
75 typedef typename base_type::pointers pointers;
76 typedef typename base_type::is_double is_double;
77 using base_type::ptrs_;
80 typedef typename base_type::char_type char_type;
81 typedef typename base_type::category category;
85 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
86 direct_adapter(const Direct& d) : base_type(d) { }
87 direct_adapter(const direct_adapter& d) : base_type(d) { }
88 # define BOOST_PP_LOCAL_LIMITS (1, BOOST_IOSTREAMS_MAX_FORWARDING_ARITY)
93 is_same<U, direct_adapter<Direct> >,
98 direct_adapter(const U& u)
99 : base_type(forward(u, is_direct<U>()))
101 # define BOOST_PP_LOCAL_LIMITS (2, BOOST_IOSTREAMS_MAX_FORWARDING_ARITY)
104 #define BOOST_PP_LOCAL_MACRO(n) \
105 template<BOOST_PP_ENUM_PARAMS(n, typename P)> \
106 direct_adapter(BOOST_PP_ENUM_BINARY_PARAMS(n, const P, &p)) \
107 : base_type(Direct(BOOST_PP_ENUM_PARAMS(n, p))) \
110 #include BOOST_PP_LOCAL_ITERATE()
111 #undef BOOST_PP_LOCAL_MACRO
115 std::streamsize read(char_type* s, std::streamsize n);
116 std::streamsize write(const char_type* s, std::streamsize n);
117 std::streampos seek( stream_offset, BOOST_IOS::seekdir,
118 BOOST_IOS::openmode = BOOST_IOS::in | BOOST_IOS::out );
120 void close(BOOST_IOS::openmode which);
121 #ifndef BOOST_IOSTREAMS_NO_LOCALE
122 void imbue(const std::locale&);
125 // Direct device access.
127 Direct& operator*() { return d_; }
128 Direct* operator->() { return &d_; }
129 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
132 static Direct forward(const U& u, mpl::true_) { return u; }
134 static Direct forward(const U& u, mpl::false_) { return Direct(u); }
138 //--------------Definition of wrap_direct and unwrap_direct-------------------//
140 template<typename Device>
141 struct wrap_direct_traits
144 direct_adapter<Device>,
149 template<typename Device>
150 typename wrap_direct_traits<Device>::type
151 inline wrap_direct(Device dev)
153 typedef typename wrap_direct_traits<Device>::type type;
157 template<typename Device>
158 inline Device& unwrap_direct(Device& d) { return d; }
160 template<typename Device>
161 inline Device& unwrap_direct(direct_adapter<Device>& d) { return *d; }
163 //--------------Implementation of direct_adapter_base-------------------------//
165 template<typename Direct>
166 direct_adapter_base<Direct>::direct_adapter_base(const Direct& d) : d_(d)
168 init_input(is_convertible<category, input>());
169 init_output(is_convertible<category, output>());
172 template<typename Direct>
173 void direct_adapter_base<Direct>::init_input(mpl::true_)
175 std::pair<char_type*, char_type*> seq = iostreams::input_sequence(d_);
176 ptrs_.first().beg = seq.first;
177 ptrs_.first().ptr = seq.first;
178 ptrs_.first().end = seq.second;
181 template<typename Direct>
182 void direct_adapter_base<Direct>::init_output(mpl::true_)
184 std::pair<char_type*, char_type*> seq = iostreams::output_sequence(d_);
185 ptrs_.second().beg = seq.first;
186 ptrs_.second().ptr = seq.first;
187 ptrs_.second().end = seq.second;
190 //--------------Implementation of direct_adapter------------------------------//
192 template<typename Direct>
193 inline std::streamsize direct_adapter<Direct>::read
194 (char_type* s, std::streamsize n)
197 pointers& get = ptrs_.first();
198 std::streamsize avail =
199 static_cast<std::streamsize>(get.end - get.ptr);
200 std::streamsize result = (std::min)(n, avail);
201 std::copy(get.ptr, get.ptr + result, s);
203 return result != 0 ? result : -1;
206 template<typename Direct>
207 inline std::streamsize direct_adapter<Direct>::write
208 (const char_type* s, std::streamsize n)
211 pointers& put = ptrs_.second();
212 if (n > static_cast<std::streamsize>(put.end - put.ptr))
213 boost::throw_exception(write_area_exhausted());
214 std::copy(s, s + n, put.ptr);
219 template<typename Direct>
220 inline std::streampos direct_adapter<Direct>::seek
221 ( stream_offset off, BOOST_IOS::seekdir way,
222 BOOST_IOS::openmode which )
225 pointers& get = ptrs_.first();
226 pointers& put = ptrs_.second();
227 if (way == BOOST_IOS::cur && get.ptr != put.ptr)
228 boost::throw_exception(bad_seek());
230 if ((which & BOOST_IOS::in) || !is_double::value) {
231 if (way == BOOST_IOS::beg)
233 else if (way == BOOST_IOS::cur)
234 next = get.ptr - get.beg + off;
236 next = get.end - get.beg + off;
237 if (next >= 0 && next <= get.end - get.beg)
238 get.ptr = get.beg + next;
240 boost::throw_exception(bad_seek());
242 if ((which & BOOST_IOS::out) && is_double::value) {
243 if (way == BOOST_IOS::beg)
245 else if (way == BOOST_IOS::cur)
246 next = put.ptr - put.beg + off;
248 next = put.end - put.beg + off;
249 if (next >= 0 && next <= put.end - put.beg)
250 put.ptr = put.beg + next;
252 boost::throw_exception(bad_seek());
254 return offset_to_position(next);
257 template<typename Direct>
258 void direct_adapter<Direct>::close()
260 BOOST_STATIC_ASSERT((!is_convertible<category, two_sequence>::value));
261 detail::close_all(d_);
264 template<typename Direct>
265 void direct_adapter<Direct>::close(BOOST_IOS::openmode which)
267 BOOST_STATIC_ASSERT((is_convertible<category, two_sequence>::value));
268 boost::iostreams::close(d_, which);
271 #ifndef BOOST_IOSTREAMS_NO_LOCALE
272 template<typename Direct>
273 void direct_adapter<Direct>::imbue(const std::locale& loc)
274 { boost::iostreams::imbue(d_, loc); }
277 } } } // End namespaces detail, iostreams, boost.
279 #include <boost/iostreams/detail/config/enable_warnings.hpp>
281 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED