29#ifndef _GLIBCXX_SYNCSTREAM
30#define _GLIBCXX_SYNCSTREAM 1
32#if __cplusplus > 201703L
35#if _GLIBCXX_USE_CXX11_ABI
37#define __cpp_lib_syncbuf 201803L
39#pragma GCC system_header
51namespace std _GLIBCXX_VISIBILITY(default)
53_GLIBCXX_BEGIN_NAMESPACE_VERSION
55 template<
typename _CharT,
typename _Traits,
typename _Alloc>
56 class basic_syncbuf :
public __syncbuf_base<_CharT, _Traits>
59 using char_type = _CharT;
60 using int_type =
typename _Traits::int_type;
61 using pos_type =
typename _Traits::pos_type;
62 using off_type =
typename _Traits::off_type;
63 using traits_type = _Traits;
64 using allocator_type = _Alloc;
65 using streambuf_type = basic_streambuf<_CharT, _Traits>;
68 : basic_syncbuf(nullptr, allocator_type{})
72 basic_syncbuf(streambuf_type* __obuf)
73 : basic_syncbuf(__obuf, allocator_type{})
76 basic_syncbuf(streambuf_type* __obuf,
const allocator_type& __alloc)
77 : __syncbuf_base<_CharT, _Traits>(__obuf)
82 basic_syncbuf(basic_syncbuf&& __other)
83 : __syncbuf_base<_CharT, _Traits>(__other._M_wrapped)
84 , _M_impl(
std::
move(__other._M_impl))
85 , _M_mtx(
std::
move(__other._M_mtx))
87 this->_M_emit_on_sync = __other._M_emit_on_sync;
88 this->_M_needs_sync = __other._M_needs_sync;
89 __other._M_wrapped =
nullptr;
103 operator=(basic_syncbuf&& __other)
108 this->_M_emit_on_sync = __other._M_emit_on_sync;
109 this->_M_needs_sync = __other._M_needs_sync;
110 this->_M_wrapped = __other._M_wrapped;
111 __other._M_wrapped =
nullptr;
118 swap(basic_syncbuf& __other)
120 using _ATr = allocator_traits<_Alloc>;
121 if constexpr (!_ATr::propagate_on_container_swap::value)
122 __glibcxx_assert(get_allocator() == __other.get_allocator());
125 std::swap(this->_M_emit_on_sync, __other._M_emit_on_sync);
126 std::swap(this->_M_needs_sync, __other._M_needs_sync);
127 std::swap(this->_M_wrapped, __other._M_wrapped);
134 if (!this->_M_wrapped)
139 const lock_guard<__mutex> __l(_M_mtx);
140 if (
auto __size = __s.size())
142 auto __n = this->_M_wrapped->sputn(__s.data(), __size);
151 if (this->_M_needs_sync)
153 this->_M_needs_sync =
false;
154 if (this->_M_wrapped->pubsync() != 0)
161 get_wrapped() const noexcept
162 {
return this->_M_wrapped; }
165 get_allocator() const noexcept
166 {
return _M_impl.get_allocator(); }
169 set_emit_on_sync(
bool __b)
noexcept
170 { this->_M_emit_on_sync = __b; }
176 this->_M_needs_sync =
true;
177 if (this->_M_emit_on_sync && !emit())
183 overflow(int_type __c)
override
185 int_type __eof = traits_type::eof();
186 if (__builtin_expect(!traits_type::eq_int_type(__c, __eof),
true))
187 return _M_impl.sputc(__c);
192 xsputn(
const char_type* __s,
streamsize __n)
override
193 {
return _M_impl.sputn(__s, __n); }
196 basic_stringbuf<char_type, traits_type, allocator_type> _M_impl;
200#if _GLIBCXX_HAS_GTHREADS
204 : _M_mtx(__t ? &_S_get_mutex(__t) : nullptr)
208 swap(__mutex& __other)
noexcept
225 _S_get_mutex(
void* __t)
227 const unsigned char __mask = 0xf;
228 static mutex __m[__mask + 1];
230 auto __key = _Hash_impl::hash(__t) & __mask;
235 void swap(__mutex&&) noexcept { }
239 __mutex(__mutex&&) =
default;
240 __mutex& operator=(__mutex&&) =
default;
245 template <
typename _CharT,
typename _Traits,
typename _Alloc>
246 class basic_osyncstream :
public basic_ostream<_CharT, _Traits>
248 using __ostream_type = basic_ostream<_CharT, _Traits>;
252 using char_type = _CharT;
253 using traits_type = _Traits;
254 using allocator_type = _Alloc;
255 using int_type =
typename traits_type::int_type;
256 using pos_type =
typename traits_type::pos_type;
257 using off_type =
typename traits_type::off_type;
258 using syncbuf_type = basic_syncbuf<_CharT, _Traits, _Alloc>;
259 using streambuf_type =
typename syncbuf_type::streambuf_type;
262 syncbuf_type _M_syncbuf;
265 basic_osyncstream(streambuf_type* __buf,
const allocator_type& __a)
266 : _M_syncbuf(__buf, __a)
269 explicit basic_osyncstream(streambuf_type* __buf)
273 basic_osyncstream(basic_ostream<char_type, traits_type>& __os,
274 const allocator_type& __a)
275 : basic_osyncstream(__os.rdbuf(), __a)
278 explicit basic_osyncstream(basic_ostream<char_type, traits_type>& __os)
279 : basic_osyncstream(__os.rdbuf())
282 basic_osyncstream(basic_osyncstream&& __rhs) noexcept
287 ~basic_osyncstream() =
default;
289 basic_osyncstream& operator=(basic_osyncstream&&) noexcept = default;
291 syncbuf_type* rdbuf() const noexcept
292 {
return const_cast<syncbuf_type*
>(&_M_syncbuf); }
294 streambuf_type* get_wrapped() const noexcept
295 {
return _M_syncbuf.get_wrapped(); }
299 if (!_M_syncbuf.emit())
304 template <
class _CharT,
class _Traits,
class _Allocator>
306 swap(basic_syncbuf<_CharT, _Traits, _Allocator>& __x,
307 basic_syncbuf<_CharT, _Traits, _Allocator>& __y)
noexcept
310 using syncbuf = basic_syncbuf<char>;
311 using wsyncbuf = basic_syncbuf<wchar_t>;
313 using osyncstream = basic_osyncstream<char>;
314 using wosyncstream = basic_osyncstream<wchar_t>;
315_GLIBCXX_END_NAMESPACE_VERSION
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
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.
static const iostate failbit
Indicates that an input operation failed to read the expected characters, or that an output operation...