2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_STREAMBUF
11 #define _LIBCPP_STREAMBUF
19 template <class charT, class traits = char_traits<charT> >
24 typedef charT char_type;
25 typedef traits traits_type;
26 typedef typename traits_type::int_type int_type;
27 typedef typename traits_type::pos_type pos_type;
28 typedef typename traits_type::off_type off_type;
30 virtual ~basic_streambuf();
32 // 27.6.2.2.1 locales:
33 locale pubimbue(const locale& loc);
34 locale getloc() const;
36 // 27.6.2.2.2 buffer and positioning:
37 basic_streambuf* pubsetbuf(char_type* s, streamsize n);
38 pos_type pubseekoff(off_type off, ios_base::seekdir way,
39 ios_base::openmode which = ios_base::in | ios_base::out);
40 pos_type pubseekpos(pos_type sp,
41 ios_base::openmode which = ios_base::in | ios_base::out);
45 // 27.6.2.2.3 Get area:
46 streamsize in_avail();
50 streamsize sgetn(char_type* s, streamsize n);
52 // 27.6.2.2.4 Putback:
53 int_type sputbackc(char_type c);
56 // 27.6.2.2.5 Put area:
57 int_type sputc(char_type c);
58 streamsize sputn(const char_type* s, streamsize n);
62 basic_streambuf(const basic_streambuf& rhs);
63 basic_streambuf& operator=(const basic_streambuf& rhs);
64 void swap(basic_streambuf& rhs);
66 // 27.6.2.3.2 Get area:
67 char_type* eback() const;
68 char_type* gptr() const;
69 char_type* egptr() const;
71 void setg(char_type* gbeg, char_type* gnext, char_type* gend);
73 // 27.6.2.3.3 Put area:
74 char_type* pbase() const;
75 char_type* pptr() const;
76 char_type* epptr() const;
78 void setp(char_type* pbeg, char_type* pend);
80 // 27.6.2.4 virtual functions:
81 // 27.6.2.4.1 Locales:
82 virtual void imbue(const locale& loc);
84 // 27.6.2.4.2 Buffer management and positioning:
85 virtual basic_streambuf* setbuf(char_type* s, streamsize n);
86 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
87 ios_base::openmode which = ios_base::in | ios_base::out);
88 virtual pos_type seekpos(pos_type sp,
89 ios_base::openmode which = ios_base::in | ios_base::out);
92 // 27.6.2.4.3 Get area:
93 virtual streamsize showmanyc();
94 virtual streamsize xsgetn(char_type* s, streamsize n);
95 virtual int_type underflow();
96 virtual int_type uflow();
98 // 27.6.2.4.4 Putback:
99 virtual int_type pbackfail(int_type c = traits_type::eof());
101 // 27.6.2.4.5 Put area:
102 virtual streamsize xsputn(const char_type* s, streamsize n);
103 virtual int_type overflow (int_type c = traits_type::eof());
110 #include <__assert> // all public C++ headers provide the assertion handler
112 #include <__fwd/streambuf.h>
118 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
119 # pragma GCC system_header
123 #include <__undef_macros>
125 _LIBCPP_BEGIN_NAMESPACE_STD
127 template <class _CharT, class _Traits>
128 class _LIBCPP_TEMPLATE_VIS basic_streambuf {
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;
137 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
138 "traits_type::char_type must be the same type as CharT");
140 virtual ~basic_streambuf();
142 // 27.6.2.2.1 locales:
143 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 locale pubimbue(const locale& __loc) {
150 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 locale getloc() const { return __loc_; }
152 // 27.6.2.2.2 buffer and positioning:
153 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_streambuf* pubsetbuf(char_type* __s, streamsize __n) {
154 return setbuf(__s, __n);
157 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type
158 pubseekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which = ios_base::in | ios_base::out) {
159 return seekoff(__off, __way, __which);
162 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type
163 pubseekpos(pos_type __sp, ios_base::openmode __which = ios_base::in | ios_base::out) {
164 return seekpos(__sp, __which);
167 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int pubsync() { return sync(); }
169 // Get and put areas:
170 // 27.6.2.2.3 Get area:
171 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 streamsize in_avail() {
172 if (__ninp_ < __einp_)
173 return static_cast<streamsize>(__einp_ - __ninp_);
177 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type snextc() {
178 if (sbumpc() == traits_type::eof())
179 return traits_type::eof();
183 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sbumpc() {
184 if (__ninp_ == __einp_)
186 return traits_type::to_int_type(*__ninp_++);
189 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sgetc() {
190 if (__ninp_ == __einp_)
192 return traits_type::to_int_type(*__ninp_);
195 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 streamsize sgetn(char_type* __s, streamsize __n) { return xsgetn(__s, __n); }
197 // 27.6.2.2.4 Putback:
198 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sputbackc(char_type __c) {
199 if (__binp_ == __ninp_ || !traits_type::eq(__c, __ninp_[-1]))
200 return pbackfail(traits_type::to_int_type(__c));
201 return traits_type::to_int_type(*--__ninp_);
204 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sungetc() {
205 if (__binp_ == __ninp_)
207 return traits_type::to_int_type(*--__ninp_);
210 // 27.6.2.2.5 Put area:
211 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sputc(char_type __c) {
212 if (__nout_ == __eout_)
213 return overflow(traits_type::to_int_type(__c));
215 return traits_type::to_int_type(__c);
218 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 streamsize sputn(const char_type* __s, streamsize __n) {
219 return xsputn(__s, __n);
224 basic_streambuf(const basic_streambuf& __rhs);
225 basic_streambuf& operator=(const basic_streambuf& __rhs);
226 void swap(basic_streambuf& __rhs);
228 // 27.6.2.3.2 Get area:
229 _LIBCPP_HIDE_FROM_ABI char_type* eback() const { return __binp_; }
230 _LIBCPP_HIDE_FROM_ABI char_type* gptr() const { return __ninp_; }
231 _LIBCPP_HIDE_FROM_ABI char_type* egptr() const { return __einp_; }
233 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void gbump(int __n) { __ninp_ += __n; }
235 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void setg(char_type* __gbeg, char_type* __gnext, char_type* __gend) {
241 // 27.6.2.3.3 Put area:
242 _LIBCPP_HIDE_FROM_ABI char_type* pbase() const { return __bout_; }
243 _LIBCPP_HIDE_FROM_ABI char_type* pptr() const { return __nout_; }
244 _LIBCPP_HIDE_FROM_ABI char_type* epptr() const { return __eout_; }
246 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void pbump(int __n) { __nout_ += __n; }
248 _LIBCPP_HIDE_FROM_ABI void __pbump(streamsize __n) { __nout_ += __n; }
250 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void setp(char_type* __pbeg, char_type* __pend) {
251 __bout_ = __nout_ = __pbeg;
255 // 27.6.2.4 virtual functions:
256 // 27.6.2.4.1 Locales:
257 virtual void imbue(const locale& __loc);
259 // 27.6.2.4.2 Buffer management and positioning:
260 virtual basic_streambuf* setbuf(char_type* __s, streamsize __n);
262 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which = ios_base::in | ios_base::out);
263 virtual pos_type seekpos(pos_type __sp, ios_base::openmode __which = ios_base::in | ios_base::out);
266 // 27.6.2.4.3 Get area:
267 virtual streamsize showmanyc();
268 virtual streamsize xsgetn(char_type* __s, streamsize __n);
269 virtual int_type underflow();
270 virtual int_type uflow();
272 // 27.6.2.4.4 Putback:
273 virtual int_type pbackfail(int_type __c = traits_type::eof());
275 // 27.6.2.4.5 Put area:
276 virtual streamsize xsputn(const char_type* __s, streamsize __n);
277 virtual int_type overflow(int_type __c = traits_type::eof());
289 template <class _CharT, class _Traits>
290 basic_streambuf<_CharT, _Traits>::~basic_streambuf() {}
292 template <class _CharT, class _Traits>
293 basic_streambuf<_CharT, _Traits>::basic_streambuf()
294 : __binp_(nullptr), __ninp_(nullptr), __einp_(nullptr), __bout_(nullptr), __nout_(nullptr), __eout_(nullptr) {}
296 template <class _CharT, class _Traits>
297 basic_streambuf<_CharT, _Traits>::basic_streambuf(const basic_streambuf& __sb)
298 : __loc_(__sb.__loc_),
299 __binp_(__sb.__binp_),
300 __ninp_(__sb.__ninp_),
301 __einp_(__sb.__einp_),
302 __bout_(__sb.__bout_),
303 __nout_(__sb.__nout_),
304 __eout_(__sb.__eout_) {}
306 template <class _CharT, class _Traits>
307 basic_streambuf<_CharT, _Traits>& basic_streambuf<_CharT, _Traits>::operator=(const basic_streambuf& __sb) {
308 __loc_ = __sb.__loc_;
309 __binp_ = __sb.__binp_;
310 __ninp_ = __sb.__ninp_;
311 __einp_ = __sb.__einp_;
312 __bout_ = __sb.__bout_;
313 __nout_ = __sb.__nout_;
314 __eout_ = __sb.__eout_;
318 template <class _CharT, class _Traits>
319 void basic_streambuf<_CharT, _Traits>::swap(basic_streambuf& __sb) {
320 std::swap(__loc_, __sb.__loc_);
321 std::swap(__binp_, __sb.__binp_);
322 std::swap(__ninp_, __sb.__ninp_);
323 std::swap(__einp_, __sb.__einp_);
324 std::swap(__bout_, __sb.__bout_);
325 std::swap(__nout_, __sb.__nout_);
326 std::swap(__eout_, __sb.__eout_);
329 template <class _CharT, class _Traits>
330 void basic_streambuf<_CharT, _Traits>::imbue(const locale&) {}
332 template <class _CharT, class _Traits>
333 basic_streambuf<_CharT, _Traits>* basic_streambuf<_CharT, _Traits>::setbuf(char_type*, streamsize) {
337 template <class _CharT, class _Traits>
338 typename basic_streambuf<_CharT, _Traits>::pos_type
339 basic_streambuf<_CharT, _Traits>::seekoff(off_type, ios_base::seekdir, ios_base::openmode) {
340 return pos_type(off_type(-1));
343 template <class _CharT, class _Traits>
344 typename basic_streambuf<_CharT, _Traits>::pos_type
345 basic_streambuf<_CharT, _Traits>::seekpos(pos_type, ios_base::openmode) {
346 return pos_type(off_type(-1));
349 template <class _CharT, class _Traits>
350 int basic_streambuf<_CharT, _Traits>::sync() {
354 template <class _CharT, class _Traits>
355 streamsize basic_streambuf<_CharT, _Traits>::showmanyc() {
359 template <class _CharT, class _Traits>
360 streamsize basic_streambuf<_CharT, _Traits>::xsgetn(char_type* __s, streamsize __n) {
361 const int_type __eof = traits_type::eof();
365 if (__ninp_ < __einp_) {
366 const streamsize __len = std::min(static_cast<streamsize>(INT_MAX), std::min(__einp_ - __ninp_, __n - __i));
367 traits_type::copy(__s, __ninp_, __len);
371 } else if ((__c = uflow()) != __eof) {
372 *__s = traits_type::to_char_type(__c);
381 template <class _CharT, class _Traits>
382 typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::underflow() {
383 return traits_type::eof();
386 template <class _CharT, class _Traits>
387 typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::uflow() {
388 if (underflow() == traits_type::eof())
389 return traits_type::eof();
390 return traits_type::to_int_type(*__ninp_++);
393 template <class _CharT, class _Traits>
394 typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::pbackfail(int_type) {
395 return traits_type::eof();
398 template <class _CharT, class _Traits>
399 streamsize basic_streambuf<_CharT, _Traits>::xsputn(const char_type* __s, streamsize __n) {
401 int_type __eof = traits_type::eof();
403 if (__nout_ >= __eout_) {
404 if (overflow(traits_type::to_int_type(*__s)) == __eof)
409 streamsize __chunk_size = std::min(__eout_ - __nout_, __n - __i);
410 traits_type::copy(__nout_, __s, __chunk_size);
411 __nout_ += __chunk_size;
419 template <class _CharT, class _Traits>
420 typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::overflow(int_type) {
421 return traits_type::eof();
424 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<char>;
426 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
427 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<wchar_t>;
430 _LIBCPP_END_NAMESPACE_STD
434 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
438 #endif // _LIBCPP_STREAMBUF