1 | // (C) Copyright Jonathan Turkanis 2005.
|
---|
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_NON_BLOCKING_ADAPTER_HPP_INCLUDED
|
---|
8 | #define BOOST_IOSTREAMS_DETAIL_NON_BLOCKING_ADAPTER_HPP_INCLUDED
|
---|
9 |
|
---|
10 | #include <boost/iostreams/detail/ios.hpp> // streamsize, seekdir, openmode.
|
---|
11 | #include <boost/iostreams/read.hpp>
|
---|
12 | #include <boost/iostreams/seek.hpp>
|
---|
13 | #include <boost/iostreams/traits.hpp>
|
---|
14 | #include <boost/iostreams/write.hpp>
|
---|
15 |
|
---|
16 | namespace boost { namespace iostreams {
|
---|
17 |
|
---|
18 | template<typename Device>
|
---|
19 | class non_blocking_adapter {
|
---|
20 | public:
|
---|
21 | typedef typename char_type_of<Device>::type char_type;
|
---|
22 | struct category
|
---|
23 | : mode_of<Device>::type, device_tag
|
---|
24 | { };
|
---|
25 | explicit non_blocking_adapter(Device& dev) : device_(dev) { }
|
---|
26 | std::streamsize read(char_type* s, std::streamsize n)
|
---|
27 | {
|
---|
28 | std::streamsize result = 0;
|
---|
29 | while (result < n) {
|
---|
30 | std::streamsize amt = iostreams::read(device_, s, n);
|
---|
31 | if (amt == -1)
|
---|
32 | break;
|
---|
33 | result += amt;
|
---|
34 | }
|
---|
35 | return result != 0 ? result : -1;
|
---|
36 | }
|
---|
37 | std::streamsize write(const char_type* s, std::streamsize n)
|
---|
38 | {
|
---|
39 | std::streamsize result = 0;
|
---|
40 | while (result < n) {
|
---|
41 | std::streamsize amt =
|
---|
42 | iostreams::write(device_, s + result, n - result);
|
---|
43 | result += amt;
|
---|
44 | }
|
---|
45 | return result;
|
---|
46 | }
|
---|
47 | std::streampos seek( stream_offset off, BOOST_IOS::seekdir way,
|
---|
48 | BOOST_IOS::openmode which =
|
---|
49 | BOOST_IOS::in | BOOST_IOS::out )
|
---|
50 | { return iostreams::seek(device_, off, way, which); }
|
---|
51 | public:
|
---|
52 | Device& device_;
|
---|
53 | };
|
---|
54 |
|
---|
55 | } } // End namespace iostreams.
|
---|
56 |
|
---|
57 | #endif // #ifndef BOOST_IOSTREAMS_DETAIL_NON_BLOCKING_ADAPTER_HPP_INCLUDED
|
---|