1// String based streams -*- C++ -*-
3// Copyright (C) 1997-2023 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25/** @file include/sstream
26 * This is a Standard C++ Library header.
30// ISO C++ 14882: 27.7 String-based streams
33#ifndef _GLIBCXX_SSTREAM
34#define _GLIBCXX_SSTREAM 1
36#pragma GCC system_header
38#include <bits/requires_hosted.h> // iostream
42#include <bits/alloc_traits.h> // allocator_traits, __allocator_like
44#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
45# define _GLIBCXX_LVAL_REF_QUAL &
47# define _GLIBCXX_LVAL_REF_QUAL
50namespace std _GLIBCXX_VISIBILITY(default)
52_GLIBCXX_BEGIN_NAMESPACE_VERSION
53_GLIBCXX_BEGIN_NAMESPACE_CXX11
55 // [27.7.1] template class basic_stringbuf
57 * @brief The actual work of input and output (for std::string).
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>.
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.)
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.
73 template<typename _CharT, typename _Traits, typename _Alloc>
74 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
76 struct __xfer_bufptrs;
78#if __cplusplus >= 201103L
79 using allocator_traits = std::allocator_traits<_Alloc>;
81 = __or_<typename allocator_traits::propagate_on_container_swap,
82 typename allocator_traits::is_always_equal>;
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;
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;
101 /// Place to stash in || out || in | out settings for current stringbuf.
102 ios_base::openmode _M_mode;
105 __string_type _M_string;
111 * @brief Starts with an empty string buffer.
113 * The default constructor initializes the parent class using its
117 : __streambuf_type(), _M_mode(ios_base::in | ios_base::out), _M_string()
121 * @brief Starts with an empty string buffer.
122 * @param __mode Whether the buffer can read, or write, or both.
124 * The default constructor initializes the parent class using its
128 basic_stringbuf(ios_base::openmode __mode)
129 : __streambuf_type(), _M_mode(__mode), _M_string()
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.
137 * This constructor initializes the parent class using its
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); }
147#if __cplusplus >= 201103L
148 basic_stringbuf(const basic_stringbuf&) = delete;
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); }
154#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
156 basic_stringbuf(const allocator_type& __a)
157 : basic_stringbuf(ios_base::in | std::ios_base::out, __a)
160 basic_stringbuf(ios_base::openmode __mode,
161 const allocator_type& __a)
162 : __streambuf_type(), _M_mode(__mode), _M_string(__a)
166 basic_stringbuf(__string_type&& __s,
167 ios_base::openmode __mode = ios_base::in
169 : __streambuf_type(), _M_mode(__mode), _M_string(std::move(__s))
170 { _M_stringbuf_init(__mode); }
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)
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); }
186 template<typename _SAlloc>
188 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
189 ios_base::openmode __mode = ios_base::in
191 : basic_stringbuf(__s, __mode, allocator_type{})
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); }
198 allocator_type get_allocator() const noexcept
199 { return _M_string.get_allocator(); }
202 // 27.8.2.2 Assign and swap:
205 operator=(const basic_stringbuf&) = delete;
208 operator=(basic_stringbuf&& __rhs)
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);
221 swap(basic_stringbuf& __rhs) noexcept(_Noexcept_swap::value)
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
233 // Getters and setters:
236 * @brief Copying out the string buffer.
237 * @return A copy of one of the underlying sequences.
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
244 str() const _GLIBCXX_LVAL_REF_QUAL
246 __string_type __ret(_M_string.get_allocator());
247 if (char_type* __hi = _M_high_mark())
248 __ret.assign(this->pbase(), __hi);
254#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
256 template<__allocator_like _SAlloc>
257 basic_string<_CharT, _Traits, _SAlloc>
258 str(const _SAlloc& __sa) const
261 return { __sv.data(), __sv.size(), __sa };
268 if (char_type* __hi = _M_high_mark())
270 // Set length to end of character sequence and add null terminator.
271 _M_string._M_set_length(_M_high_mark() - this->pbase());
273 auto __str = std::move(_M_string);
275 _M_sync(_M_string.data(), 0, 0);
279 basic_string_view<char_type, traits_type>
280 view() const noexcept
282 if (char_type* __hi = _M_high_mark())
283 return { this->pbase(), __hi };
290 * @brief Setting a new buffer.
291 * @param __s The string to use as a new sequence.
293 * Deallocates any previous stored sequence, then copies @a s to
297 str(const __string_type& __s)
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);
305#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
307 template<__allocator_like _SAlloc>
308 requires (!is_same_v<_SAlloc, _Alloc>)
310 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
312 _M_string.assign(__s.data(), __s.size());
313 _M_stringbuf_init(_M_mode);
318 str(__string_type&& __s)
320 _M_string = std::move(__s);
321 _M_stringbuf_init(_M_mode);
326 // Common initialization code goes here.
328 _M_stringbuf_init(ios_base::openmode __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);
340 streamsize __ret = -1;
341 if (_M_mode & ios_base::in)
344 __ret = this->egptr() - this->gptr();
353 pbackfail(int_type __c = traits_type::eof());
356 overflow(int_type __c = traits_type::eof());
359 * @brief Manipulates the buffer.
360 * @param __s Pointer to a buffer area.
361 * @param __n Size of @a __s.
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
369 virtual __streambuf_type*
370 setbuf(char_type* __s, streamsize __n)
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.
379 // Step 1: Destroy the current internal array.
382 // Step 2: Use the external array.
383 _M_sync(__s, __n, 0);
389 seekoff(off_type __off, ios_base::seekdir __way,
390 ios_base::openmode __mode = ios_base::in | ios_base::out);
393 seekpos(pos_type __sp,
394 ios_base::openmode __mode = ios_base::in | ios_base::out);
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.
400 _M_sync(char_type* __base, __size_type __i, __size_type __o);
402 // Internal function for correctly updating egptr() to the actual
407 if (char_type* __pptr = this->pptr())
409 char_type* __egptr = this->egptr();
410 if (!__egptr || __pptr > __egptr)
412 if (_M_mode & ios_base::in)
413 this->setg(this->eback(), this->gptr(), __pptr);
415 this->setg(__pptr, __pptr, __pptr);
420 // Works around the issue with pbump, part of the protected
421 // interface of basic_streambuf, taking just an int.
423 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off);
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__))
432 _M_high_mark() const _GLIBCXX_NOEXCEPT
434 if (char_type* __pptr = this->pptr())
436 char_type* __egptr = this->egptr();
437 if (!__egptr || __pptr > __egptr)
438 return __pptr; // Underlying sequence is [pbase, pptr).
440 return __egptr; // Underlying sequence is [pbase, egptr).
442 return 0; // Underlying character sequence is just _M_string.
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
451 __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to)
452 : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1}
454 const _CharT* const __str = __from._M_string.data();
455 const _CharT* __end = nullptr;
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();
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();
472 // Set _M_string length to the greater of the get and put areas.
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);
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]);
491 basic_stringbuf* _M_to;
496 // This type does nothing when using Copy-On-Write strings.
497 struct __xfer_bufptrs
499 __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { }
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))
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,
515 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
516 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string), __a)
523 // [27.7.2] Template class basic_istringstream
525 * @brief Controlling input for std::string.
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>.
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.
538 template<typename _CharT, typename _Traits, typename _Alloc>
539 class basic_istringstream : public basic_istream<_CharT, _Traits>
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;
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;
558 __stringbuf_type _M_stringbuf;
564 * @brief Default constructor starts with an empty string buffer.
566 * Initializes @c sb using @c in, and passes @c &sb to the base
567 * class initializer. Does not allocate any buffer.
569 * That's a lie. We initialize the base class with NULL, because the
570 * string class does its own memory management.
572 basic_istringstream()
573 : __istream_type(), _M_stringbuf(ios_base::in)
574 { this->init(&_M_stringbuf); }
577 * @brief Starts with an empty string buffer.
578 * @param __mode Whether the buffer can read, or write, or both.
580 * @c ios_base::in is automatically included in @a __mode.
582 * Initializes @c sb using @c __mode|in, and passes @c &sb to the base
583 * class initializer. Does not allocate any buffer.
585 * That's a lie. We initialize the base class with NULL, because the
586 * string class does its own memory management.
589 basic_istringstream(ios_base::openmode __mode)
590 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
591 { this->init(&_M_stringbuf); }
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.
598 * @c ios_base::in is automatically included in @a mode.
600 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
601 * to the base class initializer.
603 * That's a lie. We initialize the base class with NULL, because the
604 * string class does its own memory management.
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); }
613 * @brief The destructor does nothing.
615 * The buffer is deallocated by the stringbuf object, not the
618 ~basic_istringstream()
621#if __cplusplus >= 201103L
622 basic_istringstream(const basic_istringstream&) = delete;
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); }
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)); }
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)); }
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)
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)); }
653 template<typename _SAlloc>
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())
661 // 27.8.3.2 Assign and swap:
664 operator=(const basic_istringstream&) = delete;
667 operator=(basic_istringstream&& __rhs)
669 __istream_type::operator=(std::move(__rhs));
670 _M_stringbuf = std::move(__rhs._M_stringbuf);
675 swap(basic_istringstream& __rhs)
677 __istream_type::swap(__rhs);
678 _M_stringbuf.swap(__rhs._M_stringbuf);
684 * @brief Accessing the underlying buffer.
685 * @return The current basic_stringbuf buffer.
687 * This hides both signatures of std::basic_ios::rdbuf().
691 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
694 * @brief Copying out the string buffer.
695 * @return @c rdbuf()->str()
698 str() const _GLIBCXX_LVAL_REF_QUAL
699 { return _M_stringbuf.str(); }
701#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
703 template<__allocator_like _SAlloc>
704 basic_string<_CharT, _Traits, _SAlloc>
705 str(const _SAlloc& __sa) const
706 { return _M_stringbuf.str(__sa); }
711 { return std::move(_M_stringbuf).str(); }
713 basic_string_view<char_type, traits_type>
714 view() const noexcept
715 { return _M_stringbuf.view(); }
719 * @brief Setting a new buffer.
720 * @param __s The string to use as a new sequence.
722 * Calls @c rdbuf()->str(s).
725 str(const __string_type& __s)
726 { _M_stringbuf.str(__s); }
728#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
730 template<__allocator_like _SAlloc>
731 requires (!is_same_v<_SAlloc, _Alloc>)
733 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
734 { _M_stringbuf.str(__s); }
738 str(__string_type&& __s)
739 { _M_stringbuf.str(std::move(__s)); }
744 // [27.7.3] Template class basic_ostringstream
746 * @brief Controlling output for std::string.
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>.
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.
759 template <typename _CharT, typename _Traits, typename _Alloc>
760 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
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;
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;
779 __stringbuf_type _M_stringbuf;
782 // Constructors/destructor:
785 * @brief Default constructor starts with an empty string buffer.
787 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
788 * class initializer. Does not allocate any buffer.
790 * That's a lie. We initialize the base class with NULL, because the
791 * string class does its own memory management.
793 basic_ostringstream()
794 : __ostream_type(), _M_stringbuf(ios_base::out)
795 { this->init(&_M_stringbuf); }
798 * @brief Starts with an empty string buffer.
799 * @param __mode Whether the buffer can read, or write, or both.
801 * @c ios_base::out is automatically included in @a mode.
803 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
804 * class initializer. Does not allocate any buffer.
806 * That's a lie. We initialize the base class with NULL, because the
807 * string class does its own memory management.
810 basic_ostringstream(ios_base::openmode __mode)
811 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
812 { this->init(&_M_stringbuf); }
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.
819 * @c ios_base::out is automatically included in @a mode.
821 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
822 * to the base class initializer.
824 * That's a lie. We initialize the base class with NULL, because the
825 * string class does its own memory management.
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); }
834 * @brief The destructor does nothing.
836 * The buffer is deallocated by the stringbuf object, not the
839 ~basic_ostringstream()
842#if __cplusplus >= 201103L
843 basic_ostringstream(const basic_ostringstream&) = delete;
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); }
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)); }
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)); }
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)
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)); }
874 template<typename _SAlloc>
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())
882 // 27.8.3.2 Assign and swap:
885 operator=(const basic_ostringstream&) = delete;
888 operator=(basic_ostringstream&& __rhs)
890 __ostream_type::operator=(std::move(__rhs));
891 _M_stringbuf = std::move(__rhs._M_stringbuf);
896 swap(basic_ostringstream& __rhs)
898 __ostream_type::swap(__rhs);
899 _M_stringbuf.swap(__rhs._M_stringbuf);
905 * @brief Accessing the underlying buffer.
906 * @return The current basic_stringbuf buffer.
908 * This hides both signatures of std::basic_ios::rdbuf().
912 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
915 * @brief Copying out the string buffer.
916 * @return @c rdbuf()->str()
919 str() const _GLIBCXX_LVAL_REF_QUAL
920 { return _M_stringbuf.str(); }
922#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
924 template<__allocator_like _SAlloc>
925 basic_string<_CharT, _Traits, _SAlloc>
926 str(const _SAlloc& __sa) const
927 { return _M_stringbuf.str(__sa); }
932 { return std::move(_M_stringbuf).str(); }
934 basic_string_view<char_type, traits_type>
935 view() const noexcept
936 { return _M_stringbuf.view(); }
940 * @brief Setting a new buffer.
941 * @param __s The string to use as a new sequence.
943 * Calls @c rdbuf()->str(s).
946 str(const __string_type& __s)
947 { _M_stringbuf.str(__s); }
949#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
951 template<__allocator_like _SAlloc>
952 requires (!is_same_v<_SAlloc, _Alloc>)
954 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
955 { _M_stringbuf.str(__s); }
959 str(__string_type&& __s)
960 { _M_stringbuf.str(std::move(__s)); }
965 // [27.7.4] Template class basic_stringstream
967 * @brief Controlling input and output for std::string.
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>.
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.
980 template <typename _CharT, typename _Traits, typename _Alloc>
981 class basic_stringstream : public basic_iostream<_CharT, _Traits>
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;
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;
1000 __stringbuf_type _M_stringbuf;
1003 // Constructors/destructors
1006 * @brief Default constructor starts with an empty string buffer.
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.
1011 * That's a lie. We initialize the base class with NULL, because the
1012 * string class does its own memory management.
1014 basic_stringstream()
1015 : __iostream_type(), _M_stringbuf(ios_base::out | ios_base::in)
1016 { this->init(&_M_stringbuf); }
1019 * @brief Starts with an empty string buffer.
1020 * @param __m Whether the buffer can read, or write, or both.
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.
1025 * That's a lie. We initialize the base class with NULL, because the
1026 * string class does its own memory management.
1029 basic_stringstream(ios_base::openmode __m)
1030 : __iostream_type(), _M_stringbuf(__m)
1031 { this->init(&_M_stringbuf); }
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.
1038 * Initializes @c sb using @a __str and @c __m, and passes @c &sb
1039 * to the base class initializer.
1041 * That's a lie. We initialize the base class with NULL, because the
1042 * string class does its own memory management.
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); }
1051 * @brief The destructor does nothing.
1053 * The buffer is deallocated by the stringbuf object, not the
1054 * formatting stream.
1056 ~basic_stringstream()
1059#if __cplusplus >= 201103L
1060 basic_stringstream(const basic_stringstream&) = delete;
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); }
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); }
1073 basic_stringstream(__string_type&& __str,
1074 ios_base::openmode __mode = ios_base::in
1076 : __iostream_type(), _M_stringbuf(std::move(__str), __mode)
1077 { this->init(std::__addressof(_M_stringbuf)); }
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)
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)); }
1092 template<typename _SAlloc>
1094 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1095 ios_base::openmode __mode = ios_base::in
1097 : basic_stringstream(__str, __mode, allocator_type())
1101 // 27.8.3.2 Assign and swap:
1104 operator=(const basic_stringstream&) = delete;
1107 operator=(basic_stringstream&& __rhs)
1109 __iostream_type::operator=(std::move(__rhs));
1110 _M_stringbuf = std::move(__rhs._M_stringbuf);
1115 swap(basic_stringstream& __rhs)
1117 __iostream_type::swap(__rhs);
1118 _M_stringbuf.swap(__rhs._M_stringbuf);
1124 * @brief Accessing the underlying buffer.
1125 * @return The current basic_stringbuf buffer.
1127 * This hides both signatures of std::basic_ios::rdbuf().
1131 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
1134 * @brief Copying out the string buffer.
1135 * @return @c rdbuf()->str()
1138 str() const _GLIBCXX_LVAL_REF_QUAL
1139 { return _M_stringbuf.str(); }
1141#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1143 template<__allocator_like _SAlloc>
1144 basic_string<_CharT, _Traits, _SAlloc>
1145 str(const _SAlloc& __sa) const
1146 { return _M_stringbuf.str(__sa); }
1151 { return std::move(_M_stringbuf).str(); }
1153 basic_string_view<char_type, traits_type>
1154 view() const noexcept
1155 { return _M_stringbuf.view(); }
1159 * @brief Setting a new buffer.
1160 * @param __s The string to use as a new sequence.
1162 * Calls @c rdbuf()->str(s).
1165 str(const __string_type& __s)
1166 { _M_stringbuf.str(__s); }
1168#if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1170 template<__allocator_like _SAlloc>
1171 requires (!is_same_v<_SAlloc, _Alloc>)
1173 str(const basic_string<_CharT, _Traits, _SAlloc>& __s)
1174 { _M_stringbuf.str(__s); }
1178 str(__string_type&& __s)
1179 { _M_stringbuf.str(std::move(__s)); }
1183#if __cplusplus >= 201103L
1184 /// Swap specialization for stringbufs.
1185 template <class _CharT, class _Traits, class _Allocator>
1187 swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
1188 basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
1189 noexcept(noexcept(__x.swap(__y)))
1192 /// Swap specialization for istringstreams.
1193 template <class _CharT, class _Traits, class _Allocator>
1195 swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
1196 basic_istringstream<_CharT, _Traits, _Allocator>& __y)
1199 /// Swap specialization for ostringstreams.
1200 template <class _CharT, class _Traits, class _Allocator>
1202 swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
1203 basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
1206 /// Swap specialization for stringstreams.
1207 template <class _CharT, class _Traits, class _Allocator>
1209 swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
1210 basic_stringstream<_CharT, _Traits, _Allocator>& __y)
1214_GLIBCXX_END_NAMESPACE_CXX11
1215_GLIBCXX_END_NAMESPACE_VERSION
1218#undef _GLIBCXX_LVAL_REF_QUAL
1220#include <bits/sstream.tcc>
1222#endif /* _GLIBCXX_SSTREAM */