You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. // String based streams -*- C++ -*-
  2. // Copyright (C) 1997-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/sstream
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // ISO C++ 14882: 27.7 String-based streams
  25. //
  26. #ifndef _GLIBCXX_SSTREAM
  27. #define _GLIBCXX_SSTREAM 1
  28. #pragma GCC system_header
  29. #include <istream>
  30. #include <ostream>
  31. namespace std _GLIBCXX_VISIBILITY(default)
  32. {
  33. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  34. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  35. // [27.7.1] template class basic_stringbuf
  36. /**
  37. * @brief The actual work of input and output (for std::string).
  38. * @ingroup io
  39. *
  40. * @tparam _CharT Type of character stream.
  41. * @tparam _Traits Traits for character type, defaults to
  42. * char_traits<_CharT>.
  43. * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
  44. *
  45. * This class associates either or both of its input and output sequences
  46. * with a sequence of characters, which can be initialized from, or made
  47. * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
  48. *
  49. * For this class, open modes (of type @c ios_base::openmode) have
  50. * @c in set if the input sequence can be read, and @c out set if the
  51. * output sequence can be written.
  52. */
  53. template<typename _CharT, typename _Traits, typename _Alloc>
  54. class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
  55. {
  56. struct __xfer_bufptrs;
  57. public:
  58. // Types:
  59. typedef _CharT char_type;
  60. typedef _Traits traits_type;
  61. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  62. // 251. basic_stringbuf missing allocator_type
  63. typedef _Alloc allocator_type;
  64. typedef typename traits_type::int_type int_type;
  65. typedef typename traits_type::pos_type pos_type;
  66. typedef typename traits_type::off_type off_type;
  67. typedef basic_streambuf<char_type, traits_type> __streambuf_type;
  68. typedef basic_string<char_type, _Traits, _Alloc> __string_type;
  69. typedef typename __string_type::size_type __size_type;
  70. protected:
  71. /// Place to stash in || out || in | out settings for current stringbuf.
  72. ios_base::openmode _M_mode;
  73. // Data Members:
  74. __string_type _M_string;
  75. public:
  76. // Constructors:
  77. /**
  78. * @brief Starts with an empty string buffer.
  79. *
  80. * The default constructor initializes the parent class using its
  81. * own default ctor.
  82. */
  83. basic_stringbuf()
  84. : __streambuf_type(), _M_mode(ios_base::in | ios_base::out), _M_string()
  85. { }
  86. /**
  87. * @brief Starts with an empty string buffer.
  88. * @param __mode Whether the buffer can read, or write, or both.
  89. *
  90. * The default constructor initializes the parent class using its
  91. * own default ctor.
  92. */
  93. explicit
  94. basic_stringbuf(ios_base::openmode __mode)
  95. : __streambuf_type(), _M_mode(__mode), _M_string()
  96. { }
  97. /**
  98. * @brief Starts with an existing string buffer.
  99. * @param __str A string to copy as a starting buffer.
  100. * @param __mode Whether the buffer can read, or write, or both.
  101. *
  102. * This constructor initializes the parent class using its
  103. * own default ctor.
  104. */
  105. explicit
  106. basic_stringbuf(const __string_type& __str,
  107. ios_base::openmode __mode = ios_base::in | ios_base::out)
  108. : __streambuf_type(), _M_mode(),
  109. _M_string(__str.data(), __str.size(), __str.get_allocator())
  110. { _M_stringbuf_init(__mode); }
  111. #if __cplusplus >= 201103L
  112. basic_stringbuf(const basic_stringbuf&) = delete;
  113. basic_stringbuf(basic_stringbuf&& __rhs)
  114. : basic_stringbuf(std::move(__rhs), __xfer_bufptrs(__rhs, this))
  115. { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
  116. // 27.8.2.2 Assign and swap:
  117. basic_stringbuf&
  118. operator=(const basic_stringbuf&) = delete;
  119. basic_stringbuf&
  120. operator=(basic_stringbuf&& __rhs)
  121. {
  122. __xfer_bufptrs __st{__rhs, this};
  123. const __streambuf_type& __base = __rhs;
  124. __streambuf_type::operator=(__base);
  125. this->pubimbue(__rhs.getloc());
  126. _M_mode = __rhs._M_mode;
  127. _M_string = std::move(__rhs._M_string);
  128. __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0);
  129. return *this;
  130. }
  131. void
  132. swap(basic_stringbuf& __rhs)
  133. {
  134. __xfer_bufptrs __l_st{*this, std::__addressof(__rhs)};
  135. __xfer_bufptrs __r_st{__rhs, this};
  136. __streambuf_type& __base = __rhs;
  137. __streambuf_type::swap(__base);
  138. __rhs.pubimbue(this->pubimbue(__rhs.getloc()));
  139. std::swap(_M_mode, __rhs._M_mode);
  140. std::swap(_M_string, __rhs._M_string);
  141. }
  142. #endif
  143. // Get and set:
  144. /**
  145. * @brief Copying out the string buffer.
  146. * @return A copy of one of the underlying sequences.
  147. *
  148. * <em>If the buffer is only created in input mode, the underlying
  149. * character sequence is equal to the input sequence; otherwise, it
  150. * is equal to the output sequence.</em> [27.7.1.2]/1
  151. */
  152. __string_type
  153. str() const
  154. {
  155. __string_type __ret(_M_string.get_allocator());
  156. if (this->pptr())
  157. {
  158. // The current egptr() may not be the actual string end.
  159. if (this->pptr() > this->egptr())
  160. __ret.assign(this->pbase(), this->pptr());
  161. else
  162. __ret.assign(this->pbase(), this->egptr());
  163. }
  164. else
  165. __ret = _M_string;
  166. return __ret;
  167. }
  168. /**
  169. * @brief Setting a new buffer.
  170. * @param __s The string to use as a new sequence.
  171. *
  172. * Deallocates any previous stored sequence, then copies @a s to
  173. * use as a new one.
  174. */
  175. void
  176. str(const __string_type& __s)
  177. {
  178. // Cannot use _M_string = __s, since v3 strings are COW
  179. // (not always true now but assign() always works).
  180. _M_string.assign(__s.data(), __s.size());
  181. _M_stringbuf_init(_M_mode);
  182. }
  183. protected:
  184. // Common initialization code goes here.
  185. void
  186. _M_stringbuf_init(ios_base::openmode __mode)
  187. {
  188. _M_mode = __mode;
  189. __size_type __len = 0;
  190. if (_M_mode & (ios_base::ate | ios_base::app))
  191. __len = _M_string.size();
  192. _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
  193. }
  194. virtual streamsize
  195. showmanyc()
  196. {
  197. streamsize __ret = -1;
  198. if (_M_mode & ios_base::in)
  199. {
  200. _M_update_egptr();
  201. __ret = this->egptr() - this->gptr();
  202. }
  203. return __ret;
  204. }
  205. virtual int_type
  206. underflow();
  207. virtual int_type
  208. pbackfail(int_type __c = traits_type::eof());
  209. virtual int_type
  210. overflow(int_type __c = traits_type::eof());
  211. /**
  212. * @brief Manipulates the buffer.
  213. * @param __s Pointer to a buffer area.
  214. * @param __n Size of @a __s.
  215. * @return @c this
  216. *
  217. * If no buffer has already been created, and both @a __s and @a __n are
  218. * non-zero, then @c __s is used as a buffer; see
  219. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
  220. * for more.
  221. */
  222. virtual __streambuf_type*
  223. setbuf(char_type* __s, streamsize __n)
  224. {
  225. if (__s && __n >= 0)
  226. {
  227. // This is implementation-defined behavior, and assumes
  228. // that an external char_type array of length __n exists
  229. // and has been pre-allocated. If this is not the case,
  230. // things will quickly blow up.
  231. // Step 1: Destroy the current internal array.
  232. _M_string.clear();
  233. // Step 2: Use the external array.
  234. _M_sync(__s, __n, 0);
  235. }
  236. return this;
  237. }
  238. virtual pos_type
  239. seekoff(off_type __off, ios_base::seekdir __way,
  240. ios_base::openmode __mode = ios_base::in | ios_base::out);
  241. virtual pos_type
  242. seekpos(pos_type __sp,
  243. ios_base::openmode __mode = ios_base::in | ios_base::out);
  244. // Internal function for correctly updating the internal buffer
  245. // for a particular _M_string, due to initialization or re-sizing
  246. // of an existing _M_string.
  247. void
  248. _M_sync(char_type* __base, __size_type __i, __size_type __o);
  249. // Internal function for correctly updating egptr() to the actual
  250. // string end.
  251. void
  252. _M_update_egptr()
  253. {
  254. const bool __testin = _M_mode & ios_base::in;
  255. if (this->pptr() && this->pptr() > this->egptr())
  256. {
  257. if (__testin)
  258. this->setg(this->eback(), this->gptr(), this->pptr());
  259. else
  260. this->setg(this->pptr(), this->pptr(), this->pptr());
  261. }
  262. }
  263. // Works around the issue with pbump, part of the protected
  264. // interface of basic_streambuf, taking just an int.
  265. void
  266. _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off);
  267. private:
  268. #if __cplusplus >= 201103L
  269. #if _GLIBCXX_USE_CXX11_ABI
  270. // This type captures the state of the gptr / pptr pointers as offsets
  271. // so they can be restored in another object after moving the string.
  272. struct __xfer_bufptrs
  273. {
  274. __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to)
  275. : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1}
  276. {
  277. const _CharT* const __str = __from._M_string.data();
  278. const _CharT* __end = nullptr;
  279. if (__from.eback())
  280. {
  281. _M_goff[0] = __from.eback() - __str;
  282. _M_goff[1] = __from.gptr() - __str;
  283. _M_goff[2] = __from.egptr() - __str;
  284. __end = __from.egptr();
  285. }
  286. if (__from.pbase())
  287. {
  288. _M_poff[0] = __from.pbase() - __str;
  289. _M_poff[1] = __from.pptr() - __from.pbase();
  290. _M_poff[2] = __from.epptr() - __str;
  291. if (__from.pptr() > __end)
  292. __end = __from.pptr();
  293. }
  294. // Set _M_string length to the greater of the get and put areas.
  295. if (__end)
  296. {
  297. // The const_cast avoids changing this constructor's signature,
  298. // because it is exported from the dynamic library.
  299. auto& __mut_from = const_cast<basic_stringbuf&>(__from);
  300. __mut_from._M_string._M_length(__end - __str);
  301. }
  302. }
  303. ~__xfer_bufptrs()
  304. {
  305. char_type* __str = const_cast<char_type*>(_M_to->_M_string.data());
  306. if (_M_goff[0] != -1)
  307. _M_to->setg(__str+_M_goff[0], __str+_M_goff[1], __str+_M_goff[2]);
  308. if (_M_poff[0] != -1)
  309. _M_to->_M_pbump(__str+_M_poff[0], __str+_M_poff[2], _M_poff[1]);
  310. }
  311. basic_stringbuf* _M_to;
  312. off_type _M_goff[3];
  313. off_type _M_poff[3];
  314. };
  315. #else
  316. // This type does nothing when using Copy-On-Write strings.
  317. struct __xfer_bufptrs
  318. {
  319. __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { }
  320. };
  321. #endif
  322. // The move constructor initializes an __xfer_bufptrs temporary then
  323. // delegates to this constructor to performs moves during its lifetime.
  324. basic_stringbuf(basic_stringbuf&& __rhs, __xfer_bufptrs&&)
  325. : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
  326. _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string))
  327. { }
  328. #endif
  329. };
  330. // [27.7.2] Template class basic_istringstream
  331. /**
  332. * @brief Controlling input for std::string.
  333. * @ingroup io
  334. *
  335. * @tparam _CharT Type of character stream.
  336. * @tparam _Traits Traits for character type, defaults to
  337. * char_traits<_CharT>.
  338. * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
  339. *
  340. * This class supports reading from objects of type std::basic_string,
  341. * using the inherited functions from std::basic_istream. To control
  342. * the associated sequence, an instance of std::basic_stringbuf is used,
  343. * which this page refers to as @c sb.
  344. */
  345. template<typename _CharT, typename _Traits, typename _Alloc>
  346. class basic_istringstream : public basic_istream<_CharT, _Traits>
  347. {
  348. public:
  349. // Types:
  350. typedef _CharT char_type;
  351. typedef _Traits traits_type;
  352. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  353. // 251. basic_stringbuf missing allocator_type
  354. typedef _Alloc allocator_type;
  355. typedef typename traits_type::int_type int_type;
  356. typedef typename traits_type::pos_type pos_type;
  357. typedef typename traits_type::off_type off_type;
  358. // Non-standard types:
  359. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  360. typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
  361. typedef basic_istream<char_type, traits_type> __istream_type;
  362. private:
  363. __stringbuf_type _M_stringbuf;
  364. public:
  365. // Constructors:
  366. /**
  367. * @brief Default constructor starts with an empty string buffer.
  368. *
  369. * Initializes @c sb using @c in, and passes @c &sb to the base
  370. * class initializer. Does not allocate any buffer.
  371. *
  372. * That's a lie. We initialize the base class with NULL, because the
  373. * string class does its own memory management.
  374. */
  375. basic_istringstream()
  376. : __istream_type(), _M_stringbuf(ios_base::in)
  377. { this->init(&_M_stringbuf); }
  378. /**
  379. * @brief Starts with an empty string buffer.
  380. * @param __mode Whether the buffer can read, or write, or both.
  381. *
  382. * @c ios_base::in is automatically included in @a __mode.
  383. *
  384. * Initializes @c sb using @c __mode|in, and passes @c &sb to the base
  385. * class initializer. Does not allocate any buffer.
  386. *
  387. * That's a lie. We initialize the base class with NULL, because the
  388. * string class does its own memory management.
  389. */
  390. explicit
  391. basic_istringstream(ios_base::openmode __mode)
  392. : __istream_type(), _M_stringbuf(__mode | ios_base::in)
  393. { this->init(&_M_stringbuf); }
  394. /**
  395. * @brief Starts with an existing string buffer.
  396. * @param __str A string to copy as a starting buffer.
  397. * @param __mode Whether the buffer can read, or write, or both.
  398. *
  399. * @c ios_base::in is automatically included in @a mode.
  400. *
  401. * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
  402. * to the base class initializer.
  403. *
  404. * That's a lie. We initialize the base class with NULL, because the
  405. * string class does its own memory management.
  406. */
  407. explicit
  408. basic_istringstream(const __string_type& __str,
  409. ios_base::openmode __mode = ios_base::in)
  410. : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
  411. { this->init(&_M_stringbuf); }
  412. /**
  413. * @brief The destructor does nothing.
  414. *
  415. * The buffer is deallocated by the stringbuf object, not the
  416. * formatting stream.
  417. */
  418. ~basic_istringstream()
  419. { }
  420. #if __cplusplus >= 201103L
  421. basic_istringstream(const basic_istringstream&) = delete;
  422. basic_istringstream(basic_istringstream&& __rhs)
  423. : __istream_type(std::move(__rhs)),
  424. _M_stringbuf(std::move(__rhs._M_stringbuf))
  425. { __istream_type::set_rdbuf(&_M_stringbuf); }
  426. // 27.8.3.2 Assign and swap:
  427. basic_istringstream&
  428. operator=(const basic_istringstream&) = delete;
  429. basic_istringstream&
  430. operator=(basic_istringstream&& __rhs)
  431. {
  432. __istream_type::operator=(std::move(__rhs));
  433. _M_stringbuf = std::move(__rhs._M_stringbuf);
  434. return *this;
  435. }
  436. void
  437. swap(basic_istringstream& __rhs)
  438. {
  439. __istream_type::swap(__rhs);
  440. _M_stringbuf.swap(__rhs._M_stringbuf);
  441. }
  442. #endif
  443. // Members:
  444. /**
  445. * @brief Accessing the underlying buffer.
  446. * @return The current basic_stringbuf buffer.
  447. *
  448. * This hides both signatures of std::basic_ios::rdbuf().
  449. */
  450. __stringbuf_type*
  451. rdbuf() const
  452. { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
  453. /**
  454. * @brief Copying out the string buffer.
  455. * @return @c rdbuf()->str()
  456. */
  457. __string_type
  458. str() const
  459. { return _M_stringbuf.str(); }
  460. /**
  461. * @brief Setting a new buffer.
  462. * @param __s The string to use as a new sequence.
  463. *
  464. * Calls @c rdbuf()->str(s).
  465. */
  466. void
  467. str(const __string_type& __s)
  468. { _M_stringbuf.str(__s); }
  469. };
  470. // [27.7.3] Template class basic_ostringstream
  471. /**
  472. * @brief Controlling output for std::string.
  473. * @ingroup io
  474. *
  475. * @tparam _CharT Type of character stream.
  476. * @tparam _Traits Traits for character type, defaults to
  477. * char_traits<_CharT>.
  478. * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
  479. *
  480. * This class supports writing to objects of type std::basic_string,
  481. * using the inherited functions from std::basic_ostream. To control
  482. * the associated sequence, an instance of std::basic_stringbuf is used,
  483. * which this page refers to as @c sb.
  484. */
  485. template <typename _CharT, typename _Traits, typename _Alloc>
  486. class basic_ostringstream : public basic_ostream<_CharT, _Traits>
  487. {
  488. public:
  489. // Types:
  490. typedef _CharT char_type;
  491. typedef _Traits traits_type;
  492. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  493. // 251. basic_stringbuf missing allocator_type
  494. typedef _Alloc allocator_type;
  495. typedef typename traits_type::int_type int_type;
  496. typedef typename traits_type::pos_type pos_type;
  497. typedef typename traits_type::off_type off_type;
  498. // Non-standard types:
  499. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  500. typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
  501. typedef basic_ostream<char_type, traits_type> __ostream_type;
  502. private:
  503. __stringbuf_type _M_stringbuf;
  504. public:
  505. // Constructors/destructor:
  506. /**
  507. * @brief Default constructor starts with an empty string buffer.
  508. *
  509. * Initializes @c sb using @c mode|out, and passes @c &sb to the base
  510. * class initializer. Does not allocate any buffer.
  511. *
  512. * That's a lie. We initialize the base class with NULL, because the
  513. * string class does its own memory management.
  514. */
  515. basic_ostringstream()
  516. : __ostream_type(), _M_stringbuf(ios_base::out)
  517. { this->init(&_M_stringbuf); }
  518. /**
  519. * @brief Starts with an empty string buffer.
  520. * @param __mode Whether the buffer can read, or write, or both.
  521. *
  522. * @c ios_base::out is automatically included in @a mode.
  523. *
  524. * Initializes @c sb using @c mode|out, and passes @c &sb to the base
  525. * class initializer. Does not allocate any buffer.
  526. *
  527. * That's a lie. We initialize the base class with NULL, because the
  528. * string class does its own memory management.
  529. */
  530. explicit
  531. basic_ostringstream(ios_base::openmode __mode)
  532. : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
  533. { this->init(&_M_stringbuf); }
  534. /**
  535. * @brief Starts with an existing string buffer.
  536. * @param __str A string to copy as a starting buffer.
  537. * @param __mode Whether the buffer can read, or write, or both.
  538. *
  539. * @c ios_base::out is automatically included in @a mode.
  540. *
  541. * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
  542. * to the base class initializer.
  543. *
  544. * That's a lie. We initialize the base class with NULL, because the
  545. * string class does its own memory management.
  546. */
  547. explicit
  548. basic_ostringstream(const __string_type& __str,
  549. ios_base::openmode __mode = ios_base::out)
  550. : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
  551. { this->init(&_M_stringbuf); }
  552. /**
  553. * @brief The destructor does nothing.
  554. *
  555. * The buffer is deallocated by the stringbuf object, not the
  556. * formatting stream.
  557. */
  558. ~basic_ostringstream()
  559. { }
  560. #if __cplusplus >= 201103L
  561. basic_ostringstream(const basic_ostringstream&) = delete;
  562. basic_ostringstream(basic_ostringstream&& __rhs)
  563. : __ostream_type(std::move(__rhs)),
  564. _M_stringbuf(std::move(__rhs._M_stringbuf))
  565. { __ostream_type::set_rdbuf(&_M_stringbuf); }
  566. // 27.8.3.2 Assign and swap:
  567. basic_ostringstream&
  568. operator=(const basic_ostringstream&) = delete;
  569. basic_ostringstream&
  570. operator=(basic_ostringstream&& __rhs)
  571. {
  572. __ostream_type::operator=(std::move(__rhs));
  573. _M_stringbuf = std::move(__rhs._M_stringbuf);
  574. return *this;
  575. }
  576. void
  577. swap(basic_ostringstream& __rhs)
  578. {
  579. __ostream_type::swap(__rhs);
  580. _M_stringbuf.swap(__rhs._M_stringbuf);
  581. }
  582. #endif
  583. // Members:
  584. /**
  585. * @brief Accessing the underlying buffer.
  586. * @return The current basic_stringbuf buffer.
  587. *
  588. * This hides both signatures of std::basic_ios::rdbuf().
  589. */
  590. __stringbuf_type*
  591. rdbuf() const
  592. { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
  593. /**
  594. * @brief Copying out the string buffer.
  595. * @return @c rdbuf()->str()
  596. */
  597. __string_type
  598. str() const
  599. { return _M_stringbuf.str(); }
  600. /**
  601. * @brief Setting a new buffer.
  602. * @param __s The string to use as a new sequence.
  603. *
  604. * Calls @c rdbuf()->str(s).
  605. */
  606. void
  607. str(const __string_type& __s)
  608. { _M_stringbuf.str(__s); }
  609. };
  610. // [27.7.4] Template class basic_stringstream
  611. /**
  612. * @brief Controlling input and output for std::string.
  613. * @ingroup io
  614. *
  615. * @tparam _CharT Type of character stream.
  616. * @tparam _Traits Traits for character type, defaults to
  617. * char_traits<_CharT>.
  618. * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
  619. *
  620. * This class supports reading from and writing to objects of type
  621. * std::basic_string, using the inherited functions from
  622. * std::basic_iostream. To control the associated sequence, an instance
  623. * of std::basic_stringbuf is used, which this page refers to as @c sb.
  624. */
  625. template <typename _CharT, typename _Traits, typename _Alloc>
  626. class basic_stringstream : public basic_iostream<_CharT, _Traits>
  627. {
  628. public:
  629. // Types:
  630. typedef _CharT char_type;
  631. typedef _Traits traits_type;
  632. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  633. // 251. basic_stringbuf missing allocator_type
  634. typedef _Alloc allocator_type;
  635. typedef typename traits_type::int_type int_type;
  636. typedef typename traits_type::pos_type pos_type;
  637. typedef typename traits_type::off_type off_type;
  638. // Non-standard Types:
  639. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  640. typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
  641. typedef basic_iostream<char_type, traits_type> __iostream_type;
  642. private:
  643. __stringbuf_type _M_stringbuf;
  644. public:
  645. // Constructors/destructors
  646. /**
  647. * @brief Default constructor starts with an empty string buffer.
  648. *
  649. * Initializes @c sb using the mode @c in|out, and passes @c &sb
  650. * to the base class initializer. Does not allocate any buffer.
  651. *
  652. * That's a lie. We initialize the base class with NULL, because the
  653. * string class does its own memory management.
  654. */
  655. basic_stringstream()
  656. : __iostream_type(), _M_stringbuf(ios_base::out | ios_base::in)
  657. { this->init(&_M_stringbuf); }
  658. /**
  659. * @brief Starts with an empty string buffer.
  660. * @param __m Whether the buffer can read, or write, or both.
  661. *
  662. * Initializes @c sb using the mode from @c __m, and passes @c &sb
  663. * to the base class initializer. Does not allocate any buffer.
  664. *
  665. * That's a lie. We initialize the base class with NULL, because the
  666. * string class does its own memory management.
  667. */
  668. explicit
  669. basic_stringstream(ios_base::openmode __m)
  670. : __iostream_type(), _M_stringbuf(__m)
  671. { this->init(&_M_stringbuf); }
  672. /**
  673. * @brief Starts with an existing string buffer.
  674. * @param __str A string to copy as a starting buffer.
  675. * @param __m Whether the buffer can read, or write, or both.
  676. *
  677. * Initializes @c sb using @a __str and @c __m, and passes @c &sb
  678. * to the base class initializer.
  679. *
  680. * That's a lie. We initialize the base class with NULL, because the
  681. * string class does its own memory management.
  682. */
  683. explicit
  684. basic_stringstream(const __string_type& __str,
  685. ios_base::openmode __m = ios_base::out | ios_base::in)
  686. : __iostream_type(), _M_stringbuf(__str, __m)
  687. { this->init(&_M_stringbuf); }
  688. /**
  689. * @brief The destructor does nothing.
  690. *
  691. * The buffer is deallocated by the stringbuf object, not the
  692. * formatting stream.
  693. */
  694. ~basic_stringstream()
  695. { }
  696. #if __cplusplus >= 201103L
  697. basic_stringstream(const basic_stringstream&) = delete;
  698. basic_stringstream(basic_stringstream&& __rhs)
  699. : __iostream_type(std::move(__rhs)),
  700. _M_stringbuf(std::move(__rhs._M_stringbuf))
  701. { __iostream_type::set_rdbuf(&_M_stringbuf); }
  702. // 27.8.3.2 Assign and swap:
  703. basic_stringstream&
  704. operator=(const basic_stringstream&) = delete;
  705. basic_stringstream&
  706. operator=(basic_stringstream&& __rhs)
  707. {
  708. __iostream_type::operator=(std::move(__rhs));
  709. _M_stringbuf = std::move(__rhs._M_stringbuf);
  710. return *this;
  711. }
  712. void
  713. swap(basic_stringstream& __rhs)
  714. {
  715. __iostream_type::swap(__rhs);
  716. _M_stringbuf.swap(__rhs._M_stringbuf);
  717. }
  718. #endif
  719. // Members:
  720. /**
  721. * @brief Accessing the underlying buffer.
  722. * @return The current basic_stringbuf buffer.
  723. *
  724. * This hides both signatures of std::basic_ios::rdbuf().
  725. */
  726. __stringbuf_type*
  727. rdbuf() const
  728. { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
  729. /**
  730. * @brief Copying out the string buffer.
  731. * @return @c rdbuf()->str()
  732. */
  733. __string_type
  734. str() const
  735. { return _M_stringbuf.str(); }
  736. /**
  737. * @brief Setting a new buffer.
  738. * @param __s The string to use as a new sequence.
  739. *
  740. * Calls @c rdbuf()->str(s).
  741. */
  742. void
  743. str(const __string_type& __s)
  744. { _M_stringbuf.str(__s); }
  745. };
  746. #if __cplusplus >= 201103L
  747. /// Swap specialization for stringbufs.
  748. template <class _CharT, class _Traits, class _Allocator>
  749. inline void
  750. swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
  751. basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
  752. { __x.swap(__y); }
  753. /// Swap specialization for istringstreams.
  754. template <class _CharT, class _Traits, class _Allocator>
  755. inline void
  756. swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
  757. basic_istringstream<_CharT, _Traits, _Allocator>& __y)
  758. { __x.swap(__y); }
  759. /// Swap specialization for ostringstreams.
  760. template <class _CharT, class _Traits, class _Allocator>
  761. inline void
  762. swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
  763. basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
  764. { __x.swap(__y); }
  765. /// Swap specialization for stringstreams.
  766. template <class _CharT, class _Traits, class _Allocator>
  767. inline void
  768. swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
  769. basic_stringstream<_CharT, _Traits, _Allocator>& __y)
  770. { __x.swap(__y); }
  771. #endif
  772. _GLIBCXX_END_NAMESPACE_CXX11
  773. _GLIBCXX_END_NAMESPACE_VERSION
  774. } // namespace
  775. #include <bits/sstream.tcc>
  776. #endif /* _GLIBCXX_SSTREAM */