]>
Commit | Line | Data |
---|---|---|
54c1bf78 | 1 | // Stream buffer classes -*- C++ -*- |
de96ac46 | 2 | |
99dee823 | 3 | // Copyright (C) 1997-2021 Free Software Foundation, Inc. |
de96ac46 BK |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
de96ac46 BK |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
de96ac46 | 24 | |
f910786b | 25 | /** @file include/streambuf |
0aa06b18 | 26 | * This is a Standard C++ Library header. |
2f9d51b8 PE |
27 | */ |
28 | ||
143c27b0 BK |
29 | // |
30 | // ISO C++ 14882: 27.5 Stream buffers | |
31 | // | |
32 | ||
00aca6e8 BK |
33 | #ifndef _GLIBXX_STREAMBUF |
34 | #define _GLIBXX_STREAMBUF 1 | |
54c1bf78 BK |
35 | |
36 | #pragma GCC system_header | |
37 | ||
38 | #include <bits/c++config.h> | |
39 | #include <iosfwd> | |
54c1bf78 BK |
40 | #include <bits/localefwd.h> |
41 | #include <bits/ios_base.h> | |
0002d5d2 | 42 | #include <bits/cpp_type_traits.h> |
105c6331 | 43 | #include <ext/type_traits.h> |
54c1bf78 | 44 | |
12ffa228 BK |
45 | namespace std _GLIBCXX_VISIBILITY(default) |
46 | { | |
47 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 48 | |
9ed83a33 JW |
49 | #define _IsUnused __attribute__ ((__unused__)) |
50 | ||
54c1bf78 BK |
51 | template<typename _CharT, typename _Traits> |
52 | streamsize | |
0002d5d2 PC |
53 | __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>*, |
54 | basic_streambuf<_CharT, _Traits>*, bool&); | |
55 | ||
840ceb34 PE |
56 | /** |
57 | * @brief The actual work of input and output (interface). | |
0eb95b0d | 58 | * @ingroup io |
840ceb34 | 59 | * |
d632488a BK |
60 | * @tparam _CharT Type of character stream. |
61 | * @tparam _Traits Traits for character type, defaults to | |
62 | * char_traits<_CharT>. | |
63 | * | |
840ceb34 PE |
64 | * This is a base class. Derived stream buffers each control a |
65 | * pair of character sequences: one for input, and one for output. | |
66 | * | |
67 | * Section [27.5.1] of the standard describes the requirements and | |
68 | * behavior of stream buffer classes. That section (three paragraphs) | |
69 | * is reproduced here, for simplicity and accuracy. | |
70 | * | |
71 | * -# Stream buffers can impose various constraints on the sequences | |
72 | * they control. Some constraints are: | |
73 | * - The controlled input sequence can be not readable. | |
74 | * - The controlled output sequence can be not writable. | |
75 | * - The controlled sequences can be associated with the contents of | |
76 | * other representations for character sequences, such as external | |
77 | * files. | |
78 | * - The controlled sequences can support operations @e directly to or | |
79 | * from associated sequences. | |
80 | * - The controlled sequences can impose limitations on how the | |
81 | * program can read characters from a sequence, write characters to | |
82 | * a sequence, put characters back into an input sequence, or alter | |
83 | * the stream position. | |
84 | * . | |
85 | * -# Each sequence is characterized by three pointers which, if non-null, | |
86 | * all point into the same @c charT array object. The array object | |
87 | * represents, at any moment, a (sub)sequence of characters from the | |
88 | * sequence. Operations performed on a sequence alter the values | |
89 | * stored in these pointers, perform reads and writes directly to or | |
2a60a9f6 | 90 | * from associated sequences, and alter <em>the stream position</em> and |
840ceb34 PE |
91 | * conversion state as needed to maintain this subsequence relationship. |
92 | * The three pointers are: | |
93 | * - the <em>beginning pointer</em>, or lowest element address in the | |
94 | * array (called @e xbeg here); | |
95 | * - the <em>next pointer</em>, or next element address that is a | |
96 | * current candidate for reading or writing (called @e xnext here); | |
97 | * - the <em>end pointer</em>, or first element address beyond the | |
98 | * end of the array (called @e xend here). | |
99 | * . | |
100 | * -# The following semantic constraints shall always apply for any set | |
101 | * of three pointers for a sequence, using the pointer names given | |
102 | * immediately above: | |
103 | * - If @e xnext is not a null pointer, then @e xbeg and @e xend shall | |
104 | * also be non-null pointers into the same @c charT array, as | |
105 | * described above; otherwise, @e xbeg and @e xend shall also be null. | |
106 | * - If @e xnext is not a null pointer and @e xnext < @e xend for an | |
107 | * output sequence, then a <em>write position</em> is available. | |
108 | * In this case, @e *xnext shall be assignable as the next element | |
109 | * to write (to put, or to store a character value, into the sequence). | |
110 | * - If @e xnext is not a null pointer and @e xbeg < @e xnext for an | |
111 | * input sequence, then a <em>putback position</em> is available. | |
112 | * In this case, @e xnext[-1] shall have a defined value and is the | |
113 | * next (preceding) element to store a character that is put back | |
114 | * into the input sequence. | |
115 | * - If @e xnext is not a null pointer and @e xnext< @e xend for an | |
116 | * input sequence, then a <em>read position</em> is available. | |
117 | * In this case, @e *xnext shall have a defined value and is the | |
118 | * next element to read (to get, or to obtain a character value, | |
119 | * from the sequence). | |
120 | */ | |
54c1bf78 | 121 | template<typename _CharT, typename _Traits> |
f0fd118f | 122 | class basic_streambuf |
54c1bf78 BK |
123 | { |
124 | public: | |
f0b88346 | 125 | ///@{ |
840ceb34 PE |
126 | /** |
127 | * These are standard types. They permit a standardized way of | |
2f2b63da | 128 | * referring to names of (or names dependent on) the template |
840ceb34 PE |
129 | * parameters, which are specific to the implementation. |
130 | */ | |
54c1bf78 BK |
131 | typedef _CharT char_type; |
132 | typedef _Traits traits_type; | |
133 | typedef typename traits_type::int_type int_type; | |
134 | typedef typename traits_type::pos_type pos_type; | |
135 | typedef typename traits_type::off_type off_type; | |
f0b88346 | 136 | ///@} |
840ceb34 | 137 | |
f0b88346 | 138 | ///@{ |
4312e020 | 139 | /// This is a non-standard type. |
54c1bf78 | 140 | typedef basic_streambuf<char_type, traits_type> __streambuf_type; |
f0b88346 | 141 | ///@} |
f0fd118f | 142 | |
54c1bf78 BK |
143 | friend class basic_ios<char_type, traits_type>; |
144 | friend class basic_istream<char_type, traits_type>; | |
145 | friend class basic_ostream<char_type, traits_type>; | |
146 | friend class istreambuf_iterator<char_type, traits_type>; | |
147 | friend class ostreambuf_iterator<char_type, traits_type>; | |
148 | ||
149 | friend streamsize | |
f1c69797 | 150 | __copy_streambufs_eof<>(basic_streambuf*, basic_streambuf*, bool&); |
0002d5d2 | 151 | |
f0112db9 | 152 | template<bool _IsMove, typename _CharT2> |
f0fd118f | 153 | friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, |
105c6331 | 154 | _CharT2*>::__type |
f0112db9 PC |
155 | __copy_move_a2(istreambuf_iterator<_CharT2>, |
156 | istreambuf_iterator<_CharT2>, _CharT2*); | |
0002d5d2 PC |
157 | |
158 | template<typename _CharT2> | |
105c6331 BK |
159 | friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, |
160 | istreambuf_iterator<_CharT2> >::__type | |
0002d5d2 | 161 | find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>, |
976e25f4 | 162 | const _CharT2&); |
0002d5d2 | 163 | |
e324f9cb FD |
164 | template<typename _CharT2, typename _Distance> |
165 | friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, | |
166 | void>::__type | |
167 | advance(istreambuf_iterator<_CharT2>&, _Distance); | |
168 | ||
17abcc77 | 169 | friend void __istream_extract(istream&, char*, streamsize); |
0002d5d2 | 170 | |
ceed88b1 PC |
171 | template<typename _CharT2, typename _Traits2, typename _Alloc> |
172 | friend basic_istream<_CharT2, _Traits2>& | |
173 | operator>>(basic_istream<_CharT2, _Traits2>&, | |
174 | basic_string<_CharT2, _Traits2, _Alloc>&); | |
0002d5d2 | 175 | |
e9fb72e8 PC |
176 | template<typename _CharT2, typename _Traits2, typename _Alloc> |
177 | friend basic_istream<_CharT2, _Traits2>& | |
178 | getline(basic_istream<_CharT2, _Traits2>&, | |
179 | basic_string<_CharT2, _Traits2, _Alloc>&, _CharT2); | |
180 | ||
54c1bf78 | 181 | protected: |
f1c69797 | 182 | /* |
840ceb34 PE |
183 | * This is based on _IO_FILE, just reordered to be more consistent, |
184 | * and is intended to be the most minimal abstraction for an | |
185 | * internal buffer. | |
186 | * - get == input == read | |
187 | * - put == output == write | |
840ceb34 | 188 | */ |
f1c69797 JW |
189 | char_type* _M_in_beg; ///< Start of get area. |
190 | char_type* _M_in_cur; ///< Current read area. | |
191 | char_type* _M_in_end; ///< End of get area. | |
192 | char_type* _M_out_beg; ///< Start of put area. | |
193 | char_type* _M_out_cur; ///< Current put area. | |
194 | char_type* _M_out_end; ///< End of put area. | |
391cd095 | 195 | |
4312e020 | 196 | /// Current locale setting. |
f0fd118f | 197 | locale _M_buf_locale; |
54c1bf78 | 198 | |
54c1bf78 | 199 | public: |
840ceb34 | 200 | /// Destructor deallocates no buffer space. |
f0fd118f JW |
201 | virtual |
202 | ~basic_streambuf() | |
cd16e04b | 203 | { } |
54c1bf78 | 204 | |
840ceb34 PE |
205 | // [27.5.2.2.1] locales |
206 | /** | |
207 | * @brief Entry point for imbue(). | |
93c66bc6 | 208 | * @param __loc The new locale. |
840ceb34 PE |
209 | * @return The previous locale. |
210 | * | |
93c66bc6 | 211 | * Calls the derived imbue(__loc). |
840ceb34 | 212 | */ |
f0fd118f | 213 | locale |
93c66bc6 | 214 | pubimbue(const locale& __loc) |
54c1bf78 BK |
215 | { |
216 | locale __tmp(this->getloc()); | |
217 | this->imbue(__loc); | |
a2dcfada | 218 | _M_buf_locale = __loc; |
54c1bf78 BK |
219 | return __tmp; |
220 | } | |
221 | ||
840ceb34 PE |
222 | /** |
223 | * @brief Locale access. | |
224 | * @return The current locale in effect. | |
225 | * | |
226 | * If pubimbue(loc) has been called, then the most recent @c loc | |
227 | * is returned. Otherwise the global locale in effect at the time | |
228 | * of construction is returned. | |
229 | */ | |
f0fd118f | 230 | locale |
54c1bf78 | 231 | getloc() const |
f0fd118f | 232 | { return _M_buf_locale; } |
54c1bf78 | 233 | |
840ceb34 | 234 | // [27.5.2.2.2] buffer management and positioning |
f0b88346 | 235 | ///@{ |
840ceb34 PE |
236 | /** |
237 | * @brief Entry points for derived buffer functions. | |
238 | * | |
239 | * The public versions of @c pubfoo dispatch to the protected | |
240 | * derived @c foo member functions, passing the arguments (if any) | |
241 | * and returning the result unchanged. | |
242 | */ | |
f1c69797 | 243 | basic_streambuf* |
f0fd118f | 244 | pubsetbuf(char_type* __s, streamsize __n) |
54c1bf78 BK |
245 | { return this->setbuf(__s, __n); } |
246 | ||
93c66bc6 BK |
247 | /** |
248 | * @brief Alters the stream position. | |
249 | * @param __off Offset. | |
250 | * @param __way Value for ios_base::seekdir. | |
251 | * @param __mode Value for ios_base::openmode. | |
252 | * | |
253 | * Calls virtual seekoff function. | |
254 | */ | |
f0fd118f JW |
255 | pos_type |
256 | pubseekoff(off_type __off, ios_base::seekdir __way, | |
54c1bf78 BK |
257 | ios_base::openmode __mode = ios_base::in | ios_base::out) |
258 | { return this->seekoff(__off, __way, __mode); } | |
259 | ||
93c66bc6 BK |
260 | /** |
261 | * @brief Alters the stream position. | |
262 | * @param __sp Position | |
263 | * @param __mode Value for ios_base::openmode. | |
264 | * | |
265 | * Calls virtual seekpos function. | |
266 | */ | |
f0fd118f | 267 | pos_type |
54c1bf78 BK |
268 | pubseekpos(pos_type __sp, |
269 | ios_base::openmode __mode = ios_base::in | ios_base::out) | |
270 | { return this->seekpos(__sp, __mode); } | |
271 | ||
93c66bc6 BK |
272 | /** |
273 | * @brief Calls virtual sync function. | |
274 | */ | |
f0fd118f | 275 | int |
54c1bf78 | 276 | pubsync() { return this->sync(); } |
f0b88346 | 277 | ///@} |
840ceb34 PE |
278 | |
279 | // [27.5.2.2.3] get area | |
280 | /** | |
281 | * @brief Looking ahead into the stream. | |
282 | * @return The number of characters available. | |
283 | * | |
284 | * If a read position is available, returns the number of characters | |
285 | * available for reading before the buffer must be refilled. | |
286 | * Otherwise returns the derived @c showmanyc(). | |
287 | */ | |
f0fd118f JW |
288 | streamsize |
289 | in_avail() | |
290 | { | |
71b46021 | 291 | const streamsize __ret = this->egptr() - this->gptr(); |
21a1d2c4 | 292 | return __ret ? __ret : this->showmanyc(); |
54c1bf78 BK |
293 | } |
294 | ||
840ceb34 PE |
295 | /** |
296 | * @brief Getting the next character. | |
297 | * @return The next character, or eof. | |
298 | * | |
299 | * Calls @c sbumpc(), and if that function returns | |
300 | * @c traits::eof(), so does this function. Otherwise, @c sgetc(). | |
301 | */ | |
f0fd118f | 302 | int_type |
54c1bf78 BK |
303 | snextc() |
304 | { | |
0cd1de6f | 305 | int_type __ret = traits_type::eof(); |
f0fd118f | 306 | if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(), |
29d9ed97 | 307 | __ret), true)) |
0cd1de6f BK |
308 | __ret = this->sgetc(); |
309 | return __ret; | |
54c1bf78 BK |
310 | } |
311 | ||
840ceb34 PE |
312 | /** |
313 | * @brief Getting the next character. | |
314 | * @return The next character, or eof. | |
315 | * | |
316 | * If the input read position is available, returns that character | |
317 | * and increments the read pointer, otherwise calls and returns | |
318 | * @c uflow(). | |
319 | */ | |
f0fd118f | 320 | int_type |
29d9ed97 NM |
321 | sbumpc() |
322 | { | |
323 | int_type __ret; | |
324 | if (__builtin_expect(this->gptr() < this->egptr(), true)) | |
325 | { | |
326 | __ret = traits_type::to_int_type(*this->gptr()); | |
327 | this->gbump(1); | |
328 | } | |
f0fd118f | 329 | else |
29d9ed97 NM |
330 | __ret = this->uflow(); |
331 | return __ret; | |
332 | } | |
54c1bf78 | 333 | |
840ceb34 PE |
334 | /** |
335 | * @brief Getting the next character. | |
336 | * @return The next character, or eof. | |
337 | * | |
338 | * If the input read position is available, returns that character, | |
f0fd118f | 339 | * otherwise calls and returns @c underflow(). Does not move the |
840ceb34 PE |
340 | * read position after fetching the character. |
341 | */ | |
f0fd118f | 342 | int_type |
54c1bf78 BK |
343 | sgetc() |
344 | { | |
9d3e38ec | 345 | int_type __ret; |
29d9ed97 | 346 | if (__builtin_expect(this->gptr() < this->egptr(), true)) |
71b46021 | 347 | __ret = traits_type::to_int_type(*this->gptr()); |
f0fd118f | 348 | else |
9d3e38ec PC |
349 | __ret = this->underflow(); |
350 | return __ret; | |
54c1bf78 BK |
351 | } |
352 | ||
840ceb34 PE |
353 | /** |
354 | * @brief Entry point for xsgetn. | |
93c66bc6 BK |
355 | * @param __s A buffer area. |
356 | * @param __n A count. | |
840ceb34 | 357 | * |
93c66bc6 BK |
358 | * Returns xsgetn(__s,__n). The effect is to fill @a __s[0] through |
359 | * @a __s[__n-1] with characters from the input sequence, if possible. | |
840ceb34 | 360 | */ |
f0fd118f | 361 | streamsize |
54c1bf78 BK |
362 | sgetn(char_type* __s, streamsize __n) |
363 | { return this->xsgetn(__s, __n); } | |
364 | ||
840ceb34 PE |
365 | // [27.5.2.2.4] putback |
366 | /** | |
367 | * @brief Pushing characters back into the input stream. | |
93c66bc6 | 368 | * @param __c The character to push back. |
840ceb34 PE |
369 | * @return The previous character, if possible. |
370 | * | |
93c66bc6 | 371 | * Similar to sungetc(), but @a __c is pushed onto the stream |
2a60a9f6 BK |
372 | * instead of <em>the previous character.</em> If successful, |
373 | * the next character fetched from the input stream will be @a | |
93c66bc6 | 374 | * __c. |
840ceb34 | 375 | */ |
f0fd118f | 376 | int_type |
29d9ed97 NM |
377 | sputbackc(char_type __c) |
378 | { | |
379 | int_type __ret; | |
380 | const bool __testpos = this->eback() < this->gptr(); | |
f0fd118f | 381 | if (__builtin_expect(!__testpos || |
29d9ed97 NM |
382 | !traits_type::eq(__c, this->gptr()[-1]), false)) |
383 | __ret = this->pbackfail(traits_type::to_int_type(__c)); | |
f0fd118f | 384 | else |
29d9ed97 NM |
385 | { |
386 | this->gbump(-1); | |
387 | __ret = traits_type::to_int_type(*this->gptr()); | |
388 | } | |
389 | return __ret; | |
390 | } | |
54c1bf78 | 391 | |
840ceb34 PE |
392 | /** |
393 | * @brief Moving backwards in the input stream. | |
394 | * @return The previous character, if possible. | |
395 | * | |
2a60a9f6 BK |
396 | * If a putback position is available, this function decrements |
397 | * the input pointer and returns that character. Otherwise, | |
398 | * calls and returns pbackfail(). The effect is to @a unget | |
399 | * the last character @a gotten. | |
840ceb34 | 400 | */ |
f0fd118f | 401 | int_type |
29d9ed97 NM |
402 | sungetc() |
403 | { | |
404 | int_type __ret; | |
405 | if (__builtin_expect(this->eback() < this->gptr(), true)) | |
406 | { | |
407 | this->gbump(-1); | |
408 | __ret = traits_type::to_int_type(*this->gptr()); | |
409 | } | |
f0fd118f | 410 | else |
29d9ed97 NM |
411 | __ret = this->pbackfail(); |
412 | return __ret; | |
413 | } | |
54c1bf78 | 414 | |
840ceb34 PE |
415 | // [27.5.2.2.5] put area |
416 | /** | |
417 | * @brief Entry point for all single-character output functions. | |
93c66bc6 BK |
418 | * @param __c A character to output. |
419 | * @return @a __c, if possible. | |
840ceb34 PE |
420 | * |
421 | * One of two public output functions. | |
422 | * | |
423 | * If a write position is available for the output sequence (i.e., | |
93c66bc6 BK |
424 | * the buffer is not full), stores @a __c in that position, increments |
425 | * the position, and returns @c traits::to_int_type(__c). If a write | |
426 | * position is not available, returns @c overflow(__c). | |
840ceb34 | 427 | */ |
f0fd118f | 428 | int_type |
29d9ed97 NM |
429 | sputc(char_type __c) |
430 | { | |
431 | int_type __ret; | |
432 | if (__builtin_expect(this->pptr() < this->epptr(), true)) | |
433 | { | |
434 | *this->pptr() = __c; | |
435 | this->pbump(1); | |
436 | __ret = traits_type::to_int_type(__c); | |
437 | } | |
438 | else | |
439 | __ret = this->overflow(traits_type::to_int_type(__c)); | |
440 | return __ret; | |
441 | } | |
54c1bf78 | 442 | |
840ceb34 PE |
443 | /** |
444 | * @brief Entry point for all single-character output functions. | |
93c66bc6 BK |
445 | * @param __s A buffer read area. |
446 | * @param __n A count. | |
840ceb34 PE |
447 | * |
448 | * One of two public output functions. | |
449 | * | |
450 | * | |
93c66bc6 BK |
451 | * Returns xsputn(__s,__n). The effect is to write @a __s[0] through |
452 | * @a __s[__n-1] to the output sequence, if possible. | |
840ceb34 | 453 | */ |
f0fd118f | 454 | streamsize |
54c1bf78 BK |
455 | sputn(const char_type* __s, streamsize __n) |
456 | { return this->xsputn(__s, __n); } | |
457 | ||
458 | protected: | |
840ceb34 PE |
459 | /** |
460 | * @brief Base constructor. | |
461 | * | |
462 | * Only called from derived constructors, and sets up all the | |
463 | * buffer data to zero, including the pointers described in the | |
464 | * basic_streambuf class description. Note that, as a result, | |
465 | * - the class starts with no read nor write positions available, | |
466 | * - this is not an error | |
467 | */ | |
54c1bf78 | 468 | basic_streambuf() |
f0fd118f | 469 | : _M_in_beg(0), _M_in_cur(0), _M_in_end(0), |
f10eea7b | 470 | _M_out_beg(0), _M_out_cur(0), _M_out_end(0), |
f0fd118f | 471 | _M_buf_locale(locale()) |
54c1bf78 BK |
472 | { } |
473 | ||
840ceb34 | 474 | // [27.5.2.3.1] get area access |
f0b88346 | 475 | ///@{ |
840ceb34 PE |
476 | /** |
477 | * @brief Access to the get area. | |
478 | * | |
479 | * These functions are only available to other protected functions, | |
480 | * including derived classes. | |
481 | * | |
482 | * - eback() returns the beginning pointer for the input sequence | |
483 | * - gptr() returns the next pointer for the input sequence | |
484 | * - egptr() returns the end pointer for the input sequence | |
485 | */ | |
f0fd118f | 486 | char_type* |
54c1bf78 BK |
487 | eback() const { return _M_in_beg; } |
488 | ||
f0fd118f | 489 | char_type* |
54c1bf78 BK |
490 | gptr() const { return _M_in_cur; } |
491 | ||
f0fd118f | 492 | char_type* |
54c1bf78 | 493 | egptr() const { return _M_in_end; } |
f0b88346 | 494 | ///@} |
840ceb34 PE |
495 | |
496 | /** | |
497 | * @brief Moving the read position. | |
93c66bc6 | 498 | * @param __n The delta by which to move. |
840ceb34 PE |
499 | * |
500 | * This just advances the read position without returning any data. | |
501 | */ | |
f0fd118f | 502 | void |
54c1bf78 BK |
503 | gbump(int __n) { _M_in_cur += __n; } |
504 | ||
840ceb34 PE |
505 | /** |
506 | * @brief Setting the three read area pointers. | |
93c66bc6 BK |
507 | * @param __gbeg A pointer. |
508 | * @param __gnext A pointer. | |
509 | * @param __gend A pointer. | |
510 | * @post @a __gbeg == @c eback(), @a __gnext == @c gptr(), and | |
511 | * @a __gend == @c egptr() | |
840ceb34 | 512 | */ |
f0fd118f | 513 | void |
54c1bf78 BK |
514 | setg(char_type* __gbeg, char_type* __gnext, char_type* __gend) |
515 | { | |
516 | _M_in_beg = __gbeg; | |
517 | _M_in_cur = __gnext; | |
518 | _M_in_end = __gend; | |
54c1bf78 BK |
519 | } |
520 | ||
840ceb34 | 521 | // [27.5.2.3.2] put area access |
f0b88346 | 522 | ///@{ |
840ceb34 PE |
523 | /** |
524 | * @brief Access to the put area. | |
525 | * | |
526 | * These functions are only available to other protected functions, | |
527 | * including derived classes. | |
528 | * | |
529 | * - pbase() returns the beginning pointer for the output sequence | |
530 | * - pptr() returns the next pointer for the output sequence | |
531 | * - epptr() returns the end pointer for the output sequence | |
532 | */ | |
f0fd118f | 533 | char_type* |
54c1bf78 BK |
534 | pbase() const { return _M_out_beg; } |
535 | ||
f0fd118f | 536 | char_type* |
54c1bf78 BK |
537 | pptr() const { return _M_out_cur; } |
538 | ||
f0fd118f | 539 | char_type* |
54c1bf78 | 540 | epptr() const { return _M_out_end; } |
f0b88346 | 541 | ///@} |
840ceb34 PE |
542 | |
543 | /** | |
544 | * @brief Moving the write position. | |
93c66bc6 | 545 | * @param __n The delta by which to move. |
840ceb34 PE |
546 | * |
547 | * This just advances the write position without returning any data. | |
548 | */ | |
f0fd118f | 549 | void |
54c1bf78 BK |
550 | pbump(int __n) { _M_out_cur += __n; } |
551 | ||
840ceb34 PE |
552 | /** |
553 | * @brief Setting the three write area pointers. | |
93c66bc6 BK |
554 | * @param __pbeg A pointer. |
555 | * @param __pend A pointer. | |
556 | * @post @a __pbeg == @c pbase(), @a __pbeg == @c pptr(), and | |
557 | * @a __pend == @c epptr() | |
840ceb34 | 558 | */ |
f0fd118f | 559 | void |
54c1bf78 | 560 | setp(char_type* __pbeg, char_type* __pend) |
f0fd118f JW |
561 | { |
562 | _M_out_beg = _M_out_cur = __pbeg; | |
b5bc3efb | 563 | _M_out_end = __pend; |
54c1bf78 BK |
564 | } |
565 | ||
840ceb34 PE |
566 | // [27.5.2.4] virtual functions |
567 | // [27.5.2.4.1] locales | |
568 | /** | |
569 | * @brief Changes translations. | |
93c66bc6 | 570 | * @param __loc A new locale. |
840ceb34 | 571 | * |
2a60a9f6 BK |
572 | * Translations done during I/O which depend on the current |
573 | * locale are changed by this call. The standard adds, | |
574 | * <em>Between invocations of this function a class derived | |
575 | * from streambuf can safely cache results of calls to locale | |
576 | * functions and to members of facets so obtained.</em> | |
a2dcfada PC |
577 | * |
578 | * @note Base class version does nothing. | |
840ceb34 | 579 | */ |
f0fd118f | 580 | virtual void |
9ed83a33 | 581 | imbue(const locale& __loc _IsUnused) |
a2dcfada | 582 | { } |
54c1bf78 | 583 | |
840ceb34 PE |
584 | // [27.5.2.4.2] buffer management and positioning |
585 | /** | |
28dac70a | 586 | * @brief Manipulates the buffer. |
840ceb34 PE |
587 | * |
588 | * Each derived class provides its own appropriate behavior. See | |
f0fd118f | 589 | * the next-to-last paragraph of |
10d43d2f | 590 | * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering |
a40fff0e | 591 | * for more on this function. |
840ceb34 PE |
592 | * |
593 | * @note Base class version does nothing, returns @c this. | |
594 | */ | |
f0fd118f | 595 | virtual basic_streambuf<char_type,_Traits>* |
54c1bf78 BK |
596 | setbuf(char_type*, streamsize) |
597 | { return this; } | |
f0fd118f | 598 | |
840ceb34 PE |
599 | /** |
600 | * @brief Alters the stream positions. | |
601 | * | |
602 | * Each derived class provides its own appropriate behavior. | |
603 | * @note Base class version does nothing, returns a @c pos_type | |
604 | * that represents an invalid stream position. | |
605 | */ | |
f0fd118f | 606 | virtual pos_type |
54c1bf78 BK |
607 | seekoff(off_type, ios_base::seekdir, |
608 | ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out) | |
f0fd118f | 609 | { return pos_type(off_type(-1)); } |
54c1bf78 | 610 | |
840ceb34 PE |
611 | /** |
612 | * @brief Alters the stream positions. | |
613 | * | |
614 | * Each derived class provides its own appropriate behavior. | |
615 | * @note Base class version does nothing, returns a @c pos_type | |
616 | * that represents an invalid stream position. | |
617 | */ | |
f0fd118f JW |
618 | virtual pos_type |
619 | seekpos(pos_type, | |
54c1bf78 | 620 | ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out) |
f0fd118f | 621 | { return pos_type(off_type(-1)); } |
54c1bf78 | 622 | |
840ceb34 PE |
623 | /** |
624 | * @brief Synchronizes the buffer arrays with the controlled sequences. | |
625 | * @return -1 on failure. | |
626 | * | |
627 | * Each derived class provides its own appropriate behavior, | |
2a60a9f6 | 628 | * including the definition of @a failure. |
840ceb34 PE |
629 | * @note Base class version does nothing, returns zero. |
630 | */ | |
f0fd118f | 631 | virtual int |
54c1bf78 BK |
632 | sync() { return 0; } |
633 | ||
840ceb34 PE |
634 | // [27.5.2.4.3] get area |
635 | /** | |
636 | * @brief Investigating the data available. | |
637 | * @return An estimate of the number of characters available in the | |
638 | * input sequence, or -1. | |
639 | * | |
2a60a9f6 BK |
640 | * <em>If it returns a positive value, then successive calls to |
641 | * @c underflow() will not return @c traits::eof() until at | |
642 | * least that number of characters have been supplied. If @c | |
643 | * showmanyc() returns -1, then calls to @c underflow() or @c | |
644 | * uflow() will fail.</em> [27.5.2.4.3]/1 | |
840ceb34 PE |
645 | * |
646 | * @note Base class version does nothing, returns zero. | |
2a60a9f6 | 647 | * @note The standard adds that <em>the intention is not only that the |
840ceb34 | 648 | * calls [to underflow or uflow] will not return @c eof() but |
2a60a9f6 BK |
649 | * that they will return immediately.</em> |
650 | * @note The standard adds that <em>the morphemes of @c showmanyc are | |
651 | * @b es-how-many-see, not @b show-manic.</em> | |
840ceb34 | 652 | */ |
f0fd118f | 653 | virtual streamsize |
54c1bf78 BK |
654 | showmanyc() { return 0; } |
655 | ||
840ceb34 PE |
656 | /** |
657 | * @brief Multiple character extraction. | |
93c66bc6 BK |
658 | * @param __s A buffer area. |
659 | * @param __n Maximum number of characters to assign. | |
840ceb34 PE |
660 | * @return The number of characters assigned. |
661 | * | |
93c66bc6 BK |
662 | * Fills @a __s[0] through @a __s[__n-1] with characters from the input |
663 | * sequence, as if by @c sbumpc(). Stops when either @a __n characters | |
840ceb34 PE |
664 | * have been copied, or when @c traits::eof() would be copied. |
665 | * | |
666 | * It is expected that derived classes provide a more efficient | |
667 | * implementation by overriding this definition. | |
668 | */ | |
f0fd118f | 669 | virtual streamsize |
54c1bf78 BK |
670 | xsgetn(char_type* __s, streamsize __n); |
671 | ||
840ceb34 PE |
672 | /** |
673 | * @brief Fetches more data from the controlled sequence. | |
674 | * @return The first character from the <em>pending sequence</em>. | |
675 | * | |
676 | * Informally, this function is called when the input buffer is | |
677 | * exhausted (or does not exist, as buffering need not actually be | |
2a60a9f6 | 678 | * done). If a buffer exists, it is @a refilled. In either case, the |
840ceb34 PE |
679 | * next available character is returned, or @c traits::eof() to |
680 | * indicate a null pending sequence. | |
681 | * | |
28dac70a | 682 | * For a formal definition of the pending sequence, see a good text |
840ceb34 PE |
683 | * such as Langer & Kreft, or [27.5.2.4.3]/7-14. |
684 | * | |
685 | * A functioning input streambuf can be created by overriding only | |
686 | * this function (no buffer area will be used). For an example, see | |
10d43d2f | 687 | * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html |
840ceb34 PE |
688 | * |
689 | * @note Base class version does nothing, returns eof(). | |
690 | */ | |
f0fd118f | 691 | virtual int_type |
54c1bf78 BK |
692 | underflow() |
693 | { return traits_type::eof(); } | |
694 | ||
840ceb34 PE |
695 | /** |
696 | * @brief Fetches more data from the controlled sequence. | |
697 | * @return The first character from the <em>pending sequence</em>. | |
698 | * | |
699 | * Informally, this function does the same thing as @c underflow(), | |
700 | * and in fact is required to call that function. It also returns | |
701 | * the new character, like @c underflow() does. However, this | |
702 | * function also moves the read position forward by one. | |
703 | */ | |
f0fd118f JW |
704 | virtual int_type |
705 | uflow() | |
54c1bf78 BK |
706 | { |
707 | int_type __ret = traits_type::eof(); | |
f0fd118f | 708 | const bool __testeof = traits_type::eq_int_type(this->underflow(), |
0cd1de6f | 709 | __ret); |
90319d16 | 710 | if (!__testeof) |
54c1bf78 | 711 | { |
71b46021 PC |
712 | __ret = traits_type::to_int_type(*this->gptr()); |
713 | this->gbump(1); | |
54c1bf78 | 714 | } |
f0fd118f | 715 | return __ret; |
54c1bf78 BK |
716 | } |
717 | ||
840ceb34 PE |
718 | // [27.5.2.4.4] putback |
719 | /** | |
720 | * @brief Tries to back up the input sequence. | |
93c66bc6 | 721 | * @param __c The character to be inserted back into the sequence. |
2a60a9f6 | 722 | * @return eof() on failure, <em>some other value</em> on success |
840ceb34 PE |
723 | * @post The constraints of @c gptr(), @c eback(), and @c pptr() |
724 | * are the same as for @c underflow(). | |
725 | * | |
726 | * @note Base class version does nothing, returns eof(). | |
727 | */ | |
f0fd118f | 728 | virtual int_type |
9ed83a33 | 729 | pbackfail(int_type __c _IsUnused = traits_type::eof()) |
54c1bf78 BK |
730 | { return traits_type::eof(); } |
731 | ||
732 | // Put area: | |
840ceb34 PE |
733 | /** |
734 | * @brief Multiple character insertion. | |
93c66bc6 BK |
735 | * @param __s A buffer area. |
736 | * @param __n Maximum number of characters to write. | |
840ceb34 PE |
737 | * @return The number of characters written. |
738 | * | |
93c66bc6 | 739 | * Writes @a __s[0] through @a __s[__n-1] to the output sequence, as if |
840ceb34 PE |
740 | * by @c sputc(). Stops when either @a n characters have been |
741 | * copied, or when @c sputc() would return @c traits::eof(). | |
742 | * | |
743 | * It is expected that derived classes provide a more efficient | |
744 | * implementation by overriding this definition. | |
745 | */ | |
f0fd118f | 746 | virtual streamsize |
54c1bf78 BK |
747 | xsputn(const char_type* __s, streamsize __n); |
748 | ||
840ceb34 PE |
749 | /** |
750 | * @brief Consumes data from the buffer; writes to the | |
751 | * controlled sequence. | |
93c66bc6 | 752 | * @param __c An additional character to consume. |
840ceb34 | 753 | * @return eof() to indicate failure, something else (usually |
93c66bc6 | 754 | * @a __c, or not_eof()) |
840ceb34 | 755 | * |
2a60a9f6 BK |
756 | * Informally, this function is called when the output buffer |
757 | * is full (or does not exist, as buffering need not actually | |
758 | * be done). If a buffer exists, it is @a consumed, with | |
759 | * <em>some effect</em> on the controlled sequence. | |
760 | * (Typically, the buffer is written out to the sequence | |
761 | * verbatim.) In either case, the character @a c is also | |
93c66bc6 | 762 | * written out, if @a __c is not @c eof(). |
840ceb34 | 763 | * |
28dac70a | 764 | * For a formal definition of this function, see a good text |
840ceb34 PE |
765 | * such as Langer & Kreft, or [27.5.2.4.5]/3-7. |
766 | * | |
767 | * A functioning output streambuf can be created by overriding only | |
768 | * this function (no buffer area will be used). | |
769 | * | |
770 | * @note Base class version does nothing, returns eof(). | |
771 | */ | |
f0fd118f | 772 | virtual int_type |
9ed83a33 | 773 | overflow(int_type __c _IsUnused = traits_type::eof()) |
54c1bf78 BK |
774 | { return traits_type::eof(); } |
775 | ||
a74bc411 JW |
776 | #if _GLIBCXX_USE_DEPRECATED && __cplusplus <= 201402L |
777 | // Annex D.6 (removed in C++17) | |
54c1bf78 | 778 | public: |
840ceb34 PE |
779 | /** |
780 | * @brief Tosses a character. | |
781 | * | |
782 | * Advances the read pointer, ignoring the character that would have | |
783 | * been read. | |
784 | * | |
785 | * See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html | |
4dd9d9db | 786 | */ |
eef9bf4c | 787 | _GLIBCXX_DEPRECATED_SUGGEST("std::basic_streambuf::sbumpc") |
f0fd118f JW |
788 | void |
789 | stossc() | |
54c1bf78 | 790 | { |
f0fd118f | 791 | if (this->gptr() < this->egptr()) |
71b46021 | 792 | this->gbump(1); |
f0fd118f | 793 | else |
54c1bf78 BK |
794 | this->uflow(); |
795 | } | |
725dc051 | 796 | #endif |
54c1bf78 | 797 | |
1139a735 | 798 | // Also used by specializations for char and wchar_t in src. |
f0fd118f | 799 | void |
1139a735 PC |
800 | __safe_gbump(streamsize __n) { _M_in_cur += __n; } |
801 | ||
802 | void | |
803 | __safe_pbump(streamsize __n) { _M_out_cur += __n; } | |
804 | ||
f0fd118f | 805 | #if __cplusplus < 201103L |
54c1bf78 | 806 | private: |
f0fd118f JW |
807 | #else |
808 | protected: | |
9b817548 JW |
809 | #endif |
810 | basic_streambuf(const basic_streambuf&); | |
f0fd118f JW |
811 | |
812 | basic_streambuf& | |
9b817548 | 813 | operator=(const basic_streambuf&); |
f0fd118f | 814 | |
9b817548 | 815 | #if __cplusplus >= 201103L |
f0fd118f JW |
816 | void |
817 | swap(basic_streambuf& __sb) | |
818 | { | |
819 | std::swap(_M_in_beg, __sb._M_in_beg); | |
820 | std::swap(_M_in_cur, __sb._M_in_cur); | |
821 | std::swap(_M_in_end, __sb._M_in_end); | |
822 | std::swap(_M_out_beg, __sb._M_out_beg); | |
823 | std::swap(_M_out_cur, __sb._M_out_cur); | |
824 | std::swap(_M_out_end, __sb._M_out_end); | |
825 | std::swap(_M_buf_locale, __sb._M_buf_locale); | |
826 | } | |
827 | #endif | |
54c1bf78 | 828 | }; |
2bf8accc | 829 | |
9b817548 JW |
830 | #if __cplusplus >= 201103L |
831 | template<typename _CharT, typename _Traits> | |
832 | std::basic_streambuf<_CharT, _Traits>:: | |
833 | basic_streambuf(const basic_streambuf&) = default; | |
834 | ||
835 | template<typename _CharT, typename _Traits> | |
836 | std::basic_streambuf<_CharT, _Traits>& | |
837 | std::basic_streambuf<_CharT, _Traits>:: | |
838 | operator=(const basic_streambuf&) = default; | |
839 | #endif | |
840 | ||
2bf8accc PC |
841 | // Explicit specialization declarations, defined in src/streambuf.cc. |
842 | template<> | |
843 | streamsize | |
6f4d3d86 PC |
844 | __copy_streambufs_eof(basic_streambuf<char>* __sbin, |
845 | basic_streambuf<char>* __sbout, bool& __ineof); | |
2bf8accc PC |
846 | #ifdef _GLIBCXX_USE_WCHAR_T |
847 | template<> | |
848 | streamsize | |
6f4d3d86 PC |
849 | __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin, |
850 | basic_streambuf<wchar_t>* __sbout, bool& __ineof); | |
2bf8accc | 851 | #endif |
3cbc7af0 | 852 | |
9ed83a33 JW |
853 | #undef _IsUnused |
854 | ||
12ffa228 BK |
855 | _GLIBCXX_END_NAMESPACE_VERSION |
856 | } // namespace | |
54c1bf78 | 857 | |
ee3ee948 | 858 | #include <bits/streambuf.tcc> |
54c1bf78 | 859 | |
1143680e | 860 | #endif /* _GLIBCXX_STREAMBUF */ |