29#ifndef _GLIBCXX_GENERATOR
30#define _GLIBCXX_GENERATOR
34#pragma GCC system_header
39#define __glibcxx_want_generator
42#ifdef __cpp_lib_generator
46#include <bits/elements_of.h>
47#include <bits/uses_allocator.h>
62namespace std _GLIBCXX_VISIBILITY(default)
64_GLIBCXX_BEGIN_NAMESPACE_VERSION
81 template<
typename _Ref,
typename _Val =
void,
typename _Alloc =
void>
89 template<
typename _Ref,
typename _Val>
90 using _Reference_t = __conditional_t<is_void_v<_Val>,
94 template<
typename _Reference>
95 using _Yield_t = __conditional_t<is_reference_v<_Reference>,
100 template<
typename _Ref,
typename _Val>
101 using _Yield2_t = _Yield_t<_Reference_t<_Ref, _Val>>;
103 template<
typename>
constexpr bool __is_generator =
false;
104 template<
typename _Val,
typename _Ref,
typename _Alloc>
105 constexpr bool __is_generator<std::generator<_Val, _Ref, _Alloc>> =
true;
109 template<
typename _Yielded>
110 class _Promise_erased
112 static_assert(is_reference_v<_Yielded>);
113 using _Yielded_deref = remove_reference_t<_Yielded>;
114 using _Yielded_decvref = remove_cvref_t<_Yielded>;
115 using _ValuePtr = add_pointer_t<_Yielded>;
116 using _Coro_handle = std::coroutine_handle<_Promise_erased>;
118 template<
typename,
typename,
typename>
119 friend class std::generator;
121 template<
typename _Gen>
122 struct _Recursive_awaiter;
124 friend struct _Recursive_awaiter;
125 struct _Copy_awaiter;
126 struct _Subyield_state;
127 struct _Final_awaiter;
130 initial_suspend() const noexcept
134 yield_value(_Yielded __val)
noexcept
141 yield_value(
const _Yielded_deref& __val)
142 noexcept (is_nothrow_constructible_v<_Yielded_decvref,
143 const _Yielded_deref&>)
144 requires (is_rvalue_reference_v<_Yielded>
145 && constructible_from<_Yielded_decvref,
146 const _Yielded_deref&>)
147 {
return _Copy_awaiter(_Yielded_decvref(__val), _M_bottom_value()); }
149 template<
typename _R2,
typename _V2,
typename _A2,
typename _U2>
150 requires std::same_as<_Yield2_t<_R2, _V2>, _Yielded>
152 yield_value(ranges::elements_of<generator<_R2, _V2, _A2>&&, _U2> __r)
154 {
return _Recursive_awaiter {
std::move(__r.range) }; }
159 template<
typename _R2,
typename _V2,
typename _A2,
typename _U2>
160 requires std::same_as<_Yield2_t<_R2, _V2>, _Yielded>
162 yield_value(ranges::elements_of<generator<_R2, _V2, _A2>&, _U2> __r)
164 {
return _Recursive_awaiter {
std::move(__r.range) }; }
166 template<ranges::input_range _R,
typename _Alloc>
167 requires convertible_to<ranges::range_reference_t<_R>, _Yielded>
169 yield_value(ranges::elements_of<_R, _Alloc> __r)
171 auto __n = [] (allocator_arg_t, _Alloc,
172 ranges::iterator_t<_R> __i,
173 ranges::sentinel_t<_R> __s)
174 -> generator<_Yielded, ranges::range_value_t<_R>, _Alloc> {
175 for (; __i != __s; ++__i)
176 co_yield static_cast<_Yielded
>(*__i);
178 return yield_value(ranges::elements_of(__n(allocator_arg,
180 ranges::begin(__r.range),
181 ranges::end(__r.range))));
186 final_suspend() noexcept
190 unhandled_exception()
197 if (_M_nest._M_is_bottom())
203 void await_transform() =
delete;
204 void return_void() const noexcept {}
208 _M_bottom_value() noexcept
209 {
return _M_nest._M_bottom_value(*
this); }
213 {
return _M_nest._M_value(*
this); }
215 _Subyield_state _M_nest;
216 std::exception_ptr _M_except;
219 template<
typename _Yielded>
220 struct _Promise_erased<_Yielded>::_Subyield_state
224 _Coro_handle _M_bottom;
225 _Coro_handle _M_parent;
231 _ValuePtr _M_value =
nullptr;
240 _M_is_bottom() const noexcept
241 {
return !std::holds_alternative<_Frame>(this->_M_stack); }
246 if (
auto __f = std::get_if<_Frame>(&this->_M_stack))
247 return __f->_M_bottom.promise()._M_nest._M_top();
249 auto __bf = std::get_if<_Bottom_frame>(&this->_M_stack);
250 __glibcxx_assert(__bf);
255 _M_push(_Coro_handle __current, _Coro_handle __subyield)
noexcept
257 __glibcxx_assert(&__current.promise()._M_nest ==
this);
258 __glibcxx_assert(this->_M_top() == __current);
260 __subyield.promise()._M_nest._M_jump_in(__current, __subyield);
263 std::coroutine_handle<>
266 if (
auto __f = std::get_if<_Frame>(&this->_M_stack))
270 auto __p = this->_M_top() = __f->_M_parent;
275 return std::noop_coroutine();
279 _M_jump_in(_Coro_handle __rest, _Coro_handle __new)
noexcept
281 __glibcxx_assert(&__new.promise()._M_nest ==
this);
282 __glibcxx_assert(this->_M_is_bottom());
287 __glibcxx_assert(!this->_M_top());
289 auto& __rn = __rest.promise()._M_nest;
290 __rn._M_top() = __new;
293 auto __bott = __rest;
294 if (
auto __f = std::get_if<_Frame>(&__rn._M_stack))
298 __bott = __f->_M_bottom;
300 this->_M_stack = _Frame {
307 _M_bottom_value(_Promise_erased& __current)
noexcept
309 __glibcxx_assert(&__current._M_nest ==
this);
310 if (
auto __bf = std::get_if<_Bottom_frame>(&this->_M_stack))
311 return __bf->_M_value;
312 auto __f = std::get_if<_Frame>(&this->_M_stack);
313 __glibcxx_assert(__f);
314 auto& __p = __f->_M_bottom.promise();
315 return __p._M_nest._M_value(__p);
319 _M_value(_Promise_erased& __current)
noexcept
321 __glibcxx_assert(&__current._M_nest ==
this);
322 auto __bf = std::get_if<_Bottom_frame>(&this->_M_stack);
323 __glibcxx_assert(__bf);
324 return __bf->_M_value;
328 template<
typename _Yielded>
329 struct _Promise_erased<_Yielded>::_Final_awaiter
331 bool await_ready() noexcept
334 template<
typename _Promise>
335 auto await_suspend(std::coroutine_handle<_Promise> __c)
noexcept
337#ifdef __glibcxx_is_pointer_interconvertible
339 _Promise_erased, _Promise>);
342 auto& __n = __c.promise()._M_nest;
346 void await_resume() noexcept {}
349 template<
typename _Yielded>
350 struct _Promise_erased<_Yielded>::_Copy_awaiter
352 _Yielded_decvref _M_value;
353 _ValuePtr& _M_bottom_value;
355 constexpr bool await_ready() noexcept
358 template<
typename _Promise>
359 void await_suspend(std::coroutine_handle<_Promise>)
noexcept
361#ifdef __glibcxx_is_pointer_interconvertible
363 _Promise_erased, _Promise>);
369 await_resume() const noexcept
373 template<
typename _Yielded>
374 template<
typename _Gen>
375 struct _Promise_erased<_Yielded>::_Recursive_awaiter
378 static_assert(__is_generator<_Gen>);
379 static_assert(std::same_as<typename _Gen::yielded, _Yielded>);
381 _Recursive_awaiter(_Gen __gen) noexcept
383 { this->_M_gen._M_mark_as_started(); }
386 await_ready() const noexcept
390 template<
typename _Promise>
391 std::coroutine_handle<>
392 await_suspend(std::coroutine_handle<_Promise> __p)
noexcept
394#ifdef __glibcxx_is_pointer_interconvertible
396 _Promise_erased, _Promise>);
399 auto __c = _Coro_handle::from_address(__p.address());
400 auto __t = _Coro_handle::from_address(this->_M_gen._M_coro.address());
401 __p.promise()._M_nest._M_push(__c, __t);
407 if (
auto __e = _M_gen._M_coro.promise()._M_except)
414 alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
415 char _M_data[__STDCPP_DEFAULT_NEW_ALIGNMENT__];
418 _M_cnt(std::size_t __sz)
noexcept
420 auto __blksz =
sizeof(_Alloc_block);
421 return (__sz + __blksz - 1) / __blksz;
425 template<
typename _All>
426 concept _Stateless_alloc
427 = default_initializable<_All>
428 && allocator_traits<_All>::is_always_equal::value;
430 template<
typename _Allocator>
433 using _Rebound = __alloc_rebind<_Allocator, _Alloc_block>;
434 using _Rebound_ATr = allocator_traits<_Rebound>;
435 static_assert(is_pointer_v<typename _Rebound_ATr::pointer>,
436 "Must use allocators for true pointers with generators");
439 _M_alloc_address(std::uintptr_t __fn, std::uintptr_t __fsz)
noexcept
441 auto __an = __fn + __fsz;
442 auto __ba =
alignof(_Rebound);
443 return reinterpret_cast<_Rebound*
>(((__an + __ba - 1) / __ba) * __ba);
447 _M_alloc_size(std::size_t __csz)
noexcept
449 auto __ba =
alignof(_Rebound);
454 return __csz + __ba +
sizeof(_Rebound);
458 _M_allocate(_Rebound __b, std::size_t __csz)
460 if constexpr (_Stateless_alloc<_Rebound>)
462 return __b.allocate(_Alloc_block::_M_cnt(__csz));
465 auto __nsz = _Alloc_block::_M_cnt(_M_alloc_size(__csz));
466 auto __f = __b.allocate(__nsz);
467 auto __fn =
reinterpret_cast<std::uintptr_t
>(__f);
468 auto __an = _M_alloc_address(__fn, __csz);
476 operator new(std::size_t __sz)
477 requires default_initializable<_Rebound>
478 {
return _M_allocate({}, __sz); }
483 template<
typename _Alloc,
typename... _Args>
485 operator new(std::size_t __sz,
486 allocator_arg_t,
const _Alloc& __a,
489 static_assert(convertible_to<const _Alloc&, _Allocator>,
490 "the allocator argument to the coroutine must be "
491 "convertible to the generator's allocator type");
492 return _M_allocate(_Rebound(_Allocator(__a)), __sz);
495 template<
typename _This,
typename _Alloc,
typename... _Args>
497 operator new(std::size_t __sz,
499 allocator_arg_t,
const _Alloc& __a,
502 static_assert(convertible_to<const _Alloc&, _Allocator>,
503 "the allocator argument to the coroutine must be "
504 "convertible to the generator's allocator type");
505 return _M_allocate(_Rebound(_Allocator(__a)), __sz);
509 operator delete(
void* __ptr, std::size_t __csz)
noexcept
511 if constexpr (_Stateless_alloc<_Rebound>)
514 return __b.deallocate(
reinterpret_cast<_Alloc_block*
>(__ptr),
515 _Alloc_block::_M_cnt(__csz));
519 auto __nsz = _Alloc_block::_M_cnt(_M_alloc_size(__csz));
520 auto __fn =
reinterpret_cast<std::uintptr_t
>(__ptr);
521 auto __an = _M_alloc_address(__fn, __csz);
524 __b.deallocate(
reinterpret_cast<_Alloc_block*
>(__ptr), __nsz);
530 class _Promise_alloc<void>
532 using _Dealloc_fn = void (*)(
void*, std::size_t);
535 _M_dealloc_address(std::uintptr_t __fn, std::uintptr_t __fsz)
noexcept
537 auto __an = __fn + __fsz;
538 auto __ba =
alignof(_Dealloc_fn);
539 auto __aligned = ((__an + __ba - 1) / __ba) * __ba;
540 return reinterpret_cast<_Dealloc_fn*
>(__aligned);
543 template<
typename _Rebound>
545 _M_alloc_address(std::uintptr_t __fn, std::uintptr_t __fsz)
noexcept
546 requires (!_Stateless_alloc<_Rebound>)
548 auto __ba =
alignof(_Rebound);
549 auto __da = _M_dealloc_address(__fn, __fsz);
550 auto __aan =
reinterpret_cast<std::uintptr_t
>(__da);
551 __aan +=
sizeof(_Dealloc_fn);
552 auto __aligned = ((__aan + __ba - 1) / __ba) * __ba;
553 return reinterpret_cast<_Rebound*
>(__aligned);
556 template<
typename _Rebound>
558 _M_alloc_size(std::size_t __csz)
noexcept
562 std::size_t __aa = 0;
563 std::size_t __as = 0;
564 if constexpr (!std::same_as<_Rebound, void>)
566 __aa =
alignof(_Rebound);
567 __as =
sizeof(_Rebound);
569 auto __ba = __aa +
alignof(_Dealloc_fn);
570 return __csz + __ba + __as +
sizeof(_Dealloc_fn);
573 template<
typename _Rebound>
575 _M_deallocator(
void* __ptr, std::size_t __csz)
noexcept
577 auto __asz = _M_alloc_size<_Rebound>(__csz);
578 auto __nblk = _Alloc_block::_M_cnt(__asz);
580 if constexpr (_Stateless_alloc<_Rebound>)
583 __b.deallocate(
reinterpret_cast<_Alloc_block*
>(__ptr), __nblk);
587 auto __fn =
reinterpret_cast<std::uintptr_t
>(__ptr);
588 auto __an = _M_alloc_address<_Rebound>(__fn, __csz);
591 __b.deallocate(
reinterpret_cast<_Alloc_block*
>(__ptr), __nblk);
595 template<
typename _Alloc>
597 _M_allocate(
const _Alloc& __a, std::size_t __csz)
599 using _Rebound = __alloc_rebind<_Alloc, _Alloc_block>;
600 using _Rebound_ATr = allocator_traits<_Rebound>;
602 static_assert(is_pointer_v<typename _Rebound_ATr::pointer>,
603 "Must use allocators for true pointers with generators");
605 _Dealloc_fn __d = &_M_deallocator<_Rebound>;
606 auto __b =
static_cast<_Rebound
>(__a);
607 auto __asz = _M_alloc_size<_Rebound>(__csz);
608 auto __nblk = _Alloc_block::_M_cnt(__asz);
609 void* __p = __b.allocate(__nblk);
610 auto __pn =
reinterpret_cast<std::uintptr_t
>(__p);
611 *_M_dealloc_address(__pn, __csz) = __d;
612 if constexpr (!_Stateless_alloc<_Rebound>)
614 auto __an = _M_alloc_address<_Rebound>(__pn, __csz);
621 operator new(std::size_t __sz)
623 auto __nsz = _M_alloc_size<void>(__sz);
624 _Dealloc_fn __d = [] (
void* __ptr, std::size_t __sz)
626 ::operator
delete(__ptr, _M_alloc_size<void>(__sz));
628 auto __p = ::operator
new(__nsz);
629 auto __pn =
reinterpret_cast<uintptr_t
>(__p);
630 *_M_dealloc_address(__pn, __sz) = __d;
634 template<
typename _Alloc,
typename... _Args>
636 operator new(std::size_t __sz,
637 allocator_arg_t,
const _Alloc& __a,
639 {
return _M_allocate(__a, __sz); }
641 template<
typename _This,
typename _Alloc,
typename... _Args>
643 operator new(std::size_t __sz,
645 allocator_arg_t,
const _Alloc& __a,
647 {
return _M_allocate(__a, __sz); }
650 operator delete(
void* __ptr, std::size_t __sz)
noexcept
653 auto __pn =
reinterpret_cast<uintptr_t
>(__ptr);
654 __d = *_M_dealloc_address(__pn, __sz);
659 template<
typename _Tp>
660 concept _Cv_unqualified_object = is_object_v<_Tp>
661 && same_as<_Tp, remove_cv_t<_Tp>>;
665 template<
typename _Ref,
typename _Val,
typename _Alloc>
669 using _Value = __conditional_t<is_void_v<_Val>,
670 remove_cvref_t<_Ref>,
672 static_assert(__gen::_Cv_unqualified_object<_Value>,
673 "Generator value must be a cv-unqualified object type");
674 using _Reference = __gen::_Reference_t<_Ref, _Val>;
675 static_assert(is_reference_v<_Reference>
676 || (__gen::_Cv_unqualified_object<_Reference>
677 && copy_constructible<_Reference>),
678 "Generator reference type must be either a cv-unqualified "
679 "object type that is trivially constructible or a "
682 using _RRef = __conditional_t<
683 is_reference_v<_Reference>,
684 remove_reference_t<_Reference>&&,
688 static_assert(common_reference_with<_Reference&&, _Value&&>);
689 static_assert(common_reference_with<_Reference&&, _RRef&&>);
690 static_assert(common_reference_with<_RRef&&, const _Value&>);
692 using _Yielded = __gen::_Yield_t<_Reference>;
693 using _Erased_promise = __gen::_Promise_erased<_Yielded>;
697 friend _Erased_promise;
698 friend struct _Erased_promise::_Subyield_state;
700 using yielded = _Yielded;
702 struct promise_type : _Erased_promise, __gen::_Promise_alloc<_Alloc>
704 generator get_return_object() noexcept
705 {
return { coroutine_handle<promise_type>::from_promise(*
this) }; }
708#ifdef __glibcxx_is_pointer_interconvertible
713 generator(
const generator&) =
delete;
715 generator(generator&& __other) noexcept
716 : _M_coro(std::__exchange(__other._M_coro,
nullptr)),
717 _M_began(std::__exchange(__other._M_began,
false))
722 if (
auto& __c = this->_M_coro)
727 operator=(generator __other)
noexcept
729 swap(__other._M_coro, this->_M_coro);
730 swap(__other._M_began, this->_M_began);
737 this->_M_mark_as_started();
738 auto __h = _Coro_handle::from_promise(_M_coro.promise());
739 __h.promise()._M_nest._M_top() = __h;
748 using _Coro_handle = std::coroutine_handle<_Erased_promise>;
750 generator(coroutine_handle<promise_type> __coro) noexcept
751 : _M_coro { move(__coro) }
755 _M_mark_as_started() noexcept
757 __glibcxx_assert(!this->_M_began);
758 this->_M_began =
true;
761 coroutine_handle<promise_type> _M_coro;
762 bool _M_began =
false;
765 template<
class _Ref,
class _Val,
class _Alloc>
766 struct generator<_Ref, _Val, _Alloc>::_Iterator
768 using value_type = _Value;
769 using difference_type = ptrdiff_t;
772 operator==(
const _Iterator& __i, default_sentinel_t)
noexcept
773 {
return __i._M_coro.done(); }
775 friend class generator;
777 _Iterator(_Iterator&& __o) noexcept
778 : _M_coro(std::__exchange(__o._M_coro, {}))
782 operator=(_Iterator&& __o)
noexcept
784 this->_M_coro = std::__exchange(__o._M_coro, {});
797 { this->operator++(); }
801 const noexcept(is_nothrow_move_constructible_v<_Reference>)
803 auto& __p = this->_M_coro.promise();
804 return static_cast<_Reference
>(*__p._M_value());
808 friend class generator;
810 _Iterator(_Coro_handle __g)
816 auto& __t = this->_M_coro.promise()._M_nest._M_top();
820 _Coro_handle _M_coro;
827 template<
typename _Ref,
typename _Val =
void>
828 using generator = std::generator<_Ref, _Val, polymorphic_allocator<std::byte>>;
832_GLIBCXX_END_NAMESPACE_VERSION
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
constexpr bool is_pointer_interconvertible_base_of_v
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
exception_ptr current_exception() noexcept
void rethrow_exception(exception_ptr)
Throw the object pointed to by the exception_ptr.
ISO C++ entities toplevel namespace is std.
constexpr default_sentinel_t default_sentinel
A default sentinel value.
The ranges::view_interface class template.