spanstream 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // Streams based on std::span -*- C++ -*-
  2. // Copyright The GNU Toolchain Authors.
  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 spanstream
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_SPANSTREAM
  24. #define _GLIBCXX_SPANSTREAM 1
  25. #pragma GCC system_header
  26. #if __cplusplus > 202002L
  27. #include <span>
  28. #include <streambuf>
  29. #include <istream>
  30. #include <ostream>
  31. #include <bits/ranges_base.h>
  32. #if __cpp_lib_span
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  36. #define __cpp_lib_spanstream 202106L
  37. template<typename _CharT, typename _Traits>
  38. class basic_spanbuf
  39. : public basic_streambuf<_CharT, _Traits>
  40. {
  41. using __streambuf_type = basic_streambuf<_CharT, _Traits>;
  42. public:
  43. using char_type = _CharT;
  44. using int_type = typename _Traits::int_type;
  45. using pos_type = typename _Traits::pos_type;
  46. using off_type = typename _Traits::off_type;
  47. using traits_type = _Traits;
  48. // [spanbuf.ctor], constructors
  49. basic_spanbuf() : basic_spanbuf(ios_base::in | ios_base::out)
  50. { }
  51. explicit
  52. basic_spanbuf(ios_base::openmode __which)
  53. : __streambuf_type(), _M_mode(__which)
  54. { }
  55. explicit
  56. basic_spanbuf(std::span<_CharT> __s,
  57. ios_base::openmode __which = ios_base::in | ios_base::out)
  58. : __streambuf_type(), _M_mode(__which)
  59. { span(__s); }
  60. basic_spanbuf(const basic_spanbuf&) = delete;
  61. /** Move constructor.
  62. *
  63. * Transfers the buffer and pointers into the get and put areas from
  64. * `__rhs` to `*this`.
  65. *
  66. * In this implementation `rhs` is left unchanged,
  67. * but that is not guaranteed by the standard.
  68. */
  69. basic_spanbuf(basic_spanbuf&& __rhs)
  70. : __streambuf_type(__rhs), _M_mode(__rhs._M_mode), _M_buf(__rhs._M_buf)
  71. { }
  72. // [spanbuf.assign], assignment and swap
  73. basic_spanbuf& operator=(const basic_spanbuf&) = delete;
  74. basic_spanbuf&
  75. operator=(basic_spanbuf&& __rhs)
  76. {
  77. basic_spanbuf(std::move(__rhs)).swap(*this);
  78. return *this;
  79. }
  80. void
  81. swap(basic_spanbuf& __rhs)
  82. {
  83. __streambuf_type::swap(__rhs);
  84. std::swap(_M_mode, __rhs._M_mode);
  85. std::swap(_M_buf, __rhs._M_buf);
  86. }
  87. // [spanbuf.members], member functions
  88. std::span<_CharT>
  89. span() const noexcept
  90. {
  91. if (_M_mode & ios_base::out)
  92. return {this->pbase(), this->pptr()};
  93. else
  94. return _M_buf;
  95. }
  96. void
  97. span(std::span<_CharT> __s) noexcept
  98. {
  99. _M_buf = __s;
  100. if (_M_mode & ios_base::out)
  101. {
  102. this->setp(__s.data(), __s.data() + __s.size());
  103. if (_M_mode & ios_base::ate)
  104. this->pbump(__s.size());
  105. }
  106. if (_M_mode & ios_base::in)
  107. this->setg(__s.data(), __s.data(), __s.data() + __s.size());
  108. }
  109. protected:
  110. // [spanbuf.virtuals], overridden virtual functions
  111. basic_streambuf<_CharT, _Traits>*
  112. setbuf(_CharT* __s, streamsize __n) override
  113. {
  114. span({__s, __n});
  115. return this;
  116. }
  117. pos_type
  118. seekoff(off_type __off, ios_base::seekdir __way,
  119. ios_base::openmode __which = ios_base::in | ios_base::out) override
  120. {
  121. pos_type __ret = pos_type(off_type(-1));
  122. if (__way == ios_base::beg)
  123. {
  124. if (0 <= __off && __off <= _M_buf.size())
  125. {
  126. if (__which & ios_base::in)
  127. this->setg(this->eback(), this->eback() + __off, this->egptr());
  128. if (__which & ios_base::out)
  129. {
  130. this->setp(this->pbase(), this->epptr());
  131. this->pbump(__off);
  132. }
  133. __ret = pos_type(__off);
  134. }
  135. }
  136. else
  137. {
  138. off_type __base;
  139. __which &= (ios_base::in|ios_base::out);
  140. if (__which == ios_base::out)
  141. __base = this->pptr() - this->pbase();
  142. else if (__way == ios_base::cur)
  143. {
  144. if (__which == ios_base::in)
  145. __base = this->gptr() - this->eback();
  146. else
  147. return __ret;
  148. }
  149. else if (__way == ios_base::end)
  150. __base = _M_buf.size();
  151. if (__builtin_add_overflow(__base, __off, &__off))
  152. return __ret;
  153. if (__off < 0 || __off > _M_buf.size())
  154. return __ret;
  155. if (__which & ios_base::in)
  156. this->setg(this->eback(), this->eback() + __off, this->egptr());
  157. if (__which & ios_base::out)
  158. {
  159. this->setp(this->pbase(), this->epptr());
  160. this->pbump(__off);
  161. }
  162. __ret = pos_type(__off);
  163. }
  164. return __ret;
  165. }
  166. pos_type
  167. seekpos(pos_type __sp,
  168. ios_base::openmode __which = ios_base::in | ios_base::out) override
  169. { return seekoff(off_type(__sp), ios_base::beg, __which); }
  170. private:
  171. ios_base::openmode _M_mode;
  172. std::span<_CharT> _M_buf;
  173. };
  174. template<typename _CharT, typename _Traits>
  175. inline void
  176. swap(basic_spanbuf<_CharT, _Traits>& __x,
  177. basic_spanbuf<_CharT, _Traits>& __y)
  178. { __x.swap(__y); }
  179. using spanbuf = basic_spanbuf<char>;
  180. using wspanbuf = basic_spanbuf<wchar_t>;
  181. template<typename _CharT, typename _Traits>
  182. class basic_ispanstream
  183. : public basic_istream<_CharT, _Traits>
  184. {
  185. using __istream_type = basic_istream<_CharT, _Traits>;
  186. public:
  187. using char_type = _CharT;
  188. using int_type = typename _Traits::int_type;
  189. using pos_type = typename _Traits::pos_type;
  190. using off_type = typename _Traits::off_type;
  191. using traits_type = _Traits;
  192. // [ispanstream.ctor], constructors
  193. explicit
  194. basic_ispanstream(std::span<_CharT> __s,
  195. ios_base::openmode __which = ios_base::in)
  196. : __istream_type(std::__addressof(_M_sb)),
  197. _M_sb(__s, __which | ios_base::in)
  198. { }
  199. basic_ispanstream(const basic_ispanstream&) = delete;
  200. basic_ispanstream(basic_ispanstream&& __rhs)
  201. : __istream_type(std::move(__rhs)), _M_sb(std::move(__rhs._M_sb))
  202. {
  203. __istream_type::set_rdbuf(std::addressof(_M_sb));
  204. }
  205. template<typename _Ros>
  206. requires ranges::borrowed_range<_Ros>
  207. && (!convertible_to<_Ros, std::span<_CharT>>)
  208. && convertible_to<_Ros, std::span<const _CharT>>
  209. explicit
  210. basic_ispanstream(_Ros&& __s)
  211. : __istream_type(std::__addressof(_M_sb)),
  212. _M_sb(ios_base::in)
  213. {
  214. std::span<const _CharT> __sp(std::forward<_Ros>(__s));
  215. _M_sb.span({const_cast<_CharT*>(__sp.data()), __sp.size()});
  216. }
  217. // [ispanstream.assign], assignment and swap
  218. basic_ispanstream& operator=(const basic_ispanstream&) = delete;
  219. basic_ispanstream& operator=(basic_ispanstream&& __rhs) = default;
  220. void
  221. swap(basic_ispanstream& __rhs)
  222. {
  223. __istream_type::swap(__rhs);
  224. _M_sb.swap(__rhs._M_sb);
  225. }
  226. // [ispanstream.members], member functions
  227. basic_spanbuf<_CharT, _Traits>*
  228. rdbuf() const noexcept
  229. {
  230. return const_cast<basic_spanbuf<_CharT, _Traits>*>(std::__addressof(_M_sb));
  231. }
  232. std::span<const _CharT>
  233. span() const noexcept
  234. { return _M_sb.span(); }
  235. void
  236. span(std::span<_CharT> __s) noexcept
  237. { return _M_sb.span(__s); }
  238. template<typename _Ros>
  239. requires ranges::borrowed_range<_Ros>
  240. && (!convertible_to<_Ros, std::span<_CharT>>)
  241. && convertible_to<_Ros, std::span<const _CharT>>
  242. void
  243. span(_Ros&& __s) noexcept
  244. {
  245. std::span<const _CharT> __sp(std::forward<_Ros>(__s));
  246. _M_sb.span({const_cast<_CharT*>(__sp.data()), __sp.size()});
  247. }
  248. private:
  249. basic_spanbuf<_CharT, _Traits> _M_sb;
  250. };
  251. template<typename _CharT, typename _Traits>
  252. inline void
  253. swap(basic_ispanstream<_CharT, _Traits>& __x,
  254. basic_ispanstream<_CharT, _Traits>& __y)
  255. { __x.swap(__y); }
  256. using ispanstream = basic_ispanstream<char>;
  257. using wispanstream = basic_ispanstream<wchar_t>;
  258. template<typename _CharT, typename _Traits>
  259. class basic_ospanstream
  260. : public basic_ostream<_CharT, _Traits>
  261. {
  262. using __ostream_type = basic_ostream<_CharT, _Traits>;
  263. public:
  264. using char_type = _CharT;
  265. using int_type = typename _Traits::int_type;
  266. using pos_type = typename _Traits::pos_type;
  267. using off_type = typename _Traits::off_type;
  268. using traits_type = _Traits;
  269. // [ospanstream.ctor], constructors
  270. explicit
  271. basic_ospanstream(std::span<_CharT> __s,
  272. ios_base::openmode __which = ios_base::out)
  273. : __ostream_type(std::__addressof(_M_sb)),
  274. _M_sb(__s, __which | ios_base::in)
  275. { }
  276. basic_ospanstream(const basic_ospanstream&) = delete;
  277. basic_ospanstream(basic_ospanstream&& __rhs)
  278. : __ostream_type(std::move(__rhs)), _M_sb(std::move(__rhs._M_sb))
  279. {
  280. __ostream_type::set_rdbuf(std::addressof(_M_sb));
  281. }
  282. // [ospanstream.assign], assignment and swap
  283. basic_ospanstream& operator=(const basic_ospanstream&) = delete;
  284. basic_ospanstream& operator=(basic_ospanstream&& __rhs) = default;
  285. void
  286. swap(basic_ospanstream& __rhs)
  287. {
  288. __ostream_type::swap(__rhs);
  289. _M_sb.swap(__rhs._M_sb);
  290. }
  291. // [ospanstream.members], member functions
  292. basic_spanbuf<_CharT, _Traits>*
  293. rdbuf() const noexcept
  294. {
  295. return const_cast<basic_spanbuf<_CharT, _Traits>*>(std::__addressof(_M_sb));
  296. }
  297. std::span<_CharT>
  298. span() const noexcept
  299. { return _M_sb.span(); }
  300. void
  301. span(std::span<_CharT> __s) noexcept
  302. { return _M_sb.span(__s); }
  303. private:
  304. basic_spanbuf<_CharT, _Traits> _M_sb;
  305. };
  306. template<typename _CharT, typename _Traits>
  307. inline void
  308. swap(basic_ospanstream<_CharT, _Traits>& __x,
  309. basic_ospanstream<_CharT, _Traits>& __y)
  310. { __x.swap(__y); }
  311. using ospanstream = basic_ospanstream<char>;
  312. using wospanstream = basic_ospanstream<wchar_t>;
  313. template<typename _CharT, typename _Traits>
  314. class basic_spanstream
  315. : public basic_iostream<_CharT, _Traits>
  316. {
  317. using __iostream_type = basic_iostream<_CharT, _Traits>;
  318. public:
  319. using char_type = _CharT;
  320. using int_type = typename _Traits::int_type;
  321. using pos_type = typename _Traits::pos_type;
  322. using off_type = typename _Traits::off_type;
  323. using traits_type = _Traits;
  324. // [spanstream.ctor], constructors
  325. explicit
  326. basic_spanstream(std::span<_CharT> __s,
  327. ios_base::openmode __which = ios_base::out | ios_base::in)
  328. : __iostream_type(std::__addressof(_M_sb)),
  329. _M_sb(__s, __which)
  330. { }
  331. basic_spanstream(const basic_spanstream&) = delete;
  332. basic_spanstream(basic_spanstream&& __rhs)
  333. : __iostream_type(std::move(__rhs)), _M_sb(std::move(__rhs._M_sb))
  334. {
  335. __iostream_type::set_rdbuf(std::addressof(_M_sb));
  336. }
  337. // [spanstream.assign], assignment and swap
  338. basic_spanstream& operator=(const basic_spanstream&) = delete;
  339. basic_spanstream& operator=(basic_spanstream&& __rhs) = default;
  340. void
  341. swap(basic_spanstream& __rhs)
  342. {
  343. __iostream_type::swap(__rhs);
  344. _M_sb.swap(__rhs._M_sb);
  345. }
  346. // [spanstream.members], members
  347. basic_spanbuf<_CharT, _Traits>*
  348. rdbuf() const noexcept
  349. {
  350. return const_cast<basic_spanbuf<_CharT, _Traits>*>(std::__addressof(_M_sb));
  351. }
  352. std::span<_CharT>
  353. span() const noexcept
  354. { return _M_sb.span(); }
  355. void
  356. span(std::span<_CharT> __s) noexcept
  357. { return _M_sb.span(__s); }
  358. private:
  359. basic_spanbuf<_CharT, _Traits> _M_sb;
  360. };
  361. template<typename _CharT, typename _Traits>
  362. inline void
  363. swap(basic_spanstream<_CharT, _Traits>& __x,
  364. basic_spanstream<_CharT, _Traits>& __y)
  365. { __x.swap(__y); }
  366. using spanstream = basic_spanstream<char>;
  367. using wspanstream = basic_spanstream<wchar_t>;
  368. _GLIBCXX_END_NAMESPACE_VERSION
  369. } // namespace std
  370. #endif // __cpp_lib_span
  371. #endif // C++23
  372. #endif // _GLIBCXX_SPANSTREAM