]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/sstream
libstdc++: Implement C++20 features for <sstream>
[thirdparty/gcc.git] / libstdc++-v3 / include / std / sstream
1 // String based streams -*- C++ -*-
2
3 // Copyright (C) 1997-2020 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 <istream>
39 #include <ostream>
40
41 namespace std _GLIBCXX_VISIBILITY(default)
42 {
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 _GLIBCXX_BEGIN_NAMESPACE_CXX11
45
46 // [27.7.1] template class basic_stringbuf
47 /**
48 * @brief The actual work of input and output (for std::string).
49 * @ingroup io
50 *
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>.
55 *
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.)
59 *
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.
63 */
64 template<typename _CharT, typename _Traits, typename _Alloc>
65 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
66 {
67 struct __xfer_bufptrs;
68 public:
69 // Types:
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;
78
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;
82
83 protected:
84 /// Place to stash in || out || in | out settings for current stringbuf.
85 ios_base::openmode _M_mode;
86
87 // Data Members:
88 __string_type _M_string;
89
90 public:
91 // Constructors:
92
93 /**
94 * @brief Starts with an empty string buffer.
95 *
96 * The default constructor initializes the parent class using its
97 * own default ctor.
98 */
99 basic_stringbuf()
100 : __streambuf_type(), _M_mode(ios_base::in | ios_base::out), _M_string()
101 { }
102
103 /**
104 * @brief Starts with an empty string buffer.
105 * @param __mode Whether the buffer can read, or write, or both.
106 *
107 * The default constructor initializes the parent class using its
108 * own default ctor.
109 */
110 explicit
111 basic_stringbuf(ios_base::openmode __mode)
112 : __streambuf_type(), _M_mode(__mode), _M_string()
113 { }
114
115 /**
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.
119 *
120 * This constructor initializes the parent class using its
121 * own default ctor.
122 */
123 explicit
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); }
129
130 #if __cplusplus >= 201103L
131 basic_stringbuf(const basic_stringbuf&) = delete;
132
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); }
136
137 // 27.8.2.2 Assign and swap:
138
139 basic_stringbuf&
140 operator=(const basic_stringbuf&) = delete;
141
142 basic_stringbuf&
143 operator=(basic_stringbuf&& __rhs)
144 {
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);
152 return *this;
153 }
154
155 void
156 swap(basic_stringbuf& __rhs)
157 {
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);
165 }
166 #endif
167
168 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
169 using __sv_type = basic_string_view<char_type, traits_type>;
170
171 basic_stringbuf(const allocator_type& __a)
172 : basic_stringbuf(ios_base::in | std::ios_base::out, __a)
173 { }
174
175 basic_stringbuf(ios_base::openmode __mode,
176 const allocator_type& __a)
177 : __streambuf_type(), _M_mode(__mode), _M_string(__a)
178 { }
179
180 explicit
181 basic_stringbuf(__string_type&& __s,
182 ios_base::openmode __mode = ios_base::in
183 | ios_base::out )
184 : __streambuf_type(), _M_mode(__mode), _M_string(move(__s))
185 { }
186
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 )
191 { }
192
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)
199 { }
200
201 template<typename _SAlloc>
202 explicit
203 basic_stringbuf(const basic_string<_CharT, _Traits, _SAlloc>& __s,
204 ios_base::openmode __mode = ios_base::in
205 | ios_base::out)
206 : basic_stringbuf(__s, __mode, allocator_type{})
207 { }
208
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); }
212
213 allocator_type get_allocator() const noexcept
214 { return _M_string.get_allocator(); }
215 #endif
216
217 // Get and set:
218 /**
219 * @brief Copying out the string buffer.
220 * @return A copy of one of the underlying sequences.
221 *
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
225 */
226 __string_type
227 str() const
228 {
229 __string_type __ret(_M_string.get_allocator());
230 if (char_type* __pptr = this->pptr())
231 {
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);
236 else
237 __ret.assign(this->pbase(), __egptr);
238 }
239 else
240 __ret = _M_string;
241 return __ret;
242 }
243
244 /**
245 * @brief Setting a new buffer.
246 * @param __s The string to use as a new sequence.
247 *
248 * Deallocates any previous stored sequence, then copies @a s to
249 * use as a new one.
250 */
251 void
252 str(const __string_type& __s)
253 {
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);
258 }
259
260 #if __cplusplus > 201703L && _GLIBCXX_USE_CXX11_ABI
261 __sv_type
262 view() const noexcept
263 {
264 if (this->pptr())
265 {
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());
269 else
270 return __sv_type(this->pbase(), this->egptr());
271 }
272 else
273 return static_cast<__sv_type>(_M_string);
274 }
275 #endif
276
277 protected:
278 // Common initialization code goes here.
279 void
280 _M_stringbuf_init(ios_base::openmode __mode)
281 {
282 _M_mode = __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);
287 }
288
289 virtual streamsize
290 showmanyc()
291 {
292 streamsize __ret = -1;
293 if (_M_mode & ios_base::in)
294 {
295 _M_update_egptr();
296 __ret = this->egptr() - this->gptr();
297 }
298 return __ret;
299 }
300
301 virtual int_type
302 underflow();
303
304 virtual int_type
305 pbackfail(int_type __c = traits_type::eof());
306
307 virtual int_type
308 overflow(int_type __c = traits_type::eof());
309
310 /**
311 * @brief Manipulates the buffer.
312 * @param __s Pointer to a buffer area.
313 * @param __n Size of @a __s.
314 * @return @c this
315 *
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
319 * for more.
320 */
321 virtual __streambuf_type*
322 setbuf(char_type* __s, streamsize __n)
323 {
324 if (__s && __n >= 0)
325 {
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.
330
331 // Step 1: Destroy the current internal array.
332 _M_string.clear();
333
334 // Step 2: Use the external array.
335 _M_sync(__s, __n, 0);
336 }
337 return this;
338 }
339
340 virtual pos_type
341 seekoff(off_type __off, ios_base::seekdir __way,
342 ios_base::openmode __mode = ios_base::in | ios_base::out);
343
344 virtual pos_type
345 seekpos(pos_type __sp,
346 ios_base::openmode __mode = ios_base::in | ios_base::out);
347
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.
351 void
352 _M_sync(char_type* __base, __size_type __i, __size_type __o);
353
354 // Internal function for correctly updating egptr() to the actual
355 // string end.
356 void
357 _M_update_egptr()
358 {
359 const bool __testin = _M_mode & ios_base::in;
360 if (this->pptr() && this->pptr() > this->egptr())
361 {
362 if (__testin)
363 this->setg(this->eback(), this->gptr(), this->pptr());
364 else
365 this->setg(this->pptr(), this->pptr(), this->pptr());
366 }
367 }
368
369 // Works around the issue with pbump, part of the protected
370 // interface of basic_streambuf, taking just an int.
371 void
372 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off);
373
374 private:
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
380 {
381 __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to)
382 : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1}
383 {
384 const _CharT* const __str = __from._M_string.data();
385 const _CharT* __end = nullptr;
386 if (__from.eback())
387 {
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();
392 }
393 if (__from.pbase())
394 {
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();
400 }
401
402 // Set _M_string length to the greater of the get and put areas.
403 if (__end)
404 {
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);
409 }
410 }
411
412 ~__xfer_bufptrs()
413 {
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]);
419 }
420
421 basic_stringbuf* _M_to;
422 off_type _M_goff[3];
423 off_type _M_poff[3];
424 };
425 #else
426 // This type does nothing when using Copy-On-Write strings.
427 struct __xfer_bufptrs
428 {
429 __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { }
430 };
431 #endif
432
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))
438 { }
439
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,
444 __xfer_bufptrs&&)
445 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
446 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string), __a)
447 { }
448 #endif
449 #endif
450 };
451
452
453 // [27.7.2] Template class basic_istringstream
454 /**
455 * @brief Controlling input for std::string.
456 * @ingroup io
457 *
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>.
462 *
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.
467 */
468 template<typename _CharT, typename _Traits, typename _Alloc>
469 class basic_istringstream : public basic_istream<_CharT, _Traits>
470 {
471 public:
472 // Types:
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;
481
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;
486
487 private:
488 __stringbuf_type _M_stringbuf;
489
490 public:
491 // Constructors:
492
493 /**
494 * @brief Default constructor starts with an empty string buffer.
495 *
496 * Initializes @c sb using @c in, and passes @c &sb to the base
497 * class initializer. Does not allocate any buffer.
498 *
499 * That's a lie. We initialize the base class with NULL, because the
500 * string class does its own memory management.
501 */
502 basic_istringstream()
503 : __istream_type(), _M_stringbuf(ios_base::in)
504 { this->init(&_M_stringbuf); }
505
506 /**
507 * @brief Starts with an empty string buffer.
508 * @param __mode Whether the buffer can read, or write, or both.
509 *
510 * @c ios_base::in is automatically included in @a __mode.
511 *
512 * Initializes @c sb using @c __mode|in, and passes @c &sb to the base
513 * class initializer. Does not allocate any buffer.
514 *
515 * That's a lie. We initialize the base class with NULL, because the
516 * string class does its own memory management.
517 */
518 explicit
519 basic_istringstream(ios_base::openmode __mode)
520 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
521 { this->init(&_M_stringbuf); }
522
523 /**
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.
527 *
528 * @c ios_base::in is automatically included in @a mode.
529 *
530 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
531 * to the base class initializer.
532 *
533 * That's a lie. We initialize the base class with NULL, because the
534 * string class does its own memory management.
535 */
536 explicit
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); }
541
542 /**
543 * @brief The destructor does nothing.
544 *
545 * The buffer is deallocated by the stringbuf object, not the
546 * formatting stream.
547 */
548 ~basic_istringstream()
549 { }
550
551 #if __cplusplus >= 201103L
552 basic_istringstream(const basic_istringstream&) = delete;
553
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); }
558
559 // 27.8.3.2 Assign and swap:
560
561 basic_istringstream&
562 operator=(const basic_istringstream&) = delete;
563
564 basic_istringstream&
565 operator=(basic_istringstream&& __rhs)
566 {
567 __istream_type::operator=(std::move(__rhs));
568 _M_stringbuf = std::move(__rhs._M_stringbuf);
569 return *this;
570 }
571
572 void
573 swap(basic_istringstream& __rhs)
574 {
575 __istream_type::swap(__rhs);
576 _M_stringbuf.swap(__rhs._M_stringbuf);
577 }
578 #endif
579
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)); }
584
585 explicit
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)); }
590
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)
595 { }
596
597 template<typename _SAlloc>
598 basic_istringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
599 ios_base::openmode __mode,
600 const allocator_type& __a)
601 : __istream_type(),
602 _M_stringbuf(__string_type(__str.data(), __str.size()),
603 __mode | ios_base::in, __a)
604 { this->init(std::__addressof(_M_stringbuf)); }
605
606 template<typename _SAlloc>
607 explicit
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())
611 { }
612 #endif
613
614 // Members:
615 /**
616 * @brief Accessing the underlying buffer.
617 * @return The current basic_stringbuf buffer.
618 *
619 * This hides both signatures of std::basic_ios::rdbuf().
620 */
621 __stringbuf_type*
622 rdbuf() const
623 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
624
625 /**
626 * @brief Copying out the string buffer.
627 * @return @c rdbuf()->str()
628 */
629 __string_type
630 str() const
631 { return _M_stringbuf.str(); }
632
633 /**
634 * @brief Setting a new buffer.
635 * @param __s The string to use as a new sequence.
636 *
637 * Calls @c rdbuf()->str(s).
638 */
639 void
640 str(const __string_type& __s)
641 { _M_stringbuf.str(__s); }
642
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(); }
647 #endif
648 };
649
650
651 // [27.7.3] Template class basic_ostringstream
652 /**
653 * @brief Controlling output for std::string.
654 * @ingroup io
655 *
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>.
660 *
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.
665 */
666 template <typename _CharT, typename _Traits, typename _Alloc>
667 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
668 {
669 public:
670 // Types:
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;
679
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;
684
685 private:
686 __stringbuf_type _M_stringbuf;
687
688 public:
689 // Constructors/destructor:
690
691 /**
692 * @brief Default constructor starts with an empty string buffer.
693 *
694 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
695 * class initializer. Does not allocate any buffer.
696 *
697 * That's a lie. We initialize the base class with NULL, because the
698 * string class does its own memory management.
699 */
700 basic_ostringstream()
701 : __ostream_type(), _M_stringbuf(ios_base::out)
702 { this->init(&_M_stringbuf); }
703
704 /**
705 * @brief Starts with an empty string buffer.
706 * @param __mode Whether the buffer can read, or write, or both.
707 *
708 * @c ios_base::out is automatically included in @a mode.
709 *
710 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
711 * class initializer. Does not allocate any buffer.
712 *
713 * That's a lie. We initialize the base class with NULL, because the
714 * string class does its own memory management.
715 */
716 explicit
717 basic_ostringstream(ios_base::openmode __mode)
718 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
719 { this->init(&_M_stringbuf); }
720
721 /**
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.
725 *
726 * @c ios_base::out is automatically included in @a mode.
727 *
728 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
729 * to the base class initializer.
730 *
731 * That's a lie. We initialize the base class with NULL, because the
732 * string class does its own memory management.
733 */
734 explicit
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); }
739
740 /**
741 * @brief The destructor does nothing.
742 *
743 * The buffer is deallocated by the stringbuf object, not the
744 * formatting stream.
745 */
746 ~basic_ostringstream()
747 { }
748
749 #if __cplusplus >= 201103L
750 basic_ostringstream(const basic_ostringstream&) = delete;
751
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); }
756
757 // 27.8.3.2 Assign and swap:
758
759 basic_ostringstream&
760 operator=(const basic_ostringstream&) = delete;
761
762 basic_ostringstream&
763 operator=(basic_ostringstream&& __rhs)
764 {
765 __ostream_type::operator=(std::move(__rhs));
766 _M_stringbuf = std::move(__rhs._M_stringbuf);
767 return *this;
768 }
769
770 void
771 swap(basic_ostringstream& __rhs)
772 {
773 __ostream_type::swap(__rhs);
774 _M_stringbuf.swap(__rhs._M_stringbuf);
775 }
776 #endif
777
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)); }
782
783 explicit
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)); }
788
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)
793 { }
794
795 template<typename _SAlloc>
796 basic_ostringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
797 ios_base::openmode __mode,
798 const allocator_type& __a)
799 : __ostream_type(),
800 _M_stringbuf(__string_type(__str.data(), __str.size()),
801 __mode | ios_base::out, __a)
802 { this->init(std::__addressof(_M_stringbuf)); }
803
804 template<typename _SAlloc>
805 explicit
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())
809 { }
810 #endif
811
812 // Members:
813 /**
814 * @brief Accessing the underlying buffer.
815 * @return The current basic_stringbuf buffer.
816 *
817 * This hides both signatures of std::basic_ios::rdbuf().
818 */
819 __stringbuf_type*
820 rdbuf() const
821 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
822
823 /**
824 * @brief Copying out the string buffer.
825 * @return @c rdbuf()->str()
826 */
827 __string_type
828 str() const
829 { return _M_stringbuf.str(); }
830
831 /**
832 * @brief Setting a new buffer.
833 * @param __s The string to use as a new sequence.
834 *
835 * Calls @c rdbuf()->str(s).
836 */
837 void
838 str(const __string_type& __s)
839 { _M_stringbuf.str(__s); }
840
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(); }
845 #endif
846 };
847
848
849 // [27.7.4] Template class basic_stringstream
850 /**
851 * @brief Controlling input and output for std::string.
852 * @ingroup io
853 *
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>.
858 *
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.
863 */
864 template <typename _CharT, typename _Traits, typename _Alloc>
865 class basic_stringstream : public basic_iostream<_CharT, _Traits>
866 {
867 public:
868 // Types:
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;
877
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;
882
883 private:
884 __stringbuf_type _M_stringbuf;
885
886 public:
887 // Constructors/destructors
888
889 /**
890 * @brief Default constructor starts with an empty string buffer.
891 *
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.
894 *
895 * That's a lie. We initialize the base class with NULL, because the
896 * string class does its own memory management.
897 */
898 basic_stringstream()
899 : __iostream_type(), _M_stringbuf(ios_base::out | ios_base::in)
900 { this->init(&_M_stringbuf); }
901
902 /**
903 * @brief Starts with an empty string buffer.
904 * @param __m Whether the buffer can read, or write, or both.
905 *
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.
908 *
909 * That's a lie. We initialize the base class with NULL, because the
910 * string class does its own memory management.
911 */
912 explicit
913 basic_stringstream(ios_base::openmode __m)
914 : __iostream_type(), _M_stringbuf(__m)
915 { this->init(&_M_stringbuf); }
916
917 /**
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.
921 *
922 * Initializes @c sb using @a __str and @c __m, and passes @c &sb
923 * to the base class initializer.
924 *
925 * That's a lie. We initialize the base class with NULL, because the
926 * string class does its own memory management.
927 */
928 explicit
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); }
933
934 /**
935 * @brief The destructor does nothing.
936 *
937 * The buffer is deallocated by the stringbuf object, not the
938 * formatting stream.
939 */
940 ~basic_stringstream()
941 { }
942
943 #if __cplusplus >= 201103L
944 basic_stringstream(const basic_stringstream&) = delete;
945
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); }
950
951 // 27.8.3.2 Assign and swap:
952
953 basic_stringstream&
954 operator=(const basic_stringstream&) = delete;
955
956 basic_stringstream&
957 operator=(basic_stringstream&& __rhs)
958 {
959 __iostream_type::operator=(std::move(__rhs));
960 _M_stringbuf = std::move(__rhs._M_stringbuf);
961 return *this;
962 }
963
964 void
965 swap(basic_stringstream& __rhs)
966 {
967 __iostream_type::swap(__rhs);
968 _M_stringbuf.swap(__rhs._M_stringbuf);
969 }
970 #endif
971
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); }
976
977 explicit
978 basic_stringstream(__string_type&& __str,
979 ios_base::openmode __mode = ios_base::out
980 | ios_base::out)
981 : __iostream_type(), _M_stringbuf(std::move(__str), __mode)
982 { this->init(std::__addressof(_M_stringbuf)); }
983
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)
988 { }
989
990 template<typename _SAlloc>
991 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
992 ios_base::openmode __mode, const allocator_type& __a)
993 : __iostream_type(),
994 _M_stringbuf(__string_type(__str.data(), __str.size()), __mode, __a)
995 { this->init(std::__addressof(_M_stringbuf)); }
996
997 template<typename _SAlloc>
998 explicit
999 basic_stringstream(const basic_string<_CharT, _Traits, _SAlloc>& __str,
1000 ios_base::openmode __mode = ios_base::in
1001 | ios_base::out)
1002 : basic_stringstream(__str, __mode, allocator_type())
1003 { }
1004 #endif
1005
1006 // Members:
1007 /**
1008 * @brief Accessing the underlying buffer.
1009 * @return The current basic_stringbuf buffer.
1010 *
1011 * This hides both signatures of std::basic_ios::rdbuf().
1012 */
1013 __stringbuf_type*
1014 rdbuf() const
1015 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
1016
1017 /**
1018 * @brief Copying out the string buffer.
1019 * @return @c rdbuf()->str()
1020 */
1021 __string_type
1022 str() const
1023 { return _M_stringbuf.str(); }
1024
1025 /**
1026 * @brief Setting a new buffer.
1027 * @param __s The string to use as a new sequence.
1028 *
1029 * Calls @c rdbuf()->str(s).
1030 */
1031 void
1032 str(const __string_type& __s)
1033 { _M_stringbuf.str(__s); }
1034
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(); }
1039 #endif
1040 };
1041
1042 #if __cplusplus >= 201103L
1043 /// Swap specialization for stringbufs.
1044 template <class _CharT, class _Traits, class _Allocator>
1045 inline void
1046 swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
1047 basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
1048 { __x.swap(__y); }
1049
1050 /// Swap specialization for istringstreams.
1051 template <class _CharT, class _Traits, class _Allocator>
1052 inline void
1053 swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
1054 basic_istringstream<_CharT, _Traits, _Allocator>& __y)
1055 { __x.swap(__y); }
1056
1057 /// Swap specialization for ostringstreams.
1058 template <class _CharT, class _Traits, class _Allocator>
1059 inline void
1060 swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
1061 basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
1062 { __x.swap(__y); }
1063
1064 /// Swap specialization for stringstreams.
1065 template <class _CharT, class _Traits, class _Allocator>
1066 inline void
1067 swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
1068 basic_stringstream<_CharT, _Traits, _Allocator>& __y)
1069 { __x.swap(__y); }
1070 #endif
1071
1072 _GLIBCXX_END_NAMESPACE_CXX11
1073 _GLIBCXX_END_NAMESPACE_VERSION
1074 } // namespace
1075
1076 #include <bits/sstream.tcc>
1077
1078 #endif /* _GLIBCXX_SSTREAM */