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