1 // Stream buffer classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008 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 2, 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 // You should have received a copy of the GNU General Public License
18 // along with this library; see the file COPYING. If not, write to
19 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 // Boston, MA 02110-1301, USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 * This is a Standard C++ Library header.
36 // ISO C++ 14882: 27.5 Stream buffers
39 #ifndef _GLIBXX_STREAMBUF
40 #define _GLIBXX_STREAMBUF 1
42 #pragma GCC system_header
44 #include <bits/c++config.h>
46 #include <bits/localefwd.h>
47 #include <bits/ios_base.h>
48 #include <bits/cpp_type_traits.h>
49 #include <ext/type_traits.h>
51 _GLIBCXX_BEGIN_NAMESPACE(std)
53 template<typename _CharT, typename _Traits>
55 __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>*,
56 basic_streambuf<_CharT, _Traits>*, bool&);
59 * @brief The actual work of input and output (interface).
61 * This is a base class. Derived stream buffers each control a
62 * pair of character sequences: one for input, and one for output.
64 * Section [27.5.1] of the standard describes the requirements and
65 * behavior of stream buffer classes. That section (three paragraphs)
66 * is reproduced here, for simplicity and accuracy.
68 * -# Stream buffers can impose various constraints on the sequences
69 * they control. Some constraints are:
70 * - The controlled input sequence can be not readable.
71 * - The controlled output sequence can be not writable.
72 * - The controlled sequences can be associated with the contents of
73 * other representations for character sequences, such as external
75 * - The controlled sequences can support operations @e directly to or
76 * from associated sequences.
77 * - The controlled sequences can impose limitations on how the
78 * program can read characters from a sequence, write characters to
79 * a sequence, put characters back into an input sequence, or alter
80 * the stream position.
82 * -# Each sequence is characterized by three pointers which, if non-null,
83 * all point into the same @c charT array object. The array object
84 * represents, at any moment, a (sub)sequence of characters from the
85 * sequence. Operations performed on a sequence alter the values
86 * stored in these pointers, perform reads and writes directly to or
87 * from associated sequences, and alter "the stream position" and
88 * conversion state as needed to maintain this subsequence relationship.
89 * The three pointers are:
90 * - the <em>beginning pointer</em>, or lowest element address in the
91 * array (called @e xbeg here);
92 * - the <em>next pointer</em>, or next element address that is a
93 * current candidate for reading or writing (called @e xnext here);
94 * - the <em>end pointer</em>, or first element address beyond the
95 * end of the array (called @e xend here).
97 * -# The following semantic constraints shall always apply for any set
98 * of three pointers for a sequence, using the pointer names given
100 * - If @e xnext is not a null pointer, then @e xbeg and @e xend shall
101 * also be non-null pointers into the same @c charT array, as
102 * described above; otherwise, @e xbeg and @e xend shall also be null.
103 * - If @e xnext is not a null pointer and @e xnext < @e xend for an
104 * output sequence, then a <em>write position</em> is available.
105 * In this case, @e *xnext shall be assignable as the next element
106 * to write (to put, or to store a character value, into the sequence).
107 * - If @e xnext is not a null pointer and @e xbeg < @e xnext for an
108 * input sequence, then a <em>putback position</em> is available.
109 * In this case, @e xnext[-1] shall have a defined value and is the
110 * next (preceding) element to store a character that is put back
111 * into the input sequence.
112 * - If @e xnext is not a null pointer and @e xnext< @e xend for an
113 * input sequence, then a <em>read position</em> is available.
114 * In this case, @e *xnext shall have a defined value and is the
115 * next element to read (to get, or to obtain a character value,
116 * from the sequence).
118 template<typename _CharT, typename _Traits>
119 class basic_streambuf
124 * These are standard types. They permit a standardized way of
125 * referring to names of (or names dependant on) the template
126 * parameters, which are specific to the implementation.
128 typedef _CharT char_type;
129 typedef _Traits traits_type;
130 typedef typename traits_type::int_type int_type;
131 typedef typename traits_type::pos_type pos_type;
132 typedef typename traits_type::off_type off_type;
136 /// This is a non-standard type.
137 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
140 friend class basic_ios<char_type, traits_type>;
141 friend class basic_istream<char_type, traits_type>;
142 friend class basic_ostream<char_type, traits_type>;
143 friend class istreambuf_iterator<char_type, traits_type>;
144 friend class ostreambuf_iterator<char_type, traits_type>;
147 __copy_streambufs_eof<>(__streambuf_type*, __streambuf_type*, bool&);
149 template<bool _IsMove, typename _CharT2>
150 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
152 __copy_move_a2(istreambuf_iterator<_CharT2>,
153 istreambuf_iterator<_CharT2>, _CharT2*);
155 template<typename _CharT2>
156 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
157 istreambuf_iterator<_CharT2> >::__type
158 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
161 template<typename _CharT2, typename _Traits2>
162 friend basic_istream<_CharT2, _Traits2>&
163 operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2*);
165 template<typename _CharT2, typename _Traits2, typename _Alloc>
166 friend basic_istream<_CharT2, _Traits2>&
167 operator>>(basic_istream<_CharT2, _Traits2>&,
168 basic_string<_CharT2, _Traits2, _Alloc>&);
170 template<typename _CharT2, typename _Traits2, typename _Alloc>
171 friend basic_istream<_CharT2, _Traits2>&
172 getline(basic_istream<_CharT2, _Traits2>&,
173 basic_string<_CharT2, _Traits2, _Alloc>&, _CharT2);
178 * This is based on _IO_FILE, just reordered to be more consistent,
179 * and is intended to be the most minimal abstraction for an
181 * - get == input == read
182 * - put == output == write
184 char_type* _M_in_beg; // Start of get area.
185 char_type* _M_in_cur; // Current read area.
186 char_type* _M_in_end; // End of get area.
187 char_type* _M_out_beg; // Start of put area.
188 char_type* _M_out_cur; // Current put area.
189 char_type* _M_out_end; // End of put area.
191 /// Current locale setting.
192 locale _M_buf_locale;
195 /// Destructor deallocates no buffer space.
200 // [27.5.2.2.1] locales
202 * @brief Entry point for imbue().
203 * @param loc The new locale.
204 * @return The previous locale.
206 * Calls the derived imbue(loc).
209 pubimbue(const locale &__loc)
211 locale __tmp(this->getloc());
213 _M_buf_locale = __loc;
218 * @brief Locale access.
219 * @return The current locale in effect.
221 * If pubimbue(loc) has been called, then the most recent @c loc
222 * is returned. Otherwise the global locale in effect at the time
223 * of construction is returned.
227 { return _M_buf_locale; }
229 // [27.5.2.2.2] buffer management and positioning
232 * @brief Entry points for derived buffer functions.
234 * The public versions of @c pubfoo dispatch to the protected
235 * derived @c foo member functions, passing the arguments (if any)
236 * and returning the result unchanged.
239 pubsetbuf(char_type* __s, streamsize __n)
240 { return this->setbuf(__s, __n); }
243 pubseekoff(off_type __off, ios_base::seekdir __way,
244 ios_base::openmode __mode = ios_base::in | ios_base::out)
245 { return this->seekoff(__off, __way, __mode); }
248 pubseekpos(pos_type __sp,
249 ios_base::openmode __mode = ios_base::in | ios_base::out)
250 { return this->seekpos(__sp, __mode); }
253 pubsync() { return this->sync(); }
256 // [27.5.2.2.3] get area
258 * @brief Looking ahead into the stream.
259 * @return The number of characters available.
261 * If a read position is available, returns the number of characters
262 * available for reading before the buffer must be refilled.
263 * Otherwise returns the derived @c showmanyc().
268 const streamsize __ret = this->egptr() - this->gptr();
269 return __ret ? __ret : this->showmanyc();
273 * @brief Getting the next character.
274 * @return The next character, or eof.
276 * Calls @c sbumpc(), and if that function returns
277 * @c traits::eof(), so does this function. Otherwise, @c sgetc().
282 int_type __ret = traits_type::eof();
283 if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(),
285 __ret = this->sgetc();
290 * @brief Getting the next character.
291 * @return The next character, or eof.
293 * If the input read position is available, returns that character
294 * and increments the read pointer, otherwise calls and returns
301 if (__builtin_expect(this->gptr() < this->egptr(), true))
303 __ret = traits_type::to_int_type(*this->gptr());
307 __ret = this->uflow();
312 * @brief Getting the next character.
313 * @return The next character, or eof.
315 * If the input read position is available, returns that character,
316 * otherwise calls and returns @c underflow(). Does not move the
317 * read position after fetching the character.
323 if (__builtin_expect(this->gptr() < this->egptr(), true))
324 __ret = traits_type::to_int_type(*this->gptr());
326 __ret = this->underflow();
331 * @brief Entry point for xsgetn.
332 * @param s A buffer area.
335 * Returns xsgetn(s,n). The effect is to fill @a s[0] through
336 * @a s[n-1] with characters from the input sequence, if possible.
339 sgetn(char_type* __s, streamsize __n)
340 { return this->xsgetn(__s, __n); }
342 // [27.5.2.2.4] putback
344 * @brief Pushing characters back into the input stream.
345 * @param c The character to push back.
346 * @return The previous character, if possible.
348 * Similar to sungetc(), but @a c is pushed onto the stream instead
349 * of "the previous character". If successful, the next character
350 * fetched from the input stream will be @a c.
353 sputbackc(char_type __c)
356 const bool __testpos = this->eback() < this->gptr();
357 if (__builtin_expect(!__testpos ||
358 !traits_type::eq(__c, this->gptr()[-1]), false))
359 __ret = this->pbackfail(traits_type::to_int_type(__c));
363 __ret = traits_type::to_int_type(*this->gptr());
369 * @brief Moving backwards in the input stream.
370 * @return The previous character, if possible.
372 * If a putback position is available, this function decrements the
373 * input pointer and returns that character. Otherwise, calls and
374 * returns pbackfail(). The effect is to "unget" the last character
381 if (__builtin_expect(this->eback() < this->gptr(), true))
384 __ret = traits_type::to_int_type(*this->gptr());
387 __ret = this->pbackfail();
391 // [27.5.2.2.5] put area
393 * @brief Entry point for all single-character output functions.
394 * @param c A character to output.
395 * @return @a c, if possible.
397 * One of two public output functions.
399 * If a write position is available for the output sequence (i.e.,
400 * the buffer is not full), stores @a c in that position, increments
401 * the position, and returns @c traits::to_int_type(c). If a write
402 * position is not available, returns @c overflow(c).
408 if (__builtin_expect(this->pptr() < this->epptr(), true))
412 __ret = traits_type::to_int_type(__c);
415 __ret = this->overflow(traits_type::to_int_type(__c));
420 * @brief Entry point for all single-character output functions.
421 * @param s A buffer read area.
424 * One of two public output functions.
427 * Returns xsputn(s,n). The effect is to write @a s[0] through
428 * @a s[n-1] to the output sequence, if possible.
431 sputn(const char_type* __s, streamsize __n)
432 { return this->xsputn(__s, __n); }
436 * @brief Base constructor.
438 * Only called from derived constructors, and sets up all the
439 * buffer data to zero, including the pointers described in the
440 * basic_streambuf class description. Note that, as a result,
441 * - the class starts with no read nor write positions available,
442 * - this is not an error
445 : _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
446 _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
447 _M_buf_locale(locale())
450 // [27.5.2.3.1] get area access
453 * @brief Access to the get area.
455 * These functions are only available to other protected functions,
456 * including derived classes.
458 * - eback() returns the beginning pointer for the input sequence
459 * - gptr() returns the next pointer for the input sequence
460 * - egptr() returns the end pointer for the input sequence
463 eback() const { return _M_in_beg; }
466 gptr() const { return _M_in_cur; }
469 egptr() const { return _M_in_end; }
473 * @brief Moving the read position.
474 * @param n The delta by which to move.
476 * This just advances the read position without returning any data.
479 gbump(int __n) { _M_in_cur += __n; }
482 * @brief Setting the three read area pointers.
483 * @param gbeg A pointer.
484 * @param gnext A pointer.
485 * @param gend A pointer.
486 * @post @a gbeg == @c eback(), @a gnext == @c gptr(), and
487 * @a gend == @c egptr()
490 setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
497 // [27.5.2.3.2] put area access
500 * @brief Access to the put area.
502 * These functions are only available to other protected functions,
503 * including derived classes.
505 * - pbase() returns the beginning pointer for the output sequence
506 * - pptr() returns the next pointer for the output sequence
507 * - epptr() returns the end pointer for the output sequence
510 pbase() const { return _M_out_beg; }
513 pptr() const { return _M_out_cur; }
516 epptr() const { return _M_out_end; }
520 * @brief Moving the write position.
521 * @param n The delta by which to move.
523 * This just advances the write position without returning any data.
526 pbump(int __n) { _M_out_cur += __n; }
529 * @brief Setting the three write area pointers.
530 * @param pbeg A pointer.
531 * @param pend A pointer.
532 * @post @a pbeg == @c pbase(), @a pbeg == @c pptr(), and
533 * @a pend == @c epptr()
536 setp(char_type* __pbeg, char_type* __pend)
538 _M_out_beg = _M_out_cur = __pbeg;
542 // [27.5.2.4] virtual functions
543 // [27.5.2.4.1] locales
545 * @brief Changes translations.
546 * @param loc A new locale.
548 * Translations done during I/O which depend on the current locale
549 * are changed by this call. The standard adds, "Between invocations
550 * of this function a class derived from streambuf can safely cache
551 * results of calls to locale functions and to members of facets
554 * @note Base class version does nothing.
560 // [27.5.2.4.2] buffer management and positioning
562 * @brief Manipulates the buffer.
564 * Each derived class provides its own appropriate behavior. See
565 * the next-to-last paragraph of
566 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for
567 * more on this function.
569 * @note Base class version does nothing, returns @c this.
571 virtual basic_streambuf<char_type,_Traits>*
572 setbuf(char_type*, streamsize)
576 * @brief Alters the stream positions.
578 * Each derived class provides its own appropriate behavior.
579 * @note Base class version does nothing, returns a @c pos_type
580 * that represents an invalid stream position.
583 seekoff(off_type, ios_base::seekdir,
584 ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
585 { return pos_type(off_type(-1)); }
588 * @brief Alters the stream positions.
590 * Each derived class provides its own appropriate behavior.
591 * @note Base class version does nothing, returns a @c pos_type
592 * that represents an invalid stream position.
596 ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
597 { return pos_type(off_type(-1)); }
600 * @brief Synchronizes the buffer arrays with the controlled sequences.
601 * @return -1 on failure.
603 * Each derived class provides its own appropriate behavior,
604 * including the definition of "failure".
605 * @note Base class version does nothing, returns zero.
610 // [27.5.2.4.3] get area
612 * @brief Investigating the data available.
613 * @return An estimate of the number of characters available in the
614 * input sequence, or -1.
616 * "If it returns a positive value, then successive calls to
617 * @c underflow() will not return @c traits::eof() until at least that
618 * number of characters have been supplied. If @c showmanyc()
619 * returns -1, then calls to @c underflow() or @c uflow() will fail."
622 * @note Base class version does nothing, returns zero.
623 * @note The standard adds that "the intention is not only that the
624 * calls [to underflow or uflow] will not return @c eof() but
625 * that they will return "immediately".
626 * @note The standard adds that "the morphemes of @c showmanyc are
627 * "es-how-many-see", not "show-manic".
630 showmanyc() { return 0; }
633 * @brief Multiple character extraction.
634 * @param s A buffer area.
635 * @param n Maximum number of characters to assign.
636 * @return The number of characters assigned.
638 * Fills @a s[0] through @a s[n-1] with characters from the input
639 * sequence, as if by @c sbumpc(). Stops when either @a n characters
640 * have been copied, or when @c traits::eof() would be copied.
642 * It is expected that derived classes provide a more efficient
643 * implementation by overriding this definition.
646 xsgetn(char_type* __s, streamsize __n);
649 * @brief Fetches more data from the controlled sequence.
650 * @return The first character from the <em>pending sequence</em>.
652 * Informally, this function is called when the input buffer is
653 * exhausted (or does not exist, as buffering need not actually be
654 * done). If a buffer exists, it is "refilled". In either case, the
655 * next available character is returned, or @c traits::eof() to
656 * indicate a null pending sequence.
658 * For a formal definition of the pending sequence, see a good text
659 * such as Langer & Kreft, or [27.5.2.4.3]/7-14.
661 * A functioning input streambuf can be created by overriding only
662 * this function (no buffer area will be used). For an example, see
663 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#6
665 * @note Base class version does nothing, returns eof().
669 { return traits_type::eof(); }
672 * @brief Fetches more data from the controlled sequence.
673 * @return The first character from the <em>pending sequence</em>.
675 * Informally, this function does the same thing as @c underflow(),
676 * and in fact is required to call that function. It also returns
677 * the new character, like @c underflow() does. However, this
678 * function also moves the read position forward by one.
683 int_type __ret = traits_type::eof();
684 const bool __testeof = traits_type::eq_int_type(this->underflow(),
688 __ret = traits_type::to_int_type(*this->gptr());
694 // [27.5.2.4.4] putback
696 * @brief Tries to back up the input sequence.
697 * @param c The character to be inserted back into the sequence.
698 * @return eof() on failure, "some other value" on success
699 * @post The constraints of @c gptr(), @c eback(), and @c pptr()
700 * are the same as for @c underflow().
702 * @note Base class version does nothing, returns eof().
705 pbackfail(int_type /* __c */ = traits_type::eof())
706 { return traits_type::eof(); }
710 * @brief Multiple character insertion.
711 * @param s A buffer area.
712 * @param n Maximum number of characters to write.
713 * @return The number of characters written.
715 * Writes @a s[0] through @a s[n-1] to the output sequence, as if
716 * by @c sputc(). Stops when either @a n characters have been
717 * copied, or when @c sputc() would return @c traits::eof().
719 * It is expected that derived classes provide a more efficient
720 * implementation by overriding this definition.
723 xsputn(const char_type* __s, streamsize __n);
726 * @brief Consumes data from the buffer; writes to the
727 * controlled sequence.
728 * @param c An additional character to consume.
729 * @return eof() to indicate failure, something else (usually
730 * @a c, or not_eof())
732 * Informally, this function is called when the output buffer is full
733 * (or does not exist, as buffering need not actually be done). If a
734 * buffer exists, it is "consumed", with "some effect" on the
735 * controlled sequence. (Typically, the buffer is written out to the
736 * sequence verbatim.) In either case, the character @a c is also
737 * written out, if @a c is not @c eof().
739 * For a formal definition of this function, see a good text
740 * such as Langer & Kreft, or [27.5.2.4.5]/3-7.
742 * A functioning output streambuf can be created by overriding only
743 * this function (no buffer area will be used).
745 * @note Base class version does nothing, returns eof().
748 overflow(int_type /* __c */ = traits_type::eof())
749 { return traits_type::eof(); }
751 #if _GLIBCXX_DEPRECATED
755 * @brief Tosses a character.
757 * Advances the read pointer, ignoring the character that would have
760 * See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
765 if (this->gptr() < this->egptr())
773 // _GLIBCXX_RESOLVE_LIB_DEFECTS
774 // Side effect of DR 50.
775 basic_streambuf(const __streambuf_type& __sb)
776 : _M_in_beg(__sb._M_in_beg), _M_in_cur(__sb._M_in_cur),
777 _M_in_end(__sb._M_in_end), _M_out_beg(__sb._M_out_beg),
778 _M_out_cur(__sb._M_out_cur), _M_out_end(__sb._M_out_cur),
779 _M_buf_locale(__sb._M_buf_locale)
783 operator=(const __streambuf_type&) { return *this; };
786 // Explicit specialization declarations, defined in src/streambuf.cc.
789 __copy_streambufs_eof(basic_streambuf<char>* __sbin,
790 basic_streambuf<char>* __sbout, bool& __ineof);
791 #ifdef _GLIBCXX_USE_WCHAR_T
794 __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin,
795 basic_streambuf<wchar_t>* __sbout, bool& __ineof);
798 _GLIBCXX_END_NAMESPACE
800 #ifndef _GLIBCXX_EXPORT_TEMPLATE
801 # include <bits/streambuf.tcc>
804 #endif /* _GLIBCXX_STREAMBUF */