7#include <function2/function2.hpp>
48 assert(!state_->current.has_value());
49 state_->current.emplace(std::forward<U>(value));
60 State* state_ {
nullptr};
77 std::size_t stack_size_bytes = 262144,
79 if (!
static_cast<bool>(body)) {
80 throw std::invalid_argument(
"generator body is null.");
83 auto state = std::make_shared<State>();
92 return Generator(std::move(coroutine), std::move(state));
102 : stack_size_bytes_(262144)
106 return Generator::Make(std::move(body), stack_size_bytes_, std::move(memory_resource_));
110 stack_size_bytes_ = stack_size_bytes;
111 return std::move(*
this);
115 memory_resource_ = std::move(resource);
116 return std::move(*
this);
120 std::size_t stack_size_bytes_ {0};
134 [[nodiscard]]
bool IsDone() const noexcept {
135 return coroutine_.
IsDone();
144 if (coroutine_.
IsDone()) {
148 state_->current.reset();
151 return state_->current.has_value();
161 if (!state_->current.has_value()) {
162 throw std::logic_error(
"generator has no value.");
165 T value = std::move(*state_->current);
166 state_->current.reset();
172 std::optional<T> current;
175 explicit Generator(Coroutine coroutine, std::shared_ptr<State> state)
176 : state_(std::move(state))
177 , coroutine_(std::move(coroutine)) {}
179 std::shared_ptr<State> state_;
180 Coroutine coroutine_;
Provides a mechanism for a coroutine to suspend itself.
Definition coroutine_suspend_context.hpp:17
virtual void Suspend()=0
Suspends the current coroutine's execution.
static Coroutine Make(CoroutineBody body, std::size_t stack_size_bytes=kDefaultStackSizeBytes, MemoryResourceSharedPtr resource=GetDefaultMemoryResource())
Creates a new coroutine with the specified body and stack size.
void Resume()
Resumes the execution of the coroutine.
bool IsDone() const noexcept
Checks if the coroutine has finished its execution.
Context provided to the generator body to yield values.
Definition generator.hpp:37
YieldContext(const YieldContext &)=delete
YieldContext(YieldContext &&)=delete
YieldContext & operator=(const YieldContext &)=delete
YieldContext & operator=(YieldContext &&)=delete
void operator()(U &&value)
Definition generator.hpp:45
A stackful generator that yields values of type T.
Definition generator.hpp:27
bool IsDone() const noexcept
Checks if the generator has finished its execution.
Definition generator.hpp:134
Generator(Generator &&) noexcept=default
bool Next()
Resumes execution until a value is yielded or the generator completes.
Definition generator.hpp:142
fu2::unique_function< void(YieldContext &)> Body
Definition generator.hpp:64
static Generator Make(Body body, std::size_t stack_size_bytes=262144, MemoryResourceSharedPtr resource=GetDefaultMemoryResource())
Creates a new generator with the specified body and stack size.
Definition generator.hpp:76
Generator(const Generator &)=delete
T DetachValue()
Moves out the current value and clears it.
Definition generator.hpp:159
Main entry point for the cortex coroutine library.
Context for suspending coroutine execution.
Definition base_coroutine.hpp:14
MemoryResourceSharedPtr GetDefaultMemoryResource()
std::shared_ptr< MemoryResource > MemoryResourceSharedPtr
Definition memory_resource.hpp:30
A builder class for creating Generator instances with custom configuration.
Definition generator.hpp:99
Builder()
Definition generator.hpp:101
Builder SetMemoryResource(MemoryResourceSharedPtr resource) &&noexcept
Definition generator.hpp:114
Builder SetStackSizeInBytes(std::size_t stack_size_bytes) &&noexcept
Definition generator.hpp:109
Generator Build(Body body) &&
Definition generator.hpp:105