29#ifndef _GLIBCXX_SYNCSTREAM
30#define _GLIBCXX_SYNCSTREAM 1
33#pragma GCC system_header
40#define __glibcxx_want_syncbuf
43#ifdef __cpp_lib_syncbuf
50namespace std _GLIBCXX_VISIBILITY(default)
52_GLIBCXX_BEGIN_NAMESPACE_VERSION
54 template<
typename _CharT,
typename _Traits,
typename _Alloc>
55 class basic_syncbuf :
public __syncbuf_base<_CharT, _Traits>
58 using char_type = _CharT;
59 using int_type =
typename _Traits::int_type;
60 using pos_type =
typename _Traits::pos_type;
61 using off_type =
typename _Traits::off_type;
62 using traits_type = _Traits;
63 using allocator_type = _Alloc;
64 using streambuf_type = basic_streambuf<_CharT, _Traits>;
67 : basic_syncbuf(nullptr, allocator_type{})
71 basic_syncbuf(streambuf_type* __obuf)
72 : basic_syncbuf(__obuf, allocator_type{})
75 basic_syncbuf(streambuf_type* __obuf,
const allocator_type& __alloc)
76 : __syncbuf_base<_CharT, _Traits>(__obuf)
81 basic_syncbuf(basic_syncbuf&& __other)
82 : __syncbuf_base<_CharT, _Traits>(__other._M_wrapped)
83 , _M_impl(std::move(__other._M_impl))
84 , _M_mtx(std::move(__other._M_mtx))
86 this->_M_emit_on_sync = __other._M_emit_on_sync;
87 this->_M_needs_sync = __other._M_needs_sync;
88 __other._M_wrapped =
nullptr;
102 operator=(basic_syncbuf&& __other)
107 this->_M_emit_on_sync = __other._M_emit_on_sync;
108 this->_M_needs_sync = __other._M_needs_sync;
109 this->_M_wrapped = __other._M_wrapped;
110 __other._M_wrapped =
nullptr;
117 swap(basic_syncbuf& __other)
119 using _ATr = allocator_traits<_Alloc>;
120 if constexpr (!_ATr::propagate_on_container_swap::value)
121 __glibcxx_assert(get_allocator() == __other.get_allocator());
123 std::swap(_M_impl, __other._M_impl);
124 std::swap(this->_M_emit_on_sync, __other._M_emit_on_sync);
125 std::swap(this->_M_needs_sync, __other._M_needs_sync);
126 std::swap(this->_M_wrapped, __other._M_wrapped);
127 std::swap(_M_mtx, __other._M_mtx);
133 if (!this->_M_wrapped)
138 const lock_guard<__mutex> __l(_M_mtx);
139 if (
auto __size = __s.size())
141 auto __n = this->_M_wrapped->sputn(__s.data(), __size);
150 if (this->_M_needs_sync)
152 this->_M_needs_sync =
false;
153 if (this->_M_wrapped->pubsync() != 0)
160 get_wrapped() const noexcept
161 {
return this->_M_wrapped; }
164 get_allocator() const noexcept
165 {
return _M_impl.get_allocator(); }
168 set_emit_on_sync(
bool __b)
noexcept
169 { this->_M_emit_on_sync = __b; }
175 this->_M_needs_sync =
true;
176 if (this->_M_emit_on_sync && !emit())
182 overflow(int_type __c)
override
184 int_type __eof = traits_type::eof();
185 if (__builtin_expect(!traits_type::eq_int_type(__c, __eof),
true))
186 return _M_impl.sputc(__c);
191 xsputn(
const char_type* __s, streamsize __n)
override
192 {
return _M_impl.sputn(__s, __n); }
195 basic_stringbuf<char_type, traits_type, allocator_type> _M_impl;
199#if _GLIBCXX_HAS_GTHREADS
200 mutex* _M_mtx =
nullptr;
204 extern mutex& __syncbuf_get_mutex(
void*);
205 if (__t) _M_mtx = &__syncbuf_get_mutex(__t);
209 swap(__mutex& __other)
noexcept
210 { std::swap(_M_mtx, __other._M_mtx); }
225 void swap(__mutex&&) noexcept { }
229 __mutex(__mutex&&) =
default;
230 __mutex& operator=(__mutex&&) =
default;
235 template <
typename _CharT,
typename _Traits,
typename _Alloc>
236 class basic_osyncstream :
public basic_ostream<_CharT, _Traits>
238 using __ostream_type = basic_ostream<_CharT, _Traits>;
242 using char_type = _CharT;
243 using traits_type = _Traits;
244 using allocator_type = _Alloc;
245 using int_type =
typename traits_type::int_type;
246 using pos_type =
typename traits_type::pos_type;
247 using off_type =
typename traits_type::off_type;
248 using syncbuf_type = basic_syncbuf<_CharT, _Traits, _Alloc>;
249 using streambuf_type =
typename syncbuf_type::streambuf_type;
252 syncbuf_type _M_syncbuf;
255 basic_osyncstream(streambuf_type* __buf,
const allocator_type& __a)
256 : _M_syncbuf(__buf, __a)
259 explicit basic_osyncstream(streambuf_type* __buf)
263 basic_osyncstream(basic_ostream<char_type, traits_type>& __os,
264 const allocator_type& __a)
265 : basic_osyncstream(__os.rdbuf(), __a)
268 explicit basic_osyncstream(basic_ostream<char_type, traits_type>& __os)
269 : basic_osyncstream(__os.rdbuf())
272 basic_osyncstream(basic_osyncstream&& __rhs) noexcept
277 ~basic_osyncstream() =
default;
279 basic_osyncstream& operator=(basic_osyncstream&&) =
default;
281 syncbuf_type* rdbuf() const noexcept
282 {
return const_cast<syncbuf_type*
>(&_M_syncbuf); }
284 streambuf_type* get_wrapped() const noexcept
285 {
return _M_syncbuf.get_wrapped(); }
289 if (!_M_syncbuf.emit())
290 this->setstate(ios_base::failbit);
294 template <
class _CharT,
class _Traits,
class _Allocator>
296 swap(basic_syncbuf<_CharT, _Traits, _Allocator>& __x,
297 basic_syncbuf<_CharT, _Traits, _Allocator>& __y)
noexcept
300 using syncbuf = basic_syncbuf<char>;
301 using wsyncbuf = basic_syncbuf<wchar_t>;
303 using osyncstream = basic_osyncstream<char>;
304 using wosyncstream = basic_osyncstream<wchar_t>;
305_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.
Template class basic_ostream.