1 // Stream buffer classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file include/streambuf
27 * This is a Standard C++ Library header.
31 // ISO C++ 14882: 27.5 Stream buffers
34 #ifndef _GLIBXX_STREAMBUF
35 #define _GLIBXX_STREAMBUF 1
37 #pragma GCC system_header
39 #include <bits/c++config.h>
41 #include <bits/localefwd.h>
42 #include <bits/ios_base.h>
43 #include <bits/cpp_type_traits.h>
44 #include <ext/type_traits.h>
46 namespace std _GLIBCXX_VISIBILITY(default)
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 template<typename _CharT, typename _Traits>
52 __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>*,
53 basic_streambuf<_CharT, _Traits>*, bool&);
56 * @brief The actual work of input and output (interface).
59 * This is a base class. Derived stream buffers each control a
60 * pair of character sequences: one for input, and one for output.
62 * Section [27.5.1] of the standard describes the requirements and
63 * behavior of stream buffer classes. That section (three paragraphs)
64 * is reproduced here, for simplicity and accuracy.
66 * -# Stream buffers can impose various constraints on the sequences
67 * they control. Some constraints are:
68 * - The controlled input sequence can be not readable.
69 * - The controlled output sequence can be not writable.
70 * - The controlled sequences can be associated with the contents of
71 * other representations for character sequences, such as external
73 * - The controlled sequences can support operations @e directly to or
74 * from associated sequences.
75 * - The controlled sequences can impose limitations on how the
76 * program can read characters from a sequence, write characters to
77 * a sequence, put characters back into an input sequence, or alter
78 * the stream position.
80 * -# Each sequence is characterized by three pointers which, if non-null,
81 * all point into the same @c charT array object. The array object
82 * represents, at any moment, a (sub)sequence of characters from the
83 * sequence. Operations performed on a sequence alter the values
84 * stored in these pointers, perform reads and writes directly to or
85 * from associated sequences, and alter <em>the stream position</em> and
86 * conversion state as needed to maintain this subsequence relationship.
87 * The three pointers are:
88 * - the <em>beginning pointer</em>, or lowest element address in the
89 * array (called @e xbeg here);
90 * - the <em>next pointer</em>, or next element address that is a
91 * current candidate for reading or writing (called @e xnext here);
92 * - the <em>end pointer</em>, or first element address beyond the
93 * end of the array (called @e xend here).
95 * -# The following semantic constraints shall always apply for any set
96 * of three pointers for a sequence, using the pointer names given
98 * - If @e xnext is not a null pointer, then @e xbeg and @e xend shall
99 * also be non-null pointers into the same @c charT array, as
100 * described above; otherwise, @e xbeg and @e xend shall also be null.
101 * - If @e xnext is not a null pointer and @e xnext < @e xend for an
102 * output sequence, then a <em>write position</em> is available.
103 * In this case, @e *xnext shall be assignable as the next element
104 * to write (to put, or to store a character value, into the sequence).
105 * - If @e xnext is not a null pointer and @e xbeg < @e xnext for an
106 * input sequence, then a <em>putback position</em> is available.
107 * In this case, @e xnext[-1] shall have a defined value and is the
108 * next (preceding) element to store a character that is put back
109 * into the input sequence.
110 * - If @e xnext is not a null pointer and @e xnext< @e xend for an
111 * input sequence, then a <em>read position</em> is available.
112 * In this case, @e *xnext shall have a defined value and is the
113 * next element to read (to get, or to obtain a character value,
114 * from the sequence).
116 template<typename _CharT, typename _Traits>
117 class basic_streambuf
122 * These are standard types. They permit a standardized way of
123 * referring to names of (or names dependant on) the template
124 * parameters, which are specific to the implementation.
126 typedef _CharT char_type;
127 typedef _Traits traits_type;
128 typedef typename traits_type::int_type int_type;
129 typedef typename traits_type::pos_type pos_type;
130 typedef typename traits_type::off_type off_type;
134 /// This is a non-standard type.
135 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
138 friend class basic_ios<char_type, traits_type>;
139 friend class basic_istream<char_type, traits_type>;
140 friend class basic_ostream<char_type, traits_type>;
141 friend class istreambuf_iterator<char_type, traits_type>;
142 friend class ostreambuf_iterator<char_type, traits_type>;
145 __copy_streambufs_eof<>(__streambuf_type*, __streambuf_type*, bool&);
147 template<bool _IsMove, typename _CharT2>
148 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
150 __copy_move_a2(istreambuf_iterator<_CharT2>,
151 istreambuf_iterator<_CharT2>, _CharT2*);
153 template<typename _CharT2>
154 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
155 istreambuf_iterator<_CharT2> >::__type
156 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
159 template<typename _CharT2, typename _Traits2>
160 friend basic_istream<_CharT2, _Traits2>&
161 operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2*);
163 template<typename _CharT2, typename _Traits2, typename _Alloc>
164 friend basic_istream<_CharT2, _Traits2>&
165 operator>>(basic_istream<_CharT2, _Traits2>&,
166 basic_string<_CharT2, _Traits2, _Alloc>&);
168 template<typename _CharT2, typename _Traits2, typename _Alloc>
169 friend basic_istream<_CharT2, _Traits2>&
170 getline(basic_istream<_CharT2, _Traits2>&,
171 basic_string<_CharT2, _Traits2, _Alloc>&, _CharT2);
176 * This is based on _IO_FILE, just reordered to be more consistent,
177 * and is intended to be the most minimal abstraction for an
179 * - get == input == read
180 * - put == output == write
182 char_type* _M_in_beg; // Start of get area.
183 char_type* _M_in_cur; // Current read area.
184 char_type* _M_in_end; // End of get area.
185 char_type* _M_out_beg; // Start of put area.
186 char_type* _M_out_cur; // Current put area.
187 char_type* _M_out_end; // End of put area.
189 /// Current locale setting.
190 locale _M_buf_locale;
193 /// Destructor deallocates no buffer space.
198 // [27.5.2.2.1] locales
200 * @brief Entry point for imbue().
201 * @param __loc The new locale.
202 * @return The previous locale.
204 * Calls the derived imbue(__loc).
207 pubimbue(const locale& __loc)
209 locale __tmp(this->getloc());
211 _M_buf_locale = __loc;
216 * @brief Locale access.
217 * @return The current locale in effect.
219 * If pubimbue(loc) has been called, then the most recent @c loc
220 * is returned. Otherwise the global locale in effect at the time
221 * of construction is returned.
225 { return _M_buf_locale; }
227 // [27.5.2.2.2] buffer management and positioning
230 * @brief Entry points for derived buffer functions.
232 * The public versions of @c pubfoo dispatch to the protected
233 * derived @c foo member functions, passing the arguments (if any)
234 * and returning the result unchanged.
237 pubsetbuf(char_type* __s, streamsize __n)
238 { return this->setbuf(__s, __n); }
241 * @brief Alters the stream position.
242 * @param __off Offset.
243 * @param __way Value for ios_base::seekdir.
244 * @param __mode Value for ios_base::openmode.
246 * Calls virtual seekoff function.
249 pubseekoff(off_type __off, ios_base::seekdir __way,
250 ios_base::openmode __mode = ios_base::in | ios_base::out)
251 { return this->seekoff(__off, __way, __mode); }
254 * @brief Alters the stream position.
255 * @param __sp Position
256 * @param __mode Value for ios_base::openmode.
258 * Calls virtual seekpos function.
261 pubseekpos(pos_type __sp,
262 ios_base::openmode __mode = ios_base::in | ios_base::out)
263 { return this->seekpos(__sp, __mode); }
266 * @brief Calls virtual sync function.
269 pubsync() { return this->sync(); }
272 // [27.5.2.2.3] get area
274 * @brief Looking ahead into the stream.
275 * @return The number of characters available.
277 * If a read position is available, returns the number of characters
278 * available for reading before the buffer must be refilled.
279 * Otherwise returns the derived @c showmanyc().
284 const streamsize __ret = this->egptr() - this->gptr();
285 return __ret ? __ret : this->showmanyc();
289 * @brief Getting the next character.
290 * @return The next character, or eof.
292 * Calls @c sbumpc(), and if that function returns
293 * @c traits::eof(), so does this function. Otherwise, @c sgetc().
298 int_type __ret = traits_type::eof();
299 if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(),
301 __ret = this->sgetc();
306 * @brief Getting the next character.
307 * @return The next character, or eof.
309 * If the input read position is available, returns that character
310 * and increments the read pointer, otherwise calls and returns
317 if (__builtin_expect(this->gptr() < this->egptr(), true))
319 __ret = traits_type::to_int_type(*this->gptr());
323 __ret = this->uflow();
328 * @brief Getting the next character.
329 * @return The next character, or eof.
331 * If the input read position is available, returns that character,
332 * otherwise calls and returns @c underflow(). Does not move the
333 * read position after fetching the character.
339 if (__builtin_expect(this->gptr() < this->egptr(), true))
340 __ret = traits_type::to_int_type(*this->gptr());
342 __ret = this->underflow();
347 * @brief Entry point for xsgetn.
348 * @param __s A buffer area.
349 * @param __n A count.
351 * Returns xsgetn(__s,__n). The effect is to fill @a __s[0] through
352 * @a __s[__n-1] with characters from the input sequence, if possible.
355 sgetn(char_type* __s, streamsize __n)
356 { return this->xsgetn(__s, __n); }
358 // [27.5.2.2.4] putback
360 * @brief Pushing characters back into the input stream.
361 * @param __c The character to push back.
362 * @return The previous character, if possible.
364 * Similar to sungetc(), but @a __c is pushed onto the stream
365 * instead of <em>the previous character.</em> If successful,
366 * the next character fetched from the input stream will be @a
370 sputbackc(char_type __c)
373 const bool __testpos = this->eback() < this->gptr();
374 if (__builtin_expect(!__testpos ||
375 !traits_type::eq(__c, this->gptr()[-1]), false))
376 __ret = this->pbackfail(traits_type::to_int_type(__c));
380 __ret = traits_type::to_int_type(*this->gptr());
386 * @brief Moving backwards in the input stream.
387 * @return The previous character, if possible.
389 * If a putback position is available, this function decrements
390 * the input pointer and returns that character. Otherwise,
391 * calls and returns pbackfail(). The effect is to @a unget
392 * the last character @a gotten.
398 if (__builtin_expect(this->eback() < this->gptr(), true))
401 __ret = traits_type::to_int_type(*this->gptr());
404 __ret = this->pbackfail();
408 // [27.5.2.2.5] put area
410 * @brief Entry point for all single-character output functions.
411 * @param __c A character to output.
412 * @return @a __c, if possible.
414 * One of two public output functions.
416 * If a write position is available for the output sequence (i.e.,
417 * the buffer is not full), stores @a __c in that position, increments
418 * the position, and returns @c traits::to_int_type(__c). If a write
419 * position is not available, returns @c overflow(__c).
425 if (__builtin_expect(this->pptr() < this->epptr(), true))
429 __ret = traits_type::to_int_type(__c);
432 __ret = this->overflow(traits_type::to_int_type(__c));
437 * @brief Entry point for all single-character output functions.
438 * @param __s A buffer read area.
439 * @param __n A count.
441 * One of two public output functions.
444 * Returns xsputn(__s,__n). The effect is to write @a __s[0] through
445 * @a __s[__n-1] to the output sequence, if possible.
448 sputn(const char_type* __s, streamsize __n)
449 { return this->xsputn(__s, __n); }
453 * @brief Base constructor.
455 * Only called from derived constructors, and sets up all the
456 * buffer data to zero, including the pointers described in the
457 * basic_streambuf class description. Note that, as a result,
458 * - the class starts with no read nor write positions available,
459 * - this is not an error
462 : _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
463 _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
464 _M_buf_locale(locale())
467 // [27.5.2.3.1] get area access
470 * @brief Access to the get area.
472 * These functions are only available to other protected functions,
473 * including derived classes.
475 * - eback() returns the beginning pointer for the input sequence
476 * - gptr() returns the next pointer for the input sequence
477 * - egptr() returns the end pointer for the input sequence
480 eback() const { return _M_in_beg; }
483 gptr() const { return _M_in_cur; }
486 egptr() const { return _M_in_end; }
490 * @brief Moving the read position.
491 * @param __n The delta by which to move.
493 * This just advances the read position without returning any data.
496 gbump(int __n) { _M_in_cur += __n; }
499 * @brief Setting the three read area pointers.
500 * @param __gbeg A pointer.
501 * @param __gnext A pointer.
502 * @param __gend A pointer.
503 * @post @a __gbeg == @c eback(), @a __gnext == @c gptr(), and
504 * @a __gend == @c egptr()
507 setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
514 // [27.5.2.3.2] put area access
517 * @brief Access to the put area.
519 * These functions are only available to other protected functions,
520 * including derived classes.
522 * - pbase() returns the beginning pointer for the output sequence
523 * - pptr() returns the next pointer for the output sequence
524 * - epptr() returns the end pointer for the output sequence
527 pbase() const { return _M_out_beg; }
530 pptr() const { return _M_out_cur; }
533 epptr() const { return _M_out_end; }
537 * @brief Moving the write position.
538 * @param __n The delta by which to move.
540 * This just advances the write position without returning any data.
543 pbump(int __n) { _M_out_cur += __n; }
546 * @brief Setting the three write area pointers.
547 * @param __pbeg A pointer.
548 * @param __pend A pointer.
549 * @post @a __pbeg == @c pbase(), @a __pbeg == @c pptr(), and
550 * @a __pend == @c epptr()
553 setp(char_type* __pbeg, char_type* __pend)
555 _M_out_beg = _M_out_cur = __pbeg;
559 // [27.5.2.4] virtual functions
560 // [27.5.2.4.1] locales
562 * @brief Changes translations.
563 * @param __loc A new locale.
565 * Translations done during I/O which depend on the current
566 * locale are changed by this call. The standard adds,
567 * <em>Between invocations of this function a class derived
568 * from streambuf can safely cache results of calls to locale
569 * functions and to members of facets so obtained.</em>
571 * @note Base class version does nothing.
574 imbue(const locale& __loc)
577 // [27.5.2.4.2] buffer management and positioning
579 * @brief Manipulates the buffer.
581 * Each derived class provides its own appropriate behavior. See
582 * the next-to-last paragraph of
583 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
584 * for more on this function.
586 * @note Base class version does nothing, returns @c this.
588 virtual basic_streambuf<char_type,_Traits>*
589 setbuf(char_type*, streamsize)
593 * @brief Alters the stream positions.
595 * Each derived class provides its own appropriate behavior.
596 * @note Base class version does nothing, returns a @c pos_type
597 * that represents an invalid stream position.
600 seekoff(off_type, ios_base::seekdir,
601 ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
602 { return pos_type(off_type(-1)); }
605 * @brief Alters the stream positions.
607 * Each derived class provides its own appropriate behavior.
608 * @note Base class version does nothing, returns a @c pos_type
609 * that represents an invalid stream position.
613 ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
614 { return pos_type(off_type(-1)); }
617 * @brief Synchronizes the buffer arrays with the controlled sequences.
618 * @return -1 on failure.
620 * Each derived class provides its own appropriate behavior,
621 * including the definition of @a failure.
622 * @note Base class version does nothing, returns zero.
627 // [27.5.2.4.3] get area
629 * @brief Investigating the data available.
630 * @return An estimate of the number of characters available in the
631 * input sequence, or -1.
633 * <em>If it returns a positive value, then successive calls to
634 * @c underflow() will not return @c traits::eof() until at
635 * least that number of characters have been supplied. If @c
636 * showmanyc() returns -1, then calls to @c underflow() or @c
637 * uflow() will fail.</em> [27.5.2.4.3]/1
639 * @note Base class version does nothing, returns zero.
640 * @note The standard adds that <em>the intention is not only that the
641 * calls [to underflow or uflow] will not return @c eof() but
642 * that they will return immediately.</em>
643 * @note The standard adds that <em>the morphemes of @c showmanyc are
644 * @b es-how-many-see, not @b show-manic.</em>
647 showmanyc() { return 0; }
650 * @brief Multiple character extraction.
651 * @param __s A buffer area.
652 * @param __n Maximum number of characters to assign.
653 * @return The number of characters assigned.
655 * Fills @a __s[0] through @a __s[__n-1] with characters from the input
656 * sequence, as if by @c sbumpc(). Stops when either @a __n characters
657 * have been copied, or when @c traits::eof() would be copied.
659 * It is expected that derived classes provide a more efficient
660 * implementation by overriding this definition.
663 xsgetn(char_type* __s, streamsize __n);
666 * @brief Fetches more data from the controlled sequence.
667 * @return The first character from the <em>pending sequence</em>.
669 * Informally, this function is called when the input buffer is
670 * exhausted (or does not exist, as buffering need not actually be
671 * done). If a buffer exists, it is @a refilled. In either case, the
672 * next available character is returned, or @c traits::eof() to
673 * indicate a null pending sequence.
675 * For a formal definition of the pending sequence, see a good text
676 * such as Langer & Kreft, or [27.5.2.4.3]/7-14.
678 * A functioning input streambuf can be created by overriding only
679 * this function (no buffer area will be used). For an example, see
680 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25.html
682 * @note Base class version does nothing, returns eof().
686 { return traits_type::eof(); }
689 * @brief Fetches more data from the controlled sequence.
690 * @return The first character from the <em>pending sequence</em>.
692 * Informally, this function does the same thing as @c underflow(),
693 * and in fact is required to call that function. It also returns
694 * the new character, like @c underflow() does. However, this
695 * function also moves the read position forward by one.
700 int_type __ret = traits_type::eof();
701 const bool __testeof = traits_type::eq_int_type(this->underflow(),
705 __ret = traits_type::to_int_type(*this->gptr());
711 // [27.5.2.4.4] putback
713 * @brief Tries to back up the input sequence.
714 * @param __c The character to be inserted back into the sequence.
715 * @return eof() on failure, <em>some other value</em> on success
716 * @post The constraints of @c gptr(), @c eback(), and @c pptr()
717 * are the same as for @c underflow().
719 * @note Base class version does nothing, returns eof().
722 pbackfail(int_type __c = traits_type::eof())
723 { return traits_type::eof(); }
727 * @brief Multiple character insertion.
728 * @param __s A buffer area.
729 * @param __n Maximum number of characters to write.
730 * @return The number of characters written.
732 * Writes @a __s[0] through @a __s[__n-1] to the output sequence, as if
733 * by @c sputc(). Stops when either @a n characters have been
734 * copied, or when @c sputc() would return @c traits::eof().
736 * It is expected that derived classes provide a more efficient
737 * implementation by overriding this definition.
740 xsputn(const char_type* __s, streamsize __n);
743 * @brief Consumes data from the buffer; writes to the
744 * controlled sequence.
745 * @param __c An additional character to consume.
746 * @return eof() to indicate failure, something else (usually
747 * @a __c, or not_eof())
749 * Informally, this function is called when the output buffer
750 * is full (or does not exist, as buffering need not actually
751 * be done). If a buffer exists, it is @a consumed, with
752 * <em>some effect</em> on the controlled sequence.
753 * (Typically, the buffer is written out to the sequence
754 * verbatim.) In either case, the character @a c is also
755 * written out, if @a __c is not @c eof().
757 * For a formal definition of this function, see a good text
758 * such as Langer & Kreft, or [27.5.2.4.5]/3-7.
760 * A functioning output streambuf can be created by overriding only
761 * this function (no buffer area will be used).
763 * @note Base class version does nothing, returns eof().
766 overflow(int_type __c = traits_type::eof())
767 { return traits_type::eof(); }
769 #if _GLIBCXX_USE_DEPRECATED
773 * @brief Tosses a character.
775 * Advances the read pointer, ignoring the character that would have
778 * See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
783 if (this->gptr() < this->egptr())
790 // Also used by specializations for char and wchar_t in src.
792 __safe_gbump(streamsize __n) { _M_in_cur += __n; }
795 __safe_pbump(streamsize __n) { _M_out_cur += __n; }
798 // _GLIBCXX_RESOLVE_LIB_DEFECTS
799 // Side effect of DR 50.
800 basic_streambuf(const __streambuf_type& __sb)
801 : _M_in_beg(__sb._M_in_beg), _M_in_cur(__sb._M_in_cur),
802 _M_in_end(__sb._M_in_end), _M_out_beg(__sb._M_out_beg),
803 _M_out_cur(__sb._M_out_cur), _M_out_end(__sb._M_out_cur),
804 _M_buf_locale(__sb._M_buf_locale)
808 operator=(const __streambuf_type&) { return *this; };
811 // Explicit specialization declarations, defined in src/streambuf.cc.
814 __copy_streambufs_eof(basic_streambuf<char>* __sbin,
815 basic_streambuf<char>* __sbout, bool& __ineof);
816 #ifdef _GLIBCXX_USE_WCHAR_T
819 __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin,
820 basic_streambuf<wchar_t>* __sbout, bool& __ineof);
823 _GLIBCXX_END_NAMESPACE_VERSION
826 #include <bits/streambuf.tcc>
828 #endif /* _GLIBCXX_STREAMBUF */