libstdc++
sstream
Go to the documentation of this file.
1// String based streams -*- C++ -*-
2
3// Copyright (C) 1997-2023 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/sstream
26 * This is a Standard C++ Library header.
27 */
28
29//
30// ISO C++ 14882: 27.7 String-based streams
31//
32
33#ifndef _GLIBCXX_SSTREAM
34#define _GLIBCXX_SSTREAM 1
35
36#pragma GCC system_header
37
38#include <bits/requires_hosted.h> // iostream
39
40#include <istream>
41#include <ostream>
42#include <bits/alloc_traits.h> // allocator_traits, __allocator_like
43
44#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
45# define _GLIBCXX_LVAL_REF_QUAL &
46#else
47# define _GLIBCXX_LVAL_REF_QUAL
48#endif
49
50namespace std _GLIBCXX_VISIBILITY(default)
51{
52_GLIBCXX_BEGIN_NAMESPACE_VERSION
53_GLIBCXX_BEGIN_NAMESPACE_CXX11
54
55 // [27.7.1] template class basic_stringbuf
56 /**
57 * @brief The actual work of input and output (for std::string).
58 * @ingroup io
59 *
60 * @tparam _CharT Type of character stream.
61 * @tparam _Traits Traits for character type, defaults to
62 * char_traits<_CharT>.
63 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
64 *
65 * This class associates either or both of its input and output sequences
66 * with a sequence of characters, which can be initialized from, or made
67 * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
68 *
69 * For this class, open modes (of type @c ios_base::openmode) have
70 * @c in set if the input sequence can be read, and @c out set if the
71 * output sequence can be written.
72 */
73 template<typename _CharT, typename _Traits, typename _Alloc>
74 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
75 {
76 struct __xfer_bufptrs;
77
78#if __cplusplus >= 201103L
79 using allocator_traits = std::allocator_traits<_Alloc>;
80 using _Noexcept_swap
81 = __or_<typename allocator_traits::propagate_on_container_swap,
82 typename allocator_traits::is_always_equal>;
83#endif
84
85 public:
86 // Types:
87 typedef _CharT char_type;
88 typedef _Traits traits_type;
89 // _GLIBCXX_RESOLVE_LIB_DEFECTS
90 // 251. basic_stringbuf missing allocator_type
91 typedef _Alloc allocator_type;
92 typedef typename traits_type::int_type int_type;
93 typedef typename traits_type::pos_type pos_type;
94 typedef typename traits_type::off_type off_type;
95
96 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
97 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
98 typedef typename __string_type::size_type __size_type;
99
100 protected:
101 /// Place to stash in || out || in | out settings for current stringbuf.
102 ios_base::openmode _M_mode;
103
104 // Data Members:
105 __string_type _M_string;
106
107 public:
108 // Constructors:
109
110 /**
111 * @brief Starts with an empty string buffer.
112 *
113 * The default constructor initializes the parent class using its
114 * own default ctor.
115 */
116 basic_stringbuf()
117 : __streambuf_type(), _M_mode(ios_base::in | ios_base::out), _M_string()
118 { }
119
120 /**
121 * @brief Starts with an empty string buffer.
122 * @param __mode Whether the buffer can read, or write, or both.
123 *
124 * The default constructor initializes the parent class using its
125 * own default ctor.
126 */
127 explicit
128 basic_stringbuf(ios_base::openmode __mode)
129 : __streambuf_type(), _M_mode(__mode), _M_string()
130 { }
131
132 /**
133 * @brief Starts with an existing string buffer.
134 * @param __str A string to copy as a starting buffer.
135 * @param __mode Whether the buffer can read, or write, or both.
136 *
137 * This constructor initializes the parent class using its
138 * own default ctor.
139 */
140 explicit
141 basic_stringbuf(const __string_type& __str,
142 ios_base::openmode __mode = ios_base::in | ios_base::out)
143 : __streambuf_type(), _M_mode(),
144 _M_string(__str.data(), __str.size(), __str.get_allocator())
145 { _M_stringbuf_init(__mode); }
146
147#if __cplusplus >= 201103L
148 basic_stringbuf(const basic_stringbuf&) = delete;
149
150 basic_stringbuf(basic_stringbuf&& __rhs)
151 : basic_stringbuf(std::move(__rhs), __xfer_bufptrs(__rhs, this))
152 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
153
154#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
155 explicit
156 basic_stringbuf(const allocator_type& __a)
157 : basic_stringbuf(ios_base::in | std::ios_base::out, __a)
158 { }
159
160 basic_stringbuf(ios_base::openmode __mode,
161 const allocator_type& __a)
162 : __streambuf_type(), _M_mode(__mode), _M_string(__a)
163 { }
164
165 explicit
166 basic_stringbuf(__string_type&& __s,
167 ios_base::openmode __mode = ios_base::in
168 | ios_base::out)
169 : __streambuf_type(), _M_mode(__mode), _M_string(std::move(__s))
170 { _M_stringbuf_init(__mode); }
171
172 template<typename _SAlloc>
173 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
174 const allocator_type& __a)
175 : basic_stringbuf(__s, ios_base::in | std::ios_base::out, __a)
176 { }
177
178 template<typename _SAlloc>
179 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
180 ios_base::openmode __mode,
181 const allocator_type& __a)
182 : __streambuf_type(), _M_mode(__mode),
183 _M_string(__s.data(), __s.size(), __a)
184 { _M_stringbuf_init(__mode); }
185
186 template<typename _SAlloc>
187 explicit
188 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
189 ios_base::openmode __mode = ios_base::in
190 | ios_base::out)
191 : basic_stringbuf(__s, __mode, allocator_type{})
192 { }
193
194 basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a)
195 : basic_stringbuf(std::move(__rhs), __a, __xfer_bufptrs(__rhs, this))
196 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
197
198 allocator_type get_allocator() const noexcept
199 { return _M_string.get_allocator(); }
200#endif // C++20
201
202 // 27.8.2.2 Assign and swap:
203
204 basic_stringbuf&
205 operator=(const basic_stringbuf&) = delete;
206
207 basic_stringbuf&
208 operator=(basic_stringbuf&& __rhs)
209 {
210 __xfer_bufptrs __st{__rhs, this};
211 const __streambuf_type& __base = __rhs;
212 __streambuf_type::operator=(__base);
213 this->pubimbue(__rhs.getloc());
214 _M_mode = __rhs._M_mode;
215 _M_string = std::move(__rhs._M_string);
216 __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0);
217 return *this;
218 }
219
220 void
221 swap(basic_stringbuf& __rhs) noexcept(_Noexcept_swap::value)
222 {
223 __xfer_bufptrs __l_st{*this, std::__addressof(__rhs)};
224 __xfer_bufptrs __r_st{__rhs, this};
225 __streambuf_type& __base = __rhs;
226 __streambuf_type::swap(__base);
227 __rhs.pubimbue(this->pubimbue(__rhs.getloc()));
228 std::swap(_M_mode, __rhs._M_mode);
229 std::swap(_M_string, __rhs._M_string); // XXX not exception safe
230 }
231#endif // C++11
232
233 // Getters and setters:
234
235 /**
236 * @brief Copying out the string buffer.
237 * @return A copy of one of the underlying sequences.
238 *
239 * <em>If the buffer is only created in input mode, the underlying
240 * character sequence is equal to the input sequence; otherwise, it
241 * is equal to the output sequence.</em> [27.7.1.2]/1
242 */
243 __string_type
244 str() const _GLIBCXX_LVAL_REF_QUAL
245 {
246 __string_type __ret(_M_string.get_allocator());
247 if (char_type* __hi = _M_high_mark())
248 __ret.assign(this->pbase(), __hi);
249 else
250 __ret = _M_string;
251 return __ret;
252 }
253
254#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
255#if __cpp_concepts
256 template<__allocator_like _SAlloc>
257 basic_string<_CharT, _Traits, _SAlloc>
258 str(const _SAlloc& __sa) const
259 {
260 auto __sv = view();
261 return { __sv.data(), __sv.size(), __sa };
262 }
263#endif
264
265 __string_type
266 str() &&
267 {
268 if (char_type* __hi = _M_high_mark())
269 {
270 // Set length to end of character sequence and add null terminator.
271 _M_string._M_set_length(_M_high_mark() - this->pbase());
272 }
273 auto __str = std::move(_M_string);
274 _M_string.clear();
275 _M_sync(_M_string.data(), 0, 0);
276 return __str;
277 }
278
279 basic_string_view<char_type, traits_type>
280 view() const noexcept
281 {
282 if (char_type* __hi = _M_high_mark())
283 return { this->pbase(), __hi };
284 else
285 return _M_string;
286 }
287#endif // C++20
288
289 /**
290 * @brief Setting a new buffer.
291 * @param __s The string to use as a new sequence.
292 *
293 * Deallocates any previous stored sequence, then copies @a s to
294 * use as a new one.
295 */
296 void
297 str(const __string_type& __s)
298 {
299 // Cannot use _M_string = __s, since v3 strings are COW
300 // (not always true now but assign() always works).
301 _M_string.assign(__s.data(), __s.size());
302 _M_stringbuf_init(_M_mode);
303 }
304
305#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
306#if __cpp_concepts
307 template<__allocator_like _SAlloc>
308 requires (!is_same_v<_SAlloc, _Alloc>)
309 void
310 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
311 {
312 _M_string.assign(__s.data(), __s.size());
313 _M_stringbuf_init(_M_mode);
314 }
315#endif
316
317 void
318 str(__string_type&& __s)
319 {
320 _M_string = std::move(__s);
321 _M_stringbuf_init(_M_mode);
322 }
323#endif
324
325 protected:
326 // Common initialization code goes here.
327 void
328 _M_stringbuf_init(ios_base::openmode __mode)
329 {
330 _M_mode = __mode;
331 __size_type __len = 0;
332 if (_M_mode & (ios_base::ate | ios_base::app))
333 __len = _M_string.size();
334 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
335 }
336
337 virtual streamsize
338 showmanyc()
339 {
340 streamsize __ret = -1;
341 if (_M_mode & ios_base::in)
342 {
343 _M_update_egptr();
344 __ret = this->egptr() - this->gptr();
345 }
346 return __ret;
347 }
348
349 virtual int_type
350 underflow();
351
352 virtual int_type
353 pbackfail(int_type __c = traits_type::eof());
354
355 virtual int_type
356 overflow(int_type __c = traits_type::eof());
357
358 /**
359 * @brief Manipulates the buffer.
360 * @param __s Pointer to a buffer area.
361 * @param __n Size of @a __s.
362 * @return @c this
363 *
364 * If no buffer has already been created, and both @a __s and @a __n are
365 * non-zero, then @c __s is used as a buffer; see
366 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
367 * for more.
368 */
369 virtual __streambuf_type*
370 setbuf(char_type* __s, streamsize __n)
371 {
372 if (__s && __n >= 0)
373 {
374 // This is implementation-defined behavior, and assumes
375 // that an external char_type array of length __n exists
376 // and has been pre-allocated. If this is not the case,
377 // things will quickly blow up.
378
379 // Step 1: Destroy the current internal array.
380 _M_string.clear();
381
382 // Step 2: Use the external array.
383 _M_sync(__s, __n, 0);
384 }
385 return this;
386 }
387
388 virtual pos_type
389 seekoff(off_type __off, ios_base::seekdir __way,
390 ios_base::openmode __mode = ios_base::in | ios_base::out);
391
392 virtual pos_type
393 seekpos(pos_type __sp,
394 ios_base::openmode __mode = ios_base::in | ios_base::out);
395
396 // Internal function for correctly updating the internal buffer
397 // for a particular _M_string, due to initialization or re-sizing
398 // of an existing _M_string.
399 void
400 _M_sync(char_type* __base, __size_type __i, __size_type __o);
401
402 // Internal function for correctly updating egptr() to the actual
403 // string end.
404 void
405 _M_update_egptr()
406 {
407 if (char_type* __pptr = this->pptr())
408 {
409 char_type* __egptr = this->egptr();
410 if (!__egptr || __pptr > __egptr)
411 {
412 if (_M_mode & ios_base::in)
413 this->setg(this->eback(), this->gptr(), __pptr);
414 else
415 this->setg(__pptr, __pptr, __pptr);
416 }
417 }
418 }
419
420 // Works around the issue with pbump, part of the protected
421 // interface of basic_streambuf, taking just an int.
422 void
423 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off);
424
425 private:
426 // Return a pointer to the end of the underlying character sequence.
427 // This might not be the same character as _M_string.end() because
428 // basic_stringbuf::overflow might have written to unused capacity
429 // in _M_string without updating its length.
430 __attribute__((__always_inline__))
431 char_type*
432 _M_high_mark() const _GLIBCXX_NOEXCEPT
433 {
434 if (char_type* __pptr = this->pptr())
435 {
436 char_type* __egptr = this->egptr();
437 if (!__egptr || __pptr > __egptr)
438 return __pptr; // Underlying sequence is [pbase, pptr).
439 else
440 return __egptr; // Underlying sequence is [pbase, egptr).
441 }
442 return 0; // Underlying character sequence is just _M_string.
443 }
444
445#if __cplusplus >= 201103L
446#if _GLIBCXX_USE_CXX11_ABI
447 // This type captures the state of the gptr / pptr pointers as offsets
448 // so they can be restored in another object after moving the string.
449 struct __xfer_bufptrs
450 {
451 __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to)
452 : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1}
453 {
454 const _CharT* const __str = __from._M_string.data();
455 const _CharT* __end = nullptr;
456 if (__from.eback())
457 {
458 _M_goff[0] = __from.eback() - __str;
459 _M_goff[1] = __from.gptr() - __str;
460 _M_goff[2] = __from.egptr() - __str;
461 __end = __from.egptr();
462 }
463 if (__from.pbase())
464 {
465 _M_poff[0] = __from.pbase() - __str;
466 _M_poff[1] = __from.pptr() - __from.pbase();
467 _M_poff[2] = __from.epptr() - __str;
468 if (!__end || __from.pptr() > __end)
469 __end = __from.pptr();
470 }
471
472 // Set _M_string length to the greater of the get and put areas.
473 if (__end)
474 {
475 // The const_cast avoids changing this constructor's signature,
476 // because it is exported from the dynamic library.
477 auto& __mut_from = const_cast<basic_stringbuf&>(__from);
478 __mut_from._M_string._M_length(__end - __str);
479 }
480 }
481
482 ~__xfer_bufptrs()
483 {
484 char_type* __str = const_cast<char_type*>(_M_to->_M_string.data());
485 if (_M_goff[0] != -1)
486 _M_to->setg(__str+_M_goff[0], __str+_M_goff[1], __str+_M_goff[2]);
487 if (_M_poff[0] != -1)
488 _M_to->_M_pbump(__str+_M_poff[0], __str+_M_poff[2], _M_poff[1]);
489 }
490
491 basic_stringbuf* _M_to;
492 off_type _M_goff[3];
493 off_type _M_poff[3];
494 };
495#else
496 // This type does nothing when using Copy-On-Write strings.
497 struct __xfer_bufptrs
498 {
499 __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { }
500 };
501#endif
502
503 // The move constructor initializes an __xfer_bufptrs temporary then
504 // delegates to this constructor to performs moves during its lifetime.
505 basic_stringbuf(basic_stringbuf&& __rhs, __xfer_bufptrs&&)
506 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
507 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string))
508 { }
509
510#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
511 // The move constructor initializes an __xfer_bufptrs temporary then
512 // delegates to this constructor to performs moves during its lifetime.
513 basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a,
514 __xfer_bufptrs&&)
515 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
516 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string), __a)
517 { }
518#endif
519#endif // C++11
520 };
521
522
523 // [27.7.2] Template class basic_istringstream
524 /**
525 * @brief Controlling input for std::string.
526 * @ingroup io
527 *
528 * @tparam _CharT Type of character stream.
529 * @tparam _Traits Traits for character type, defaults to
530 * char_traits<_CharT>.
531 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
532 *
533 * This class supports reading from objects of type std::basic_string,
534 * using the inherited functions from std::basic_istream. To control
535 * the associated sequence, an instance of std::basic_stringbuf is used,
536 * which this page refers to as @c sb.
537 */
538 template<typename _CharT, typename _Traits, typename _Alloc>
539 class basic_istringstream : public basic_istream<_CharT, _Traits>
540 {
541 public:
542 // Types:
543 typedef _CharT char_type;
544 typedef _Traits traits_type;
545 // _GLIBCXX_RESOLVE_LIB_DEFECTS
546 // 251. basic_stringbuf missing allocator_type
547 typedef _Alloc allocator_type;
548 typedef typename traits_type::int_type int_type;
549 typedef typename traits_type::pos_type pos_type;
550 typedef typename traits_type::off_type off_type;
551
552 // Non-standard types:
553 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
554 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
555 typedef basic_istream<char_type, traits_type> __istream_type;
556
557 private:
558 __stringbuf_type _M_stringbuf;
559
560 public:
561 // Constructors:
562
563 /**
564 * @brief Default constructor starts with an empty string buffer.
565 *
566 * Initializes @c sb using @c in, and passes @c &sb to the base
567 * class initializer. Does not allocate any buffer.
568 *
569 * That's a lie. We initialize the base class with NULL, because the
570 * string class does its own memory management.
571 */
572 basic_istringstream()
573 : __istream_type(), _M_stringbuf(ios_base::in)
574 { this->init(&_M_stringbuf); }
575
576 /**
577 * @brief Starts with an empty string buffer.
578 * @param __mode Whether the buffer can read, or write, or both.
579 *
580 * @c ios_base::in is automatically included in @a __mode.
581 *
582 * Initializes @c sb using @c __mode|in, and passes @c &sb to the base
583 * class initializer. Does not allocate any buffer.
584 *
585 * That's a lie. We initialize the base class with NULL, because the
586 * string class does its own memory management.
587 */
588 explicit
589 basic_istringstream(ios_base::openmode __mode)
590 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
591 { this->init(&_M_stringbuf); }
592
593 /**
594 * @brief Starts with an existing string buffer.
595 * @param __str A string to copy as a starting buffer.
596 * @param __mode Whether the buffer can read, or write, or both.
597 *
598 * @c ios_base::in is automatically included in @a mode.
599 *
600 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
601 * to the base class initializer.
602 *
603 * That's a lie. We initialize the base class with NULL, because the
604 * string class does its own memory management.
605 */
606 explicit
607 basic_istringstream(const __string_type& __str,
608 ios_base::openmode __mode = ios_base::in)
609 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
610 { this->init(&_M_stringbuf); }
611
612 /**
613 * @brief The destructor does nothing.
614 *
615 * The buffer is deallocated by the stringbuf object, not the
616 * formatting stream.
617 */
618 ~basic_istringstream()
619 { }
620
621#if __cplusplus >= 201103L
622 basic_istringstream(const basic_istringstream&) = delete;
623
624 basic_istringstream(basic_istringstream&& __rhs)
625 : __istream_type(std::move(__rhs)),
626 _M_stringbuf(std::move(__rhs._M_stringbuf))
627 { __istream_type::set_rdbuf(&_M_stringbuf); }
628
629#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
630 basic_istringstream(ios_base::openmode __mode, const allocator_type& __a)
631 : __istream_type(), _M_stringbuf(__mode | ios_base::in, __a)
632 { this->init(std::__addressof(_M_stringbuf)); }
633
634 explicit
635 basic_istringstream(__string_type&& __str,
636 ios_base::openmode __mode = ios_base::in)
637 : __istream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::in)
638 { this->init(std::__addressof(_M_stringbuf)); }
639
640 template<typename _SAlloc>
641 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
642 const allocator_type& __a)
643 : basic_istringstream(__str, ios_base::in, __a)
644 { }
645
646 template<typename _SAlloc>
647 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
648 ios_base::openmode __mode,
649 const allocator_type& __a)
650 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in, __a)
651 { this->init(std::__addressof(_M_stringbuf)); }
652
653 template<typename _SAlloc>
654 explicit
655 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
656 ios_base::openmode __mode = ios_base::in)
657 : basic_istringstream(__str, __mode, allocator_type())
658 { }
659#endif // C++20
660
661 // 27.8.3.2 Assign and swap:
662
663 basic_istringstream&
664 operator=(const basic_istringstream&) = delete;
665
666 basic_istringstream&
667 operator=(basic_istringstream&& __rhs)
668 {
669 __istream_type::operator=(std::move(__rhs));
670 _M_stringbuf = std::move(__rhs._M_stringbuf);
671 return *this;
672 }
673
674 void
675 swap(basic_istringstream& __rhs)
676 {
677 __istream_type::swap(__rhs);
678 _M_stringbuf.swap(__rhs._M_stringbuf);
679 }
680#endif // C++11
681
682 // Members:
683 /**
684 * @brief Accessing the underlying buffer.
685 * @return The current basic_stringbuf buffer.
686 *
687 * This hides both signatures of std::basic_ios::rdbuf().
688 */
689 __stringbuf_type*
690 rdbuf() const
691 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
692
693 /**
694 * @brief Copying out the string buffer.
695 * @return @c rdbuf()->str()
696 */
697 __string_type
698 str() const _GLIBCXX_LVAL_REF_QUAL
699 { return _M_stringbuf.str(); }
700
701#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
702#if __cpp_concepts
703 template<__allocator_like _SAlloc>
704 basic_string<_CharT, _Traits, _SAlloc>
705 str(const _SAlloc& __sa) const
706 { return _M_stringbuf.str(__sa); }
707#endif
708
709 __string_type
710 str() &&
711 { return std::move(_M_stringbuf).str(); }
712
713 basic_string_view<char_type, traits_type>
714 view() const noexcept
715 { return _M_stringbuf.view(); }
716#endif
717
718 /**
719 * @brief Setting a new buffer.
720 * @param __s The string to use as a new sequence.
721 *
722 * Calls @c rdbuf()->str(s).
723 */
724 void
725 str(const __string_type& __s)
726 { _M_stringbuf.str(__s); }
727
728#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
729#if __cpp_concepts
730 template<__allocator_like _SAlloc>
731 requires (!is_same_v<_SAlloc, _Alloc>)
732 void
733 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
734 { _M_stringbuf.str(__s); }
735#endif
736
737 void
738 str(__string_type&& __s)
739 { _M_stringbuf.str(std::move(__s)); }
740#endif
741 };
742
743
744 // [27.7.3] Template class basic_ostringstream
745 /**
746 * @brief Controlling output for std::string.
747 * @ingroup io
748 *
749 * @tparam _CharT Type of character stream.
750 * @tparam _Traits Traits for character type, defaults to
751 * char_traits<_CharT>.
752 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
753 *
754 * This class supports writing to objects of type std::basic_string,
755 * using the inherited functions from std::basic_ostream. To control
756 * the associated sequence, an instance of std::basic_stringbuf is used,
757 * which this page refers to as @c sb.
758 */
759 template <typename _CharT, typename _Traits, typename _Alloc>
760 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
761 {
762 public:
763 // Types:
764 typedef _CharT char_type;
765 typedef _Traits traits_type;
766 // _GLIBCXX_RESOLVE_LIB_DEFECTS
767 // 251. basic_stringbuf missing allocator_type
768 typedef _Alloc allocator_type;
769 typedef typename traits_type::int_type int_type;
770 typedef typename traits_type::pos_type pos_type;
771 typedef typename traits_type::off_type off_type;
772
773 // Non-standard types:
774 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
775 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
776 typedef basic_ostream<char_type, traits_type> __ostream_type;
777
778 private:
779 __stringbuf_type _M_stringbuf;
780
781 public:
782 // Constructors/destructor:
783
784 /**
785 * @brief Default constructor starts with an empty string buffer.
786 *
787 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
788 * class initializer. Does not allocate any buffer.
789 *
790 * That's a lie. We initialize the base class with NULL, because the
791 * string class does its own memory management.
792 */
793 basic_ostringstream()
794 : __ostream_type(), _M_stringbuf(ios_base::out)
795 { this->init(&_M_stringbuf); }
796
797 /**
798 * @brief Starts with an empty string buffer.
799 * @param __mode Whether the buffer can read, or write, or both.
800 *
801 * @c ios_base::out is automatically included in @a mode.
802 *
803 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
804 * class initializer. Does not allocate any buffer.
805 *
806 * That's a lie. We initialize the base class with NULL, because the
807 * string class does its own memory management.
808 */
809 explicit
810 basic_ostringstream(ios_base::openmode __mode)
811 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
812 { this->init(&_M_stringbuf); }
813
814 /**
815 * @brief Starts with an existing string buffer.
816 * @param __str A string to copy as a starting buffer.
817 * @param __mode Whether the buffer can read, or write, or both.
818 *
819 * @c ios_base::out is automatically included in @a mode.
820 *
821 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
822 * to the base class initializer.
823 *
824 * That's a lie. We initialize the base class with NULL, because the
825 * string class does its own memory management.
826 */
827 explicit
828 basic_ostringstream(const __string_type& __str,
829 ios_base::openmode __mode = ios_base::out)
830 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
831 { this->init(&_M_stringbuf); }
832
833 /**
834 * @brief The destructor does nothing.
835 *
836 * The buffer is deallocated by the stringbuf object, not the
837 * formatting stream.
838 */
839 ~basic_ostringstream()
840 { }
841
842#if __cplusplus >= 201103L
843 basic_ostringstream(const basic_ostringstream&) = delete;
844
845 basic_ostringstream(basic_ostringstream&& __rhs)
846 : __ostream_type(std::move(__rhs)),
847 _M_stringbuf(std::move(__rhs._M_stringbuf))
848 { __ostream_type::set_rdbuf(&_M_stringbuf); }
849
850#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
851 basic_ostringstream(ios_base::openmode __mode, const allocator_type& __a)
852 : __ostream_type(), _M_stringbuf(__mode | ios_base::out, __a)
853 { this->init(std::__addressof(_M_stringbuf)); }
854
855 explicit
856 basic_ostringstream(__string_type&& __str,
857 ios_base::openmode __mode = ios_base::out)
858 : __ostream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::out)
859 { this->init(std::__addressof(_M_stringbuf)); }
860
861 template<typename _SAlloc>
862 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
863 const allocator_type& __a)
864 : basic_ostringstream(__str, ios_base::out, __a)
865 { }
866
867 template<typename _SAlloc>
868 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
869 ios_base::openmode __mode,
870 const allocator_type& __a)
871 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out, __a)
872 { this->init(std::__addressof(_M_stringbuf)); }
873
874 template<typename _SAlloc>
875 explicit
876 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
877 ios_base::openmode __mode = ios_base::out)
878 : basic_ostringstream(__str, __mode, allocator_type())
879 { }
880#endif // C++20
881
882 // 27.8.3.2 Assign and swap:
883
884 basic_ostringstream&
885 operator=(const basic_ostringstream&) = delete;
886
887 basic_ostringstream&
888 operator=(basic_ostringstream&& __rhs)
889 {
890 __ostream_type::operator=(std::move(__rhs));
891 _M_stringbuf = std::move(__rhs._M_stringbuf);
892 return *this;
893 }
894
895 void
896 swap(basic_ostringstream& __rhs)
897 {
898 __ostream_type::swap(__rhs);
899 _M_stringbuf.swap(__rhs._M_stringbuf);
900 }
901#endif // C++11
902
903 // Members:
904 /**
905 * @brief Accessing the underlying buffer.
906 * @return The current basic_stringbuf buffer.
907 *
908 * This hides both signatures of std::basic_ios::rdbuf().
909 */
910 __stringbuf_type*
911 rdbuf() const
912 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
913
914 /**
915 * @brief Copying out the string buffer.
916 * @return @c rdbuf()->str()
917 */
918 __string_type
919 str() const _GLIBCXX_LVAL_REF_QUAL
920 { return _M_stringbuf.str(); }
921
922#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
923#if __cpp_concepts
924 template<__allocator_like _SAlloc>
925 basic_string<_CharT, _Traits, _SAlloc>
926 str(const _SAlloc& __sa) const
927 { return _M_stringbuf.str(__sa); }
928#endif
929
930 __string_type
931 str() &&
932 { return std::move(_M_stringbuf).str(); }
933
934 basic_string_view<char_type, traits_type>
935 view() const noexcept
936 { return _M_stringbuf.view(); }
937#endif
938
939 /**
940 * @brief Setting a new buffer.
941 * @param __s The string to use as a new sequence.
942 *
943 * Calls @c rdbuf()->str(s).
944 */
945 void
946 str(const __string_type& __s)
947 { _M_stringbuf.str(__s); }
948
949#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
950#if __cpp_concepts
951 template<__allocator_like _SAlloc>
952 requires (!is_same_v<_SAlloc, _Alloc>)
953 void
954 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
955 { _M_stringbuf.str(__s); }
956#endif
957
958 void
959 str(__string_type&& __s)
960 { _M_stringbuf.str(std::move(__s)); }
961#endif
962 };
963
964
965 // [27.7.4] Template class basic_stringstream
966 /**
967 * @brief Controlling input and output for std::string.
968 * @ingroup io
969 *
970 * @tparam _CharT Type of character stream.
971 * @tparam _Traits Traits for character type, defaults to
972 * char_traits<_CharT>.
973 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
974 *
975 * This class supports reading from and writing to objects of type
976 * std::basic_string, using the inherited functions from
977 * std::basic_iostream. To control the associated sequence, an instance
978 * of std::basic_stringbuf is used, which this page refers to as @c sb.
979 */
980 template <typename _CharT, typename _Traits, typename _Alloc>
981 class basic_stringstream : public basic_iostream<_CharT, _Traits>
982 {
983 public:
984 // Types:
985 typedef _CharT char_type;
986 typedef _Traits traits_type;
987 // _GLIBCXX_RESOLVE_LIB_DEFECTS
988 // 251. basic_stringbuf missing allocator_type
989 typedef _Alloc allocator_type;
990 typedef typename traits_type::int_type int_type;
991 typedef typename traits_type::pos_type pos_type;
992 typedef typename traits_type::off_type off_type;
993
994 // Non-standard Types:
995 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
996 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
997 typedef basic_iostream<char_type, traits_type> __iostream_type;
998
999 private:
1000 __stringbuf_type _M_stringbuf;
1001
1002 public:
1003 // Constructors/destructors
1004
1005 /**
1006 * @brief Default constructor starts with an empty string buffer.
1007 *
1008 * Initializes @c sb using the mode @c in|out, and passes @c &sb
1009 * to the base class initializer. Does not allocate any buffer.
1010 *
1011 * That's a lie. We initialize the base class with NULL, because the
1012 * string class does its own memory management.
1013 */
1014 basic_stringstream()
1015 : __iostream_type(), _M_stringbuf(ios_base::out | ios_base::in)
1016 { this->init(&_M_stringbuf); }
1017
1018 /**
1019 * @brief Starts with an empty string buffer.
1020 * @param __m Whether the buffer can read, or write, or both.
1021 *
1022 * Initializes @c sb using the mode from @c __m, and passes @c &sb
1023 * to the base class initializer. Does not allocate any buffer.
1024 *
1025 * That's a lie. We initialize the base class with NULL, because the
1026 * string class does its own memory management.
1027 */
1028 explicit
1029 basic_stringstream(ios_base::openmode __m)
1030 : __iostream_type(), _M_stringbuf(__m)
1031 { this->init(&_M_stringbuf); }
1032
1033 /**
1034 * @brief Starts with an existing string buffer.
1035 * @param __str A string to copy as a starting buffer.
1036 * @param __m Whether the buffer can read, or write, or both.
1037 *
1038 * Initializes @c sb using @a __str and @c __m, and passes @c &sb
1039 * to the base class initializer.
1040 *
1041 * That's a lie. We initialize the base class with NULL, because the
1042 * string class does its own memory management.
1043 */
1044 explicit
1045 basic_stringstream(const __string_type& __str,
1046 ios_base::openmode __m = ios_base::out | ios_base::in)
1047 : __iostream_type(), _M_stringbuf(__str, __m)
1048 { this->init(&_M_stringbuf); }
1049
1050 /**
1051 * @brief The destructor does nothing.
1052 *
1053 * The buffer is deallocated by the stringbuf object, not the
1054 * formatting stream.
1055 */
1056 ~basic_stringstream()
1057 { }
1058
1059#if __cplusplus >= 201103L
1060 basic_stringstream(const basic_stringstream&) = delete;
1061
1062 basic_stringstream(basic_stringstream&& __rhs)
1063 : __iostream_type(std::move(__rhs)),
1064 _M_stringbuf(std::move(__rhs._M_stringbuf))
1065 { __iostream_type::set_rdbuf(&_M_stringbuf); }
1066
1067#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1068 basic_stringstream(ios_base::openmode __mode, const allocator_type& __a)
1069 : __iostream_type(), _M_stringbuf(__mode, __a)
1070 { this->init(&_M_stringbuf); }
1071
1072 explicit
1073 basic_stringstream(__string_type&& __str,
1074 ios_base::openmode __mode = ios_base::in
1075 | ios_base::out)
1076 : __iostream_type(), _M_stringbuf(std::move(__str), __mode)
1077 { this->init(std::__addressof(_M_stringbuf)); }
1078
1079 template<typename _SAlloc>
1080 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1081 const allocator_type& __a)
1082 : basic_stringstream(__str, ios_base::in | ios_base::out, __a)
1083 { }
1084
1085 template<typename _SAlloc>
1086 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1087 ios_base::openmode __mode,
1088 const allocator_type& __a)
1089 : __iostream_type(), _M_stringbuf(__str, __mode, __a)
1090 { this->init(std::__addressof(_M_stringbuf)); }
1091
1092 template<typename _SAlloc>
1093 explicit
1094 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1095 ios_base::openmode __mode = ios_base::in
1096 | ios_base::out)
1097 : basic_stringstream(__str, __mode, allocator_type())
1098 { }
1099#endif // C++20
1100
1101 // 27.8.3.2 Assign and swap:
1102
1103 basic_stringstream&
1104 operator=(const basic_stringstream&) = delete;
1105
1106 basic_stringstream&
1107 operator=(basic_stringstream&& __rhs)
1108 {
1109 __iostream_type::operator=(std::move(__rhs));
1110 _M_stringbuf = std::move(__rhs._M_stringbuf);
1111 return *this;
1112 }
1113
1114 void
1115 swap(basic_stringstream& __rhs)
1116 {
1117 __iostream_type::swap(__rhs);
1118 _M_stringbuf.swap(__rhs._M_stringbuf);
1119 }
1120#endif // C++11
1121
1122 // Members:
1123 /**
1124 * @brief Accessing the underlying buffer.
1125 * @return The current basic_stringbuf buffer.
1126 *
1127 * This hides both signatures of std::basic_ios::rdbuf().
1128 */
1129 __stringbuf_type*
1130 rdbuf() const
1131 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
1132
1133 /**
1134 * @brief Copying out the string buffer.
1135 * @return @c rdbuf()->str()
1136 */
1137 __string_type
1138 str() const _GLIBCXX_LVAL_REF_QUAL
1139 { return _M_stringbuf.str(); }
1140
1141#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1142#if __cpp_concepts
1143 template<__allocator_like _SAlloc>
1144 basic_string<_CharT, _Traits, _SAlloc>
1145 str(const _SAlloc& __sa) const
1146 { return _M_stringbuf.str(__sa); }
1147#endif
1148
1149 __string_type
1150 str() &&
1151 { return std::move(_M_stringbuf).str(); }
1152
1153 basic_string_view<char_type, traits_type>
1154 view() const noexcept
1155 { return _M_stringbuf.view(); }
1156#endif
1157
1158 /**
1159 * @brief Setting a new buffer.
1160 * @param __s The string to use as a new sequence.
1161 *
1162 * Calls @c rdbuf()->str(s).
1163 */
1164 void
1165 str(const __string_type& __s)
1166 { _M_stringbuf.str(__s); }
1167
1168#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1169#if __cpp_concepts
1170 template<__allocator_like _SAlloc>
1171 requires (!is_same_v<_SAlloc, _Alloc>)
1172 void
1173 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
1174 { _M_stringbuf.str(__s); }
1175#endif
1176
1177 void
1178 str(__string_type&& __s)
1179 { _M_stringbuf.str(std::move(__s)); }
1180#endif
1181 };
1182
1183#if __cplusplus >= 201103L
1184 /// Swap specialization for stringbufs.
1185 template <class _CharT, class _Traits, class _Allocator>
1186 inline void
1187 swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
1188 basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
1189 noexcept(noexcept(__x.swap(__y)))
1190 { __x.swap(__y); }
1191
1192 /// Swap specialization for istringstreams.
1193 template <class _CharT, class _Traits, class _Allocator>
1194 inline void
1195 swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
1196 basic_istringstream<_CharT, _Traits, _Allocator>& __y)
1197 { __x.swap(__y); }
1198
1199 /// Swap specialization for ostringstreams.
1200 template <class _CharT, class _Traits, class _Allocator>
1201 inline void
1202 swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
1203 basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
1204 { __x.swap(__y); }
1205
1206 /// Swap specialization for stringstreams.
1207 template <class _CharT, class _Traits, class _Allocator>
1208 inline void
1209 swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
1210 basic_stringstream<_CharT, _Traits, _Allocator>& __y)
1211 { __x.swap(__y); }
1212#endif // C++11
1213
1214_GLIBCXX_END_NAMESPACE_CXX11
1215_GLIBCXX_END_NAMESPACE_VERSION
1216} // namespace
1217
1218#undef _GLIBCXX_LVAL_REF_QUAL
1219
1220#include <bits/sstream.tcc>
1221
1222#endif /* _GLIBCXX_SSTREAM */