1 1.1 mrg // <syncstream> -*- C++ -*- 2 1.1 mrg 3 1.1 mrg // Copyright (C) 2020-2022 Free Software Foundation, Inc. 4 1.1 mrg // 5 1.1 mrg // This file is part of the GNU ISO C++ Library. This library is free 6 1.1 mrg // software; you can redistribute it and/or modify it under the 7 1.1 mrg // terms of the GNU General Public License as published by the 8 1.1 mrg // Free Software Foundation; either version 3, or (at your option) 9 1.1 mrg // any later version. 10 1.1 mrg 11 1.1 mrg // This library is distributed in the hope that it will be useful, 12 1.1 mrg // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 mrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 1.1 mrg // GNU General Public License for more details. 15 1.1 mrg 16 1.1 mrg // Under Section 7 of GPL version 3, you are granted additional 17 1.1 mrg // permissions described in the GCC Runtime Library Exception, version 18 1.1 mrg // 3.1, as published by the Free Software Foundation. 19 1.1 mrg 20 1.1 mrg // You should have received a copy of the GNU General Public License and 21 1.1 mrg // a copy of the GCC Runtime Library Exception along with this program; 22 1.1 mrg // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 1.1 mrg // <http://www.gnu.org/licenses/>. 24 1.1 mrg 25 1.1 mrg /** @file include/syncstream 26 1.1 mrg * This is a Standard C++ Library header. 27 1.1 mrg */ 28 1.1 mrg 29 1.1 mrg #ifndef _GLIBCXX_SYNCSTREAM 30 1.1 mrg #define _GLIBCXX_SYNCSTREAM 1 31 1.1 mrg 32 1.1 mrg #if __cplusplus > 201703L 33 1.1 mrg 34 1.1 mrg #include <bits/c++config.h> 35 1.1 mrg #if _GLIBCXX_USE_CXX11_ABI 36 1.1 mrg 37 1.1 mrg #define __cpp_lib_syncbuf 201803L 38 1.1 mrg 39 1.1 mrg #pragma GCC system_header 40 1.1 mrg 41 1.1 mrg #include <sstream> 42 1.1 mrg 43 1.1 mrg #include <bits/alloc_traits.h> 44 1.1 mrg #include <bits/allocator.h> 45 1.1 mrg #include <bits/functexcept.h> 46 1.1 mrg #include <bits/functional_hash.h> 47 1.1 mrg #include <bits/std_mutex.h> 48 1.1 mrg 49 1.1 mrg namespace std _GLIBCXX_VISIBILITY(default) 50 1.1 mrg { 51 1.1 mrg _GLIBCXX_BEGIN_NAMESPACE_VERSION 52 1.1 mrg 53 1.1 mrg template<typename _CharT, typename _Traits, typename _Alloc> 54 1.1 mrg class basic_syncbuf : public __syncbuf_base<_CharT, _Traits> 55 1.1 mrg { 56 1.1 mrg public: 57 1.1 mrg using char_type = _CharT; 58 1.1 mrg using int_type = typename _Traits::int_type; 59 1.1 mrg using pos_type = typename _Traits::pos_type; 60 1.1 mrg using off_type = typename _Traits::off_type; 61 1.1 mrg using traits_type = _Traits; 62 1.1 mrg using allocator_type = _Alloc; 63 1.1 mrg using streambuf_type = basic_streambuf<_CharT, _Traits>; 64 1.1 mrg 65 1.1 mrg basic_syncbuf() 66 1.1 mrg : basic_syncbuf(nullptr, allocator_type{}) 67 1.1 mrg { } 68 1.1 mrg 69 1.1 mrg explicit 70 1.1 mrg basic_syncbuf(streambuf_type* __obuf) 71 1.1 mrg : basic_syncbuf(__obuf, allocator_type{}) 72 1.1 mrg { } 73 1.1 mrg 74 1.1 mrg basic_syncbuf(streambuf_type* __obuf, const allocator_type& __alloc) 75 1.1 mrg : __syncbuf_base<_CharT, _Traits>(__obuf) 76 1.1 mrg , _M_impl(__alloc) 77 1.1 mrg , _M_mtx(__obuf) 78 1.1 mrg { } 79 1.1 mrg 80 1.1 mrg basic_syncbuf(basic_syncbuf&& __other) 81 1.1 mrg : __syncbuf_base<_CharT, _Traits>(__other._M_wrapped) 82 1.1 mrg , _M_impl(std::move(__other._M_impl)) 83 1.1 mrg , _M_mtx(std::move(__other._M_mtx)) 84 1.1 mrg { 85 1.1 mrg this->_M_emit_on_sync = __other._M_emit_on_sync; 86 1.1 mrg this->_M_needs_sync = __other._M_needs_sync; 87 1.1 mrg __other._M_wrapped = nullptr; 88 1.1 mrg } 89 1.1 mrg 90 1.1 mrg ~basic_syncbuf() 91 1.1 mrg { 92 1.1 mrg __try 93 1.1 mrg { 94 1.1 mrg emit(); 95 1.1 mrg } 96 1.1 mrg __catch (...) 97 1.1 mrg { } 98 1.1 mrg } 99 1.1 mrg 100 1.1 mrg basic_syncbuf& 101 1.1 mrg operator=(basic_syncbuf&& __other) 102 1.1 mrg { 103 1.1 mrg emit(); 104 1.1 mrg 105 1.1 mrg _M_impl = std::move(__other._M_impl); 106 1.1 mrg this->_M_emit_on_sync = __other._M_emit_on_sync; 107 1.1 mrg this->_M_needs_sync = __other._M_needs_sync; 108 1.1 mrg this->_M_wrapped = __other._M_wrapped; 109 1.1 mrg __other._M_wrapped = nullptr; 110 1.1 mrg _M_mtx = std::move(__other._M_mtx); 111 1.1 mrg 112 1.1 mrg return *this; 113 1.1 mrg } 114 1.1 mrg 115 1.1 mrg void 116 1.1 mrg swap(basic_syncbuf& __other) 117 1.1 mrg { 118 1.1 mrg using _ATr = allocator_traits<_Alloc>; 119 1.1 mrg if constexpr (!_ATr::propagate_on_container_swap::value) 120 1.1 mrg __glibcxx_assert(get_allocator() == __other.get_allocator()); 121 1.1 mrg 122 1.1 mrg std::swap(_M_impl, __other._M_impl); 123 1.1 mrg std::swap(this->_M_emit_on_sync, __other._M_emit_on_sync); 124 1.1 mrg std::swap(this->_M_needs_sync, __other._M_needs_sync); 125 1.1 mrg std::swap(this->_M_wrapped, __other._M_wrapped); 126 1.1 mrg std::swap(_M_mtx, __other._M_mtx); 127 1.1 mrg } 128 1.1 mrg 129 1.1 mrg bool 130 1.1 mrg emit() 131 1.1 mrg { 132 1.1 mrg if (!this->_M_wrapped) 133 1.1 mrg return false; 134 1.1 mrg 135 1.1 mrg auto __s = std::move(_M_impl).str(); 136 1.1 mrg 137 1.1 mrg const lock_guard<__mutex> __l(_M_mtx); 138 1.1 mrg if (auto __size = __s.size()) 139 1.1 mrg { 140 1.1 mrg auto __n = this->_M_wrapped->sputn(__s.data(), __size); 141 1.1 mrg if (__n != __size) 142 1.1 mrg { 143 1.1 mrg __s.erase(0, __n); 144 1.1 mrg _M_impl.str(std::move(__s)); 145 1.1 mrg return false; 146 1.1 mrg } 147 1.1 mrg } 148 1.1 mrg 149 1.1 mrg if (this->_M_needs_sync) 150 1.1 mrg { 151 1.1 mrg this->_M_needs_sync = false; 152 1.1 mrg if (this->_M_wrapped->pubsync() != 0) 153 1.1 mrg return false; 154 1.1 mrg } 155 1.1 mrg return true; 156 1.1 mrg } 157 1.1 mrg 158 1.1 mrg streambuf_type* 159 1.1 mrg get_wrapped() const noexcept 160 1.1 mrg { return this->_M_wrapped; } 161 1.1 mrg 162 1.1 mrg allocator_type 163 1.1 mrg get_allocator() const noexcept 164 1.1 mrg { return _M_impl.get_allocator(); } 165 1.1 mrg 166 1.1 mrg void 167 1.1 mrg set_emit_on_sync(bool __b) noexcept 168 1.1 mrg { this->_M_emit_on_sync = __b; } 169 1.1 mrg 170 1.1 mrg protected: 171 1.1 mrg int 172 1.1 mrg sync() override 173 1.1 mrg { 174 1.1 mrg this->_M_needs_sync = true; 175 1.1 mrg if (this->_M_emit_on_sync && !emit()) 176 1.1 mrg return -1; 177 1.1 mrg return 0; 178 1.1 mrg } 179 1.1 mrg 180 1.1 mrg int_type 181 1.1 mrg overflow(int_type __c) override 182 1.1 mrg { 183 1.1 mrg int_type __eof = traits_type::eof(); 184 1.1 mrg if (__builtin_expect(!traits_type::eq_int_type(__c, __eof), true)) 185 1.1 mrg return _M_impl.sputc(__c); 186 1.1 mrg return __eof; 187 1.1 mrg } 188 1.1 mrg 189 1.1 mrg streamsize 190 1.1 mrg xsputn(const char_type* __s, streamsize __n) override 191 1.1 mrg { return _M_impl.sputn(__s, __n); } 192 1.1 mrg 193 1.1 mrg private: 194 1.1 mrg basic_stringbuf<char_type, traits_type, allocator_type> _M_impl; 195 1.1 mrg 196 1.1 mrg struct __mutex 197 1.1 mrg { 198 1.1 mrg #if _GLIBCXX_HAS_GTHREADS 199 1.1 mrg mutex* _M_mtx; 200 1.1 mrg 201 1.1 mrg __mutex(void* __t) 202 1.1 mrg : _M_mtx(__t ? &_S_get_mutex(__t) : nullptr) 203 1.1 mrg { } 204 1.1 mrg 205 1.1 mrg void 206 1.1 mrg swap(__mutex& __other) noexcept 207 1.1 mrg { std::swap(_M_mtx, __other._M_mtx); } 208 1.1 mrg 209 1.1 mrg void 210 1.1 mrg lock() 211 1.1 mrg { 212 1.1 mrg _M_mtx->lock(); 213 1.1 mrg } 214 1.1 mrg 215 1.1 mrg void 216 1.1 mrg unlock() 217 1.1 mrg { 218 1.1 mrg _M_mtx->unlock(); 219 1.1 mrg } 220 1.1 mrg 221 1.1 mrg // FIXME: This should be put in the .so 222 1.1 mrg static mutex& 223 1.1 mrg _S_get_mutex(void* __t) 224 1.1 mrg { 225 1.1 mrg const unsigned char __mask = 0xf; 226 1.1 mrg static mutex __m[__mask + 1]; 227 1.1 mrg 228 1.1 mrg auto __key = _Hash_impl::hash(__t) & __mask; 229 1.1 mrg return __m[__key]; 230 1.1 mrg } 231 1.1 mrg #else 232 1.1 mrg __mutex(void*) { } 233 1.1 mrg void swap(__mutex&&) noexcept { } 234 1.1 mrg void lock() { } 235 1.1 mrg void unlock() { } 236 1.1 mrg #endif 237 1.1 mrg __mutex(__mutex&&) = default; 238 1.1 mrg __mutex& operator=(__mutex&&) = default; 239 1.1 mrg }; 240 1.1 mrg __mutex _M_mtx; 241 1.1 mrg }; 242 1.1 mrg 243 1.1 mrg template <typename _CharT, typename _Traits, typename _Alloc> 244 1.1 mrg class basic_osyncstream : public basic_ostream<_CharT, _Traits> 245 1.1 mrg { 246 1.1 mrg using __ostream_type = basic_ostream<_CharT, _Traits>; 247 1.1 mrg 248 1.1 mrg public: 249 1.1 mrg // Types: 250 1.1 mrg using char_type = _CharT; 251 1.1 mrg using traits_type = _Traits; 252 1.1 mrg using allocator_type = _Alloc; 253 1.1 mrg using int_type = typename traits_type::int_type; 254 1.1 mrg using pos_type = typename traits_type::pos_type; 255 1.1 mrg using off_type = typename traits_type::off_type; 256 1.1 mrg using syncbuf_type = basic_syncbuf<_CharT, _Traits, _Alloc>; 257 1.1 mrg using streambuf_type = typename syncbuf_type::streambuf_type; 258 1.1 mrg 259 1.1 mrg private: 260 1.1 mrg syncbuf_type _M_syncbuf; 261 1.1 mrg 262 1.1 mrg public: 263 1.1 mrg basic_osyncstream(streambuf_type* __buf, const allocator_type& __a) 264 1.1 mrg : _M_syncbuf(__buf, __a) 265 1.1 mrg { this->init(std::__addressof(_M_syncbuf)); } 266 1.1 mrg 267 1.1 mrg explicit basic_osyncstream(streambuf_type* __buf) 268 1.1 mrg : _M_syncbuf(__buf) 269 1.1 mrg { this->init(std::__addressof(_M_syncbuf)); } 270 1.1 mrg 271 1.1 mrg basic_osyncstream(basic_ostream<char_type, traits_type>& __os, 272 1.1 mrg const allocator_type& __a) 273 1.1 mrg : basic_osyncstream(__os.rdbuf(), __a) 274 1.1 mrg { this->init(std::__addressof(_M_syncbuf)); } 275 1.1 mrg 276 1.1 mrg explicit basic_osyncstream(basic_ostream<char_type, traits_type>& __os) 277 1.1 mrg : basic_osyncstream(__os.rdbuf()) 278 1.1 mrg { this->init(std::__addressof(_M_syncbuf)); } 279 1.1 mrg 280 1.1 mrg basic_osyncstream(basic_osyncstream&& __rhs) noexcept 281 1.1 mrg : __ostream_type(std::move(__rhs)), 282 1.1 mrg _M_syncbuf(std::move(__rhs._M_syncbuf)) 283 1.1 mrg { __ostream_type::set_rdbuf(std::__addressof(_M_syncbuf)); } 284 1.1 mrg 285 1.1 mrg ~basic_osyncstream() = default; 286 1.1 mrg 287 1.1 mrg basic_osyncstream& operator=(basic_osyncstream&&) = default; 288 1.1 mrg 289 1.1 mrg syncbuf_type* rdbuf() const noexcept 290 1.1 mrg { return const_cast<syncbuf_type*>(&_M_syncbuf); } 291 1.1 mrg 292 1.1 mrg streambuf_type* get_wrapped() const noexcept 293 1.1 mrg { return _M_syncbuf.get_wrapped(); } 294 1.1 mrg 295 1.1 mrg void emit() 296 1.1 mrg { 297 1.1 mrg if (!_M_syncbuf.emit()) 298 1.1 mrg this->setstate(ios_base::failbit); 299 1.1 mrg } 300 1.1 mrg }; 301 1.1 mrg 302 1.1 mrg template <class _CharT, class _Traits, class _Allocator> 303 1.1 mrg inline void 304 1.1 mrg swap(basic_syncbuf<_CharT, _Traits, _Allocator>& __x, 305 1.1 mrg basic_syncbuf<_CharT, _Traits, _Allocator>& __y) noexcept 306 1.1 mrg { __x.swap(__y); } 307 1.1 mrg 308 1.1 mrg using syncbuf = basic_syncbuf<char>; 309 1.1 mrg using wsyncbuf = basic_syncbuf<wchar_t>; 310 1.1 mrg 311 1.1 mrg using osyncstream = basic_osyncstream<char>; 312 1.1 mrg using wosyncstream = basic_osyncstream<wchar_t>; 313 1.1 mrg _GLIBCXX_END_NAMESPACE_VERSION 314 1.1 mrg } // namespace std 315 1.1 mrg #endif // _GLIBCXX_USE_CXX11_ABI 316 1.1 mrg #endif // C++2a 317 1.1 mrg #endif /* _GLIBCXX_SYNCSTREAM */ 318