1 // (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.)
6 // See http://www.boost.org/libs/iostreams for documentation.
8 #ifndef BOOST_IOSTREAMS_EXAMPLE_CONTAINTER_DEVICE_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_EXAMPLE_CONTAINTER_DEVICE_HPP_INCLUDED
11 #include <algorithm> // copy, min.
13 #include <boost/config.hpp> // BOOST_NO_STDC_NAMESPACE.
14 #include <boost/iostreams/categories.hpp>
15 #include <boost/iostreams/detail/ios.hpp> // failure.
18 #include <boost/iostreams/detail/config/disable_warnings.hpp>
20 namespace boost { namespace iostreams { namespace example {
23 // Model of Source which reads from an STL-compatible sequence
24 // whose iterators are random-access iterators.
26 template<typename Container>
27 class container_source {
29 typedef typename Container::value_type char_type;
30 typedef source_tag category;
31 container_source(Container& container)
32 : container_(container), pos_(0)
34 std::streamsize read(char_type* s, std::streamsize n)
38 static_cast<std::streamsize>(container_.size() - pos_);
39 std::streamsize result = (min)(n, amt);
41 std::copy( container_.begin() + pos_,
42 container_.begin() + pos_ + result,
50 Container& container() { return container_; }
52 container_source operator=(const container_source&);
53 typedef typename Container::size_type size_type;
54 Container& container_;
59 // Model of Sink which appends to an STL-compatible sequence.
61 template<typename Container>
62 class container_sink {
64 typedef typename Container::value_type char_type;
65 typedef sink_tag category;
66 container_sink(Container& container) : container_(container) { }
67 std::streamsize write(const char_type* s, std::streamsize n)
69 container_.insert(container_.end(), s, s + n);
72 Container& container() { return container_; }
74 container_sink operator=(const container_sink&);
75 Container& container_;
79 // Model of SeekableDevice which accessS an TL-compatible sequence
80 // whose iterators are random-access iterators.
82 template<typename Container>
83 class container_device {
85 typedef typename Container::value_type char_type;
86 typedef seekable_device_tag category;
87 container_device(Container& container)
88 : container_(container), pos_(0)
91 std::streamsize read(char_type* s, std::streamsize n)
95 static_cast<std::streamsize>(container_.size() - pos_);
96 std::streamsize result = (min)(n, amt);
98 std::copy( container_.begin() + pos_,
99 container_.begin() + pos_ + result,
107 std::streamsize write(const char_type* s, std::streamsize n)
110 std::streamsize result = 0;
111 if (pos_ != container_.size()) {
112 std::streamsize amt =
113 static_cast<std::streamsize>(container_.size() - pos_);
114 result = (min)(n, amt);
115 std::copy(s, s + result, container_.begin() + pos_);
119 container_.insert(container_.end(), s, s + n);
120 pos_ = container_.size();
124 stream_offset seek(stream_offset off, BOOST_IOS::seekdir way)
128 // Determine new value of pos_
130 if (way == BOOST_IOS::beg) {
132 } else if (way == BOOST_IOS::cur) {
134 } else if (way == BOOST_IOS::end) {
135 next = container_.size() + off - 1;
137 throw BOOST_IOSTREAMS_FAILURE("bad seek direction");
141 if (next < 0 || next >= static_cast<stream_offset>(container_.size()))
142 throw BOOST_IOSTREAMS_FAILURE("bad seek offset");
148 Container& container() { return container_; }
150 container_device operator=(const container_device&);
151 typedef typename Container::size_type size_type;
152 Container& container_;
156 } } } // End namespaces example, iostreams, boost.
158 #include <boost/iostreams/detail/config/enable_warnings.hpp>
160 #endif // #ifndef BOOST_IOSTREAMS_EXAMPLE_CONTAINTER_DEVICE_HPP_INCLUDED