xref: /freebsd-12.1/contrib/libc++/include/sstream (revision b1d04644)
1// -*- C++ -*-
2//===--------------------------- sstream ----------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_SSTREAM
12#define _LIBCPP_SSTREAM
13
14/*
15    sstream synopsis
16
17template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
18class basic_stringbuf
19    : public basic_streambuf<charT, traits>
20{
21public:
22    typedef charT                          char_type;
23    typedef traits                         traits_type;
24    typedef typename traits_type::int_type int_type;
25    typedef typename traits_type::pos_type pos_type;
26    typedef typename traits_type::off_type off_type;
27    typedef Allocator                      allocator_type;
28
29    // 27.8.1.1 Constructors:
30    explicit basic_stringbuf(ios_base::openmode which = ios_base::in | ios_base::out);
31    explicit basic_stringbuf(const basic_string<char_type, traits_type, allocator_type>& str,
32                             ios_base::openmode which = ios_base::in | ios_base::out);
33    basic_stringbuf(basic_stringbuf&& rhs);
34
35    // 27.8.1.2 Assign and swap:
36    basic_stringbuf& operator=(basic_stringbuf&& rhs);
37    void swap(basic_stringbuf& rhs);
38
39    // 27.8.1.3 Get and set:
40    basic_string<char_type, traits_type, allocator_type> str() const;
41    void str(const basic_string<char_type, traits_type, allocator_type>& s);
42
43protected:
44    // 27.8.1.4 Overridden virtual functions:
45    virtual int_type underflow();
46    virtual int_type pbackfail(int_type c = traits_type::eof());
47    virtual int_type overflow (int_type c = traits_type::eof());
48    virtual basic_streambuf<char_type, traits_type>* setbuf(char_type*, streamsize);
49    virtual pos_type seekoff(off_type off, ios_base::seekdir way,
50                             ios_base::openmode which = ios_base::in | ios_base::out);
51    virtual pos_type seekpos(pos_type sp,
52                             ios_base::openmode which = ios_base::in | ios_base::out);
53};
54
55template <class charT, class traits, class Allocator>
56  void swap(basic_stringbuf<charT, traits, Allocator>& x,
57            basic_stringbuf<charT, traits, Allocator>& y);
58
59typedef basic_stringbuf<char>    stringbuf;
60typedef basic_stringbuf<wchar_t> wstringbuf;
61
62template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
63class basic_istringstream
64    : public basic_istream<charT, traits>
65{
66public:
67    typedef charT                          char_type;
68    typedef traits                         traits_type;
69    typedef typename traits_type::int_type int_type;
70    typedef typename traits_type::pos_type pos_type;
71    typedef typename traits_type::off_type off_type;
72    typedef Allocator                      allocator_type;
73
74    // 27.8.2.1 Constructors:
75    explicit basic_istringstream(ios_base::openmode which = ios_base::in);
76    explicit basic_istringstream(const basic_string<char_type, traits_type,allocator_type>& str,
77                                 ios_base::openmode which = ios_base::in);
78    basic_istringstream(basic_istringstream&& rhs);
79
80    // 27.8.2.2 Assign and swap:
81    basic_istringstream& operator=(basic_istringstream&& rhs);
82    void swap(basic_istringstream& rhs);
83
84    // 27.8.2.3 Members:
85    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
86    basic_string<char_type, traits_type, allocator_type> str() const;
87    void str(const basic_string<char_type, traits_type, allocator_type>& s);
88};
89
90template <class charT, class traits, class Allocator>
91  void swap(basic_istringstream<charT, traits, Allocator>& x,
92            basic_istringstream<charT, traits, Allocator>& y);
93
94typedef basic_istringstream<char>    istringstream;
95typedef basic_istringstream<wchar_t> wistringstream;
96
97template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
98class basic_ostringstream
99    : public basic_ostream<charT, traits>
100{
101public:
102    // types:
103    typedef charT                          char_type;
104    typedef traits                         traits_type;
105    typedef typename traits_type::int_type int_type;
106    typedef typename traits_type::pos_type pos_type;
107    typedef typename traits_type::off_type off_type;
108    typedef Allocator                      allocator_type;
109
110    // 27.8.3.1 Constructors/destructor:
111    explicit basic_ostringstream(ios_base::openmode which = ios_base::out);
112    explicit basic_ostringstream(const basic_string<char_type, traits_type, allocator_type>& str,
113                                 ios_base::openmode which = ios_base::out);
114    basic_ostringstream(basic_ostringstream&& rhs);
115
116    // 27.8.3.2 Assign/swap:
117    basic_ostringstream& operator=(basic_ostringstream&& rhs);
118    void swap(basic_ostringstream& rhs);
119
120    // 27.8.3.3 Members:
121    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
122    basic_string<char_type, traits_type, allocator_type> str() const;
123    void str(const basic_string<char_type, traits_type, allocator_type>& s);
124};
125
126template <class charT, class traits, class Allocator>
127  void swap(basic_ostringstream<charT, traits, Allocator>& x,
128            basic_ostringstream<charT, traits, Allocator>& y);
129
130typedef basic_ostringstream<char>    ostringstream;
131typedef basic_ostringstream<wchar_t> wostringstream;
132
133template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
134class basic_stringstream
135    : public basic_iostream<charT, traits>
136{
137public:
138    // types:
139    typedef charT                          char_type;
140    typedef traits                         traits_type;
141    typedef typename traits_type::int_type int_type;
142    typedef typename traits_type::pos_type pos_type;
143    typedef typename traits_type::off_type off_type;
144    typedef Allocator                      allocator_type;
145
146    // constructors/destructor
147    explicit basic_stringstream(ios_base::openmode which = ios_base::out|ios_base::in);
148    explicit basic_stringstream(const basic_string<char_type, traits_type, allocator_type>& str,
149                                ios_base::openmode which = ios_base::out|ios_base::in);
150    basic_stringstream(basic_stringstream&& rhs);
151
152    // 27.8.5.1 Assign/swap:
153    basic_stringstream& operator=(basic_stringstream&& rhs);
154    void swap(basic_stringstream& rhs);
155
156    // Members:
157    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
158    basic_string<char_type, traits_type, allocator_type> str() const;
159    void str(const basic_string<char_type, traits_type, allocator_type>& str);
160};
161
162template <class charT, class traits, class Allocator>
163  void swap(basic_stringstream<charT, traits, Allocator>& x,
164            basic_stringstream<charT, traits, Allocator>& y);
165
166typedef basic_stringstream<char>    stringstream;
167typedef basic_stringstream<wchar_t> wstringstream;
168
169}  // std
170
171*/
172
173#include <__config>
174#include <ostream>
175#include <istream>
176#include <string>
177
178#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
179#pragma GCC system_header
180#endif
181
182_LIBCPP_BEGIN_NAMESPACE_STD
183
184// basic_stringbuf
185
186template <class _CharT, class _Traits, class _Allocator>
187class _LIBCPP_VISIBLE basic_stringbuf
188    : public basic_streambuf<_CharT, _Traits>
189{
190public:
191    typedef _CharT                         char_type;
192    typedef _Traits                        traits_type;
193    typedef typename traits_type::int_type int_type;
194    typedef typename traits_type::pos_type pos_type;
195    typedef typename traits_type::off_type off_type;
196    typedef _Allocator                     allocator_type;
197
198    typedef basic_string<char_type, traits_type, allocator_type> string_type;
199
200private:
201
202    string_type __str_;
203    mutable char_type* __hm_;
204    ios_base::openmode __mode_;
205
206public:
207    // 27.8.1.1 Constructors:
208    explicit basic_stringbuf(ios_base::openmode __wch = ios_base::in | ios_base::out);
209    explicit basic_stringbuf(const string_type& __s,
210                             ios_base::openmode __wch = ios_base::in | ios_base::out);
211#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
212    basic_stringbuf(basic_stringbuf&& __rhs);
213#endif
214
215    // 27.8.1.2 Assign and swap:
216#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
217    basic_stringbuf& operator=(basic_stringbuf&& __rhs);
218#endif
219    void swap(basic_stringbuf& __rhs);
220
221    // 27.8.1.3 Get and set:
222    string_type str() const;
223    void str(const string_type& __s);
224
225protected:
226    // 27.8.1.4 Overridden virtual functions:
227    virtual int_type underflow();
228    virtual int_type pbackfail(int_type __c = traits_type::eof());
229    virtual int_type overflow (int_type __c = traits_type::eof());
230    virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
231                             ios_base::openmode __wch = ios_base::in | ios_base::out);
232    virtual pos_type seekpos(pos_type __sp,
233                             ios_base::openmode __wch = ios_base::in | ios_base::out);
234};
235
236template <class _CharT, class _Traits, class _Allocator>
237inline _LIBCPP_INLINE_VISIBILITY
238basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(ios_base::openmode __wch)
239    : __hm_(0),
240      __mode_(__wch)
241{
242    str(string_type());
243}
244
245template <class _CharT, class _Traits, class _Allocator>
246inline _LIBCPP_INLINE_VISIBILITY
247basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(const string_type& __s,
248                             ios_base::openmode __wch)
249    : __hm_(0),
250      __mode_(__wch)
251{
252    str(__s);
253}
254
255#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
256
257template <class _CharT, class _Traits, class _Allocator>
258basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(basic_stringbuf&& __rhs)
259    : __mode_(__rhs.__mode_)
260{
261    ptrdiff_t __ninp = __rhs.gptr()  - __rhs.eback();
262    ptrdiff_t __einp = __rhs.egptr() - __rhs.eback();
263    ptrdiff_t __nout = __rhs.pptr()  - __rhs.pbase();
264    ptrdiff_t __eout = __rhs.epptr() - __rhs.pbase();
265    ptrdiff_t __hm   = __rhs.__hm_   - __rhs.pbase();
266    __str_ = _VSTD::move(__rhs.__str_);
267    char_type* __p = const_cast<char_type*>(__str_.data());
268    this->setg(__p, __p + __ninp, __p + __einp);
269    this->setp(__p, __p + __eout);
270    this->pbump(__nout);
271    __hm_ = __p + __hm;
272    __p = const_cast<char_type*>(__rhs.__str_.data());
273    __rhs.setg(__p, __p, __p);
274    __rhs.setp(__p, __p);
275    __rhs.__hm_ = __p;
276    this->pubimbue(__rhs.getloc());
277}
278
279template <class _CharT, class _Traits, class _Allocator>
280basic_stringbuf<_CharT, _Traits, _Allocator>&
281basic_stringbuf<_CharT, _Traits, _Allocator>::operator=(basic_stringbuf&& __rhs)
282{
283    ptrdiff_t __ninp = __rhs.gptr()  - __rhs.eback();
284    ptrdiff_t __einp = __rhs.egptr() - __rhs.eback();
285    ptrdiff_t __nout = __rhs.pptr()  - __rhs.pbase();
286    ptrdiff_t __eout = __rhs.epptr() - __rhs.pbase();
287    ptrdiff_t __hm   = __rhs.__hm_   - __rhs.pbase();
288    __mode_ = __rhs.__mode_;
289    __str_ = _VSTD::move(__rhs.__str_);
290    char_type* __p = const_cast<char_type*>(__str_.data());
291    this->setg(__p, __p + __ninp, __p + __einp);
292    this->setp(__p, __p + __eout);
293    this->pbump(__nout);
294    __hm_ = __p + __hm;
295    __p = const_cast<char_type*>(__rhs.__str_.data());
296    __rhs.setg(__p, __p, __p);
297    __rhs.setp(__p, __p);
298    __rhs.__hm_ = __p;
299    this->pubimbue(__rhs.getloc());
300    return *this;
301}
302
303#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
304
305template <class _CharT, class _Traits, class _Allocator>
306void
307basic_stringbuf<_CharT, _Traits, _Allocator>::swap(basic_stringbuf& __rhs)
308{
309    ptrdiff_t __rninp = __rhs.gptr()  - __rhs.eback();
310    ptrdiff_t __reinp = __rhs.egptr() - __rhs.eback();
311    ptrdiff_t __rnout = __rhs.pptr()  - __rhs.pbase();
312    ptrdiff_t __reout = __rhs.epptr() - __rhs.pbase();
313    ptrdiff_t __rhm   = __rhs.__hm_   - __rhs.pbase();
314    ptrdiff_t __lninp = this->gptr()  - this->eback();
315    ptrdiff_t __leinp = this->egptr() - this->eback();
316    ptrdiff_t __lnout = this->pptr()  - this->pbase();
317    ptrdiff_t __leout = this->epptr() - this->pbase();
318    ptrdiff_t __lhm   = this->__hm_   - this->pbase();
319    _VSTD::swap(__mode_, __rhs.__mode_);
320    __str_.swap(__rhs.__str_);
321    char_type* __p = const_cast<char_type*>(__str_.data());
322    this->setg(__p, __p + __rninp, __p + __reinp);
323    this->setp(__p, __p + __reout);
324    this->pbump(__rnout);
325    __hm_ = __p + __rhm;
326    __p = const_cast<char_type*>(__rhs.__str_.data());
327    __rhs.setg(__p, __p + __lninp, __p + __leinp);
328    __rhs.setp(__p, __p + __leout);
329    __rhs.pbump(__lnout);
330    __rhs.__hm_ = __p + __lhm;
331    locale __tl = __rhs.getloc();
332    __rhs.pubimbue(this->getloc());
333    this->pubimbue(__tl);
334}
335
336template <class _CharT, class _Traits, class _Allocator>
337inline _LIBCPP_INLINE_VISIBILITY
338void
339swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
340     basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
341{
342    __x.swap(__y);
343}
344
345template <class _CharT, class _Traits, class _Allocator>
346basic_string<_CharT, _Traits, _Allocator>
347basic_stringbuf<_CharT, _Traits, _Allocator>::str() const
348{
349    if (__mode_ & ios_base::out)
350    {
351        if (__hm_ < this->pptr())
352            __hm_ = this->pptr();
353        return string_type(this->pbase(), __hm_, __str_.get_allocator());
354    }
355    else if (__mode_ & ios_base::in)
356        return string_type(this->eback(), this->egptr(), __str_.get_allocator());
357    return string_type(__str_.get_allocator());
358}
359
360template <class _CharT, class _Traits, class _Allocator>
361void
362basic_stringbuf<_CharT, _Traits, _Allocator>::str(const string_type& __s)
363{
364    __str_ = __s;
365    __hm_ = 0;
366    if (__mode_ & ios_base::in)
367    {
368        __hm_ = const_cast<char_type*>(__str_.data()) + __str_.size();
369        this->setg(const_cast<char_type*>(__str_.data()),
370                   const_cast<char_type*>(__str_.data()),
371                   __hm_);
372    }
373    if (__mode_ & ios_base::out)
374    {
375        typename string_type::size_type __sz = __str_.size();
376        __hm_ = const_cast<char_type*>(__str_.data()) + __sz;
377        __str_.resize(__str_.capacity());
378        this->setp(const_cast<char_type*>(__str_.data()),
379                   const_cast<char_type*>(__str_.data()) + __str_.size());
380        if (__mode_ & (ios_base::app | ios_base::ate))
381            this->pbump(__sz);
382    }
383}
384
385template <class _CharT, class _Traits, class _Allocator>
386typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
387basic_stringbuf<_CharT, _Traits, _Allocator>::underflow()
388{
389    if (__hm_ < this->pptr())
390        __hm_ = this->pptr();
391    if (__mode_ & ios_base::in)
392    {
393        if (this->egptr() < __hm_)
394            this->setg(this->eback(), this->gptr(), __hm_);
395        if (this->gptr() < this->egptr())
396            return traits_type::to_int_type(*this->gptr());
397    }
398    return traits_type::eof();
399}
400
401template <class _CharT, class _Traits, class _Allocator>
402typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
403basic_stringbuf<_CharT, _Traits, _Allocator>::pbackfail(int_type __c)
404{
405    if (__hm_ < this->pptr())
406        __hm_ = this->pptr();
407    if (this->eback() < this->gptr())
408    {
409        if (traits_type::eq_int_type(__c, traits_type::eof()))
410        {
411            this->setg(this->eback(), this->gptr()-1, __hm_);
412            return traits_type::not_eof(__c);
413        }
414        if ((__mode_ & ios_base::out) ||
415            traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
416        {
417            this->setg(this->eback(), this->gptr()-1, __hm_);
418            *this->gptr() = traits_type::to_char_type(__c);
419            return __c;
420        }
421    }
422    return traits_type::eof();
423}
424
425template <class _CharT, class _Traits, class _Allocator>
426typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
427basic_stringbuf<_CharT, _Traits, _Allocator>::overflow(int_type __c)
428{
429    if (!traits_type::eq_int_type(__c, traits_type::eof()))
430    {
431        ptrdiff_t __ninp = this->gptr()  - this->eback();
432        if (this->pptr() == this->epptr())
433        {
434            if (!(__mode_ & ios_base::out))
435                return traits_type::eof();
436#ifndef _LIBCPP_NO_EXCEPTIONS
437            try
438            {
439#endif  // _LIBCPP_NO_EXCEPTIONS
440                ptrdiff_t __nout = this->pptr()  - this->pbase();
441                ptrdiff_t __hm = __hm_ - this->pbase();
442                __str_.push_back(char_type());
443                __str_.resize(__str_.capacity());
444                char_type* __p = const_cast<char_type*>(__str_.data());
445                this->setp(__p, __p + __str_.size());
446                this->pbump(__nout);
447                __hm_ = this->pbase() + __hm;
448#ifndef _LIBCPP_NO_EXCEPTIONS
449            }
450            catch (...)
451            {
452                return traits_type::eof();
453            }
454#endif  // _LIBCPP_NO_EXCEPTIONS
455        }
456        __hm_ = _VSTD::max(this->pptr() + 1, __hm_);
457        if (__mode_ & ios_base::in)
458        {
459            char_type* __p = const_cast<char_type*>(__str_.data());
460            this->setg(__p, __p + __ninp, __hm_);
461        }
462        return this->sputc(__c);
463    }
464    return traits_type::not_eof(__c);
465}
466
467template <class _CharT, class _Traits, class _Allocator>
468typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type
469basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff(off_type __off,
470                                                      ios_base::seekdir __way,
471                                                      ios_base::openmode __wch)
472{
473    if (__hm_ < this->pptr())
474        __hm_ = this->pptr();
475    if ((__wch & (ios_base::in | ios_base::out)) == 0)
476        return pos_type(-1);
477    if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out)
478        && __way == ios_base::cur)
479        return pos_type(-1);
480    off_type __noff;
481    switch (__way)
482    {
483    case ios_base::beg:
484        __noff = 0;
485        break;
486    case ios_base::cur:
487        if (__wch & ios_base::in)
488            __noff = this->gptr() - this->eback();
489        else
490            __noff = this->pptr() - this->pbase();
491        break;
492    case ios_base::end:
493        __noff = __hm_ - __str_.data();
494        break;
495    default:
496        return pos_type(-1);
497    }
498    __noff += __off;
499    if (__noff < 0 || __hm_ - __str_.data() < __noff)
500        return pos_type(-1);
501    if (__noff != 0)
502    {
503        if ((__wch & ios_base::in) && this->gptr() == 0)
504            return pos_type(-1);
505        if ((__wch & ios_base::out) && this->pptr() == 0)
506            return pos_type(-1);
507    }
508    if (__wch & ios_base::in)
509        this->setg(this->eback(), this->eback() + __noff, __hm_);
510    if (__wch & ios_base::out)
511    {
512        this->setp(this->pbase(), this->epptr());
513        this->pbump(__noff);
514    }
515    return pos_type(__noff);
516}
517
518template <class _CharT, class _Traits, class _Allocator>
519inline _LIBCPP_INLINE_VISIBILITY
520typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type
521basic_stringbuf<_CharT, _Traits, _Allocator>::seekpos(pos_type __sp,
522                                                      ios_base::openmode __wch)
523{
524    return seekoff(__sp, ios_base::beg, __wch);
525}
526
527// basic_istringstream
528
529template <class _CharT, class _Traits, class _Allocator>
530class _LIBCPP_VISIBLE basic_istringstream
531    : public basic_istream<_CharT, _Traits>
532{
533public:
534    typedef _CharT                         char_type;
535    typedef _Traits                        traits_type;
536    typedef typename traits_type::int_type int_type;
537    typedef typename traits_type::pos_type pos_type;
538    typedef typename traits_type::off_type off_type;
539    typedef _Allocator                     allocator_type;
540
541    typedef basic_string<char_type, traits_type, allocator_type> string_type;
542
543private:
544    basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
545
546public:
547    // 27.8.2.1 Constructors:
548    explicit basic_istringstream(ios_base::openmode __wch = ios_base::in);
549    explicit basic_istringstream(const string_type& __s,
550                                 ios_base::openmode __wch = ios_base::in);
551#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
552    basic_istringstream(basic_istringstream&& __rhs);
553
554    // 27.8.2.2 Assign and swap:
555    basic_istringstream& operator=(basic_istringstream&& __rhs);
556#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
557    void swap(basic_istringstream& __rhs);
558
559    // 27.8.2.3 Members:
560    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
561    string_type str() const;
562    void str(const string_type& __s);
563};
564
565template <class _CharT, class _Traits, class _Allocator>
566inline _LIBCPP_INLINE_VISIBILITY
567basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(ios_base::openmode __wch)
568    : basic_istream<_CharT, _Traits>(&__sb_),
569      __sb_(__wch | ios_base::in)
570{
571}
572
573template <class _CharT, class _Traits, class _Allocator>
574inline _LIBCPP_INLINE_VISIBILITY
575basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(const string_type& __s,
576                                                                      ios_base::openmode __wch)
577    : basic_istream<_CharT, _Traits>(&__sb_),
578      __sb_(__s, __wch | ios_base::in)
579{
580}
581
582#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
583
584template <class _CharT, class _Traits, class _Allocator>
585inline _LIBCPP_INLINE_VISIBILITY
586basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(basic_istringstream&& __rhs)
587    : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs)),
588      __sb_(_VSTD::move(__rhs.__sb_))
589{
590    basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
591}
592
593template <class _CharT, class _Traits, class _Allocator>
594basic_istringstream<_CharT, _Traits, _Allocator>&
595basic_istringstream<_CharT, _Traits, _Allocator>::operator=(basic_istringstream&& __rhs)
596{
597    basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
598    __sb_ = _VSTD::move(__rhs.__sb_);
599    return *this;
600}
601
602#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
603
604template <class _CharT, class _Traits, class _Allocator>
605inline _LIBCPP_INLINE_VISIBILITY
606void
607basic_istringstream<_CharT, _Traits, _Allocator>::swap(basic_istringstream& __rhs)
608{
609    basic_istream<char_type, traits_type>::swap(__rhs);
610    __sb_.swap(__rhs.__sb_);
611}
612
613template <class _CharT, class _Traits, class _Allocator>
614inline _LIBCPP_INLINE_VISIBILITY
615void
616swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
617     basic_istringstream<_CharT, _Traits, _Allocator>& __y)
618{
619    __x.swap(__y);
620}
621
622template <class _CharT, class _Traits, class _Allocator>
623inline _LIBCPP_INLINE_VISIBILITY
624basic_stringbuf<_CharT, _Traits, _Allocator>*
625basic_istringstream<_CharT, _Traits, _Allocator>::rdbuf() const
626{
627    return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
628}
629
630template <class _CharT, class _Traits, class _Allocator>
631inline _LIBCPP_INLINE_VISIBILITY
632basic_string<_CharT, _Traits, _Allocator>
633basic_istringstream<_CharT, _Traits, _Allocator>::str() const
634{
635    return __sb_.str();
636}
637
638template <class _CharT, class _Traits, class _Allocator>
639inline _LIBCPP_INLINE_VISIBILITY
640void
641basic_istringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s)
642{
643    __sb_.str(__s);
644}
645
646// basic_ostringstream
647
648template <class _CharT, class _Traits, class _Allocator>
649class _LIBCPP_VISIBLE basic_ostringstream
650    : public basic_ostream<_CharT, _Traits>
651{
652public:
653    typedef _CharT                         char_type;
654    typedef _Traits                        traits_type;
655    typedef typename traits_type::int_type int_type;
656    typedef typename traits_type::pos_type pos_type;
657    typedef typename traits_type::off_type off_type;
658    typedef _Allocator                     allocator_type;
659
660    typedef basic_string<char_type, traits_type, allocator_type> string_type;
661
662private:
663    basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
664
665public:
666    // 27.8.2.1 Constructors:
667    explicit basic_ostringstream(ios_base::openmode __wch = ios_base::out);
668    explicit basic_ostringstream(const string_type& __s,
669                                 ios_base::openmode __wch = ios_base::out);
670#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
671    basic_ostringstream(basic_ostringstream&& __rhs);
672
673    // 27.8.2.2 Assign and swap:
674    basic_ostringstream& operator=(basic_ostringstream&& __rhs);
675#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
676    void swap(basic_ostringstream& __rhs);
677
678    // 27.8.2.3 Members:
679    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
680    string_type str() const;
681    void str(const string_type& __s);
682};
683
684template <class _CharT, class _Traits, class _Allocator>
685inline _LIBCPP_INLINE_VISIBILITY
686basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(ios_base::openmode __wch)
687    : basic_ostream<_CharT, _Traits>(&__sb_),
688      __sb_(__wch | ios_base::out)
689{
690}
691
692template <class _CharT, class _Traits, class _Allocator>
693inline _LIBCPP_INLINE_VISIBILITY
694basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(const string_type& __s,
695                                                                      ios_base::openmode __wch)
696    : basic_ostream<_CharT, _Traits>(&__sb_),
697      __sb_(__s, __wch | ios_base::out)
698{
699}
700
701#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
702
703template <class _CharT, class _Traits, class _Allocator>
704inline _LIBCPP_INLINE_VISIBILITY
705basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(basic_ostringstream&& __rhs)
706    : basic_ostream<_CharT, _Traits>(_VSTD::move(__rhs)),
707      __sb_(_VSTD::move(__rhs.__sb_))
708{
709    basic_ostream<_CharT, _Traits>::set_rdbuf(&__sb_);
710}
711
712template <class _CharT, class _Traits, class _Allocator>
713basic_ostringstream<_CharT, _Traits, _Allocator>&
714basic_ostringstream<_CharT, _Traits, _Allocator>::operator=(basic_ostringstream&& __rhs)
715{
716    basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
717    __sb_ = _VSTD::move(__rhs.__sb_);
718    return *this;
719}
720
721#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
722
723template <class _CharT, class _Traits, class _Allocator>
724inline _LIBCPP_INLINE_VISIBILITY
725void
726basic_ostringstream<_CharT, _Traits, _Allocator>::swap(basic_ostringstream& __rhs)
727{
728    basic_ostream<char_type, traits_type>::swap(__rhs);
729    __sb_.swap(__rhs.__sb_);
730}
731
732template <class _CharT, class _Traits, class _Allocator>
733inline _LIBCPP_INLINE_VISIBILITY
734void
735swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
736     basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
737{
738    __x.swap(__y);
739}
740
741template <class _CharT, class _Traits, class _Allocator>
742inline _LIBCPP_INLINE_VISIBILITY
743basic_stringbuf<_CharT, _Traits, _Allocator>*
744basic_ostringstream<_CharT, _Traits, _Allocator>::rdbuf() const
745{
746    return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
747}
748
749template <class _CharT, class _Traits, class _Allocator>
750inline _LIBCPP_INLINE_VISIBILITY
751basic_string<_CharT, _Traits, _Allocator>
752basic_ostringstream<_CharT, _Traits, _Allocator>::str() const
753{
754    return __sb_.str();
755}
756
757template <class _CharT, class _Traits, class _Allocator>
758inline _LIBCPP_INLINE_VISIBILITY
759void
760basic_ostringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s)
761{
762    __sb_.str(__s);
763}
764
765// basic_stringstream
766
767template <class _CharT, class _Traits, class _Allocator>
768class _LIBCPP_VISIBLE basic_stringstream
769    : public basic_iostream<_CharT, _Traits>
770{
771public:
772    typedef _CharT                         char_type;
773    typedef _Traits                        traits_type;
774    typedef typename traits_type::int_type int_type;
775    typedef typename traits_type::pos_type pos_type;
776    typedef typename traits_type::off_type off_type;
777    typedef _Allocator                     allocator_type;
778
779    typedef basic_string<char_type, traits_type, allocator_type> string_type;
780
781private:
782    basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
783
784public:
785    // 27.8.2.1 Constructors:
786    explicit basic_stringstream(ios_base::openmode __wch = ios_base::in | ios_base::out);
787    explicit basic_stringstream(const string_type& __s,
788                                ios_base::openmode __wch = ios_base::in | ios_base::out);
789#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
790    basic_stringstream(basic_stringstream&& __rhs);
791
792    // 27.8.2.2 Assign and swap:
793    basic_stringstream& operator=(basic_stringstream&& __rhs);
794#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
795    void swap(basic_stringstream& __rhs);
796
797    // 27.8.2.3 Members:
798    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
799    string_type str() const;
800    void str(const string_type& __s);
801};
802
803template <class _CharT, class _Traits, class _Allocator>
804inline _LIBCPP_INLINE_VISIBILITY
805basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(ios_base::openmode __wch)
806    : basic_iostream<_CharT, _Traits>(&__sb_),
807      __sb_(__wch)
808{
809}
810
811template <class _CharT, class _Traits, class _Allocator>
812inline _LIBCPP_INLINE_VISIBILITY
813basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(const string_type& __s,
814                                                                    ios_base::openmode __wch)
815    : basic_iostream<_CharT, _Traits>(&__sb_),
816      __sb_(__s, __wch)
817{
818}
819
820#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
821
822template <class _CharT, class _Traits, class _Allocator>
823inline _LIBCPP_INLINE_VISIBILITY
824basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(basic_stringstream&& __rhs)
825    : basic_iostream<_CharT, _Traits>(_VSTD::move(__rhs)),
826      __sb_(_VSTD::move(__rhs.__sb_))
827{
828    basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
829}
830
831template <class _CharT, class _Traits, class _Allocator>
832basic_stringstream<_CharT, _Traits, _Allocator>&
833basic_stringstream<_CharT, _Traits, _Allocator>::operator=(basic_stringstream&& __rhs)
834{
835    basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
836    __sb_ = _VSTD::move(__rhs.__sb_);
837    return *this;
838}
839
840#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
841
842template <class _CharT, class _Traits, class _Allocator>
843inline _LIBCPP_INLINE_VISIBILITY
844void
845basic_stringstream<_CharT, _Traits, _Allocator>::swap(basic_stringstream& __rhs)
846{
847    basic_iostream<char_type, traits_type>::swap(__rhs);
848    __sb_.swap(__rhs.__sb_);
849}
850
851template <class _CharT, class _Traits, class _Allocator>
852inline _LIBCPP_INLINE_VISIBILITY
853void
854swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
855     basic_stringstream<_CharT, _Traits, _Allocator>& __y)
856{
857    __x.swap(__y);
858}
859
860template <class _CharT, class _Traits, class _Allocator>
861inline _LIBCPP_INLINE_VISIBILITY
862basic_stringbuf<_CharT, _Traits, _Allocator>*
863basic_stringstream<_CharT, _Traits, _Allocator>::rdbuf() const
864{
865    return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
866}
867
868template <class _CharT, class _Traits, class _Allocator>
869inline _LIBCPP_INLINE_VISIBILITY
870basic_string<_CharT, _Traits, _Allocator>
871basic_stringstream<_CharT, _Traits, _Allocator>::str() const
872{
873    return __sb_.str();
874}
875
876template <class _CharT, class _Traits, class _Allocator>
877inline _LIBCPP_INLINE_VISIBILITY
878void
879basic_stringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s)
880{
881    __sb_.str(__s);
882}
883
884_LIBCPP_END_NAMESPACE_STD
885
886#endif  // _LIBCPP_SSTREAM
887