summaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/std/syncstream
blob: 07aab65223ece97bd1b9c4b9745fd1497695eb2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// <syncstream> -*- C++ -*-

// Copyright (C) 2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file include/syncstream
 *  This is a Standard C++ Library header.
 */

#ifndef _GLIBCXX_SYNCSTREAM
#define _GLIBCXX_SYNCSTREAM 1

#if __cplusplus > 201703L

#include <bits/c++config.h>
#if _GLIBCXX_USE_CXX11_ABI

#define __cpp_lib_syncbuf 201803L

#pragma GCC system_header

#include <sstream>

#include <bits/alloc_traits.h>
#include <bits/allocator.h>
#include <bits/functexcept.h>
#include <bits/functional_hash.h>
#include <bits/std_mutex.h>

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

  template<typename _CharT, typename _Traits = char_traits<_CharT>,
	    typename _Alloc = allocator<_CharT>>
    class basic_syncbuf : public __syncbuf_base<_CharT, _Traits>
    {
    public:
      using char_type = _CharT;
      using int_type = typename _Traits::int_type;
      using pos_type = typename _Traits::pos_type;
      using off_type = typename _Traits::off_type;
      using traits_type = _Traits;
      using allocator_type = _Alloc;
      using streambuf_type = basic_streambuf<_CharT, _Traits>;

      basic_syncbuf()
      : basic_syncbuf(nullptr, allocator_type{})
      { }

      explicit
      basic_syncbuf(streambuf_type* __obuf)
      : basic_syncbuf(__obuf, allocator_type{})
      { }

      basic_syncbuf(streambuf_type* __obuf, const allocator_type& __alloc)
      : __syncbuf_base<_CharT, _Traits>(__obuf)
      , _M_impl(__alloc)
      , _M_mtx(__obuf)
      { }

      basic_syncbuf(basic_syncbuf&& __other)
      : __syncbuf_base<_CharT, _Traits>(__other._M_wrapped)
      , _M_impl(std::move(__other._M_impl))
      , _M_mtx(std::move(__other._M_mtx))
      {
	this->_M_emit_on_sync = __other._M_emit_on_sync;
	this->_M_needs_sync = __other._M_needs_sync;
	__other._M_wrapped = nullptr;
      }

      ~basic_syncbuf()
      {
	__try
	  {
	    emit();
	  }
	__catch (...)
	  { }
      }

      basic_syncbuf&
      operator=(basic_syncbuf&& __other)
      {
	emit();

	_M_impl = std::move(__other._M_impl);
	this->_M_emit_on_sync = __other._M_emit_on_sync;
	this->_M_needs_sync = __other._M_needs_sync;
	this->_M_wrapped = __other._M_wrapped;
	__other._M_wrapped = nullptr;
	_M_mtx = std::move(__other._M_mtx);

	return *this;
      }

      void
      swap(basic_syncbuf& __other) noexcept
      {
	using _ATr = allocator_traits<_Alloc>;
	if constexpr (!_ATr::propagate_on_container_swap::value)
	  __glibcxx_assert(get_allocator() == __other.get_allocator());

	std::swap(_M_impl, __other._M_impl);
	std::swap(this->_M_emit_on_sync, __other._M_emit_on_sync);
	std::swap(this->_M_needs_sync, __other._M_needs_sync);
	std::swap(this->_M_wrapped, __other._M_wrapped);
	std::swap(_M_mtx, __other._M_mtx);
      }

      bool
      emit()
      {
	if (!this->_M_wrapped)
	  return false;

	auto __s = std::move(_M_impl).str();

	const lock_guard<__mutex> __l(_M_mtx);
	if (auto __size = __s.size())
	  {
	    auto __n = this->_M_wrapped->sputn(__s.data(), __size);
	    if (__n != __size)
	      {
		__s.erase(0, __n);
		_M_impl.str(std::move(__s));
		return false;
	      }
	  }

	if (this->_M_needs_sync)
	  {
	    this->_M_needs_sync = false;
	    if (this->_M_wrapped->pubsync() != 0)
	      return false;
	  }
	return true;
      }

      streambuf_type*
      get_wrapped() const noexcept
      { return this->_M_wrapped; }

      allocator_type
      get_allocator() const noexcept
      { return _M_impl.get_allocator(); }

      void
      set_emit_on_sync(bool __b) noexcept
      { this->_M_emit_on_sync = __b; }

    protected:
      int
      sync() override
      {
	this->_M_needs_sync = true;
	if (this->_M_emit_on_sync && !emit())
	  return -1;
	return 0;
      }

      int_type
      overflow(int_type __c) override
      {
	int_type __eof = traits_type::eof();
	if (__builtin_expect(!traits_type::eq_int_type(__c, __eof), true))
	  return _M_impl.sputc(__c);
	return __eof;
      }

      streamsize
      xsputn(const char_type* __s, streamsize __n) override
      { return _M_impl.sputn(__s, __n); }

    private:
      basic_stringbuf<char_type, traits_type, allocator_type> _M_impl;

      struct __mutex
      {
#if _GLIBCXX_HAS_GTHREADS
	mutex* _M_mtx;

	__mutex(void* __t)
	  : _M_mtx(__t ? &_S_get_mutex(__t) : nullptr)
	{ }

	void
	swap(__mutex& __other) noexcept
	{ std::swap(_M_mtx, __other._M_mtx); }

	void
	lock()
	{
	  _M_mtx->lock();
	}

	void
	unlock()
	{
	  _M_mtx->unlock();
	}

	// FIXME: This should be put in the .so
	static mutex&
	_S_get_mutex(void* __t)
	{
	  const unsigned char __mask = 0xf;
	  static mutex __m[__mask + 1];

	  auto __key = _Hash_impl::hash(__t) & __mask;
	  return __m[__key];
	}
#else
	__mutex(void*) { }
	void swap(__mutex&&) noexcept { }
	void lock() { }
	void unlock() { }
#endif
	__mutex(__mutex&&) = default;
	__mutex& operator=(__mutex&&) = default;
      };
      __mutex _M_mtx;
    };

  template <typename _CharT, typename _Traits = char_traits<_CharT>,
	    typename _Alloc = allocator<_CharT>>
    class basic_osyncstream : public basic_ostream<_CharT, _Traits>
    {
      using __ostream_type = basic_ostream<_CharT, _Traits>;

    public:
      // Types:
      using char_type = _CharT;
      using traits_type = _Traits;
      using allocator_type = _Alloc;
      using int_type = typename traits_type::int_type;
      using pos_type = typename traits_type::pos_type;
      using off_type = typename traits_type::off_type;
      using syncbuf_type = basic_syncbuf<_CharT, _Traits, _Alloc>;
      using streambuf_type = typename syncbuf_type::streambuf_type;

    private:
      syncbuf_type _M_syncbuf;

    public:
      basic_osyncstream(streambuf_type* __buf, const allocator_type& __a)
	: _M_syncbuf(__buf, __a)
      { this->init(std::__addressof(_M_syncbuf)); }

      explicit basic_osyncstream(streambuf_type* __buf)
	: _M_syncbuf(__buf)
      { this->init(std::__addressof(_M_syncbuf)); }

      basic_osyncstream(basic_ostream<char_type, traits_type>& __os,
		        const allocator_type& __a)
	: basic_osyncstream(__os.rdbuf(), __a)
      { this->init(std::__addressof(_M_syncbuf)); }

      explicit basic_osyncstream(basic_ostream<char_type, traits_type>& __os)
	: basic_osyncstream(__os.rdbuf())
      { this->init(std::__addressof(_M_syncbuf)); }

      basic_osyncstream(basic_osyncstream&& __rhs) noexcept
	: __ostream_type(std::move(__rhs)),
	_M_syncbuf(std::move(__rhs._M_syncbuf))
      { __ostream_type::set_rdbuf(std::__addressof(_M_syncbuf)); }

      ~basic_osyncstream() = default;

      basic_osyncstream& operator=(basic_osyncstream&&) noexcept = default;

      syncbuf_type* rdbuf() const noexcept
      { return const_cast<syncbuf_type*>(&_M_syncbuf); }

      streambuf_type* get_wrapped() const noexcept
      { return _M_syncbuf.get_wrapped(); }

      void emit()
      {
	if (!_M_syncbuf.emit())
	  this->setstate(ios_base::failbit);
      }
    };

  template <class _CharT, class _Traits, class _Allocator>
    inline void
    swap(basic_syncbuf<_CharT, _Traits, _Allocator>& __x,
	 basic_syncbuf<_CharT, _Traits, _Allocator>& __y) noexcept
    { __x.swap(__y); }

  using syncbuf = basic_syncbuf<char>;
  using wsyncbuf = basic_syncbuf<wchar_t>;

  using osyncstream = basic_osyncstream<char>;
  using wosyncstream = basic_osyncstream<wchar_t>;
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // _GLIBCXX_USE_CXX11_ABI
#endif // C++2a
#endif	/* _GLIBCXX_SYNCSTREAM */