Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
2 * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
5 * Silicon Graphics Computer Systems, Inc.
10 * This material is provided "as is", with absolutely no warranty expressed
11 * or implied. Any use is at your own risk.
13 * Permission to use or copy this software for any purpose is hereby granted
14 * without fee, provided the above notices are retained on all copies.
15 * Permission to modify the code and to distribute modified code is granted,
16 * provided the above notices are retained, and a notice that the code was
17 * modified is included with the above copyright notice.
20 #ifndef _STLP_INTERNAL_STREAMBUF
21 #define _STLP_INTERNAL_STREAMBUF
23 #ifndef _STLP_IOS_BASE_H
24 #include <stl/_ios_base.h> // Needed for ios_base bitfield members.
25 // <ios_base> includes <iosfwd>.
28 #ifndef _STLP_STDIO_FILE_H
29 #include <stl/_stdio_file.h> // Declaration of struct FILE, and of
30 // functions to manipulate it.
35 //----------------------------------------------------------------------
36 // Class basic_streambuf<>, the base class of the streambuf hierarchy.
38 // A basic_streambuf<> manages an input (get) area and an output (put)
39 // area. Each is described by three pointers: a beginning, an end, and a
40 // current position. basic_streambuf<> contains some very simple member
41 // functions that manipulate those six pointers, but almost all of the real
42 // functionality gets delegated to protected virtual member functions.
43 // All of the public member functions are inline, and most of the protected
44 // member functions are virtual.
46 // Although basic_streambuf<> is not abstract, it is useful only as a base
47 // class. Its virtual member functions have default definitions such that
48 // reading from a basic_streambuf<> will always yield EOF, and writing to a
49 // basic_streambuf<> will always fail.
51 // The second template parameter, _Traits, defaults to char_traits<_CharT>.
52 // The default is declared in header <iosfwd>, and it isn't declared here
53 // because C++ language rules do not allow it to be declared twice.
55 template <class _CharT, class _Traits>
62 friend class basic_istream<_CharT, _Traits>;
63 friend class basic_ostream<_CharT, _Traits>;
66 typedef _CharT char_type;
67 typedef typename _Traits::int_type int_type;
68 typedef typename _Traits::pos_type pos_type;
69 typedef typename _Traits::off_type off_type;
70 typedef _Traits traits_type;
72 private: // Data members.
74 char_type* _M_gbegin; // Beginning of get area
75 char_type* _M_gnext; // Current position within the get area
76 char_type* _M_gend; // End of get area
78 char_type* _M_pbegin; // Beginning of put area
79 char_type* _M_pnext; // Current position within the put area
80 char_type* _M_pend; // End of put area
82 locale _M_locale; // The streambuf's locale object
84 public: // Extension: locking, for thread safety.
87 public: // Destructor.
88 _STLP_DECLSPEC virtual ~basic_streambuf();
90 protected: // The default constructor.
91 _STLP_DECLSPEC basic_streambuf();
93 protected: // Protected interface to the get area.
94 char_type* eback() const { return _M_gbegin; } // Beginning
95 char_type* gptr() const { return _M_gnext; } // Current position
96 char_type* egptr() const { return _M_gend; } // End
98 void gbump(int __n) { _M_gnext += __n; }
99 void setg(char_type* __gbegin, char_type* __gnext, char_type* __gend) {
100 _M_gbegin = __gbegin;
106 // An alternate public interface to the above functions
107 // which allows us to avoid using templated friends which
108 // are not supported on some compilers.
110 char_type* _M_eback() const { return eback(); }
111 char_type* _M_gptr() const { return gptr(); }
112 char_type* _M_egptr() const { return egptr(); }
113 void _M_gbump(int __n) { gbump(__n); }
114 void _M_setg(char_type* __gbegin, char_type* __gnext, char_type* __gend)
115 { setg(__gbegin, __gnext, __gend); }
117 protected: // Protected interface to the put area
119 char_type* pbase() const { return _M_pbegin; } // Beginning
120 char_type* pptr() const { return _M_pnext; } // Current position
121 char_type* epptr() const { return _M_pend; } // End
123 void pbump(int __n) { _M_pnext += __n; }
124 void setp(char_type* __pbegin, char_type* __pend) {
125 _M_pbegin = __pbegin;
130 protected: // Virtual buffer management functions.
132 _STLP_DECLSPEC virtual basic_streambuf<_CharT, _Traits>* setbuf(char_type*, streamsize);
134 // Alters the stream position, using an integer offset. In this
135 // class seekoff does nothing; subclasses are expected to override it.
136 _STLP_DECLSPEC virtual pos_type seekoff(off_type, ios_base::seekdir,
137 ios_base::openmode = ios_base::in | ios_base::out);
139 // Alters the stream position, using a previously obtained streampos. In
140 // this class seekpos does nothing; subclasses are expected to override it.
141 _STLP_DECLSPEC virtual pos_type
142 seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
144 // Synchronizes (i.e. flushes) the buffer. All subclasses are expected to
145 // override this virtual member function.
146 _STLP_DECLSPEC virtual int sync();
149 public: // Buffer management.
150 basic_streambuf<_CharT, _Traits>* pubsetbuf(char_type* __s, streamsize __n)
151 { return this->setbuf(__s, __n); }
153 pos_type pubseekoff(off_type __offset, ios_base::seekdir __way,
154 ios_base::openmode __mod = ios_base::in | ios_base::out)
155 { return this->seekoff(__offset, __way, __mod); }
157 pos_type pubseekpos(pos_type __sp,
158 ios_base::openmode __mod = ios_base::in | ios_base::out)
159 { return this->seekpos(__sp, __mod); }
161 int pubsync() { return this->sync(); }
163 protected: // Virtual get area functions, as defined in
164 // 17.5.2.4.3 and 17.5.2.4.4 of the standard.
165 // Returns a lower bound on the number of characters that we can read,
166 // with underflow, before reaching end of file. (-1 is a special value:
167 // it means that underflow will fail.) Most subclasses should probably
168 // override this virtual member function.
169 _STLP_DECLSPEC virtual streamsize showmanyc();
171 // Reads up to __n characters. Return value is the number of
173 _STLP_DECLSPEC virtual streamsize xsgetn(char_type* __s, streamsize __n);
175 // Called when there is no read position, i.e. when gptr() is null
176 // or when gptr() >= egptr(). Subclasses are expected to override
177 // this virtual member function.
178 _STLP_DECLSPEC virtual int_type underflow();
180 // Similar to underflow(), but used for unbuffered input. Most
181 // subclasses should probably override this virtual member function.
182 _STLP_DECLSPEC virtual int_type uflow();
184 // Called when there is no putback position, i.e. when gptr() is null
185 // or when gptr() == eback(). All subclasses are expected to override
186 // this virtual member function.
187 _STLP_DECLSPEC virtual int_type pbackfail(int_type = traits_type::eof());
189 protected: // Virtual put area functions, as defined in
190 // 27.5.2.4.5 of the standard.
192 // Writes up to __n characters. Return value is the number of characters
194 _STLP_DECLSPEC virtual streamsize xsputn(const char_type* __s, streamsize __n);
196 // Extension: writes up to __n copies of __c. Return value is the number
197 // of characters written.
198 _STLP_DECLSPEC virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
200 // Called when there is no write position. All subclasses are expected to
201 // override this virtual member function.
202 _STLP_DECLSPEC virtual int_type overflow(int_type = traits_type::eof());
204 public: // Public members for writing characters.
205 // Write a single character.
206 int_type sputc(char_type __c) {
207 return ((_M_pnext < _M_pend) ? _Traits::to_int_type(*_M_pnext++ = __c)
208 : this->overflow(_Traits::to_int_type(__c)));
211 // Write __n characters.
212 streamsize sputn(const char_type* __s, streamsize __n)
213 { return this->xsputn(__s, __n); }
215 // Extension: write __n copies of __c.
216 streamsize _M_sputnc(char_type __c, streamsize __n)
217 { return this->_M_xsputnc(__c, __n); }
219 private: // Helper functions.
220 _STLP_DECLSPEC int_type _M_snextc_aux();
223 public: // Public members for reading characters.
224 streamsize in_avail() {
225 return (_M_gnext < _M_gend) ? (_M_gend - _M_gnext) : this->showmanyc();
228 // Advance to the next character and return it.
230 return ( _M_gend - _M_gnext > 1 ?
231 _Traits::to_int_type(*++_M_gnext) :
232 this->_M_snextc_aux());
235 // Return the current character and advance to the next.
237 return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext++)
241 // Return the current character without advancing to the next.
243 return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext)
247 streamsize sgetn(char_type* __s, streamsize __n)
248 { return this->xsgetn(__s, __n); }
250 int_type sputbackc(char_type __c) {
251 return ((_M_gbegin < _M_gnext) && _Traits::eq(__c, *(_M_gnext - 1)))
252 ? _Traits::to_int_type(*--_M_gnext)
253 : this->pbackfail(_Traits::to_int_type(__c));
257 return (_M_gbegin < _M_gnext)
258 ? _Traits::to_int_type(*--_M_gnext)
262 protected: // Virtual locale functions.
264 // This is a hook, called by pubimbue() just before pubimbue()
265 // sets the streambuf's locale to __loc. Note that imbue should
266 // not (and cannot, since it has no access to streambuf's private
267 // members) set the streambuf's locale itself.
268 _STLP_DECLSPEC virtual void imbue(const locale&);
270 public: // Locale-related functions.
271 _STLP_DECLSPEC locale pubimbue(const locale&);
272 locale getloc() const { return _M_locale; }
274 # ifndef _STLP_NO_ANACHRONISMS
275 void stossc() { this->sbumpc(); }
277 #if defined(__MVS__) || defined(__OS400__)
278 private: // Data members.
280 char_type* _M_gbegin; // Beginning of get area
281 char_type* _M_gnext; // Current position within the get area
282 char_type* _M_gend; // End of get area
284 char_type* _M_pbegin; // Beginning of put area
285 char_type* _M_pnext; // Current position within the put area
286 char_type* _M_pend; // End of put area
291 //----------------------------------------------------------------------
292 // Specialization: basic_streambuf<char, char_traits<char> >
294 // We implement basic_streambuf<char, char_traits<char> > very differently
295 // than the general basic_streambuf<> template. The main reason for this
296 // difference is a requirement in the C++ standard: the standard input
297 // and output streams cin and cout are required by default to be synchronized
298 // with the C library components stdin and stdout. This means it must be
299 // possible to synchronize a basic_streambuf<char> with a C buffer.
301 // There are two basic ways to do that. First, the streambuf could be
302 // unbuffered and delegate all buffering to stdio operations. This
303 // would be correct, but slow: it would require at least one virtual
304 // function call for every character. Second, the streambuf could use
305 // a C stdio FILE as its buffer.
307 // We choose the latter option. Every streambuf has pointers to two
308 // FILE objects, one for the get area and one for the put area. Ordinarily
309 // it just uses a FILE object as a convenient way to package the three
310 // get/put area pointers. If a basic_streambuf<char> is synchronized with
311 // a stdio stream, though, then the pointers are to a FILE object that's
312 // also used by the C library.
314 // The header <stl/_stdio_file.h> encapsulates the implementation details
315 // of struct FILE. It contains low-level inline functions that convert
316 // between whe FILE's internal representation and the three-pointer
317 // representation that basic_streambuf<> needs.
321 class basic_streambuf<char, char_traits<char> >
323 class _STLP_CLASS_DECLSPEC basic_streambuf<char, char_traits<char> >
326 friend class basic_istream<char, char_traits<char> >;
327 friend class basic_ostream<char, char_traits<char> >;
329 typedef char char_type;
330 typedef char_traits<char>::int_type int_type;
331 typedef char_traits<char>::pos_type pos_type;
332 typedef char_traits<char>::off_type off_type;
333 typedef char_traits<char> traits_type;
335 private: // Data members.
337 FILE* _M_get; // Reference to the get area
338 FILE* _M_put; // Reference to the put area
341 _FILEX _M_default_get; // Get area, unless we're syncing with stdio.
342 _FILEX _M_default_put; // Put area, unless we're syncing with stdio.
344 FILE _M_default_get; // Get area, unless we're syncing with stdio.
345 FILE _M_default_put; // Put area, unless we're syncing with stdio.
350 public: // Extension: locking, for thread safety.
353 public: // Destructor.
354 _STLP_DECLSPEC virtual ~basic_streambuf _STLP_PSPEC2(char, char_traits<char>) ();
357 // The default constructor; defined here inline as some compilers require it
358 _STLP_DECLSPEC basic_streambuf _STLP_PSPEC2(char, char_traits<char>) ();
359 // Extension: a constructor for streambufs synchronized with C stdio files.
360 _STLP_DECLSPEC basic_streambuf _STLP_PSPEC2(char, char_traits<char>) (FILE* __get, FILE* __put);
362 protected: // Protected interface to the get area.
363 char_type* eback() const { return _FILE_I_begin(_M_get); }
364 char_type* gptr() const { return _FILE_I_next(_M_get); }
365 char_type* egptr() const { return _FILE_I_end(_M_get); }
366 void gbump(int __n) { _FILE_I_bump(_M_get, __n); }
367 void setg(char_type* __gbegin, char_type* __gnext, char_type* __gend)
369 _FILE_I_set(_M_get, __gbegin, __gnext, __gend);
371 _change_input_mode();
376 // An alternate public interface to the above functions
377 // which allows us to avoid using templated friends which
378 // are not supported on some compilers.
380 char_type* _M_eback() const { return _FILE_I_begin(_M_get); }
381 char_type* _M_gptr() const { return _FILE_I_next(_M_get); }
382 char_type* _M_egptr() const { return _FILE_I_end(_M_get); }
384 void _M_gbump(int __n) { _FILE_I_bump(_M_get, __n); }
385 void _M_setg(char_type* __gbegin, char_type* __gnext, char_type* __gend)
386 { _FILE_I_set(_M_get, __gbegin, __gnext, __gend); }
388 protected: // Protected interface to the put area
389 char_type* pbase() const { return _FILE_O_begin(_M_put); }
390 char_type* pptr() const { return _FILE_O_next(_M_put); }
391 char_type* epptr() const { return _FILE_O_end(_M_put); }
393 void pbump(int __n) { _FILE_O_bump(_M_put, __n); }
394 void setp(char_type* __pbegin, char_type* __pend)
395 { _FILE_O_set(_M_put, __pbegin, __pbegin, __pend); }
397 protected: // Virtual buffer-management functions.
398 _STLP_DECLSPEC virtual basic_streambuf<char, char_traits<char> >* setbuf(char_type*, streamsize);
399 _STLP_DECLSPEC virtual pos_type seekoff(off_type, ios_base::seekdir,
400 ios_base::openmode = ios_base::in | ios_base::out);
401 _STLP_DECLSPEC virtual pos_type
402 seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
403 _STLP_DECLSPEC virtual int sync();
405 public: // Buffer management.
406 basic_streambuf<char, char_traits<char> >* pubsetbuf(char_type* __s, streamsize __n)
407 { return this->setbuf(__s, __n); }
409 pos_type pubseekoff(off_type __offset, ios_base::seekdir __way,
410 ios_base::openmode __mod = ios_base::in | ios_base::out)
411 { return this->seekoff(__offset, __way, __mod); }
413 pos_type pubseekpos(pos_type __sp,
414 ios_base::openmode __mod = ios_base::in | ios_base::out)
415 { return this->seekpos(__sp, __mod); }
417 int pubsync() { return this->sync(); }
419 protected: // Virtual get area functions.
420 _STLP_DECLSPEC virtual streamsize showmanyc();
421 _STLP_DECLSPEC virtual streamsize xsgetn(char_type* __s, streamsize __n);
422 _STLP_DECLSPEC virtual int_type underflow();
423 _STLP_DECLSPEC virtual int_type uflow();
424 _STLP_DECLSPEC virtual int_type pbackfail(int_type __c = traits_type::eof());
426 protected: // Virtual put area functions.
427 _STLP_DECLSPEC virtual streamsize xsputn(const char_type* __s, streamsize __n);
428 _STLP_DECLSPEC virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
429 _STLP_DECLSPEC virtual int_type overflow(int_type = traits_type::eof());
431 virtual int save_read_buffer () { return 0; }
432 virtual void _change_input_mode() {};
434 public: // Public members for writing characters.
435 // Write a single character.
436 int_type sputc(char_type __c) {
438 if( _FILE_O_avail(_M_put) > 0 )
440 _FILE_O_postincr(_M_put) = __c;
441 __res = traits_type::to_int_type(__c);
444 __res = this->overflow(traits_type::to_int_type(__c));
448 // Write __n characters.
449 streamsize sputn(const char_type* __s, streamsize __n)
450 { return this->xsputn(__s, __n); }
452 // Extension: write __n copies of __c.
453 streamsize _M_sputnc(char_type __c, streamsize __n)
454 { return this->_M_xsputnc(__c, __n); }
456 private: // Helper functions.
457 _STLP_DECLSPEC int_type _M_snextc_aux();
459 public: // Public members for reading characters.
460 streamsize in_avail()
461 { return _FILE_I_avail(_M_get) > 0 ? _FILE_I_avail(_M_get)
465 : this->showmanyc(); }
467 // Advance to the next character and return it.
469 return _FILE_I_avail(_M_get) > 1
470 ? traits_type::to_int_type(_FILE_I_preincr(_M_get))
471 : this->_M_snextc_aux();
474 // Return the current character and advance to the next.
476 return _FILE_I_avail(_M_get) > 0
477 ? traits_type::to_int_type(_FILE_I_postincr(_M_get))
481 // Return the current character without advancing to the next.
483 return _FILE_I_avail(_M_get) > 0
484 ? traits_type::to_int_type(*_FILE_I_next(_M_get))
488 streamsize sgetn(char_type* __s, streamsize __n)
489 { return this->xsgetn(__s, __n); }
491 int_type sputbackc(char_type __c) {
492 return _FILE_I_begin(_M_get) < _FILE_I_next(_M_get) &&
493 __c == *(_FILE_I_next(_M_get) - 1)
494 ? traits_type::to_int_type(_FILE_I_predecr(_M_get))
495 : this->pbackfail(traits_type::to_int_type(__c));
499 return _FILE_I_begin(_M_get) < _FILE_I_next(_M_get)
500 ? traits_type::to_int_type(_FILE_I_predecr(_M_get))
504 protected: // Virtual locale functions.
505 _STLP_DECLSPEC virtual void imbue(const locale&);
506 public: // Locale-related functions.
507 _STLP_DECLSPEC locale pubimbue(const locale&);
508 locale getloc() const { return _M_locale; }
510 # ifndef _STLP_NO_ANACHRONISMS
512 void stossc() { this->sbumpc(); }
515 #if defined(__MVS__) || defined(__OS400__)
516 private: // Data members.
518 char_type* _M_gbegin; // Beginning of get area
519 char_type* _M_gnext; // Current position within the get area
520 char_type* _M_gend; // End of get area
522 char_type* _M_pbegin; // Beginning of put area
523 char_type* _M_pnext; // Current position within the put area
524 char_type* _M_pend; // End of put area
530 # if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
531 # include <stl/_streambuf.c>