29#ifndef _GLIBCXX_SYNCSTREAM
30#define _GLIBCXX_SYNCSTREAM 1
33#pragma GCC system_header
40#define __glibcxx_want_syncbuf
43#ifdef __cpp_lib_syncbuf
52namespace std _GLIBCXX_VISIBILITY(default)
54_GLIBCXX_BEGIN_NAMESPACE_VERSION
56 template<
typename _CharT,
typename _Traits,
typename _Alloc>
57 class basic_syncbuf :
public __syncbuf_base<_CharT, _Traits>
60 using char_type = _CharT;
61 using int_type =
typename _Traits::int_type;
62 using pos_type =
typename _Traits::pos_type;
63 using off_type =
typename _Traits::off_type;
64 using traits_type = _Traits;
65 using allocator_type = _Alloc;
66 using streambuf_type = basic_streambuf<_CharT, _Traits>;
69 : basic_syncbuf(nullptr, allocator_type{})
73 basic_syncbuf(streambuf_type* __obuf)
74 : basic_syncbuf(__obuf, allocator_type{})
77 basic_syncbuf(streambuf_type* __obuf,
const allocator_type& __alloc)
78 : __syncbuf_base<_CharT, _Traits>(__obuf)
83 basic_syncbuf(basic_syncbuf&& __other)
84 : __syncbuf_base<_CharT, _Traits>(__other._M_wrapped)
85 , _M_impl(
std::
move(__other._M_impl))
86 , _M_mtx(
std::
move(__other._M_mtx))
88 this->_M_emit_on_sync = __other._M_emit_on_sync;
89 this->_M_needs_sync = __other._M_needs_sync;
90 __other._M_wrapped =
nullptr;
104 operator=(basic_syncbuf&& __other)
109 this->_M_emit_on_sync = __other._M_emit_on_sync;
110 this->_M_needs_sync = __other._M_needs_sync;
111 this->_M_wrapped = __other._M_wrapped;
112 __other._M_wrapped =
nullptr;
119 swap(basic_syncbuf& __other)
121 using _ATr = allocator_traits<_Alloc>;
122 if constexpr (!_ATr::propagate_on_container_swap::value)
123 __glibcxx_assert(get_allocator() == __other.get_allocator());
125 std::swap(_M_impl, __other._M_impl);
126 std::swap(this->_M_emit_on_sync, __other._M_emit_on_sync);
127 std::swap(this->_M_needs_sync, __other._M_needs_sync);
128 std::swap(this->_M_wrapped, __other._M_wrapped);
129 std::swap(_M_mtx, __other._M_mtx);
135 if (!this->_M_wrapped)
140 const lock_guard<__mutex> __l(_M_mtx);
141 if (
auto __size = __s.size())
143 auto __n = this->_M_wrapped->sputn(__s.data(), __size);
152 if (this->_M_needs_sync)
154 this->_M_needs_sync =
false;
155 if (this->_M_wrapped->pubsync() != 0)
162 get_wrapped() const noexcept
163 {
return this->_M_wrapped; }
166 get_allocator() const noexcept
167 {
return _M_impl.get_allocator(); }
170 set_emit_on_sync(
bool __b)
noexcept
171 { this->_M_emit_on_sync = __b; }
177 this->_M_needs_sync =
true;
178 if (this->_M_emit_on_sync && !emit())
184 overflow(int_type __c)
override
186 int_type __eof = traits_type::eof();
187 if (__builtin_expect(!traits_type::eq_int_type(__c, __eof),
true))
188 return _M_impl.sputc(__c);
193 xsputn(
const char_type* __s, streamsize __n)
override
194 {
return _M_impl.sputn(__s, __n); }
197 basic_stringbuf<char_type, traits_type, allocator_type> _M_impl;
201#if _GLIBCXX_HAS_GTHREADS
205 : _M_mtx(__t ? &_S_get_mutex(__t) : nullptr)
209 swap(__mutex& __other)
noexcept
210 { std::swap(_M_mtx, __other._M_mtx); }
226 _S_get_mutex(
void* __t)
228 const unsigned char __mask = 0xf;
229 static mutex __m[__mask + 1];
231 auto __key = _Hash_impl::hash(__t) & __mask;
236 void swap(__mutex&&) noexcept { }
240 __mutex(__mutex&&) =
default;
241 __mutex& operator=(__mutex&&) =
default;
246 template <
typename _CharT,
typename _Traits,
typename _Alloc>
247 class basic_osyncstream :
public basic_ostream<_CharT, _Traits>
249 using __ostream_type = basic_ostream<_CharT, _Traits>;
253 using char_type = _CharT;
254 using traits_type = _Traits;
255 using allocator_type = _Alloc;
256 using int_type =
typename traits_type::int_type;
257 using pos_type =
typename traits_type::pos_type;
258 using off_type =
typename traits_type::off_type;
259 using syncbuf_type = basic_syncbuf<_CharT, _Traits, _Alloc>;
260 using streambuf_type =
typename syncbuf_type::streambuf_type;
263 syncbuf_type _M_syncbuf;
266 basic_osyncstream(streambuf_type* __buf,
const allocator_type& __a)
267 : _M_syncbuf(__buf, __a)
270 explicit basic_osyncstream(streambuf_type* __buf)
274 basic_osyncstream(basic_ostream<char_type, traits_type>& __os,
275 const allocator_type& __a)
276 : basic_osyncstream(__os.rdbuf(), __a)
279 explicit basic_osyncstream(basic_ostream<char_type, traits_type>& __os)
280 : basic_osyncstream(__os.rdbuf())
283 basic_osyncstream(basic_osyncstream&& __rhs) noexcept
288 ~basic_osyncstream() =
default;
290 basic_osyncstream& operator=(basic_osyncstream&&) =
default;
292 syncbuf_type* rdbuf() const noexcept
293 {
return const_cast<syncbuf_type*
>(&_M_syncbuf); }
295 streambuf_type* get_wrapped() const noexcept
296 {
return _M_syncbuf.get_wrapped(); }
300 if (!_M_syncbuf.emit())
301 this->setstate(ios_base::failbit);
305 template <
class _CharT,
class _Traits,
class _Allocator>
307 swap(basic_syncbuf<_CharT, _Traits, _Allocator>& __x,
308 basic_syncbuf<_CharT, _Traits, _Allocator>& __y)
noexcept
311 using syncbuf = basic_syncbuf<char>;
312 using wsyncbuf = basic_syncbuf<wchar_t>;
314 using osyncstream = basic_osyncstream<char>;
315 using wosyncstream = basic_osyncstream<wchar_t>;
316_GLIBCXX_END_NAMESPACE_VERSION
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.