3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
18 #ifndef _STLP_INTERNAL_STREAMBUF
19 #define _STLP_INTERNAL_STREAMBUF
21 #ifndef _STLP_IOS_BASE_H
22 # include <stl/_ios_base.h> // Needed for ios_base bitfield members.
23 #endif // <ios_base> includes <iosfwd>.
27 //----------------------------------------------------------------------
28 // Class basic_streambuf<>, the base class of the streambuf hierarchy.
30 // A basic_streambuf<> manages an input (get) area and an output (put)
31 // area. Each is described by three pointers: a beginning, an end, and a
32 // current position. basic_streambuf<> contains some very simple member
33 // functions that manipulate those six pointers, but almost all of the real
34 // functionality gets delegated to protected virtual member functions.
35 // All of the public member functions are inline, and most of the protected
36 // member functions are virtual.
38 // Although basic_streambuf<> is not abstract, it is useful only as a base
39 // class. Its virtual member functions have default definitions such that
40 // reading from a basic_streambuf<> will always yield EOF, and writing to a
41 // basic_streambuf<> will always fail.
43 // The second template parameter, _Traits, defaults to char_traits<_CharT>.
44 // The default is declared in header <iosfwd>, and it isn't declared here
45 // because C++ language rules do not allow it to be declared twice.
47 template <class _CharT, class _Traits>
48 class basic_streambuf {
49 friend class basic_istream<_CharT, _Traits>;
50 friend class basic_ostream<_CharT, _Traits>;
53 typedef _CharT char_type;
54 typedef typename _Traits::int_type int_type;
55 typedef typename _Traits::pos_type pos_type;
56 typedef typename _Traits::off_type off_type;
57 typedef _Traits traits_type;
59 private: // Data members.
61 char_type* _M_gbegin; // Beginning of get area
62 char_type* _M_gnext; // Current position within the get area
63 char_type* _M_gend; // End of get area
65 char_type* _M_pbegin; // Beginning of put area
66 char_type* _M_pnext; // Current position within the put area
67 char_type* _M_pend; // End of put area
69 locale _M_locale; // The streambuf's locale object
71 //public: // Extension: locking, for thread safety.
72 // _STLP_mutex _M_lock;
74 public: // Destructor.
75 virtual ~basic_streambuf();
77 protected: // The default constructor.
79 #if defined (_STLP_MSVC) && (_STLP_MSVC < 1300) && defined (_STLP_USE_STATIC_LIB)
80 //We make it inline to avoid unresolved symbol.
81 : _M_gbegin(0), _M_gnext(0), _M_gend(0),
82 _M_pbegin(0), _M_pnext(0), _M_pend(0),
89 protected: // Protected interface to the get area.
90 char_type* eback() const { return _M_gbegin; } // Beginning
91 char_type* gptr() const { return _M_gnext; } // Current position
92 char_type* egptr() const { return _M_gend; } // End
94 void gbump(int __n) { _M_gnext += __n; }
95 void setg(char_type* __gbegin, char_type* __gnext, char_type* __gend) {
102 // An alternate public interface to the above functions
103 // which allows us to avoid using templated friends which
104 // are not supported on some compilers.
105 char_type* _M_eback() const { return eback(); }
106 char_type* _M_gptr() const { return gptr(); }
107 char_type* _M_egptr() const { return egptr(); }
108 void _M_gbump(int __n) { gbump(__n); }
109 void _M_setg(char_type* __gbegin, char_type* __gnext, char_type* __gend)
110 { this->setg(__gbegin, __gnext, __gend); }
112 protected: // Protected interface to the put area
114 char_type* pbase() const { return _M_pbegin; } // Beginning
115 char_type* pptr() const { return _M_pnext; } // Current position
116 char_type* epptr() const { return _M_pend; } // End
118 void pbump(int __n) { _M_pnext += __n; }
119 void setp(char_type* __pbegin, char_type* __pend) {
120 _M_pbegin = __pbegin;
125 protected: // Virtual buffer management functions.
127 virtual basic_streambuf<_CharT, _Traits>* setbuf(char_type*, streamsize);
129 // Alters the stream position, using an integer offset. In this
130 // class seekoff does nothing; subclasses are expected to override it.
131 virtual pos_type seekoff(off_type, ios_base::seekdir,
132 ios_base::openmode = ios_base::in | ios_base::out);
134 // Alters the stream position, using a previously obtained streampos. In
135 // this class seekpos does nothing; subclasses are expected to override it.
137 seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
139 // Synchronizes (i.e. flushes) the buffer. All subclasses are expected to
140 // override this virtual member function.
144 public: // Buffer management.
145 basic_streambuf<_CharT, _Traits>* pubsetbuf(char_type* __s, streamsize __n)
146 { return this->setbuf(__s, __n); }
148 pos_type pubseekoff(off_type __offset, ios_base::seekdir __way,
149 ios_base::openmode __mod = ios_base::in | ios_base::out)
150 { return this->seekoff(__offset, __way, __mod); }
152 pos_type pubseekpos(pos_type __sp,
153 ios_base::openmode __mod = ios_base::in | ios_base::out)
154 { return this->seekpos(__sp, __mod); }
156 int pubsync() { return this->sync(); }
158 protected: // Virtual get area functions, as defined in
159 // 17.5.2.4.3 and 17.5.2.4.4 of the standard.
160 // Returns a lower bound on the number of characters that we can read,
161 // with underflow, before reaching end of file. (-1 is a special value:
162 // it means that underflow will fail.) Most subclasses should probably
163 // override this virtual member function.
164 virtual streamsize showmanyc();
166 // Reads up to __n characters. Return value is the number of
168 virtual streamsize xsgetn(char_type* __s, streamsize __n);
170 // Called when there is no read position, i.e. when gptr() is null
171 // or when gptr() >= egptr(). Subclasses are expected to override
172 // this virtual member function.
173 virtual int_type underflow();
175 // Similar to underflow(), but used for unbuffered input. Most
176 // subclasses should probably override this virtual member function.
177 virtual int_type uflow();
179 // Called when there is no putback position, i.e. when gptr() is null
180 // or when gptr() == eback(). All subclasses are expected to override
181 // this virtual member function.
182 virtual int_type pbackfail(int_type = traits_type::eof());
184 protected: // Virtual put area functions, as defined in
185 // 27.5.2.4.5 of the standard.
187 // Writes up to __n characters. Return value is the number of characters
189 virtual streamsize xsputn(const char_type* __s, streamsize __n);
191 // Extension: writes up to __n copies of __c. Return value is the number
192 // of characters written.
193 virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
195 // Called when there is no write position. All subclasses are expected to
196 // override this virtual member function.
197 virtual int_type overflow(int_type = traits_type::eof());
199 public: // Public members for writing characters.
200 // Write a single character.
201 int_type sputc(char_type __c) {
202 return ((_M_pnext < _M_pend) ? _Traits::to_int_type(*_M_pnext++ = __c)
203 : this->overflow(_Traits::to_int_type(__c)));
206 // Write __n characters.
207 streamsize sputn(const char_type* __s, streamsize __n)
208 { return this->xsputn(__s, __n); }
210 // Extension: write __n copies of __c.
211 streamsize _M_sputnc(char_type __c, streamsize __n)
212 { return this->_M_xsputnc(__c, __n); }
214 private: // Helper functions.
215 int_type _M_snextc_aux();
217 public: // Public members for reading characters.
218 streamsize in_avail() {
219 return (_M_gnext < _M_gend) ? (_M_gend - _M_gnext) : this->showmanyc();
222 // Advance to the next character and return it.
224 return ( _M_gend - _M_gnext > 1 ?
225 _Traits::to_int_type(*++_M_gnext) :
226 this->_M_snextc_aux());
229 // Return the current character and advance to the next.
231 return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext++)
235 // Return the current character without advancing to the next.
237 return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext)
241 streamsize sgetn(char_type* __s, streamsize __n)
242 { return this->xsgetn(__s, __n); }
244 int_type sputbackc(char_type __c) {
245 return ((_M_gbegin < _M_gnext) && _Traits::eq(__c, *(_M_gnext - 1)))
246 ? _Traits::to_int_type(*--_M_gnext)
247 : this->pbackfail(_Traits::to_int_type(__c));
251 return (_M_gbegin < _M_gnext)
252 ? _Traits::to_int_type(*--_M_gnext)
256 protected: // Virtual locale functions.
258 // This is a hook, called by pubimbue() just before pubimbue()
259 // sets the streambuf's locale to __loc. Note that imbue should
260 // not (and cannot, since it has no access to streambuf's private
261 // members) set the streambuf's locale itself.
262 virtual void imbue(const locale&);
264 public: // Locale-related functions.
265 locale pubimbue(const locale&);
266 locale getloc() const { return _M_locale; }
268 #if !defined (_STLP_NO_ANACHRONISMS)
269 void stossc() { this->sbumpc(); }
271 #if defined (__MVS__) || defined (__OS400__)
272 private: // Data members.
274 char_type* _M_gbegin; // Beginning of get area
275 char_type* _M_gnext; // Current position within the get area
276 char_type* _M_gend; // End of get area
278 char_type* _M_pbegin; // Beginning of put area
279 char_type* _M_pnext; // Current position within the put area
280 char_type* _M_pend; // End of put area
284 #if defined (_STLP_USE_TEMPLATE_EXPORT)
285 _STLP_EXPORT_TEMPLATE_CLASS basic_streambuf<char, char_traits<char> >;
286 # if !defined (_STLP_NO_WCHAR_T)
287 _STLP_EXPORT_TEMPLATE_CLASS basic_streambuf<wchar_t, char_traits<wchar_t> >;
288 # endif // _STLP_NO_WCHAR_T
289 #endif // _STLP_USE_TEMPLATE_EXPORT
293 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
294 # include <stl/_streambuf.c>