1 // Stream buffer classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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
)
58 template<typename _CharT
, typename _Traits
>
60 __copy_streambufs_eof(basic_streambuf
<_CharT
, _Traits
>*,
61 basic_streambuf
<_CharT
, _Traits
>*, bool&);
64 * @brief The actual work of input and output (interface).
66 * This is a base class. Derived stream buffers each control a
67 * pair of character sequences: one for input, and one for output.
69 * Section [27.5.1] of the standard describes the requirements and
70 * behavior of stream buffer classes. That section (three paragraphs)
71 * is reproduced here, for simplicity and accuracy.
73 * -# Stream buffers can impose various constraints on the sequences
74 * they control. Some constraints are:
75 * - The controlled input sequence can be not readable.
76 * - The controlled output sequence can be not writable.
77 * - The controlled sequences can be associated with the contents of
78 * other representations for character sequences, such as external
80 * - The controlled sequences can support operations @e directly to or
81 * from associated sequences.
82 * - The controlled sequences can impose limitations on how the
83 * program can read characters from a sequence, write characters to
84 * a sequence, put characters back into an input sequence, or alter
85 * the stream position.
87 * -# Each sequence is characterized by three pointers which, if non-null,
88 * all point into the same @c charT array object. The array object
89 * represents, at any moment, a (sub)sequence of characters from the
90 * sequence. Operations performed on a sequence alter the values
91 * stored in these pointers, perform reads and writes directly to or
92 * from associated sequences, and alter "the stream position" and
93 * conversion state as needed to maintain this subsequence relationship.
94 * The three pointers are:
95 * - the <em>beginning pointer</em>, or lowest element address in the
96 * array (called @e xbeg here);
97 * - the <em>next pointer</em>, or next element address that is a
98 * current candidate for reading or writing (called @e xnext here);
99 * - the <em>end pointer</em>, or first element address beyond the
100 * end of the array (called @e xend here).
102 * -# The following semantic constraints shall always apply for any set
103 * of three pointers for a sequence, using the pointer names given
105 * - If @e xnext is not a null pointer, then @e xbeg and @e xend shall
106 * also be non-null pointers into the same @c charT array, as
107 * described above; otherwise, @e xbeg and @e xend shall also be null.
108 * - If @e xnext is not a null pointer and @e xnext < @e xend for an
109 * output sequence, then a <em>write position</em> is available.
110 * In this case, @e *xnext shall be assignable as the next element
111 * to write (to put, or to store a character value, into the sequence).
112 * - If @e xnext is not a null pointer and @e xbeg < @e xnext for an
113 * input sequence, then a <em>putback position</em> is available.
114 * In this case, @e xnext[-1] shall have a defined value and is the
115 * next (preceding) element to store a character that is put back
116 * into the input sequence.
117 * - If @e xnext is not a null pointer and @e xnext< @e xend for an
118 * input sequence, then a <em>read position</em> is available.
119 * In this case, @e *xnext shall have a defined value and is the
120 * next element to read (to get, or to obtain a character value,
121 * from the sequence).
123 template<typename _CharT
, typename _Traits
>
124 class basic_streambuf
129 * These are standard types. They permit a standardized way of
130 * referring to names of (or names dependant on) the template
131 * parameters, which are specific to the implementation.
133 typedef _CharT char_type
;
134 typedef _Traits traits_type
;
135 typedef typename
traits_type::int_type int_type
;
136 typedef typename
traits_type::pos_type pos_type
;
137 typedef typename
traits_type::off_type off_type
;
143 * This is a non-standard type.
146 typedef basic_streambuf
<char_type
, traits_type
> __streambuf_type
;
149 friend class basic_ios
<char_type
, traits_type
>;
150 friend class basic_istream
<char_type
, traits_type
>;
151 friend class basic_ostream
<char_type
, traits_type
>;
152 friend class istreambuf_iterator
<char_type
, traits_type
>;
153 friend class ostreambuf_iterator
<char_type
, traits_type
>;
156 __copy_streambufs_eof
<>(__streambuf_type
*, __streambuf_type
*, bool&);
158 template<typename _CharT2
>
159 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
161 __copy_aux(istreambuf_iterator
<_CharT2
>,
162 istreambuf_iterator
<_CharT2
>, _CharT2
*);
164 template<typename _CharT2
>
165 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
166 istreambuf_iterator
<_CharT2
> >::__type
167 find(istreambuf_iterator
<_CharT2
>, istreambuf_iterator
<_CharT2
>,
170 template<typename _CharT2
, typename _Traits2
>
171 friend basic_istream
<_CharT2
, _Traits2
>&
172 operator>>(basic_istream
<_CharT2
, _Traits2
>&, _CharT2
*);
174 template<typename _CharT2
, typename _Traits2
, typename _Alloc
>
175 friend basic_istream
<_CharT2
, _Traits2
>&
176 operator>>(basic_istream
<_CharT2
, _Traits2
>&,
177 basic_string
<_CharT2
, _Traits2
, _Alloc
>&);
179 template<typename _CharT2
, typename _Traits2
, typename _Alloc
>
180 friend basic_istream
<_CharT2
, _Traits2
>&
181 getline(basic_istream
<_CharT2
, _Traits2
>&,
182 basic_string
<_CharT2
, _Traits2
, _Alloc
>&, _CharT2
);
188 * This is based on _IO_FILE, just reordered to be more consistent,
189 * and is intended to be the most minimal abstraction for an
191 * - get == input == read
192 * - put == output == write
195 char_type
* _M_in_beg
; // Start of get area.
196 char_type
* _M_in_cur
; // Current read area.
197 char_type
* _M_in_end
; // End of get area.
198 char_type
* _M_out_beg
; // Start of put area.
199 char_type
* _M_out_cur
; // Current put area.
200 char_type
* _M_out_end
; // End of put area.
204 * Current locale setting.
207 locale _M_buf_locale
;
210 /// Destructor deallocates no buffer space.
215 // [27.5.2.2.1] locales
217 * @brief Entry point for imbue().
218 * @param loc The new locale.
219 * @return The previous locale.
221 * Calls the derived imbue(loc).
224 pubimbue(const locale
&__loc
)
226 locale
__tmp(this->getloc());
228 _M_buf_locale
= __loc
;
233 * @brief Locale access.
234 * @return The current locale in effect.
236 * If pubimbue(loc) has been called, then the most recent @c loc
237 * is returned. Otherwise the global locale in effect at the time
238 * of construction is returned.
242 { return _M_buf_locale
; }
244 // [27.5.2.2.2] buffer management and positioning
247 * @brief Entry points for derived buffer functions.
249 * The public versions of @c pubfoo dispatch to the protected
250 * derived @c foo member functions, passing the arguments (if any)
251 * and returning the result unchanged.
254 pubsetbuf(char_type
* __s
, streamsize __n
)
255 { return this->setbuf(__s
, __n
); }
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
); }
263 pubseekpos(pos_type __sp
,
264 ios_base::openmode __mode
= ios_base::in
| ios_base::out
)
265 { return this->seekpos(__sp
, __mode
); }
268 pubsync() { return this->sync(); }
271 // [27.5.2.2.3] get area
273 * @brief Looking ahead into the stream.
274 * @return The number of characters available.
276 * If a read position is available, returns the number of characters
277 * available for reading before the buffer must be refilled.
278 * Otherwise returns the derived @c showmanyc().
283 const streamsize __ret
= this->egptr() - this->gptr();
284 return __ret
? __ret
: this->showmanyc();
288 * @brief Getting the next character.
289 * @return The next character, or eof.
291 * Calls @c sbumpc(), and if that function returns
292 * @c traits::eof(), so does this function. Otherwise, @c sgetc().
297 int_type __ret
= traits_type::eof();
298 if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(),
300 __ret
= this->sgetc();
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 * and increments the read pointer, otherwise calls and returns
316 if (__builtin_expect(this->gptr() < this->egptr(), true))
318 __ret
= traits_type::to_int_type(*this->gptr());
322 __ret
= this->uflow();
327 * @brief Getting the next character.
328 * @return The next character, or eof.
330 * If the input read position is available, returns that character,
331 * otherwise calls and returns @c underflow(). Does not move the
332 * read position after fetching the character.
338 if (__builtin_expect(this->gptr() < this->egptr(), true))
339 __ret
= traits_type::to_int_type(*this->gptr());
341 __ret
= this->underflow();
346 * @brief Entry point for xsgetn.
347 * @param s A buffer area.
350 * Returns xsgetn(s,n). The effect is to fill @a s[0] through
351 * @a s[n-1] with characters from the input sequence, if possible.
354 sgetn(char_type
* __s
, streamsize __n
)
355 { return this->xsgetn(__s
, __n
); }
357 // [27.5.2.2.4] putback
359 * @brief Pushing characters back into the input stream.
360 * @param c The character to push back.
361 * @return The previous character, if possible.
363 * Similar to sungetc(), but @a c is pushed onto the stream instead
364 * of "the previous character". If successful, the next character
365 * fetched from the input stream will be @a c.
368 sputbackc(char_type __c
)
371 const bool __testpos
= this->eback() < this->gptr();
372 if (__builtin_expect(!__testpos
||
373 !traits_type::eq(__c
, this->gptr()[-1]), false))
374 __ret
= this->pbackfail(traits_type::to_int_type(__c
));
378 __ret
= traits_type::to_int_type(*this->gptr());
384 * @brief Moving backwards in the input stream.
385 * @return The previous character, if possible.
387 * If a putback position is available, this function decrements the
388 * input pointer and returns that character. Otherwise, calls and
389 * returns pbackfail(). The effect is to "unget" the last character
396 if (__builtin_expect(this->eback() < this->gptr(), true))
399 __ret
= traits_type::to_int_type(*this->gptr());
402 __ret
= this->pbackfail();
406 // [27.5.2.2.5] put area
408 * @brief Entry point for all single-character output functions.
409 * @param c A character to output.
410 * @return @a c, if possible.
412 * One of two public output functions.
414 * If a write position is available for the output sequence (i.e.,
415 * the buffer is not full), stores @a c in that position, increments
416 * the position, and returns @c traits::to_int_type(c). If a write
417 * position is not available, returns @c overflow(c).
423 if (__builtin_expect(this->pptr() < this->epptr(), true))
427 __ret
= traits_type::to_int_type(__c
);
430 __ret
= this->overflow(traits_type::to_int_type(__c
));
435 * @brief Entry point for all single-character output functions.
436 * @param s A buffer read area.
439 * One of two public output functions.
442 * Returns xsputn(s,n). The effect is to write @a s[0] through
443 * @a s[n-1] to the output sequence, if possible.
446 sputn(const char_type
* __s
, streamsize __n
)
447 { return this->xsputn(__s
, __n
); }
451 * @brief Base constructor.
453 * Only called from derived constructors, and sets up all the
454 * buffer data to zero, including the pointers described in the
455 * basic_streambuf class description. Note that, as a result,
456 * - the class starts with no read nor write positions available,
457 * - this is not an error
460 : _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
461 _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
462 _M_buf_locale(locale())
465 // [27.5.2.3.1] get area access
468 * @brief Access to the get area.
470 * These functions are only available to other protected functions,
471 * including derived classes.
473 * - eback() returns the beginning pointer for the input sequence
474 * - gptr() returns the next pointer for the input sequence
475 * - egptr() returns the end pointer for the input sequence
478 eback() const { return _M_in_beg
; }
481 gptr() const { return _M_in_cur
; }
484 egptr() const { return _M_in_end
; }
488 * @brief Moving the read position.
489 * @param n The delta by which to move.
491 * This just advances the read position without returning any data.
494 gbump(int __n
) { _M_in_cur
+= __n
; }
497 * @brief Setting the three read area pointers.
498 * @param gbeg A pointer.
499 * @param gnext A pointer.
500 * @param gend A pointer.
501 * @post @a gbeg == @c eback(), @a gnext == @c gptr(), and
502 * @a gend == @c egptr()
505 setg(char_type
* __gbeg
, char_type
* __gnext
, char_type
* __gend
)
512 // [27.5.2.3.2] put area access
515 * @brief Access to the put area.
517 * These functions are only available to other protected functions,
518 * including derived classes.
520 * - pbase() returns the beginning pointer for the output sequence
521 * - pptr() returns the next pointer for the output sequence
522 * - epptr() returns the end pointer for the output sequence
525 pbase() const { return _M_out_beg
; }
528 pptr() const { return _M_out_cur
; }
531 epptr() const { return _M_out_end
; }
535 * @brief Moving the write position.
536 * @param n The delta by which to move.
538 * This just advances the write position without returning any data.
541 pbump(int __n
) { _M_out_cur
+= __n
; }
544 * @brief Setting the three write area pointers.
545 * @param pbeg A pointer.
546 * @param pend A pointer.
547 * @post @a pbeg == @c pbase(), @a pbeg == @c pptr(), and
548 * @a pend == @c epptr()
551 setp(char_type
* __pbeg
, char_type
* __pend
)
553 _M_out_beg
= _M_out_cur
= __pbeg
;
557 // [27.5.2.4] virtual functions
558 // [27.5.2.4.1] locales
560 * @brief Changes translations.
561 * @param loc A new locale.
563 * Translations done during I/O which depend on the current locale
564 * are changed by this call. The standard adds, "Between invocations
565 * of this function a class derived from streambuf can safely cache
566 * results of calls to locale functions and to members of facets
569 * @note Base class version does nothing.
575 // [27.5.2.4.2] buffer management and positioning
577 * @brief Maniuplates the buffer.
579 * Each derived class provides its own appropriate behavior. See
580 * the next-to-last paragraph of
581 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for
582 * more on this function.
584 * @note Base class version does nothing, returns @c this.
586 virtual basic_streambuf
<char_type
,_Traits
>*
587 setbuf(char_type
*, streamsize
)
591 * @brief Alters the stream positions.
593 * Each derived class provides its own appropriate behavior.
594 * @note Base class version does nothing, returns a @c pos_type
595 * that represents an invalid stream position.
598 seekoff(off_type
, ios_base::seekdir
,
599 ios_base::openmode
/*__mode*/ = ios_base::in
| ios_base::out
)
600 { return pos_type(off_type(-1)); }
603 * @brief Alters the stream positions.
605 * Each derived class provides its own appropriate behavior.
606 * @note Base class version does nothing, returns a @c pos_type
607 * that represents an invalid stream position.
611 ios_base::openmode
/*__mode*/ = ios_base::in
| ios_base::out
)
612 { return pos_type(off_type(-1)); }
615 * @brief Synchronizes the buffer arrays with the controlled sequences.
616 * @return -1 on failure.
618 * Each derived class provides its own appropriate behavior,
619 * including the definition of "failure".
620 * @note Base class version does nothing, returns zero.
625 // [27.5.2.4.3] get area
627 * @brief Investigating the data available.
628 * @return An estimate of the number of characters available in the
629 * input sequence, or -1.
631 * "If it returns a positive value, then successive calls to
632 * @c underflow() will not return @c traits::eof() until at least that
633 * number of characters have been supplied. If @c showmanyc()
634 * returns -1, then calls to @c underflow() or @c uflow() will fail."
637 * @note Base class version does nothing, returns zero.
638 * @note The standard adds that "the intention is not only that the
639 * calls [to underflow or uflow] will not return @c eof() but
640 * that they will return "immediately".
641 * @note The standard adds that "the morphemes of @c showmanyc are
642 * "es-how-many-see", not "show-manic".
645 showmanyc() { return 0; }
648 * @brief Multiple character extraction.
649 * @param s A buffer area.
650 * @param n Maximum number of characters to assign.
651 * @return The number of characters assigned.
653 * Fills @a s[0] through @a s[n-1] with characters from the input
654 * sequence, as if by @c sbumpc(). Stops when either @a n characters
655 * have been copied, or when @c traits::eof() would be copied.
657 * It is expected that derived classes provide a more efficient
658 * implementation by overriding this definition.
661 xsgetn(char_type
* __s
, streamsize __n
);
664 * @brief Fetches more data from the controlled sequence.
665 * @return The first character from the <em>pending sequence</em>.
667 * Informally, this function is called when the input buffer is
668 * exhausted (or does not exist, as buffering need not actually be
669 * done). If a buffer exists, it is "refilled". In either case, the
670 * next available character is returned, or @c traits::eof() to
671 * indicate a null pending sequence.
673 * For a formal definiton of the pending sequence, see a good text
674 * such as Langer & Kreft, or [27.5.2.4.3]/7-14.
676 * A functioning input streambuf can be created by overriding only
677 * this function (no buffer area will be used). For an example, see
678 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#6
680 * @note Base class version does nothing, returns eof().
684 { return traits_type::eof(); }
687 * @brief Fetches more data from the controlled sequence.
688 * @return The first character from the <em>pending sequence</em>.
690 * Informally, this function does the same thing as @c underflow(),
691 * and in fact is required to call that function. It also returns
692 * the new character, like @c underflow() does. However, this
693 * function also moves the read position forward by one.
698 int_type __ret
= traits_type::eof();
699 const bool __testeof
= traits_type::eq_int_type(this->underflow(),
703 __ret
= traits_type::to_int_type(*this->gptr());
709 // [27.5.2.4.4] putback
711 * @brief Tries to back up the input sequence.
712 * @param c The character to be inserted back into the sequence.
713 * @return eof() on failure, "some other value" on success
714 * @post The constraints of @c gptr(), @c eback(), and @c pptr()
715 * are the same as for @c underflow().
717 * @note Base class version does nothing, returns eof().
720 pbackfail(int_type
/* __c */ = traits_type::eof())
721 { return traits_type::eof(); }
725 * @brief Multiple character insertion.
726 * @param s A buffer area.
727 * @param n Maximum number of characters to write.
728 * @return The number of characters written.
730 * Writes @a s[0] through @a s[n-1] to the output sequence, as if
731 * by @c sputc(). Stops when either @a n characters have been
732 * copied, or when @c sputc() would return @c traits::eof().
734 * It is expected that derived classes provide a more efficient
735 * implementation by overriding this definition.
738 xsputn(const char_type
* __s
, streamsize __n
);
741 * @brief Consumes data from the buffer; writes to the
742 * controlled sequence.
743 * @param c An additional character to consume.
744 * @return eof() to indicate failure, something else (usually
745 * @a c, or not_eof())
747 * Informally, this function is called when the output buffer is full
748 * (or does not exist, as buffering need not actually be done). If a
749 * buffer exists, it is "consumed", with "some effect" on the
750 * controlled sequence. (Typically, the buffer is written out to the
751 * sequence verbatim.) In either case, the character @a c is also
752 * written out, if @a c is not @c eof().
754 * For a formal definiton of this function, see a good text
755 * such as Langer & Kreft, or [27.5.2.4.5]/3-7.
757 * A functioning output streambuf can be created by overriding only
758 * this function (no buffer area will be used).
760 * @note Base class version does nothing, returns eof().
763 overflow(int_type
/* __c */ = traits_type::eof())
764 { return traits_type::eof(); }
766 #ifdef _GLIBCXX_DEPRECATED
770 * @brief Tosses a character.
772 * Advances the read pointer, ignoring the character that would have
775 * See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
777 * @note This function has been deprecated by the standard. You
778 * must define @c _GLIBCXX_DEPRECATED to make this visible; see
784 if (this->gptr() < this->egptr())
792 // _GLIBCXX_RESOLVE_LIB_DEFECTS
793 // Side effect of DR 50.
794 basic_streambuf(const __streambuf_type
& __sb
)
795 : _M_in_beg(__sb
._M_in_beg
), _M_in_cur(__sb
._M_in_cur
),
796 _M_in_end(__sb
._M_in_end
), _M_out_beg(__sb
._M_out_beg
),
797 _M_out_cur(__sb
._M_out_cur
), _M_out_end(__sb
._M_out_cur
),
798 _M_buf_locale(__sb
._M_buf_locale
)
802 operator=(const __streambuf_type
&) { return *this; };
805 // Explicit specialization declarations, defined in src/streambuf.cc.
808 __copy_streambufs_eof(basic_streambuf
<char>* __sbin
,
809 basic_streambuf
<char>* __sbout
, bool& __ineof
);
810 #ifdef _GLIBCXX_USE_WCHAR_T
813 __copy_streambufs_eof(basic_streambuf
<wchar_t>* __sbin
,
814 basic_streambuf
<wchar_t>* __sbout
, bool& __ineof
);
817 _GLIBCXX_END_NAMESPACE
819 #ifndef _GLIBCXX_EXPORT_TEMPLATE
820 # include <bits/streambuf.tcc>
823 #endif /* _GLIBCXX_STREAMBUF */