spanstream 12 KB

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