1 // String based streams -*- C++ -*-
3 // Copyright (C) 1997-2020 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
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 _GLIBCXX_BEGIN_NAMESPACE_CXX11
46 // [27.7.1] template class basic_stringbuf
48 * @brief The actual work of input and output (for std::string).
51 * @tparam _CharT Type of character stream.
52 * @tparam _Traits Traits for character type, defaults to
53 * char_traits<_CharT>.
54 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
56 * This class associates either or both of its input and output sequences
57 * with a sequence of characters, which can be initialized from, or made
58 * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
60 * For this class, open modes (of type @c ios_base::openmode) have
61 * @c in set if the input sequence can be read, and @c out set if the
62 * output sequence can be written.
64 template<typename _CharT, typename _Traits, typename _Alloc>
65 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
67 struct __xfer_bufptrs;
70 typedef _CharT char_type;
71 typedef _Traits traits_type;
72 // _GLIBCXX_RESOLVE_LIB_DEFECTS
73 // 251. basic_stringbuf missing allocator_type
74 typedef _Alloc allocator_type;
75 typedef typename traits_type::int_type int_type;
76 typedef typename traits_type::pos_type pos_type;
77 typedef typename traits_type::off_type off_type;
79 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
80 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
81 typedef typename __string_type::size_type __size_type;
84 /// Place to stash in || out || in | out settings for current stringbuf.
85 ios_base::openmode _M_mode;
88 __string_type _M_string;
94 * @brief Starts with an empty string buffer.
96 * The default constructor initializes the parent class using its
100 : __streambuf_type(), _M_mode(ios_base::in | ios_base::out), _M_string()
104 * @brief Starts with an empty string buffer.
105 * @param __mode Whether the buffer can read, or write, or both.
107 * The default constructor initializes the parent class using its
111 basic_stringbuf(ios_base::openmode __mode)
112 : __streambuf_type(), _M_mode(__mode), _M_string()
116 * @brief Starts with an existing string buffer.
117 * @param __str A string to copy as a starting buffer.
118 * @param __mode Whether the buffer can read, or write, or both.
120 * This constructor initializes the parent class using its
124 basic_stringbuf(const __string_type& __str,
125 ios_base::openmode __mode = ios_base::in | ios_base::out)
126 : __streambuf_type(), _M_mode(),
127 _M_string(__str.data(), __str.size(), __str.get_allocator())
128 { _M_stringbuf_init(__mode); }
130 #if __cplusplus >= 201103L
131 basic_stringbuf(const basic_stringbuf&) = delete;
133 basic_stringbuf(basic_stringbuf&& __rhs)
134 : basic_stringbuf(std::move(__rhs), __xfer_bufptrs(__rhs, this))
135 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
137 // 27.8.2.2 Assign and swap:
140 operator=(const basic_stringbuf&) = delete;
143 operator=(basic_stringbuf&& __rhs)
145 __xfer_bufptrs __st{__rhs, this};
146 const __streambuf_type& __base = __rhs;
147 __streambuf_type::operator=(__base);
148 this->pubimbue(__rhs.getloc());
149 _M_mode = __rhs._M_mode;
150 _M_string = std::move(__rhs._M_string);
151 __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0);
156 swap(basic_stringbuf& __rhs)
158 __xfer_bufptrs __l_st{*this, std::__addressof(__rhs)};
159 __xfer_bufptrs __r_st{__rhs, this};
160 __streambuf_type& __base = __rhs;
161 __streambuf_type::swap(__base);
162 __rhs.pubimbue(this->pubimbue(__rhs.getloc()));
163 std::swap(_M_mode, __rhs._M_mode);
164 std::swap(_M_string, __rhs._M_string);
168 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
169 using __sv_type = basic_string_view<char_type, traits_type>;
171 basic_stringbuf(const allocator_type& __a)
172 : basic_stringbuf(ios_base::in | std::ios_base::out, __a)
175 basic_stringbuf(ios_base::openmode __mode,
176 const allocator_type& __a)
177 : __streambuf_type(), _M_mode(__mode), _M_string(__a)
181 basic_stringbuf(__string_type&& __s,
182 ios_base::openmode __mode = ios_base::in
184 : __streambuf_type(), _M_mode(__mode), _M_string(move(__s))
187 template<typename _SAlloc>
188 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
189 const allocator_type& __a )
190 : basic_stringbuf(__s, ios_base::in | std::ios_base::out, __a )
193 template<typename _SAlloc>
194 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
195 ios_base::openmode __mode,
196 const allocator_type& __a)
197 : __streambuf_type(), _M_mode(__mode),
198 _M_string(static_cast<__sv_type>(__s), __a)
201 template<typename _SAlloc>
203 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
204 ios_base::openmode __mode = ios_base::in
206 : basic_stringbuf(__s, __mode, allocator_type{})
209 basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a)
210 : basic_stringbuf(std::move(__rhs), __a, __xfer_bufptrs(__rhs, this))
211 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
213 allocator_type get_allocator() const noexcept
214 { return _M_string.get_allocator(); }
219 * @brief Copying out the string buffer.
220 * @return A copy of one of the underlying sequences.
222 * <em>If the buffer is only created in input mode, the underlying
223 * character sequence is equal to the input sequence; otherwise, it
224 * is equal to the output sequence.</em> [27.7.1.2]/1
229 __string_type __ret(_M_string.get_allocator());
230 if (char_type* __pptr = this->pptr())
232 char_type* __egptr = this->egptr();
233 // The current egptr() may not be the actual string end.
234 if (!__egptr || __pptr > __egptr)
235 __ret.assign(this->pbase(), __pptr);
237 __ret.assign(this->pbase(), __egptr);
245 * @brief Setting a new buffer.
246 * @param __s The string to use as a new sequence.
248 * Deallocates any previous stored sequence, then copies @a s to
252 str(const __string_type& __s)
254 // Cannot use _M_string = __s, since v3 strings are COW
255 // (not always true now but assign() always works).
256 _M_string.assign(__s.data(), __s.size());
257 _M_stringbuf_init(_M_mode);
260 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
262 view() const noexcept
266 // The current egptr() may not be the actual string end.
267 if (this->pptr() > this->egptr())
268 return __sv_type(this->pbase(), this->pptr());
270 return __sv_type(this->pbase(), this->egptr());
273 return static_cast<__sv_type>(_M_string);
278 // Common initialization code goes here.
280 _M_stringbuf_init(ios_base::openmode __mode)
283 __size_type __len = 0;
284 if (_M_mode & (ios_base::ate | ios_base::app))
285 __len = _M_string.size();
286 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
292 streamsize __ret = -1;
293 if (_M_mode & ios_base::in)
296 __ret = this->egptr() - this->gptr();
305 pbackfail(int_type __c = traits_type::eof());
308 overflow(int_type __c = traits_type::eof());
311 * @brief Manipulates the buffer.
312 * @param __s Pointer to a buffer area.
313 * @param __n Size of @a __s.
316 * If no buffer has already been created, and both @a __s and @a __n are
317 * non-zero, then @c __s is used as a buffer; see
318 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
321 virtual __streambuf_type*
322 setbuf(char_type* __s, streamsize __n)
326 // This is implementation-defined behavior, and assumes
327 // that an external char_type array of length __n exists
328 // and has been pre-allocated. If this is not the case,
329 // things will quickly blow up.
331 // Step 1: Destroy the current internal array.
334 // Step 2: Use the external array.
335 _M_sync(__s, __n, 0);
341 seekoff(off_type __off, ios_base::seekdir __way,
342 ios_base::openmode __mode = ios_base::in | ios_base::out);
345 seekpos(pos_type __sp,
346 ios_base::openmode __mode = ios_base::in | ios_base::out);
348 // Internal function for correctly updating the internal buffer
349 // for a particular _M_string, due to initialization or re-sizing
350 // of an existing _M_string.
352 _M_sync(char_type* __base, __size_type __i, __size_type __o);
354 // Internal function for correctly updating egptr() to the actual
359 const bool __testin = _M_mode & ios_base::in;
360 if (this->pptr() && this->pptr() > this->egptr())
363 this->setg(this->eback(), this->gptr(), this->pptr());
365 this->setg(this->pptr(), this->pptr(), this->pptr());
369 // Works around the issue with pbump, part of the protected
370 // interface of basic_streambuf, taking just an int.
372 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off);
375 #if __cplusplus >= 201103L
376 #if _GLIBCXX_USE_CXX11_ABI
377 // This type captures the state of the gptr / pptr pointers as offsets
378 // so they can be restored in another object after moving the string.
379 struct __xfer_bufptrs
381 __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to)
382 : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1}
384 const _CharT* const __str = __from._M_string.data();
385 const _CharT* __end = nullptr;
388 _M_goff[0] = __from.eback() - __str;
389 _M_goff[1] = __from.gptr() - __str;
390 _M_goff[2] = __from.egptr() - __str;
391 __end = __from.egptr();
395 _M_poff[0] = __from.pbase() - __str;
396 _M_poff[1] = __from.pptr() - __from.pbase();
397 _M_poff[2] = __from.epptr() - __str;
398 if (__from.pptr() > __end)
399 __end = __from.pptr();
402 // Set _M_string length to the greater of the get and put areas.
405 // The const_cast avoids changing this constructor's signature,
406 // because it is exported from the dynamic library.
407 auto& __mut_from = const_cast<basic_stringbuf&>(__from);
408 __mut_from._M_string._M_length(__end - __str);
414 char_type* __str = const_cast<char_type*>(_M_to->_M_string.data());
415 if (_M_goff[0] != -1)
416 _M_to->setg(__str+_M_goff[0], __str+_M_goff[1], __str+_M_goff[2]);
417 if (_M_poff[0] != -1)
418 _M_to->_M_pbump(__str+_M_poff[0], __str+_M_poff[2], _M_poff[1]);
421 basic_stringbuf* _M_to;
426 // This type does nothing when using Copy-On-Write strings.
427 struct __xfer_bufptrs
429 __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { }
433 // The move constructor initializes an __xfer_bufptrs temporary then
434 // delegates to this constructor to performs moves during its lifetime.
435 basic_stringbuf(basic_stringbuf&& __rhs, __xfer_bufptrs&&)
436 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
437 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string))
440 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
441 // The move constructor initializes an __xfer_bufptrs temporary then
442 // delegates to this constructor to performs moves during its lifetime.
443 basic_stringbuf(basic_stringbuf&& __rhs, const allocator_type& __a,
445 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
446 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string), __a)
453 // [27.7.2] Template class basic_istringstream
455 * @brief Controlling input for std::string.
458 * @tparam _CharT Type of character stream.
459 * @tparam _Traits Traits for character type, defaults to
460 * char_traits<_CharT>.
461 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
463 * This class supports reading from objects of type std::basic_string,
464 * using the inherited functions from std::basic_istream. To control
465 * the associated sequence, an instance of std::basic_stringbuf is used,
466 * which this page refers to as @c sb.
468 template<typename _CharT, typename _Traits, typename _Alloc>
469 class basic_istringstream : public basic_istream<_CharT, _Traits>
473 typedef _CharT char_type;
474 typedef _Traits traits_type;
475 // _GLIBCXX_RESOLVE_LIB_DEFECTS
476 // 251. basic_stringbuf missing allocator_type
477 typedef _Alloc allocator_type;
478 typedef typename traits_type::int_type int_type;
479 typedef typename traits_type::pos_type pos_type;
480 typedef typename traits_type::off_type off_type;
482 // Non-standard types:
483 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
484 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
485 typedef basic_istream<char_type, traits_type> __istream_type;
488 __stringbuf_type _M_stringbuf;
494 * @brief Default constructor starts with an empty string buffer.
496 * Initializes @c sb using @c in, and passes @c &sb to the base
497 * class initializer. Does not allocate any buffer.
499 * That's a lie. We initialize the base class with NULL, because the
500 * string class does its own memory management.
502 basic_istringstream()
503 : __istream_type(), _M_stringbuf(ios_base::in)
504 { this->init(&_M_stringbuf); }
507 * @brief Starts with an empty string buffer.
508 * @param __mode Whether the buffer can read, or write, or both.
510 * @c ios_base::in is automatically included in @a __mode.
512 * Initializes @c sb using @c __mode|in, and passes @c &sb to the base
513 * class initializer. Does not allocate any buffer.
515 * That's a lie. We initialize the base class with NULL, because the
516 * string class does its own memory management.
519 basic_istringstream(ios_base::openmode __mode)
520 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
521 { this->init(&_M_stringbuf); }
524 * @brief Starts with an existing string buffer.
525 * @param __str A string to copy as a starting buffer.
526 * @param __mode Whether the buffer can read, or write, or both.
528 * @c ios_base::in is automatically included in @a mode.
530 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
531 * to the base class initializer.
533 * That's a lie. We initialize the base class with NULL, because the
534 * string class does its own memory management.
537 basic_istringstream(const __string_type& __str,
538 ios_base::openmode __mode = ios_base::in)
539 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
540 { this->init(&_M_stringbuf); }
543 * @brief The destructor does nothing.
545 * The buffer is deallocated by the stringbuf object, not the
548 ~basic_istringstream()
551 #if __cplusplus >= 201103L
552 basic_istringstream(const basic_istringstream&) = delete;
554 basic_istringstream(basic_istringstream&& __rhs)
555 : __istream_type(std::move(__rhs)),
556 _M_stringbuf(std::move(__rhs._M_stringbuf))
557 { __istream_type::set_rdbuf(&_M_stringbuf); }
559 // 27.8.3.2 Assign and swap:
562 operator=(const basic_istringstream&) = delete;
565 operator=(basic_istringstream&& __rhs)
567 __istream_type::operator=(std::move(__rhs));
568 _M_stringbuf = std::move(__rhs._M_stringbuf);
573 swap(basic_istringstream& __rhs)
575 __istream_type::swap(__rhs);
576 _M_stringbuf.swap(__rhs._M_stringbuf);
580 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
581 basic_istringstream(ios_base::openmode __mode, const allocator_type& __a)
582 : __istream_type(), _M_stringbuf(__mode | ios_base::in, __a)
583 { this->init(std::__addressof(_M_stringbuf)); }
586 basic_istringstream(__string_type&& __str,
587 ios_base::openmode __mode = ios_base::in )
588 : __istream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::in)
589 { this->init(std::__addressof(_M_stringbuf)); }
591 template<typename _SAlloc>
592 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
593 const allocator_type& __a)
594 : basic_istringstream(__str, ios_base::in, __a)
597 template<typename _SAlloc>
598 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
599 ios_base::openmode __mode,
600 const allocator_type& __a)
602 _M_stringbuf(__string_type(__str.data(), __str.size()),
603 __mode | ios_base::in, __a)
604 { this->init(std::__addressof(_M_stringbuf)); }
606 template<typename _SAlloc>
608 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
609 ios_base::openmode __mode = ios_base::in)
610 : basic_istringstream(__str, __mode, allocator_type())
616 * @brief Accessing the underlying buffer.
617 * @return The current basic_stringbuf buffer.
619 * This hides both signatures of std::basic_ios::rdbuf().
623 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
626 * @brief Copying out the string buffer.
627 * @return @c rdbuf()->str()
631 { return _M_stringbuf.str(); }
634 * @brief Setting a new buffer.
635 * @param __s The string to use as a new sequence.
637 * Calls @c rdbuf()->str(s).
640 str(const __string_type& __s)
641 { _M_stringbuf.str(__s); }
643 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
644 basic_string_view<char_type, traits_type>
645 view() const noexcept
646 { return _M_stringbuf.view(); }
651 // [27.7.3] Template class basic_ostringstream
653 * @brief Controlling output for std::string.
656 * @tparam _CharT Type of character stream.
657 * @tparam _Traits Traits for character type, defaults to
658 * char_traits<_CharT>.
659 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
661 * This class supports writing to objects of type std::basic_string,
662 * using the inherited functions from std::basic_ostream. To control
663 * the associated sequence, an instance of std::basic_stringbuf is used,
664 * which this page refers to as @c sb.
666 template <typename _CharT, typename _Traits, typename _Alloc>
667 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
671 typedef _CharT char_type;
672 typedef _Traits traits_type;
673 // _GLIBCXX_RESOLVE_LIB_DEFECTS
674 // 251. basic_stringbuf missing allocator_type
675 typedef _Alloc allocator_type;
676 typedef typename traits_type::int_type int_type;
677 typedef typename traits_type::pos_type pos_type;
678 typedef typename traits_type::off_type off_type;
680 // Non-standard types:
681 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
682 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
683 typedef basic_ostream<char_type, traits_type> __ostream_type;
686 __stringbuf_type _M_stringbuf;
689 // Constructors/destructor:
692 * @brief Default constructor starts with an empty string buffer.
694 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
695 * class initializer. Does not allocate any buffer.
697 * That's a lie. We initialize the base class with NULL, because the
698 * string class does its own memory management.
700 basic_ostringstream()
701 : __ostream_type(), _M_stringbuf(ios_base::out)
702 { this->init(&_M_stringbuf); }
705 * @brief Starts with an empty string buffer.
706 * @param __mode Whether the buffer can read, or write, or both.
708 * @c ios_base::out is automatically included in @a mode.
710 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
711 * class initializer. Does not allocate any buffer.
713 * That's a lie. We initialize the base class with NULL, because the
714 * string class does its own memory management.
717 basic_ostringstream(ios_base::openmode __mode)
718 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
719 { this->init(&_M_stringbuf); }
722 * @brief Starts with an existing string buffer.
723 * @param __str A string to copy as a starting buffer.
724 * @param __mode Whether the buffer can read, or write, or both.
726 * @c ios_base::out is automatically included in @a mode.
728 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
729 * to the base class initializer.
731 * That's a lie. We initialize the base class with NULL, because the
732 * string class does its own memory management.
735 basic_ostringstream(const __string_type& __str,
736 ios_base::openmode __mode = ios_base::out)
737 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
738 { this->init(&_M_stringbuf); }
741 * @brief The destructor does nothing.
743 * The buffer is deallocated by the stringbuf object, not the
746 ~basic_ostringstream()
749 #if __cplusplus >= 201103L
750 basic_ostringstream(const basic_ostringstream&) = delete;
752 basic_ostringstream(basic_ostringstream&& __rhs)
753 : __ostream_type(std::move(__rhs)),
754 _M_stringbuf(std::move(__rhs._M_stringbuf))
755 { __ostream_type::set_rdbuf(&_M_stringbuf); }
757 // 27.8.3.2 Assign and swap:
760 operator=(const basic_ostringstream&) = delete;
763 operator=(basic_ostringstream&& __rhs)
765 __ostream_type::operator=(std::move(__rhs));
766 _M_stringbuf = std::move(__rhs._M_stringbuf);
771 swap(basic_ostringstream& __rhs)
773 __ostream_type::swap(__rhs);
774 _M_stringbuf.swap(__rhs._M_stringbuf);
778 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
779 basic_ostringstream(ios_base::openmode __mode, const allocator_type& __a)
780 : __ostream_type(), _M_stringbuf(__mode | ios_base::out, __a)
781 { this->init(std::__addressof(_M_stringbuf)); }
784 basic_ostringstream(__string_type&& __str,
785 ios_base::openmode __mode = ios_base::out )
786 : __ostream_type(), _M_stringbuf(std::move(__str), __mode | ios_base::out)
787 { this->init(std::__addressof(_M_stringbuf)); }
789 template<typename _SAlloc>
790 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
791 const allocator_type& __a)
792 : basic_ostringstream(__str, ios_base::out, __a)
795 template<typename _SAlloc>
796 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
797 ios_base::openmode __mode,
798 const allocator_type& __a)
800 _M_stringbuf(__string_type(__str.data(), __str.size()),
801 __mode | ios_base::out, __a)
802 { this->init(std::__addressof(_M_stringbuf)); }
804 template<typename _SAlloc>
806 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
807 ios_base::openmode __mode = ios_base::out)
808 : basic_ostringstream(__str, __mode, allocator_type())
814 * @brief Accessing the underlying buffer.
815 * @return The current basic_stringbuf buffer.
817 * This hides both signatures of std::basic_ios::rdbuf().
821 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
824 * @brief Copying out the string buffer.
825 * @return @c rdbuf()->str()
829 { return _M_stringbuf.str(); }
832 * @brief Setting a new buffer.
833 * @param __s The string to use as a new sequence.
835 * Calls @c rdbuf()->str(s).
838 str(const __string_type& __s)
839 { _M_stringbuf.str(__s); }
841 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
842 basic_string_view<char_type, traits_type>
843 view() const noexcept
844 { return _M_stringbuf.view(); }
849 // [27.7.4] Template class basic_stringstream
851 * @brief Controlling input and output for std::string.
854 * @tparam _CharT Type of character stream.
855 * @tparam _Traits Traits for character type, defaults to
856 * char_traits<_CharT>.
857 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
859 * This class supports reading from and writing to objects of type
860 * std::basic_string, using the inherited functions from
861 * std::basic_iostream. To control the associated sequence, an instance
862 * of std::basic_stringbuf is used, which this page refers to as @c sb.
864 template <typename _CharT, typename _Traits, typename _Alloc>
865 class basic_stringstream : public basic_iostream<_CharT, _Traits>
869 typedef _CharT char_type;
870 typedef _Traits traits_type;
871 // _GLIBCXX_RESOLVE_LIB_DEFECTS
872 // 251. basic_stringbuf missing allocator_type
873 typedef _Alloc allocator_type;
874 typedef typename traits_type::int_type int_type;
875 typedef typename traits_type::pos_type pos_type;
876 typedef typename traits_type::off_type off_type;
878 // Non-standard Types:
879 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
880 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
881 typedef basic_iostream<char_type, traits_type> __iostream_type;
884 __stringbuf_type _M_stringbuf;
887 // Constructors/destructors
890 * @brief Default constructor starts with an empty string buffer.
892 * Initializes @c sb using the mode @c in|out, and passes @c &sb
893 * to the base class initializer. Does not allocate any buffer.
895 * That's a lie. We initialize the base class with NULL, because the
896 * string class does its own memory management.
899 : __iostream_type(), _M_stringbuf(ios_base::out | ios_base::in)
900 { this->init(&_M_stringbuf); }
903 * @brief Starts with an empty string buffer.
904 * @param __m Whether the buffer can read, or write, or both.
906 * Initializes @c sb using the mode from @c __m, and passes @c &sb
907 * to the base class initializer. Does not allocate any buffer.
909 * That's a lie. We initialize the base class with NULL, because the
910 * string class does its own memory management.
913 basic_stringstream(ios_base::openmode __m)
914 : __iostream_type(), _M_stringbuf(__m)
915 { this->init(&_M_stringbuf); }
918 * @brief Starts with an existing string buffer.
919 * @param __str A string to copy as a starting buffer.
920 * @param __m Whether the buffer can read, or write, or both.
922 * Initializes @c sb using @a __str and @c __m, and passes @c &sb
923 * to the base class initializer.
925 * That's a lie. We initialize the base class with NULL, because the
926 * string class does its own memory management.
929 basic_stringstream(const __string_type& __str,
930 ios_base::openmode __m = ios_base::out | ios_base::in)
931 : __iostream_type(), _M_stringbuf(__str, __m)
932 { this->init(&_M_stringbuf); }
935 * @brief The destructor does nothing.
937 * The buffer is deallocated by the stringbuf object, not the
940 ~basic_stringstream()
943 #if __cplusplus >= 201103L
944 basic_stringstream(const basic_stringstream&) = delete;
946 basic_stringstream(basic_stringstream&& __rhs)
947 : __iostream_type(std::move(__rhs)),
948 _M_stringbuf(std::move(__rhs._M_stringbuf))
949 { __iostream_type::set_rdbuf(&_M_stringbuf); }
951 // 27.8.3.2 Assign and swap:
954 operator=(const basic_stringstream&) = delete;
957 operator=(basic_stringstream&& __rhs)
959 __iostream_type::operator=(std::move(__rhs));
960 _M_stringbuf = std::move(__rhs._M_stringbuf);
965 swap(basic_stringstream& __rhs)
967 __iostream_type::swap(__rhs);
968 _M_stringbuf.swap(__rhs._M_stringbuf);
972 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
973 basic_stringstream(ios_base::openmode __mode, const allocator_type& __a)
974 : __iostream_type(), _M_stringbuf(__mode, __a)
975 { this->init(&_M_stringbuf); }
978 basic_stringstream(__string_type&& __str,
979 ios_base::openmode __mode = ios_base::out
981 : __iostream_type(), _M_stringbuf(std::move(__str), __mode)
982 { this->init(std::__addressof(_M_stringbuf)); }
984 template<typename _SAlloc>
985 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
986 const allocator_type& __a)
987 : basic_stringstream(__str, ios_base::in | ios_base::out, __a)
990 template<typename _SAlloc>
991 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
992 ios_base::openmode __mode, const allocator_type& __a)
994 _M_stringbuf(__string_type(__str.data(), __str.size()), __mode, __a)
995 { this->init(std::__addressof(_M_stringbuf)); }
997 template<typename _SAlloc>
999 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1000 ios_base::openmode __mode = ios_base::in
1002 : basic_stringstream(__str, __mode, allocator_type())
1008 * @brief Accessing the underlying buffer.
1009 * @return The current basic_stringbuf buffer.
1011 * This hides both signatures of std::basic_ios::rdbuf().
1015 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
1018 * @brief Copying out the string buffer.
1019 * @return @c rdbuf()->str()
1023 { return _M_stringbuf.str(); }
1026 * @brief Setting a new buffer.
1027 * @param __s The string to use as a new sequence.
1029 * Calls @c rdbuf()->str(s).
1032 str(const __string_type& __s)
1033 { _M_stringbuf.str(__s); }
1035 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
1036 basic_string_view<char_type, traits_type>
1037 view() const noexcept
1038 { return _M_stringbuf.view(); }
1042 #if __cplusplus >= 201103L
1043 /// Swap specialization for stringbufs.
1044 template <class _CharT, class _Traits, class _Allocator>
1046 swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
1047 basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
1050 /// Swap specialization for istringstreams.
1051 template <class _CharT, class _Traits, class _Allocator>
1053 swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
1054 basic_istringstream<_CharT, _Traits, _Allocator>& __y)
1057 /// Swap specialization for ostringstreams.
1058 template <class _CharT, class _Traits, class _Allocator>
1060 swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
1061 basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
1064 /// Swap specialization for stringstreams.
1065 template <class _CharT, class _Traits, class _Allocator>
1067 swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
1068 basic_stringstream<_CharT, _Traits, _Allocator>& __y)
1072 _GLIBCXX_END_NAMESPACE_CXX11
1073 _GLIBCXX_END_NAMESPACE_VERSION
1076 #include <bits/sstream.tcc>
1078 #endif /* _GLIBCXX_SSTREAM */