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