xref: /freebsd-12.1/contrib/libc++/include/sstream (revision 540d2a8b)
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#include <__undef_min_max>
179
180#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
181#pragma GCC system_header
182#endif
183
184_LIBCPP_BEGIN_NAMESPACE_STD
185
186// basic_stringbuf
187
188template <class _CharT, class _Traits, class _Allocator>
189class _LIBCPP_TEMPLATE_VIS basic_stringbuf
190    : public basic_streambuf<_CharT, _Traits>
191{
192public:
193    typedef _CharT                         char_type;
194    typedef _Traits                        traits_type;
195    typedef typename traits_type::int_type int_type;
196    typedef typename traits_type::pos_type pos_type;
197    typedef typename traits_type::off_type off_type;
198    typedef _Allocator                     allocator_type;
199
200    typedef basic_string<char_type, traits_type, allocator_type> string_type;
201
202private:
203
204    string_type __str_;
205    mutable char_type* __hm_;
206    ios_base::openmode __mode_;
207
208public:
209    // 27.8.1.1 Constructors:
210    inline _LIBCPP_INLINE_VISIBILITY
211    explicit basic_stringbuf(ios_base::openmode __wch = ios_base::in | ios_base::out);
212    inline _LIBCPP_INLINE_VISIBILITY
213    explicit basic_stringbuf(const string_type& __s,
214                             ios_base::openmode __wch = ios_base::in | ios_base::out);
215#ifndef _LIBCPP_CXX03_LANG
216    basic_stringbuf(basic_stringbuf&& __rhs);
217
218    // 27.8.1.2 Assign and swap:
219    basic_stringbuf& operator=(basic_stringbuf&& __rhs);
220#endif
221    void swap(basic_stringbuf& __rhs);
222
223    // 27.8.1.3 Get and set:
224    string_type str() const;
225    void str(const string_type& __s);
226
227protected:
228    // 27.8.1.4 Overridden virtual functions:
229    virtual int_type underflow();
230    virtual int_type pbackfail(int_type __c = traits_type::eof());
231    virtual int_type overflow (int_type __c = traits_type::eof());
232    virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
233                             ios_base::openmode __wch = ios_base::in | ios_base::out);
234    inline _LIBCPP_INLINE_VISIBILITY
235    virtual pos_type seekpos(pos_type __sp,
236                             ios_base::openmode __wch = ios_base::in | ios_base::out);
237};
238
239template <class _CharT, class _Traits, class _Allocator>
240basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(ios_base::openmode __wch)
241    : __hm_(0),
242      __mode_(__wch)
243{
244    str(string_type());
245}
246
247template <class _CharT, class _Traits, class _Allocator>
248basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(const string_type& __s,
249                             ios_base::openmode __wch)
250    : __hm_(0),
251      __mode_(__wch)
252{
253    str(__s);
254}
255
256#ifndef _LIBCPP_CXX03_LANG
257
258template <class _CharT, class _Traits, class _Allocator>
259basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(basic_stringbuf&& __rhs)
260    : __mode_(__rhs.__mode_)
261{
262    char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
263    ptrdiff_t __binp = -1;
264    ptrdiff_t __ninp = -1;
265    ptrdiff_t __einp = -1;
266    if (__rhs.eback() != nullptr)
267    {
268        __binp = __rhs.eback() - __p;
269        __ninp = __rhs.gptr() - __p;
270        __einp = __rhs.egptr() - __p;
271    }
272    ptrdiff_t __bout = -1;
273    ptrdiff_t __nout = -1;
274    ptrdiff_t __eout = -1;
275    if (__rhs.pbase() != nullptr)
276    {
277        __bout = __rhs.pbase() - __p;
278        __nout = __rhs.pptr() - __p;
279        __eout = __rhs.epptr() - __p;
280    }
281    ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
282    __str_ = _VSTD::move(__rhs.__str_);
283    __p = const_cast<char_type*>(__str_.data());
284    if (__binp != -1)
285        this->setg(__p + __binp, __p + __ninp, __p + __einp);
286    if (__bout != -1)
287    {
288        this->setp(__p + __bout, __p + __eout);
289        this->pbump(__nout);
290    }
291    __hm_ = __hm == -1 ? nullptr : __p + __hm;
292    __p = const_cast<char_type*>(__rhs.__str_.data());
293    __rhs.setg(__p, __p, __p);
294    __rhs.setp(__p, __p);
295    __rhs.__hm_ = __p;
296    this->pubimbue(__rhs.getloc());
297}
298
299template <class _CharT, class _Traits, class _Allocator>
300basic_stringbuf<_CharT, _Traits, _Allocator>&
301basic_stringbuf<_CharT, _Traits, _Allocator>::operator=(basic_stringbuf&& __rhs)
302{
303    char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
304    ptrdiff_t __binp = -1;
305    ptrdiff_t __ninp = -1;
306    ptrdiff_t __einp = -1;
307    if (__rhs.eback() != nullptr)
308    {
309        __binp = __rhs.eback() - __p;
310        __ninp = __rhs.gptr() - __p;
311        __einp = __rhs.egptr() - __p;
312    }
313    ptrdiff_t __bout = -1;
314    ptrdiff_t __nout = -1;
315    ptrdiff_t __eout = -1;
316    if (__rhs.pbase() != nullptr)
317    {
318        __bout = __rhs.pbase() - __p;
319        __nout = __rhs.pptr() - __p;
320        __eout = __rhs.epptr() - __p;
321    }
322    ptrdiff_t __hm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
323    __str_ = _VSTD::move(__rhs.__str_);
324    __p = const_cast<char_type*>(__str_.data());
325    if (__binp != -1)
326        this->setg(__p + __binp, __p + __ninp, __p + __einp);
327    else
328        this->setg(nullptr, nullptr, nullptr);
329    if (__bout != -1)
330    {
331        this->setp(__p + __bout, __p + __eout);
332        this->pbump(__nout);
333    }
334    else
335        this->setp(nullptr, nullptr);
336
337    __hm_ = __hm == -1 ? nullptr : __p + __hm;
338    __mode_ = __rhs.__mode_;
339    __p = const_cast<char_type*>(__rhs.__str_.data());
340    __rhs.setg(__p, __p, __p);
341    __rhs.setp(__p, __p);
342    __rhs.__hm_ = __p;
343    this->pubimbue(__rhs.getloc());
344    return *this;
345}
346
347#endif  // _LIBCPP_CXX03_LANG
348
349template <class _CharT, class _Traits, class _Allocator>
350void
351basic_stringbuf<_CharT, _Traits, _Allocator>::swap(basic_stringbuf& __rhs)
352{
353    char_type* __p = const_cast<char_type*>(__rhs.__str_.data());
354    ptrdiff_t __rbinp = -1;
355    ptrdiff_t __rninp = -1;
356    ptrdiff_t __reinp = -1;
357    if (__rhs.eback() != nullptr)
358    {
359        __rbinp = __rhs.eback() - __p;
360        __rninp = __rhs.gptr() - __p;
361        __reinp = __rhs.egptr() - __p;
362    }
363    ptrdiff_t __rbout = -1;
364    ptrdiff_t __rnout = -1;
365    ptrdiff_t __reout = -1;
366    if (__rhs.pbase() != nullptr)
367    {
368        __rbout = __rhs.pbase() - __p;
369        __rnout = __rhs.pptr() - __p;
370        __reout = __rhs.epptr() - __p;
371    }
372    ptrdiff_t __rhm = __rhs.__hm_ == nullptr ? -1 : __rhs.__hm_ - __p;
373    __p = const_cast<char_type*>(__str_.data());
374    ptrdiff_t __lbinp = -1;
375    ptrdiff_t __lninp = -1;
376    ptrdiff_t __leinp = -1;
377    if (this->eback() != nullptr)
378    {
379        __lbinp = this->eback() - __p;
380        __lninp = this->gptr() - __p;
381        __leinp = this->egptr() - __p;
382    }
383    ptrdiff_t __lbout = -1;
384    ptrdiff_t __lnout = -1;
385    ptrdiff_t __leout = -1;
386    if (this->pbase() != nullptr)
387    {
388        __lbout = this->pbase() - __p;
389        __lnout = this->pptr() - __p;
390        __leout = this->epptr() - __p;
391    }
392    ptrdiff_t __lhm = __hm_ == nullptr ? -1 : __hm_ - __p;
393    _VSTD::swap(__mode_, __rhs.__mode_);
394    __str_.swap(__rhs.__str_);
395    __p = const_cast<char_type*>(__str_.data());
396    if (__rbinp != -1)
397        this->setg(__p + __rbinp, __p + __rninp, __p + __reinp);
398    else
399        this->setg(nullptr, nullptr, nullptr);
400    if (__rbout != -1)
401    {
402        this->setp(__p + __rbout, __p + __reout);
403        this->pbump(__rnout);
404    }
405    else
406        this->setp(nullptr, nullptr);
407    __hm_ = __rhm == -1 ? nullptr : __p + __rhm;
408    __p = const_cast<char_type*>(__rhs.__str_.data());
409    if (__lbinp != -1)
410        __rhs.setg(__p + __lbinp, __p + __lninp, __p + __leinp);
411    else
412        __rhs.setg(nullptr, nullptr, nullptr);
413    if (__lbout != -1)
414    {
415        __rhs.setp(__p + __lbout, __p + __leout);
416        __rhs.pbump(__lnout);
417    }
418    else
419        __rhs.setp(nullptr, nullptr);
420    __rhs.__hm_ = __lhm == -1 ? nullptr : __p + __lhm;
421    locale __tl = __rhs.getloc();
422    __rhs.pubimbue(this->getloc());
423    this->pubimbue(__tl);
424}
425
426template <class _CharT, class _Traits, class _Allocator>
427inline _LIBCPP_INLINE_VISIBILITY
428void
429swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
430     basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
431{
432    __x.swap(__y);
433}
434
435template <class _CharT, class _Traits, class _Allocator>
436basic_string<_CharT, _Traits, _Allocator>
437basic_stringbuf<_CharT, _Traits, _Allocator>::str() const
438{
439    if (__mode_ & ios_base::out)
440    {
441        if (__hm_ < this->pptr())
442            __hm_ = this->pptr();
443        return string_type(this->pbase(), __hm_, __str_.get_allocator());
444    }
445    else if (__mode_ & ios_base::in)
446        return string_type(this->eback(), this->egptr(), __str_.get_allocator());
447    return string_type(__str_.get_allocator());
448}
449
450template <class _CharT, class _Traits, class _Allocator>
451void
452basic_stringbuf<_CharT, _Traits, _Allocator>::str(const string_type& __s)
453{
454    __str_ = __s;
455    __hm_ = 0;
456    if (__mode_ & ios_base::in)
457    {
458        __hm_ = const_cast<char_type*>(__str_.data()) + __str_.size();
459        this->setg(const_cast<char_type*>(__str_.data()),
460                   const_cast<char_type*>(__str_.data()),
461                   __hm_);
462    }
463    if (__mode_ & ios_base::out)
464    {
465        typename string_type::size_type __sz = __str_.size();
466        __hm_ = const_cast<char_type*>(__str_.data()) + __sz;
467        __str_.resize(__str_.capacity());
468        this->setp(const_cast<char_type*>(__str_.data()),
469                   const_cast<char_type*>(__str_.data()) + __str_.size());
470        if (__mode_ & (ios_base::app | ios_base::ate))
471            this->pbump(__sz);
472    }
473}
474
475template <class _CharT, class _Traits, class _Allocator>
476typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
477basic_stringbuf<_CharT, _Traits, _Allocator>::underflow()
478{
479    if (__hm_ < this->pptr())
480        __hm_ = this->pptr();
481    if (__mode_ & ios_base::in)
482    {
483        if (this->egptr() < __hm_)
484            this->setg(this->eback(), this->gptr(), __hm_);
485        if (this->gptr() < this->egptr())
486            return traits_type::to_int_type(*this->gptr());
487    }
488    return traits_type::eof();
489}
490
491template <class _CharT, class _Traits, class _Allocator>
492typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
493basic_stringbuf<_CharT, _Traits, _Allocator>::pbackfail(int_type __c)
494{
495    if (__hm_ < this->pptr())
496        __hm_ = this->pptr();
497    if (this->eback() < this->gptr())
498    {
499        if (traits_type::eq_int_type(__c, traits_type::eof()))
500        {
501            this->setg(this->eback(), this->gptr()-1, __hm_);
502            return traits_type::not_eof(__c);
503        }
504        if ((__mode_ & ios_base::out) ||
505            traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
506        {
507            this->setg(this->eback(), this->gptr()-1, __hm_);
508            *this->gptr() = traits_type::to_char_type(__c);
509            return __c;
510        }
511    }
512    return traits_type::eof();
513}
514
515template <class _CharT, class _Traits, class _Allocator>
516typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type
517basic_stringbuf<_CharT, _Traits, _Allocator>::overflow(int_type __c)
518{
519    if (!traits_type::eq_int_type(__c, traits_type::eof()))
520    {
521        ptrdiff_t __ninp = this->gptr()  - this->eback();
522        if (this->pptr() == this->epptr())
523        {
524            if (!(__mode_ & ios_base::out))
525                return traits_type::eof();
526#ifndef _LIBCPP_NO_EXCEPTIONS
527            try
528            {
529#endif  // _LIBCPP_NO_EXCEPTIONS
530                ptrdiff_t __nout = this->pptr()  - this->pbase();
531                ptrdiff_t __hm = __hm_ - this->pbase();
532                __str_.push_back(char_type());
533                __str_.resize(__str_.capacity());
534                char_type* __p = const_cast<char_type*>(__str_.data());
535                this->setp(__p, __p + __str_.size());
536                this->pbump(__nout);
537                __hm_ = this->pbase() + __hm;
538#ifndef _LIBCPP_NO_EXCEPTIONS
539            }
540            catch (...)
541            {
542                return traits_type::eof();
543            }
544#endif  // _LIBCPP_NO_EXCEPTIONS
545        }
546        __hm_ = _VSTD::max(this->pptr() + 1, __hm_);
547        if (__mode_ & ios_base::in)
548        {
549            char_type* __p = const_cast<char_type*>(__str_.data());
550            this->setg(__p, __p + __ninp, __hm_);
551        }
552        return this->sputc(__c);
553    }
554    return traits_type::not_eof(__c);
555}
556
557template <class _CharT, class _Traits, class _Allocator>
558typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type
559basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff(off_type __off,
560                                                      ios_base::seekdir __way,
561                                                      ios_base::openmode __wch)
562{
563    if (__hm_ < this->pptr())
564        __hm_ = this->pptr();
565    if ((__wch & (ios_base::in | ios_base::out)) == 0)
566        return pos_type(-1);
567    if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out)
568        && __way == ios_base::cur)
569        return pos_type(-1);
570    off_type __noff;
571    switch (__way)
572    {
573    case ios_base::beg:
574        __noff = 0;
575        break;
576    case ios_base::cur:
577        if (__wch & ios_base::in)
578            __noff = this->gptr() - this->eback();
579        else
580            __noff = this->pptr() - this->pbase();
581        break;
582    case ios_base::end:
583        __noff = __hm_ - __str_.data();
584        break;
585    default:
586        return pos_type(-1);
587    }
588    __noff += __off;
589    if (__noff < 0 || __hm_ - __str_.data() < __noff)
590        return pos_type(-1);
591    if (__noff != 0)
592    {
593        if ((__wch & ios_base::in) && this->gptr() == 0)
594            return pos_type(-1);
595        if ((__wch & ios_base::out) && this->pptr() == 0)
596            return pos_type(-1);
597    }
598    if (__wch & ios_base::in)
599        this->setg(this->eback(), this->eback() + __noff, __hm_);
600    if (__wch & ios_base::out)
601    {
602        this->setp(this->pbase(), this->epptr());
603        this->pbump(__noff);
604    }
605    return pos_type(__noff);
606}
607
608template <class _CharT, class _Traits, class _Allocator>
609typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type
610basic_stringbuf<_CharT, _Traits, _Allocator>::seekpos(pos_type __sp,
611                                                      ios_base::openmode __wch)
612{
613    return seekoff(__sp, ios_base::beg, __wch);
614}
615
616// basic_istringstream
617
618template <class _CharT, class _Traits, class _Allocator>
619class _LIBCPP_TEMPLATE_VIS basic_istringstream
620    : public basic_istream<_CharT, _Traits>
621{
622public:
623    typedef _CharT                         char_type;
624    typedef _Traits                        traits_type;
625    typedef typename traits_type::int_type int_type;
626    typedef typename traits_type::pos_type pos_type;
627    typedef typename traits_type::off_type off_type;
628    typedef _Allocator                     allocator_type;
629
630    typedef basic_string<char_type, traits_type, allocator_type> string_type;
631
632private:
633    basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
634
635public:
636    // 27.8.2.1 Constructors:
637    inline _LIBCPP_INLINE_VISIBILITY
638    explicit basic_istringstream(ios_base::openmode __wch = ios_base::in);
639    inline _LIBCPP_INLINE_VISIBILITY
640    explicit basic_istringstream(const string_type& __s,
641                                 ios_base::openmode __wch = ios_base::in);
642#ifndef _LIBCPP_CXX03_LANG
643    inline _LIBCPP_INLINE_VISIBILITY
644    basic_istringstream(basic_istringstream&& __rhs);
645
646    // 27.8.2.2 Assign and swap:
647    basic_istringstream& operator=(basic_istringstream&& __rhs);
648#endif  // _LIBCPP_CXX03_LANG
649    inline _LIBCPP_INLINE_VISIBILITY
650    void swap(basic_istringstream& __rhs);
651
652    // 27.8.2.3 Members:
653    inline _LIBCPP_INLINE_VISIBILITY
654    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
655    inline _LIBCPP_INLINE_VISIBILITY
656    string_type str() const;
657    inline _LIBCPP_INLINE_VISIBILITY
658    void str(const string_type& __s);
659};
660
661template <class _CharT, class _Traits, class _Allocator>
662basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(ios_base::openmode __wch)
663    : basic_istream<_CharT, _Traits>(&__sb_),
664      __sb_(__wch | ios_base::in)
665{
666}
667
668template <class _CharT, class _Traits, class _Allocator>
669basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(const string_type& __s,
670                                                                      ios_base::openmode __wch)
671    : basic_istream<_CharT, _Traits>(&__sb_),
672      __sb_(__s, __wch | ios_base::in)
673{
674}
675
676#ifndef _LIBCPP_CXX03_LANG
677
678template <class _CharT, class _Traits, class _Allocator>
679basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(basic_istringstream&& __rhs)
680    : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs)),
681      __sb_(_VSTD::move(__rhs.__sb_))
682{
683    basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
684}
685
686template <class _CharT, class _Traits, class _Allocator>
687basic_istringstream<_CharT, _Traits, _Allocator>&
688basic_istringstream<_CharT, _Traits, _Allocator>::operator=(basic_istringstream&& __rhs)
689{
690    basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
691    __sb_ = _VSTD::move(__rhs.__sb_);
692    return *this;
693}
694
695#endif  // _LIBCPP_CXX03_LANG
696
697template <class _CharT, class _Traits, class _Allocator>
698void basic_istringstream<_CharT, _Traits, _Allocator>::swap(basic_istringstream& __rhs)
699{
700    basic_istream<char_type, traits_type>::swap(__rhs);
701    __sb_.swap(__rhs.__sb_);
702}
703
704template <class _CharT, class _Traits, class _Allocator>
705inline _LIBCPP_INLINE_VISIBILITY
706void
707swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
708     basic_istringstream<_CharT, _Traits, _Allocator>& __y)
709{
710    __x.swap(__y);
711}
712
713template <class _CharT, class _Traits, class _Allocator>
714basic_stringbuf<_CharT, _Traits, _Allocator>*
715basic_istringstream<_CharT, _Traits, _Allocator>::rdbuf() const
716{
717    return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
718}
719
720template <class _CharT, class _Traits, class _Allocator>
721basic_string<_CharT, _Traits, _Allocator>
722basic_istringstream<_CharT, _Traits, _Allocator>::str() const
723{
724    return __sb_.str();
725}
726
727template <class _CharT, class _Traits, class _Allocator>
728void basic_istringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s)
729{
730    __sb_.str(__s);
731}
732
733// basic_ostringstream
734
735template <class _CharT, class _Traits, class _Allocator>
736class _LIBCPP_TEMPLATE_VIS basic_ostringstream
737    : public basic_ostream<_CharT, _Traits>
738{
739public:
740    typedef _CharT                         char_type;
741    typedef _Traits                        traits_type;
742    typedef typename traits_type::int_type int_type;
743    typedef typename traits_type::pos_type pos_type;
744    typedef typename traits_type::off_type off_type;
745    typedef _Allocator                     allocator_type;
746
747    typedef basic_string<char_type, traits_type, allocator_type> string_type;
748
749private:
750    basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
751
752public:
753    // 27.8.2.1 Constructors:
754    inline _LIBCPP_INLINE_VISIBILITY
755    explicit basic_ostringstream(ios_base::openmode __wch = ios_base::out);
756    inline _LIBCPP_INLINE_VISIBILITY
757    explicit basic_ostringstream(const string_type& __s,
758                                 ios_base::openmode __wch = ios_base::out);
759#ifndef _LIBCPP_CXX03_LANG
760    inline _LIBCPP_INLINE_VISIBILITY
761    basic_ostringstream(basic_ostringstream&& __rhs);
762
763    // 27.8.2.2 Assign and swap:
764    basic_ostringstream& operator=(basic_ostringstream&& __rhs);
765#endif  // _LIBCPP_CXX03_LANG
766    inline _LIBCPP_INLINE_VISIBILITY
767    void swap(basic_ostringstream& __rhs);
768
769    // 27.8.2.3 Members:
770    inline _LIBCPP_INLINE_VISIBILITY
771    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
772    inline _LIBCPP_INLINE_VISIBILITY
773    string_type str() const;
774    inline _LIBCPP_INLINE_VISIBILITY
775    void str(const string_type& __s);
776};
777
778template <class _CharT, class _Traits, class _Allocator>
779basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(ios_base::openmode __wch)
780    : basic_ostream<_CharT, _Traits>(&__sb_),
781      __sb_(__wch | ios_base::out)
782{
783}
784
785template <class _CharT, class _Traits, class _Allocator>
786basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(const string_type& __s,
787                                                                      ios_base::openmode __wch)
788    : basic_ostream<_CharT, _Traits>(&__sb_),
789      __sb_(__s, __wch | ios_base::out)
790{
791}
792
793#ifndef _LIBCPP_CXX03_LANG
794
795template <class _CharT, class _Traits, class _Allocator>
796basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(basic_ostringstream&& __rhs)
797    : basic_ostream<_CharT, _Traits>(_VSTD::move(__rhs)),
798      __sb_(_VSTD::move(__rhs.__sb_))
799{
800    basic_ostream<_CharT, _Traits>::set_rdbuf(&__sb_);
801}
802
803template <class _CharT, class _Traits, class _Allocator>
804basic_ostringstream<_CharT, _Traits, _Allocator>&
805basic_ostringstream<_CharT, _Traits, _Allocator>::operator=(basic_ostringstream&& __rhs)
806{
807    basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
808    __sb_ = _VSTD::move(__rhs.__sb_);
809    return *this;
810}
811
812#endif  // _LIBCPP_CXX03_LANG
813
814template <class _CharT, class _Traits, class _Allocator>
815void
816basic_ostringstream<_CharT, _Traits, _Allocator>::swap(basic_ostringstream& __rhs)
817{
818    basic_ostream<char_type, traits_type>::swap(__rhs);
819    __sb_.swap(__rhs.__sb_);
820}
821
822template <class _CharT, class _Traits, class _Allocator>
823inline _LIBCPP_INLINE_VISIBILITY
824void
825swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
826     basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
827{
828    __x.swap(__y);
829}
830
831template <class _CharT, class _Traits, class _Allocator>
832basic_stringbuf<_CharT, _Traits, _Allocator>*
833basic_ostringstream<_CharT, _Traits, _Allocator>::rdbuf() const
834{
835    return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
836}
837
838template <class _CharT, class _Traits, class _Allocator>
839basic_string<_CharT, _Traits, _Allocator>
840basic_ostringstream<_CharT, _Traits, _Allocator>::str() const
841{
842    return __sb_.str();
843}
844
845template <class _CharT, class _Traits, class _Allocator>
846void
847basic_ostringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s)
848{
849    __sb_.str(__s);
850}
851
852// basic_stringstream
853
854template <class _CharT, class _Traits, class _Allocator>
855class _LIBCPP_TEMPLATE_VIS basic_stringstream
856    : public basic_iostream<_CharT, _Traits>
857{
858public:
859    typedef _CharT                         char_type;
860    typedef _Traits                        traits_type;
861    typedef typename traits_type::int_type int_type;
862    typedef typename traits_type::pos_type pos_type;
863    typedef typename traits_type::off_type off_type;
864    typedef _Allocator                     allocator_type;
865
866    typedef basic_string<char_type, traits_type, allocator_type> string_type;
867
868private:
869    basic_stringbuf<char_type, traits_type, allocator_type> __sb_;
870
871public:
872    // 27.8.2.1 Constructors:
873    inline _LIBCPP_INLINE_VISIBILITY
874    explicit basic_stringstream(ios_base::openmode __wch = ios_base::in | ios_base::out);
875    inline _LIBCPP_INLINE_VISIBILITY
876    explicit basic_stringstream(const string_type& __s,
877                                ios_base::openmode __wch = ios_base::in | ios_base::out);
878#ifndef _LIBCPP_CXX03_LANG
879    inline _LIBCPP_INLINE_VISIBILITY
880    basic_stringstream(basic_stringstream&& __rhs);
881
882    // 27.8.2.2 Assign and swap:
883    basic_stringstream& operator=(basic_stringstream&& __rhs);
884#endif  // _LIBCPP_CXX03_LANG
885    inline _LIBCPP_INLINE_VISIBILITY
886    void swap(basic_stringstream& __rhs);
887
888    // 27.8.2.3 Members:
889    inline _LIBCPP_INLINE_VISIBILITY
890    basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const;
891    inline _LIBCPP_INLINE_VISIBILITY
892    string_type str() const;
893    inline _LIBCPP_INLINE_VISIBILITY
894    void str(const string_type& __s);
895};
896
897template <class _CharT, class _Traits, class _Allocator>
898basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(ios_base::openmode __wch)
899    : basic_iostream<_CharT, _Traits>(&__sb_),
900      __sb_(__wch)
901{
902}
903
904template <class _CharT, class _Traits, class _Allocator>
905basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(const string_type& __s,
906                                                                    ios_base::openmode __wch)
907    : basic_iostream<_CharT, _Traits>(&__sb_),
908      __sb_(__s, __wch)
909{
910}
911
912#ifndef _LIBCPP_CXX03_LANG
913
914template <class _CharT, class _Traits, class _Allocator>
915basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(basic_stringstream&& __rhs)
916    : basic_iostream<_CharT, _Traits>(_VSTD::move(__rhs)),
917      __sb_(_VSTD::move(__rhs.__sb_))
918{
919    basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_);
920}
921
922template <class _CharT, class _Traits, class _Allocator>
923basic_stringstream<_CharT, _Traits, _Allocator>&
924basic_stringstream<_CharT, _Traits, _Allocator>::operator=(basic_stringstream&& __rhs)
925{
926    basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
927    __sb_ = _VSTD::move(__rhs.__sb_);
928    return *this;
929}
930
931#endif  // _LIBCPP_CXX03_LANG
932
933template <class _CharT, class _Traits, class _Allocator>
934void
935basic_stringstream<_CharT, _Traits, _Allocator>::swap(basic_stringstream& __rhs)
936{
937    basic_iostream<char_type, traits_type>::swap(__rhs);
938    __sb_.swap(__rhs.__sb_);
939}
940
941template <class _CharT, class _Traits, class _Allocator>
942inline _LIBCPP_INLINE_VISIBILITY
943void
944swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
945     basic_stringstream<_CharT, _Traits, _Allocator>& __y)
946{
947    __x.swap(__y);
948}
949
950template <class _CharT, class _Traits, class _Allocator>
951basic_stringbuf<_CharT, _Traits, _Allocator>*
952basic_stringstream<_CharT, _Traits, _Allocator>::rdbuf() const
953{
954    return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_);
955}
956
957template <class _CharT, class _Traits, class _Allocator>
958basic_string<_CharT, _Traits, _Allocator>
959basic_stringstream<_CharT, _Traits, _Allocator>::str() const
960{
961    return __sb_.str();
962}
963
964template <class _CharT, class _Traits, class _Allocator>
965void
966basic_stringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s)
967{
968    __sb_.str(__s);
969}
970
971_LIBCPP_END_NAMESPACE_STD
972
973#endif  // _LIBCPP_SSTREAM
974