Chris@16
|
1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
|
Chris@16
|
2 // (C) Copyright 2003-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_DIRECT_STREAMBUF_HPP_INCLUDED
|
Chris@16
|
9 #define BOOST_IOSTREAMS_DETAIL_DIRECT_STREAMBUF_HPP_INCLUDED
|
Chris@16
|
10
|
Chris@16
|
11 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
Chris@16
|
12 # pragma once
|
Chris@16
|
13 #endif
|
Chris@16
|
14
|
Chris@16
|
15 #include <boost/assert.hpp>
|
Chris@16
|
16 #include <cstddef>
|
Chris@16
|
17 #include <typeinfo>
|
Chris@16
|
18 #include <utility> // pair.
|
Chris@16
|
19 #include <boost/config.hpp> // BOOST_DEDUCED_TYPENAME,
|
Chris@16
|
20 #include <boost/iostreams/detail/char_traits.hpp> // member template friends.
|
Chris@16
|
21 #include <boost/iostreams/detail/config/wide_streams.hpp>
|
Chris@16
|
22 #include <boost/iostreams/detail/error.hpp>
|
Chris@16
|
23 #include <boost/iostreams/detail/execute.hpp>
|
Chris@16
|
24 #include <boost/iostreams/detail/functional.hpp>
|
Chris@16
|
25 #include <boost/iostreams/detail/ios.hpp>
|
Chris@16
|
26 #include <boost/iostreams/detail/optional.hpp>
|
Chris@16
|
27 #include <boost/iostreams/detail/streambuf.hpp>
|
Chris@16
|
28 #include <boost/iostreams/detail/streambuf/linked_streambuf.hpp>
|
Chris@16
|
29 #include <boost/iostreams/operations.hpp>
|
Chris@16
|
30 #include <boost/iostreams/positioning.hpp>
|
Chris@16
|
31 #include <boost/iostreams/traits.hpp>
|
Chris@16
|
32 #include <boost/throw_exception.hpp>
|
Chris@16
|
33
|
Chris@16
|
34 // Must come last.
|
Chris@16
|
35 #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost { namespace iostreams {
|
Chris@16
|
38
|
Chris@16
|
39 namespace detail {
|
Chris@16
|
40
|
Chris@16
|
41 template< typename T,
|
Chris@16
|
42 typename Tr =
|
Chris@16
|
43 BOOST_IOSTREAMS_CHAR_TRAITS(
|
Chris@16
|
44 BOOST_DEDUCED_TYPENAME char_type_of<T>::type
|
Chris@16
|
45 ) >
|
Chris@16
|
46 class direct_streambuf
|
Chris@16
|
47 : public linked_streambuf<BOOST_DEDUCED_TYPENAME char_type_of<T>::type, Tr>
|
Chris@16
|
48 {
|
Chris@16
|
49 public:
|
Chris@16
|
50 typedef typename char_type_of<T>::type char_type;
|
Chris@16
|
51 BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr)
|
Chris@16
|
52 private:
|
Chris@16
|
53 typedef linked_streambuf<char_type, traits_type> base_type;
|
Chris@16
|
54 typedef typename category_of<T>::type category;
|
Chris@16
|
55 typedef BOOST_IOSTREAMS_BASIC_STREAMBUF(
|
Chris@16
|
56 char_type, traits_type
|
Chris@16
|
57 ) streambuf_type;
|
Chris@16
|
58 public: // stream needs access.
|
Chris@16
|
59 void open(const T& t, std::streamsize buffer_size,
|
Chris@16
|
60 std::streamsize pback_size);
|
Chris@16
|
61 bool is_open() const;
|
Chris@16
|
62 void close();
|
Chris@16
|
63 bool auto_close() const { return auto_close_; }
|
Chris@16
|
64 void set_auto_close(bool close) { auto_close_ = close; }
|
Chris@16
|
65 bool strict_sync() { return true; }
|
Chris@16
|
66
|
Chris@16
|
67 // Declared in linked_streambuf.
|
Chris@16
|
68 T* component() { return storage_.get(); }
|
Chris@16
|
69 protected:
|
Chris@16
|
70 #if !BOOST_WORKAROUND(__GNUC__, == 2)
|
Chris@16
|
71 BOOST_IOSTREAMS_USING_PROTECTED_STREAMBUF_MEMBERS(base_type)
|
Chris@16
|
72 #endif
|
Chris@16
|
73 direct_streambuf();
|
Chris@16
|
74
|
Chris@16
|
75 //--------------Virtual functions-----------------------------------------//
|
Chris@16
|
76
|
Chris@16
|
77 // Declared in linked_streambuf.
|
Chris@16
|
78 void close_impl(BOOST_IOS::openmode m);
|
Chris@16
|
79 const std::type_info& component_type() const { return typeid(T); }
|
Chris@16
|
80 void* component_impl() { return component(); }
|
Chris@16
|
81 #ifdef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
|
Chris@16
|
82 public:
|
Chris@16
|
83 #endif
|
Chris@16
|
84
|
Chris@16
|
85 // Declared in basic_streambuf.
|
Chris@16
|
86 int_type underflow();
|
Chris@16
|
87 int_type pbackfail(int_type c);
|
Chris@16
|
88 int_type overflow(int_type c);
|
Chris@16
|
89 pos_type seekoff( off_type off, BOOST_IOS::seekdir way,
|
Chris@16
|
90 BOOST_IOS::openmode which );
|
Chris@16
|
91 pos_type seekpos(pos_type sp, BOOST_IOS::openmode which);
|
Chris@16
|
92 private:
|
Chris@16
|
93 pos_type seek_impl( stream_offset off, BOOST_IOS::seekdir way,
|
Chris@16
|
94 BOOST_IOS::openmode which );
|
Chris@16
|
95 void init_input(any_tag) { }
|
Chris@16
|
96 void init_input(input);
|
Chris@16
|
97 void init_output(any_tag) { }
|
Chris@16
|
98 void init_output(output);
|
Chris@16
|
99 void init_get_area();
|
Chris@16
|
100 void init_put_area();
|
Chris@16
|
101 bool one_head() const;
|
Chris@16
|
102 bool two_head() const;
|
Chris@16
|
103 optional<T> storage_;
|
Chris@16
|
104 char_type *ibeg_, *iend_, *obeg_, *oend_;
|
Chris@16
|
105 bool auto_close_;
|
Chris@16
|
106 };
|
Chris@16
|
107
|
Chris@16
|
108 //------------------Implementation of direct_streambuf------------------------//
|
Chris@16
|
109
|
Chris@16
|
110 template<typename T, typename Tr>
|
Chris@16
|
111 direct_streambuf<T, Tr>::direct_streambuf()
|
Chris@16
|
112 : ibeg_(0), iend_(0), obeg_(0), oend_(0), auto_close_(true)
|
Chris@16
|
113 { this->set_true_eof(true); }
|
Chris@16
|
114
|
Chris@16
|
115 template<typename T, typename Tr>
|
Chris@16
|
116 void direct_streambuf<T, Tr>::open
|
Chris@16
|
117 (const T& t, std::streamsize, std::streamsize)
|
Chris@16
|
118 {
|
Chris@16
|
119 storage_.reset(t);
|
Chris@16
|
120 init_input(category());
|
Chris@16
|
121 init_output(category());
|
Chris@16
|
122 setg(0, 0, 0);
|
Chris@16
|
123 setp(0, 0);
|
Chris@16
|
124 this->set_needs_close();
|
Chris@16
|
125 }
|
Chris@16
|
126
|
Chris@16
|
127 template<typename T, typename Tr>
|
Chris@16
|
128 bool direct_streambuf<T, Tr>::is_open() const
|
Chris@16
|
129 { return ibeg_ != 0 || obeg_ != 0; }
|
Chris@16
|
130
|
Chris@16
|
131 template<typename T, typename Tr>
|
Chris@16
|
132 void direct_streambuf<T, Tr>::close()
|
Chris@16
|
133 {
|
Chris@16
|
134 base_type* self = this;
|
Chris@16
|
135 detail::execute_all( detail::call_member_close(*self, BOOST_IOS::in),
|
Chris@16
|
136 detail::call_member_close(*self, BOOST_IOS::out),
|
Chris@16
|
137 detail::call_reset(storage_) );
|
Chris@16
|
138 }
|
Chris@16
|
139
|
Chris@16
|
140 template<typename T, typename Tr>
|
Chris@16
|
141 typename direct_streambuf<T, Tr>::int_type
|
Chris@16
|
142 direct_streambuf<T, Tr>::underflow()
|
Chris@16
|
143 {
|
Chris@16
|
144 if (!ibeg_)
|
Chris@16
|
145 boost::throw_exception(cant_read());
|
Chris@16
|
146 if (!gptr())
|
Chris@16
|
147 init_get_area();
|
Chris@16
|
148 return gptr() != iend_ ?
|
Chris@16
|
149 traits_type::to_int_type(*gptr()) :
|
Chris@16
|
150 traits_type::eof();
|
Chris@16
|
151 }
|
Chris@16
|
152
|
Chris@16
|
153 template<typename T, typename Tr>
|
Chris@16
|
154 typename direct_streambuf<T, Tr>::int_type
|
Chris@16
|
155 direct_streambuf<T, Tr>::pbackfail(int_type c)
|
Chris@16
|
156 {
|
Chris@16
|
157 using namespace std;
|
Chris@16
|
158 if (!ibeg_)
|
Chris@16
|
159 boost::throw_exception(cant_read());
|
Chris@16
|
160 if (gptr() != 0 && gptr() != ibeg_) {
|
Chris@16
|
161 gbump(-1);
|
Chris@16
|
162 if (!traits_type::eq_int_type(c, traits_type::eof()))
|
Chris@16
|
163 *gptr() = traits_type::to_char_type(c);
|
Chris@16
|
164 return traits_type::not_eof(c);
|
Chris@16
|
165 }
|
Chris@16
|
166 boost::throw_exception(bad_putback());
|
Chris@16
|
167 }
|
Chris@16
|
168
|
Chris@16
|
169 template<typename T, typename Tr>
|
Chris@16
|
170 typename direct_streambuf<T, Tr>::int_type
|
Chris@16
|
171 direct_streambuf<T, Tr>::overflow(int_type c)
|
Chris@16
|
172 {
|
Chris@16
|
173 using namespace std;
|
Chris@16
|
174 if (!obeg_)
|
Chris@16
|
175 boost::throw_exception(BOOST_IOSTREAMS_FAILURE("no write access"));
|
Chris@16
|
176 if (!pptr()) init_put_area();
|
Chris@16
|
177 if (!traits_type::eq_int_type(c, traits_type::eof())) {
|
Chris@16
|
178 if (pptr() == oend_)
|
Chris@16
|
179 boost::throw_exception(
|
Chris@16
|
180 BOOST_IOSTREAMS_FAILURE("write area exhausted")
|
Chris@16
|
181 );
|
Chris@16
|
182 *pptr() = traits_type::to_char_type(c);
|
Chris@16
|
183 pbump(1);
|
Chris@16
|
184 return c;
|
Chris@16
|
185 }
|
Chris@16
|
186 return traits_type::not_eof(c);
|
Chris@16
|
187 }
|
Chris@16
|
188
|
Chris@16
|
189 template<typename T, typename Tr>
|
Chris@16
|
190 inline typename direct_streambuf<T, Tr>::pos_type
|
Chris@16
|
191 direct_streambuf<T, Tr>::seekoff
|
Chris@16
|
192 (off_type off, BOOST_IOS::seekdir way, BOOST_IOS::openmode which)
|
Chris@16
|
193 { return seek_impl(off, way, which); }
|
Chris@16
|
194
|
Chris@16
|
195 template<typename T, typename Tr>
|
Chris@16
|
196 inline typename direct_streambuf<T, Tr>::pos_type
|
Chris@16
|
197 direct_streambuf<T, Tr>::seekpos
|
Chris@16
|
198 (pos_type sp, BOOST_IOS::openmode which)
|
Chris@16
|
199 {
|
Chris@16
|
200 return seek_impl(position_to_offset(sp), BOOST_IOS::beg, which);
|
Chris@16
|
201 }
|
Chris@16
|
202
|
Chris@16
|
203 template<typename T, typename Tr>
|
Chris@16
|
204 void direct_streambuf<T, Tr>::close_impl(BOOST_IOS::openmode which)
|
Chris@16
|
205 {
|
Chris@16
|
206 if (which == BOOST_IOS::in && ibeg_ != 0) {
|
Chris@16
|
207 setg(0, 0, 0);
|
Chris@16
|
208 ibeg_ = iend_ = 0;
|
Chris@16
|
209 }
|
Chris@16
|
210 if (which == BOOST_IOS::out && obeg_ != 0) {
|
Chris@16
|
211 sync();
|
Chris@16
|
212 setp(0, 0);
|
Chris@16
|
213 obeg_ = oend_ = 0;
|
Chris@16
|
214 }
|
Chris@16
|
215 boost::iostreams::close(*storage_, which);
|
Chris@16
|
216 }
|
Chris@16
|
217
|
Chris@16
|
218 template<typename T, typename Tr>
|
Chris@16
|
219 typename direct_streambuf<T, Tr>::pos_type direct_streambuf<T, Tr>::seek_impl
|
Chris@16
|
220 (stream_offset off, BOOST_IOS::seekdir way, BOOST_IOS::openmode which)
|
Chris@16
|
221 {
|
Chris@16
|
222 using namespace std;
|
Chris@16
|
223 BOOST_IOS::openmode both = BOOST_IOS::in | BOOST_IOS::out;
|
Chris@16
|
224 if (two_head() && (which & both) == both)
|
Chris@16
|
225 boost::throw_exception(bad_seek());
|
Chris@16
|
226 stream_offset result = -1;
|
Chris@16
|
227 bool one = one_head();
|
Chris@16
|
228 if (one && (pptr() != 0 || gptr()== 0))
|
Chris@16
|
229 init_get_area(); // Switch to input mode, for code reuse.
|
Chris@16
|
230 if (one || ((which & BOOST_IOS::in) != 0 && ibeg_ != 0)) {
|
Chris@16
|
231 if (!gptr()) setg(ibeg_, ibeg_, iend_);
|
Chris@16
|
232 ptrdiff_t next = 0;
|
Chris@16
|
233 switch (way) {
|
Chris@16
|
234 case BOOST_IOS::beg: next = off; break;
|
Chris@16
|
235 case BOOST_IOS::cur: next = (gptr() - ibeg_) + off; break;
|
Chris@16
|
236 case BOOST_IOS::end: next = (iend_ - ibeg_) + off; break;
|
Chris@16
|
237 default: BOOST_ASSERT(0);
|
Chris@16
|
238 }
|
Chris@16
|
239 if (next < 0 || next > (iend_ - ibeg_))
|
Chris@16
|
240 boost::throw_exception(bad_seek());
|
Chris@16
|
241 setg(ibeg_, ibeg_ + next, iend_);
|
Chris@16
|
242 result = next;
|
Chris@16
|
243 }
|
Chris@16
|
244 if (!one && (which & BOOST_IOS::out) != 0 && obeg_ != 0) {
|
Chris@16
|
245 if (!pptr()) setp(obeg_, oend_);
|
Chris@16
|
246 ptrdiff_t next = 0;
|
Chris@16
|
247 switch (way) {
|
Chris@16
|
248 case BOOST_IOS::beg: next = off; break;
|
Chris@16
|
249 case BOOST_IOS::cur: next = (pptr() - obeg_) + off; break;
|
Chris@16
|
250 case BOOST_IOS::end: next = (oend_ - obeg_) + off; break;
|
Chris@16
|
251 default: BOOST_ASSERT(0);
|
Chris@16
|
252 }
|
Chris@16
|
253 if (next < 0 || next > (oend_ - obeg_))
|
Chris@16
|
254 boost::throw_exception(bad_seek());
|
Chris@16
|
255 pbump(static_cast<int>(next - (pptr() - obeg_)));
|
Chris@16
|
256 result = next;
|
Chris@16
|
257 }
|
Chris@16
|
258 return offset_to_position(result);
|
Chris@16
|
259 }
|
Chris@16
|
260
|
Chris@16
|
261 template<typename T, typename Tr>
|
Chris@16
|
262 void direct_streambuf<T, Tr>::init_input(input)
|
Chris@16
|
263 {
|
Chris@16
|
264 std::pair<char_type*, char_type*> p = input_sequence(*storage_);
|
Chris@16
|
265 ibeg_ = p.first;
|
Chris@16
|
266 iend_ = p.second;
|
Chris@16
|
267 }
|
Chris@16
|
268
|
Chris@16
|
269 template<typename T, typename Tr>
|
Chris@16
|
270 void direct_streambuf<T, Tr>::init_output(output)
|
Chris@16
|
271 {
|
Chris@16
|
272 std::pair<char_type*, char_type*> p = output_sequence(*storage_);
|
Chris@16
|
273 obeg_ = p.first;
|
Chris@16
|
274 oend_ = p.second;
|
Chris@16
|
275 }
|
Chris@16
|
276
|
Chris@16
|
277 template<typename T, typename Tr>
|
Chris@16
|
278 void direct_streambuf<T, Tr>::init_get_area()
|
Chris@16
|
279 {
|
Chris@16
|
280 setg(ibeg_, ibeg_, iend_);
|
Chris@16
|
281 if (one_head() && pptr()) {
|
Chris@16
|
282 gbump(static_cast<int>(pptr() - obeg_));
|
Chris@16
|
283 setp(0, 0);
|
Chris@16
|
284 }
|
Chris@16
|
285 }
|
Chris@16
|
286
|
Chris@16
|
287 template<typename T, typename Tr>
|
Chris@16
|
288 void direct_streambuf<T, Tr>::init_put_area()
|
Chris@16
|
289 {
|
Chris@16
|
290 setp(obeg_, oend_);
|
Chris@16
|
291 if (one_head() && gptr()) {
|
Chris@16
|
292 pbump(static_cast<int>(gptr() - ibeg_));
|
Chris@16
|
293 setg(0, 0, 0);
|
Chris@16
|
294 }
|
Chris@16
|
295 }
|
Chris@16
|
296
|
Chris@16
|
297 template<typename T, typename Tr>
|
Chris@16
|
298 inline bool direct_streambuf<T, Tr>::one_head() const
|
Chris@16
|
299 { return ibeg_ && obeg_ && ibeg_ == obeg_; }
|
Chris@16
|
300
|
Chris@16
|
301 template<typename T, typename Tr>
|
Chris@16
|
302 inline bool direct_streambuf<T, Tr>::two_head() const
|
Chris@16
|
303 { return ibeg_ && obeg_ && ibeg_ != obeg_; }
|
Chris@16
|
304
|
Chris@16
|
305 //----------------------------------------------------------------------------//
|
Chris@16
|
306
|
Chris@16
|
307 } // End namespace detail.
|
Chris@16
|
308
|
Chris@16
|
309 } } // End namespaces iostreams, boost.
|
Chris@16
|
310
|
Chris@16
|
311 #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC
|
Chris@16
|
312
|
Chris@16
|
313 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_DIRECT_STREAMBUF_HPP_INCLUDED
|