fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / libstdc++-v3 / include / std / std_streambuf.h
blob42b3d782b0f17cf261e772e1abca84944e28b953
1 // Stream buffer classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // 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 // ISO C++ 14882: 27.5 Stream buffers
35 /** @file streambuf
36 * This is a Standard C++ Library header. You should @c #include this header
37 * in your programs, rather than any of the "st[dl]_*.h" implementation files.
40 #ifndef _CLIBXX_STREAMBUF
41 #define _CLIBXX_STREAMBUF 1
43 #pragma GCC system_header
45 #include <bits/c++config.h>
46 #include <iosfwd>
47 #include <bits/localefwd.h>
48 #include <bits/ios_base.h>
50 namespace std
52 /**
53 * @if maint
54 * Does stuff.
55 * @endif
57 template<typename _CharT, typename _Traits>
58 streamsize
59 __copy_streambufs(basic_streambuf<_CharT, _Traits>* __sbin,
60 basic_streambuf<_CharT, _Traits>* __sbout);
62 /**
63 * @brief The actual work of input and output (interface).
65 * This is a base class. Derived stream buffers each control a
66 * pair of character sequences: one for input, and one for output.
68 * Section [27.5.1] of the standard describes the requirements and
69 * behavior of stream buffer classes. That section (three paragraphs)
70 * is reproduced here, for simplicity and accuracy.
72 * -# Stream buffers can impose various constraints on the sequences
73 * they control. Some constraints are:
74 * - The controlled input sequence can be not readable.
75 * - The controlled output sequence can be not writable.
76 * - The controlled sequences can be associated with the contents of
77 * other representations for character sequences, such as external
78 * files.
79 * - The controlled sequences can support operations @e directly to or
80 * from associated sequences.
81 * - The controlled sequences can impose limitations on how the
82 * program can read characters from a sequence, write characters to
83 * a sequence, put characters back into an input sequence, or alter
84 * the stream position.
85 * .
86 * -# Each sequence is characterized by three pointers which, if non-null,
87 * all point into the same @c charT array object. The array object
88 * represents, at any moment, a (sub)sequence of characters from the
89 * sequence. Operations performed on a sequence alter the values
90 * stored in these pointers, perform reads and writes directly to or
91 * from associated sequences, and alter "the stream position" and
92 * conversion state as needed to maintain this subsequence relationship.
93 * The three pointers are:
94 * - the <em>beginning pointer</em>, or lowest element address in the
95 * array (called @e xbeg here);
96 * - the <em>next pointer</em>, or next element address that is a
97 * current candidate for reading or writing (called @e xnext here);
98 * - the <em>end pointer</em>, or first element address beyond the
99 * end of the array (called @e xend here).
101 * -# The following semantic constraints shall always apply for any set
102 * of three pointers for a sequence, using the pointer names given
103 * immediately above:
104 * - If @e xnext is not a null pointer, then @e xbeg and @e xend shall
105 * also be non-null pointers into the same @c charT array, as
106 * described above; otherwise, @e xbeg and @e xend shall also be null.
107 * - If @e xnext is not a null pointer and @e xnext < @e xend for an
108 * output sequence, then a <em>write position</em> is available.
109 * In this case, @e *xnext shall be assignable as the next element
110 * to write (to put, or to store a character value, into the sequence).
111 * - If @e xnext is not a null pointer and @e xbeg < @e xnext for an
112 * input sequence, then a <em>putback position</em> is available.
113 * In this case, @e xnext[-1] shall have a defined value and is the
114 * next (preceding) element to store a character that is put back
115 * into the input sequence.
116 * - If @e xnext is not a null pointer and @e xnext< @e xend for an
117 * input sequence, then a <em>read position</em> is available.
118 * In this case, @e *xnext shall have a defined value and is the
119 * next element to read (to get, or to obtain a character value,
120 * from the sequence).
122 template<typename _CharT, typename _Traits>
123 class basic_streambuf
125 public:
126 //@{
128 * These are standard types. They permit a standardized way of
129 * referring to names of (or names dependant on) the template
130 * parameters, which are specific to the implementation.
132 typedef _CharT char_type;
133 typedef _Traits traits_type;
134 typedef typename traits_type::int_type int_type;
135 typedef typename traits_type::pos_type pos_type;
136 typedef typename traits_type::off_type off_type;
137 //@}
139 //@{
141 * @if maint
142 * This is a non-standard type.
143 * @endif
145 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
146 //@}
148 friend class basic_ios<char_type, traits_type>;
149 friend class basic_istream<char_type, traits_type>;
150 friend class basic_ostream<char_type, traits_type>;
151 friend class istreambuf_iterator<char_type, traits_type>;
152 friend class ostreambuf_iterator<char_type, traits_type>;
154 friend streamsize
155 __copy_streambufs<>(__streambuf_type* __sbin,
156 __streambuf_type* __sbout);
158 protected:
159 //@{
161 * @if maint
162 * This is based on _IO_FILE, just reordered to be more consistent,
163 * and is intended to be the most minimal abstraction for an
164 * internal buffer.
165 * - get == input == read
166 * - put == output == write
167 * @endif
169 char_type* _M_in_beg; // Start of get area.
170 char_type* _M_in_cur; // Current read area.
171 char_type* _M_in_end; // End of get area.
172 char_type* _M_out_beg; // Start of put area.
173 char_type* _M_out_cur; // Current put area.
174 char_type* _M_out_end; // End of put area.
177 * @if maint
178 * Current locale setting.
179 * @endif
181 locale _M_buf_locale;
183 public:
184 /// Destructor deallocates no buffer space.
185 virtual
186 ~basic_streambuf()
189 // [27.5.2.2.1] locales
191 * @brief Entry point for imbue().
192 * @param loc The new locale.
193 * @return The previous locale.
195 * Calls the derived imbue(loc).
197 locale
198 pubimbue(const locale &__loc)
200 locale __tmp(this->getloc());
201 this->imbue(__loc);
202 _M_buf_locale = __loc;
203 return __tmp;
207 * @brief Locale access.
208 * @return The current locale in effect.
210 * If pubimbue(loc) has been called, then the most recent @c loc
211 * is returned. Otherwise the global locale in effect at the time
212 * of construction is returned.
214 locale
215 getloc() const
216 { return _M_buf_locale; }
218 // [27.5.2.2.2] buffer management and positioning
219 //@{
221 * @brief Entry points for derived buffer functions.
223 * The public versions of @c pubfoo dispatch to the protected
224 * derived @c foo member functions, passing the arguments (if any)
225 * and returning the result unchanged.
227 __streambuf_type*
228 pubsetbuf(char_type* __s, streamsize __n)
229 { return this->setbuf(__s, __n); }
231 pos_type
232 pubseekoff(off_type __off, ios_base::seekdir __way,
233 ios_base::openmode __mode = ios_base::in | ios_base::out)
234 { return this->seekoff(__off, __way, __mode); }
236 pos_type
237 pubseekpos(pos_type __sp,
238 ios_base::openmode __mode = ios_base::in | ios_base::out)
239 { return this->seekpos(__sp, __mode); }
241 int
242 pubsync() { return this->sync(); }
243 //@}
245 // [27.5.2.2.3] get area
247 * @brief Looking ahead into the stream.
248 * @return The number of characters available.
250 * If a read position is available, returns the number of characters
251 * available for reading before the buffer must be refilled.
252 * Otherwise returns the derived @c showmanyc().
254 streamsize
255 in_avail()
257 const streamsize __ret = this->egptr() - this->gptr();
258 return __ret ? __ret : this->showmanyc();
262 * @brief Getting the next character.
263 * @return The next character, or eof.
265 * Calls @c sbumpc(), and if that function returns
266 * @c traits::eof(), so does this function. Otherwise, @c sgetc().
268 int_type
269 snextc()
271 int_type __ret = traits_type::eof();
272 if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(),
273 __ret), true))
274 __ret = this->sgetc();
275 return __ret;
279 * @brief Getting the next character.
280 * @return The next character, or eof.
282 * If the input read position is available, returns that character
283 * and increments the read pointer, otherwise calls and returns
284 * @c uflow().
286 int_type
287 sbumpc()
289 int_type __ret;
290 if (__builtin_expect(this->gptr() < this->egptr(), true))
292 __ret = traits_type::to_int_type(*this->gptr());
293 this->gbump(1);
295 else
296 __ret = this->uflow();
297 return __ret;
301 * @brief Getting the next character.
302 * @return The next character, or eof.
304 * If the input read position is available, returns that character,
305 * otherwise calls and returns @c underflow(). Does not move the
306 * read position after fetching the character.
308 int_type
309 sgetc()
311 int_type __ret;
312 if (__builtin_expect(this->gptr() < this->egptr(), true))
313 __ret = traits_type::to_int_type(*this->gptr());
314 else
315 __ret = this->underflow();
316 return __ret;
320 * @brief Entry point for xsgetn.
321 * @param s A buffer area.
322 * @param n A count.
324 * Returns xsgetn(s,n). The effect is to fill @a s[0] through
325 * @a s[n-1] with characters from the input sequence, if possible.
327 streamsize
328 sgetn(char_type* __s, streamsize __n)
329 { return this->xsgetn(__s, __n); }
331 // [27.5.2.2.4] putback
333 * @brief Pushing characters back into the input stream.
334 * @param c The character to push back.
335 * @return The previous character, if possible.
337 * Similar to sungetc(), but @a c is pushed onto the stream instead
338 * of "the previous character". If successful, the next character
339 * fetched from the input stream will be @a c.
341 int_type
342 sputbackc(char_type __c)
344 int_type __ret;
345 const bool __testpos = this->eback() < this->gptr();
346 if (__builtin_expect(!__testpos ||
347 !traits_type::eq(__c, this->gptr()[-1]), false))
348 __ret = this->pbackfail(traits_type::to_int_type(__c));
349 else
351 this->gbump(-1);
352 __ret = traits_type::to_int_type(*this->gptr());
354 return __ret;
358 * @brief Moving backwards in the input stream.
359 * @return The previous character, if possible.
361 * If a putback position is available, this function decrements the
362 * input pointer and returns that character. Otherwise, calls and
363 * returns pbackfail(). The effect is to "unget" the last character
364 * "gotten".
366 int_type
367 sungetc()
369 int_type __ret;
370 if (__builtin_expect(this->eback() < this->gptr(), true))
372 this->gbump(-1);
373 __ret = traits_type::to_int_type(*this->gptr());
375 else
376 __ret = this->pbackfail();
377 return __ret;
380 // [27.5.2.2.5] put area
382 * @brief Entry point for all single-character output functions.
383 * @param c A character to output.
384 * @return @a c, if possible.
386 * One of two public output functions.
388 * If a write position is available for the output sequence (i.e.,
389 * the buffer is not full), stores @a c in that position, increments
390 * the position, and returns @c traits::to_int_type(c). If a write
391 * position is not available, returns @c overflow(c).
393 int_type
394 sputc(char_type __c)
396 int_type __ret;
397 if (__builtin_expect(this->pptr() < this->epptr(), true))
399 *this->pptr() = __c;
400 this->pbump(1);
401 __ret = traits_type::to_int_type(__c);
403 else
404 __ret = this->overflow(traits_type::to_int_type(__c));
405 return __ret;
409 * @brief Entry point for all single-character output functions.
410 * @param s A buffer read area.
411 * @param n A count.
413 * One of two public output functions.
416 * Returns xsputn(s,n). The effect is to write @a s[0] through
417 * @a s[n-1] to the output sequence, if possible.
419 streamsize
420 sputn(const char_type* __s, streamsize __n)
421 { return this->xsputn(__s, __n); }
423 protected:
425 * @brief Base constructor.
427 * Only called from derived constructors, and sets up all the
428 * buffer data to zero, including the pointers described in the
429 * basic_streambuf class description. Note that, as a result,
430 * - the class starts with no read nor write positions available,
431 * - this is not an error
433 basic_streambuf()
434 : _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
435 _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
436 _M_buf_locale(locale())
439 // [27.5.2.3.1] get area access
440 //@{
442 * @brief Access to the get area.
444 * These functions are only available to other protected functions,
445 * including derived classes.
447 * - eback() returns the beginning pointer for the input sequence
448 * - gptr() returns the next pointer for the input sequence
449 * - egptr() returns the end pointer for the input sequence
451 char_type*
452 eback() const { return _M_in_beg; }
454 char_type*
455 gptr() const { return _M_in_cur; }
457 char_type*
458 egptr() const { return _M_in_end; }
459 //@}
462 * @brief Moving the read position.
463 * @param n The delta by which to move.
465 * This just advances the read position without returning any data.
467 void
468 gbump(int __n) { _M_in_cur += __n; }
471 * @brief Setting the three read area pointers.
472 * @param gbeg A pointer.
473 * @param gnext A pointer.
474 * @param gend A pointer.
475 * @post @a gbeg == @c eback(), @a gnext == @c gptr(), and
476 * @a gend == @c egptr()
478 void
479 setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
481 _M_in_beg = __gbeg;
482 _M_in_cur = __gnext;
483 _M_in_end = __gend;
486 // [27.5.2.3.2] put area access
487 //@{
489 * @brief Access to the put area.
491 * These functions are only available to other protected functions,
492 * including derived classes.
494 * - pbase() returns the beginning pointer for the output sequence
495 * - pptr() returns the next pointer for the output sequence
496 * - epptr() returns the end pointer for the output sequence
498 char_type*
499 pbase() const { return _M_out_beg; }
501 char_type*
502 pptr() const { return _M_out_cur; }
504 char_type*
505 epptr() const { return _M_out_end; }
506 //@}
509 * @brief Moving the write position.
510 * @param n The delta by which to move.
512 * This just advances the write position without returning any data.
514 void
515 pbump(int __n) { _M_out_cur += __n; }
518 * @brief Setting the three write area pointers.
519 * @param pbeg A pointer.
520 * @param pend A pointer.
521 * @post @a pbeg == @c pbase(), @a pbeg == @c pptr(), and
522 * @a pend == @c epptr()
524 void
525 setp(char_type* __pbeg, char_type* __pend)
527 _M_out_beg = _M_out_cur = __pbeg;
528 _M_out_end = __pend;
531 // [27.5.2.4] virtual functions
532 // [27.5.2.4.1] locales
534 * @brief Changes translations.
535 * @param loc A new locale.
537 * Translations done during I/O which depend on the current locale
538 * are changed by this call. The standard adds, "Between invocations
539 * of this function a class derived from streambuf can safely cache
540 * results of calls to locale functions and to members of facets
541 * so obtained."
543 * @note Base class version does nothing.
545 virtual void
546 imbue(const locale&)
549 // [27.5.2.4.2] buffer management and positioning
551 * @brief Maniuplates the buffer.
553 * Each derived class provides its own appropriate behavior. See
554 * the next-to-last paragraph of
555 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for
556 * more on this function.
558 * @note Base class version does nothing, returns @c this.
560 virtual basic_streambuf<char_type,_Traits>*
561 setbuf(char_type*, streamsize)
562 { return this; }
565 * @brief Alters the stream positions.
567 * Each derived class provides its own appropriate behavior.
568 * @note Base class version does nothing, returns a @c pos_type
569 * that represents an invalid stream position.
571 virtual pos_type
572 seekoff(off_type, ios_base::seekdir,
573 ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
574 { return pos_type(off_type(-1)); }
577 * @brief Alters the stream positions.
579 * Each derived class provides its own appropriate behavior.
580 * @note Base class version does nothing, returns a @c pos_type
581 * that represents an invalid stream position.
583 virtual pos_type
584 seekpos(pos_type,
585 ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
586 { return pos_type(off_type(-1)); }
589 * @brief Synchronizes the buffer arrays with the controlled sequences.
590 * @return -1 on failure.
592 * Each derived class provides its own appropriate behavior,
593 * including the definition of "failure".
594 * @note Base class version does nothing, returns zero.
596 virtual int
597 sync() { return 0; }
599 // [27.5.2.4.3] get area
601 * @brief Investigating the data available.
602 * @return An estimate of the number of characters available in the
603 * input sequence, or -1.
605 * "If it returns a positive value, then successive calls to
606 * @c underflow() will not return @c traits::eof() until at least that
607 * number of characters have been supplied. If @c showmanyc()
608 * returns -1, then calls to @c underflow() or @c uflow() will fail."
609 * [27.5.2.4.3]/1
611 * @note Base class version does nothing, returns zero.
612 * @note The standard adds that "the intention is not only that the
613 * calls [to underflow or uflow] will not return @c eof() but
614 * that they will return "immediately".
615 * @note The standard adds that "the morphemes of @c showmanyc are
616 * "es-how-many-see", not "show-manic".
618 virtual streamsize
619 showmanyc() { return 0; }
622 * @brief Multiple character extraction.
623 * @param s A buffer area.
624 * @param n Maximum number of characters to assign.
625 * @return The number of characters assigned.
627 * Fills @a s[0] through @a s[n-1] with characters from the input
628 * sequence, as if by @c sbumpc(). Stops when either @a n characters
629 * have been copied, or when @c traits::eof() would be copied.
631 * It is expected that derived classes provide a more efficient
632 * implementation by overriding this definition.
634 virtual streamsize
635 xsgetn(char_type* __s, streamsize __n);
638 * @brief Fetches more data from the controlled sequence.
639 * @return The first character from the <em>pending sequence</em>.
641 * Informally, this function is called when the input buffer is
642 * exhausted (or does not exist, as buffering need not actually be
643 * done). If a buffer exists, it is "refilled". In either case, the
644 * next available character is returned, or @c traits::eof() to
645 * indicate a null pending sequence.
647 * For a formal definiton of the pending sequence, see a good text
648 * such as Langer & Kreft, or [27.5.2.4.3]/7-14.
650 * A functioning input streambuf can be created by overriding only
651 * this function (no buffer area will be used). For an example, see
652 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#6
654 * @note Base class version does nothing, returns eof().
656 virtual int_type
657 underflow()
658 { return traits_type::eof(); }
661 * @brief Fetches more data from the controlled sequence.
662 * @return The first character from the <em>pending sequence</em>.
664 * Informally, this function does the same thing as @c underflow(),
665 * and in fact is required to call that function. It also returns
666 * the new character, like @c underflow() does. However, this
667 * function also moves the read position forward by one.
669 virtual int_type
670 uflow()
672 int_type __ret = traits_type::eof();
673 const bool __testeof = traits_type::eq_int_type(this->underflow(),
674 __ret);
675 if (!__testeof)
677 __ret = traits_type::to_int_type(*this->gptr());
678 this->gbump(1);
680 return __ret;
683 // [27.5.2.4.4] putback
685 * @brief Tries to back up the input sequence.
686 * @param c The character to be inserted back into the sequence.
687 * @return eof() on failure, "some other value" on success
688 * @post The constraints of @c gptr(), @c eback(), and @c pptr()
689 * are the same as for @c underflow().
691 * @note Base class version does nothing, returns eof().
693 virtual int_type
694 pbackfail(int_type /* __c */ = traits_type::eof())
695 { return traits_type::eof(); }
697 // Put area:
699 * @brief Multiple character insertion.
700 * @param s A buffer area.
701 * @param n Maximum number of characters to write.
702 * @return The number of characters written.
704 * Writes @a s[0] through @a s[n-1] to the output sequence, as if
705 * by @c sputc(). Stops when either @a n characters have been
706 * copied, or when @c sputc() would return @c traits::eof().
708 * It is expected that derived classes provide a more efficient
709 * implementation by overriding this definition.
711 virtual streamsize
712 xsputn(const char_type* __s, streamsize __n);
715 * @brief Consumes data from the buffer; writes to the
716 * controlled sequence.
717 * @param c An additional character to consume.
718 * @return eof() to indicate failure, something else (usually
719 * @a c, or not_eof())
721 * Informally, this function is called when the output buffer is full
722 * (or does not exist, as buffering need not actually be done). If a
723 * buffer exists, it is "consumed", with "some effect" on the
724 * controlled sequence. (Typically, the buffer is written out to the
725 * sequence verbatim.) In either case, the character @a c is also
726 * written out, if @a c is not @c eof().
728 * For a formal definiton of this function, see a good text
729 * such as Langer & Kreft, or [27.5.2.4.5]/3-7.
731 * A functioning output streambuf can be created by overriding only
732 * this function (no buffer area will be used).
734 * @note Base class version does nothing, returns eof().
736 virtual int_type
737 overflow(int_type /* __c */ = traits_type::eof())
738 { return traits_type::eof(); }
740 #ifdef _GLIBCXX_DEPRECATED
741 // Annex D.6
742 public:
744 * @brief Tosses a character.
746 * Advances the read pointer, ignoring the character that would have
747 * been read.
749 * See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
751 * @note This function has been deprecated by the standard. You
752 * must define @c _GLIBCXX_DEPRECATED to make this visible; see
753 * c++config.h.
755 void
756 stossc()
758 if (this->gptr() < this->egptr())
759 this->gbump(1);
760 else
761 this->uflow();
763 #endif
765 private:
766 // _GLIBCXX_RESOLVE_LIB_DEFECTS
767 // Side effect of DR 50.
768 basic_streambuf(const __streambuf_type& __sb)
769 : _M_in_beg(__sb._M_in_beg), _M_in_cur(__sb._M_in_cur),
770 _M_in_end(__sb._M_in_end), _M_out_beg(__sb._M_out_beg),
771 _M_out_cur(__sb._M_out_cur), _M_out_end(__sb._M_out_cur),
772 _M_buf_locale(__sb._M_buf_locale)
775 __streambuf_type&
776 operator=(const __streambuf_type&) { return *this; };
778 } // namespace std
780 #ifndef _GLIBCXX_EXPORT_TEMPLATE
781 # include <bits/streambuf.tcc>
782 #endif
784 #endif /* _GLIBCXX_STREAMBUF */