1 // Stream buffer classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 // 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 along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 // ISO C++ 14882: 27.5 Stream buffers
36 * This is a Standard C++ Library header.
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>
56 template<typename _CharT
, typename _Traits
>
58 __copy_streambufs(basic_streambuf
<_CharT
, _Traits
>* __sbin
,
59 basic_streambuf
<_CharT
, _Traits
>* __sbout
);
62 * @brief The actual work of input and output (interface).
64 * This is a base class. Derived stream buffers each control a
65 * pair of character sequences: one for input, and one for output.
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.
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
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.
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 "the stream position" 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).
100 * -# The following semantic constraints shall always apply for any set
101 * of three pointers for a sequence, using the pointer names given
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).
121 template<typename _CharT
, typename _Traits
>
122 class basic_streambuf
127 * These are standard types. They permit a standardized way of
128 * referring to names of (or names dependant on) the template
129 * parameters, which are specific to the implementation.
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
;
141 * This is a non-standard type.
144 typedef basic_streambuf
<char_type
, traits_type
> __streambuf_type
;
147 friend class basic_ios
<char_type
, traits_type
>;
148 friend class basic_istream
<char_type
, traits_type
>;
149 friend class basic_ostream
<char_type
, traits_type
>;
150 friend class istreambuf_iterator
<char_type
, traits_type
>;
151 friend class ostreambuf_iterator
<char_type
, traits_type
>;
154 __copy_streambufs
<>(__streambuf_type
* __sbin
,
155 __streambuf_type
* __sbout
);
157 template<typename _CharT2
, typename _Traits2
, typename _Alloc
>
158 friend basic_istream
<_CharT2
, _Traits2
>&
159 getline(basic_istream
<_CharT2
, _Traits2
>&,
160 basic_string
<_CharT2
, _Traits2
, _Alloc
>&, _CharT2
);
166 * This is based on _IO_FILE, just reordered to be more consistent,
167 * and is intended to be the most minimal abstraction for an
169 * - get == input == read
170 * - put == output == write
173 char_type
* _M_in_beg
; // Start of get area.
174 char_type
* _M_in_cur
; // Current read area.
175 char_type
* _M_in_end
; // End of get area.
176 char_type
* _M_out_beg
; // Start of put area.
177 char_type
* _M_out_cur
; // Current put area.
178 char_type
* _M_out_end
; // End of put area.
182 * Current locale setting.
185 locale _M_buf_locale
;
188 /// Destructor deallocates no buffer space.
193 // [27.5.2.2.1] locales
195 * @brief Entry point for imbue().
196 * @param loc The new locale.
197 * @return The previous locale.
199 * Calls the derived imbue(loc).
202 pubimbue(const locale
&__loc
)
204 locale
__tmp(this->getloc());
206 _M_buf_locale
= __loc
;
211 * @brief Locale access.
212 * @return The current locale in effect.
214 * If pubimbue(loc) has been called, then the most recent @c loc
215 * is returned. Otherwise the global locale in effect at the time
216 * of construction is returned.
220 { return _M_buf_locale
; }
222 // [27.5.2.2.2] buffer management and positioning
225 * @brief Entry points for derived buffer functions.
227 * The public versions of @c pubfoo dispatch to the protected
228 * derived @c foo member functions, passing the arguments (if any)
229 * and returning the result unchanged.
232 pubsetbuf(char_type
* __s
, streamsize __n
)
233 { return this->setbuf(__s
, __n
); }
236 pubseekoff(off_type __off
, ios_base::seekdir __way
,
237 ios_base::openmode __mode
= ios_base::in
| ios_base::out
)
238 { return this->seekoff(__off
, __way
, __mode
); }
241 pubseekpos(pos_type __sp
,
242 ios_base::openmode __mode
= ios_base::in
| ios_base::out
)
243 { return this->seekpos(__sp
, __mode
); }
246 pubsync() { return this->sync(); }
249 // [27.5.2.2.3] get area
251 * @brief Looking ahead into the stream.
252 * @return The number of characters available.
254 * If a read position is available, returns the number of characters
255 * available for reading before the buffer must be refilled.
256 * Otherwise returns the derived @c showmanyc().
261 const streamsize __ret
= this->egptr() - this->gptr();
262 return __ret
? __ret
: this->showmanyc();
266 * @brief Getting the next character.
267 * @return The next character, or eof.
269 * Calls @c sbumpc(), and if that function returns
270 * @c traits::eof(), so does this function. Otherwise, @c sgetc().
275 int_type __ret
= traits_type::eof();
276 if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(),
278 __ret
= this->sgetc();
283 * @brief Getting the next character.
284 * @return The next character, or eof.
286 * If the input read position is available, returns that character
287 * and increments the read pointer, otherwise calls and returns
294 if (__builtin_expect(this->gptr() < this->egptr(), true))
296 __ret
= traits_type::to_int_type(*this->gptr());
300 __ret
= this->uflow();
305 * @brief Getting the next character.
306 * @return The next character, or eof.
308 * If the input read position is available, returns that character,
309 * otherwise calls and returns @c underflow(). Does not move the
310 * read position after fetching the character.
316 if (__builtin_expect(this->gptr() < this->egptr(), true))
317 __ret
= traits_type::to_int_type(*this->gptr());
319 __ret
= this->underflow();
324 * @brief Entry point for xsgetn.
325 * @param s A buffer area.
328 * Returns xsgetn(s,n). The effect is to fill @a s[0] through
329 * @a s[n-1] with characters from the input sequence, if possible.
332 sgetn(char_type
* __s
, streamsize __n
)
333 { return this->xsgetn(__s
, __n
); }
335 // [27.5.2.2.4] putback
337 * @brief Pushing characters back into the input stream.
338 * @param c The character to push back.
339 * @return The previous character, if possible.
341 * Similar to sungetc(), but @a c is pushed onto the stream instead
342 * of "the previous character". If successful, the next character
343 * fetched from the input stream will be @a c.
346 sputbackc(char_type __c
)
349 const bool __testpos
= this->eback() < this->gptr();
350 if (__builtin_expect(!__testpos
||
351 !traits_type::eq(__c
, this->gptr()[-1]), false))
352 __ret
= this->pbackfail(traits_type::to_int_type(__c
));
356 __ret
= traits_type::to_int_type(*this->gptr());
362 * @brief Moving backwards in the input stream.
363 * @return The previous character, if possible.
365 * If a putback position is available, this function decrements the
366 * input pointer and returns that character. Otherwise, calls and
367 * returns pbackfail(). The effect is to "unget" the last character
374 if (__builtin_expect(this->eback() < this->gptr(), true))
377 __ret
= traits_type::to_int_type(*this->gptr());
380 __ret
= this->pbackfail();
384 // [27.5.2.2.5] put area
386 * @brief Entry point for all single-character output functions.
387 * @param c A character to output.
388 * @return @a c, if possible.
390 * One of two public output functions.
392 * If a write position is available for the output sequence (i.e.,
393 * the buffer is not full), stores @a c in that position, increments
394 * the position, and returns @c traits::to_int_type(c). If a write
395 * position is not available, returns @c overflow(c).
401 if (__builtin_expect(this->pptr() < this->epptr(), true))
405 __ret
= traits_type::to_int_type(__c
);
408 __ret
= this->overflow(traits_type::to_int_type(__c
));
413 * @brief Entry point for all single-character output functions.
414 * @param s A buffer read area.
417 * One of two public output functions.
420 * Returns xsputn(s,n). The effect is to write @a s[0] through
421 * @a s[n-1] to the output sequence, if possible.
424 sputn(const char_type
* __s
, streamsize __n
)
425 { return this->xsputn(__s
, __n
); }
429 * @brief Base constructor.
431 * Only called from derived constructors, and sets up all the
432 * buffer data to zero, including the pointers described in the
433 * basic_streambuf class description. Note that, as a result,
434 * - the class starts with no read nor write positions available,
435 * - this is not an error
438 : _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
439 _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
440 _M_buf_locale(locale())
443 // [27.5.2.3.1] get area access
446 * @brief Access to the get area.
448 * These functions are only available to other protected functions,
449 * including derived classes.
451 * - eback() returns the beginning pointer for the input sequence
452 * - gptr() returns the next pointer for the input sequence
453 * - egptr() returns the end pointer for the input sequence
456 eback() const { return _M_in_beg
; }
459 gptr() const { return _M_in_cur
; }
462 egptr() const { return _M_in_end
; }
466 * @brief Moving the read position.
467 * @param n The delta by which to move.
469 * This just advances the read position without returning any data.
472 gbump(int __n
) { _M_in_cur
+= __n
; }
475 * @brief Setting the three read area pointers.
476 * @param gbeg A pointer.
477 * @param gnext A pointer.
478 * @param gend A pointer.
479 * @post @a gbeg == @c eback(), @a gnext == @c gptr(), and
480 * @a gend == @c egptr()
483 setg(char_type
* __gbeg
, char_type
* __gnext
, char_type
* __gend
)
490 // [27.5.2.3.2] put area access
493 * @brief Access to the put area.
495 * These functions are only available to other protected functions,
496 * including derived classes.
498 * - pbase() returns the beginning pointer for the output sequence
499 * - pptr() returns the next pointer for the output sequence
500 * - epptr() returns the end pointer for the output sequence
503 pbase() const { return _M_out_beg
; }
506 pptr() const { return _M_out_cur
; }
509 epptr() const { return _M_out_end
; }
513 * @brief Moving the write position.
514 * @param n The delta by which to move.
516 * This just advances the write position without returning any data.
519 pbump(int __n
) { _M_out_cur
+= __n
; }
522 * @brief Setting the three write area pointers.
523 * @param pbeg A pointer.
524 * @param pend A pointer.
525 * @post @a pbeg == @c pbase(), @a pbeg == @c pptr(), and
526 * @a pend == @c epptr()
529 setp(char_type
* __pbeg
, char_type
* __pend
)
531 _M_out_beg
= _M_out_cur
= __pbeg
;
535 // [27.5.2.4] virtual functions
536 // [27.5.2.4.1] locales
538 * @brief Changes translations.
539 * @param loc A new locale.
541 * Translations done during I/O which depend on the current locale
542 * are changed by this call. The standard adds, "Between invocations
543 * of this function a class derived from streambuf can safely cache
544 * results of calls to locale functions and to members of facets
547 * @note Base class version does nothing.
553 // [27.5.2.4.2] buffer management and positioning
555 * @brief Maniuplates the buffer.
557 * Each derived class provides its own appropriate behavior. See
558 * the next-to-last paragraph of
559 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for
560 * more on this function.
562 * @note Base class version does nothing, returns @c this.
564 virtual basic_streambuf
<char_type
,_Traits
>*
565 setbuf(char_type
*, streamsize
)
569 * @brief Alters the stream positions.
571 * Each derived class provides its own appropriate behavior.
572 * @note Base class version does nothing, returns a @c pos_type
573 * that represents an invalid stream position.
576 seekoff(off_type
, ios_base::seekdir
,
577 ios_base::openmode
/*__mode*/ = ios_base::in
| ios_base::out
)
578 { return pos_type(off_type(-1)); }
581 * @brief Alters the stream positions.
583 * Each derived class provides its own appropriate behavior.
584 * @note Base class version does nothing, returns a @c pos_type
585 * that represents an invalid stream position.
589 ios_base::openmode
/*__mode*/ = ios_base::in
| ios_base::out
)
590 { return pos_type(off_type(-1)); }
593 * @brief Synchronizes the buffer arrays with the controlled sequences.
594 * @return -1 on failure.
596 * Each derived class provides its own appropriate behavior,
597 * including the definition of "failure".
598 * @note Base class version does nothing, returns zero.
603 // [27.5.2.4.3] get area
605 * @brief Investigating the data available.
606 * @return An estimate of the number of characters available in the
607 * input sequence, or -1.
609 * "If it returns a positive value, then successive calls to
610 * @c underflow() will not return @c traits::eof() until at least that
611 * number of characters have been supplied. If @c showmanyc()
612 * returns -1, then calls to @c underflow() or @c uflow() will fail."
615 * @note Base class version does nothing, returns zero.
616 * @note The standard adds that "the intention is not only that the
617 * calls [to underflow or uflow] will not return @c eof() but
618 * that they will return "immediately".
619 * @note The standard adds that "the morphemes of @c showmanyc are
620 * "es-how-many-see", not "show-manic".
623 showmanyc() { return 0; }
626 * @brief Multiple character extraction.
627 * @param s A buffer area.
628 * @param n Maximum number of characters to assign.
629 * @return The number of characters assigned.
631 * Fills @a s[0] through @a s[n-1] with characters from the input
632 * sequence, as if by @c sbumpc(). Stops when either @a n characters
633 * have been copied, or when @c traits::eof() would be copied.
635 * It is expected that derived classes provide a more efficient
636 * implementation by overriding this definition.
639 xsgetn(char_type
* __s
, streamsize __n
);
642 * @brief Fetches more data from the controlled sequence.
643 * @return The first character from the <em>pending sequence</em>.
645 * Informally, this function is called when the input buffer is
646 * exhausted (or does not exist, as buffering need not actually be
647 * done). If a buffer exists, it is "refilled". In either case, the
648 * next available character is returned, or @c traits::eof() to
649 * indicate a null pending sequence.
651 * For a formal definiton of the pending sequence, see a good text
652 * such as Langer & Kreft, or [27.5.2.4.3]/7-14.
654 * A functioning input streambuf can be created by overriding only
655 * this function (no buffer area will be used). For an example, see
656 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#6
658 * @note Base class version does nothing, returns eof().
662 { return traits_type::eof(); }
665 * @brief Fetches more data from the controlled sequence.
666 * @return The first character from the <em>pending sequence</em>.
668 * Informally, this function does the same thing as @c underflow(),
669 * and in fact is required to call that function. It also returns
670 * the new character, like @c underflow() does. However, this
671 * function also moves the read position forward by one.
676 int_type __ret
= traits_type::eof();
677 const bool __testeof
= traits_type::eq_int_type(this->underflow(),
681 __ret
= traits_type::to_int_type(*this->gptr());
687 // [27.5.2.4.4] putback
689 * @brief Tries to back up the input sequence.
690 * @param c The character to be inserted back into the sequence.
691 * @return eof() on failure, "some other value" on success
692 * @post The constraints of @c gptr(), @c eback(), and @c pptr()
693 * are the same as for @c underflow().
695 * @note Base class version does nothing, returns eof().
698 pbackfail(int_type
/* __c */ = traits_type::eof())
699 { return traits_type::eof(); }
703 * @brief Multiple character insertion.
704 * @param s A buffer area.
705 * @param n Maximum number of characters to write.
706 * @return The number of characters written.
708 * Writes @a s[0] through @a s[n-1] to the output sequence, as if
709 * by @c sputc(). Stops when either @a n characters have been
710 * copied, or when @c sputc() would return @c traits::eof().
712 * It is expected that derived classes provide a more efficient
713 * implementation by overriding this definition.
716 xsputn(const char_type
* __s
, streamsize __n
);
719 * @brief Consumes data from the buffer; writes to the
720 * controlled sequence.
721 * @param c An additional character to consume.
722 * @return eof() to indicate failure, something else (usually
723 * @a c, or not_eof())
725 * Informally, this function is called when the output buffer is full
726 * (or does not exist, as buffering need not actually be done). If a
727 * buffer exists, it is "consumed", with "some effect" on the
728 * controlled sequence. (Typically, the buffer is written out to the
729 * sequence verbatim.) In either case, the character @a c is also
730 * written out, if @a c is not @c eof().
732 * For a formal definiton of this function, see a good text
733 * such as Langer & Kreft, or [27.5.2.4.5]/3-7.
735 * A functioning output streambuf can be created by overriding only
736 * this function (no buffer area will be used).
738 * @note Base class version does nothing, returns eof().
741 overflow(int_type
/* __c */ = traits_type::eof())
742 { return traits_type::eof(); }
744 #ifdef _GLIBCXX_DEPRECATED
748 * @brief Tosses a character.
750 * Advances the read pointer, ignoring the character that would have
753 * See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
755 * @note This function has been deprecated by the standard. You
756 * must define @c _GLIBCXX_DEPRECATED to make this visible; see
762 if (this->gptr() < this->egptr())
770 // _GLIBCXX_RESOLVE_LIB_DEFECTS
771 // Side effect of DR 50.
772 basic_streambuf(const __streambuf_type
& __sb
)
773 : _M_in_beg(__sb
._M_in_beg
), _M_in_cur(__sb
._M_in_cur
),
774 _M_in_end(__sb
._M_in_end
), _M_out_beg(__sb
._M_out_beg
),
775 _M_out_cur(__sb
._M_out_cur
), _M_out_end(__sb
._M_out_cur
),
776 _M_buf_locale(__sb
._M_buf_locale
)
780 operator=(const __streambuf_type
&) { return *this; };
783 // Explicit specialization declarations, defined in src/streambuf.cc.
786 __copy_streambufs(basic_streambuf
<char>* __sbin
,
787 basic_streambuf
<char>* __sbout
);
788 #ifdef _GLIBCXX_USE_WCHAR_T
791 __copy_streambufs(basic_streambuf
<wchar_t>* __sbin
,
792 basic_streambuf
<wchar_t>* __sbout
);
796 #ifndef _GLIBCXX_EXPORT_TEMPLATE
797 # include <bits/streambuf.tcc>
800 #endif /* _GLIBCXX_STREAMBUF */