sstream 26 KB

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