Chris@16
|
1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
|
Chris@16
|
2 // (C) Copyright 2005-2007 Jonathan Turkanis
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
|
Chris@16
|
5
|
Chris@16
|
6 // See http://www.boost.org/libs/iostreams for documentation.
|
Chris@16
|
7
|
Chris@16
|
8 #ifndef BOOST_IOSTREAMS_DETAIL_NON_BLOCKING_ADAPTER_HPP_INCLUDED
|
Chris@16
|
9 #define BOOST_IOSTREAMS_DETAIL_NON_BLOCKING_ADAPTER_HPP_INCLUDED
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/iostreams/detail/ios.hpp> // streamsize, seekdir, openmode.
|
Chris@16
|
12 #include <boost/iostreams/read.hpp>
|
Chris@16
|
13 #include <boost/iostreams/seek.hpp>
|
Chris@16
|
14 #include <boost/iostreams/traits.hpp>
|
Chris@16
|
15 #include <boost/iostreams/write.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 namespace boost { namespace iostreams {
|
Chris@16
|
18
|
Chris@16
|
19 template<typename Device>
|
Chris@16
|
20 class non_blocking_adapter {
|
Chris@16
|
21 public:
|
Chris@16
|
22 typedef typename char_type_of<Device>::type char_type;
|
Chris@16
|
23 struct category
|
Chris@16
|
24 : mode_of<Device>::type, device_tag
|
Chris@16
|
25 { };
|
Chris@16
|
26 explicit non_blocking_adapter(Device& dev) : device_(dev) { }
|
Chris@16
|
27 std::streamsize read(char_type* s, std::streamsize n)
|
Chris@16
|
28 {
|
Chris@16
|
29 std::streamsize result = 0;
|
Chris@16
|
30 while (result < n) {
|
Chris@16
|
31 std::streamsize amt = iostreams::read(device_, s, n);
|
Chris@16
|
32 if (amt == -1)
|
Chris@16
|
33 break;
|
Chris@16
|
34 result += amt;
|
Chris@16
|
35 }
|
Chris@16
|
36 return result != 0 ? result : -1;
|
Chris@16
|
37 }
|
Chris@16
|
38 std::streamsize write(const char_type* s, std::streamsize n)
|
Chris@16
|
39 {
|
Chris@16
|
40 std::streamsize result = 0;
|
Chris@16
|
41 while (result < n) {
|
Chris@16
|
42 std::streamsize amt =
|
Chris@16
|
43 iostreams::write(device_, s + result, n - result);
|
Chris@16
|
44 result += amt;
|
Chris@16
|
45 }
|
Chris@16
|
46 return result;
|
Chris@16
|
47 }
|
Chris@16
|
48 std::streampos seek( stream_offset off, BOOST_IOS::seekdir way,
|
Chris@16
|
49 BOOST_IOS::openmode which =
|
Chris@16
|
50 BOOST_IOS::in | BOOST_IOS::out )
|
Chris@16
|
51 { return iostreams::seek(device_, off, way, which); }
|
Chris@16
|
52 public:
|
Chris@16
|
53 non_blocking_adapter& operator=(const non_blocking_adapter&);
|
Chris@16
|
54 Device& device_;
|
Chris@16
|
55 };
|
Chris@16
|
56
|
Chris@16
|
57 } } // End namespace iostreams.
|
Chris@16
|
58
|
Chris@16
|
59 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_NON_BLOCKING_ADAPTER_HPP_INCLUDED
|