cortex 0.0.1
Loading...
Searching...
No Matches
coroutine.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <memory>
5
8
14namespace cortex {
15
16namespace detail {
17class CoroutineImpl;
18}
19
28class Coroutine final {
29public:
30 static constexpr std::size_t kDefaultStackSizeBytes = 262144;
42 std::size_t stack_size_bytes = kDefaultStackSizeBytes,
44
52 struct Builder {
53 public:
60
69
76 Builder SetStackSizeInBytes(std::size_t stack_size_bytes) && noexcept;
77
85
86 private:
87 std::size_t stack_size_bytes_ {0};
88 MemoryResourceSharedPtr memory_resource_ {nullptr};
89 };
90
91 Coroutine(const Coroutine&) = delete;
92 Coroutine(Coroutine&&) noexcept;
93 Coroutine& operator=(const Coroutine&) = delete;
94 Coroutine& operator=(Coroutine&&) noexcept;
96
101 [[nodiscard]] std::size_t GetStackSize() const noexcept;
102
107 [[nodiscard]] bool IsDone() const noexcept;
108
117 void Resume();
118
119private:
120 struct ImplDeleter {
122 void operator()(detail::CoroutineImpl* impl) const;
123 };
124
125 explicit Coroutine(std::unique_ptr<detail::CoroutineImpl, ImplDeleter> impl);
126 std::unique_ptr<detail::CoroutineImpl, ImplDeleter> impl_;
127};
128
129} // namespace cortex
A stackful coroutine that provides a mechanism for cooperative multitasking.
Definition coroutine.hpp:28
Coroutine(const Coroutine &)=delete
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.
static constexpr std::size_t kDefaultStackSizeBytes
Definition coroutine.hpp:30
std::size_t GetStackSize() const noexcept
Gets the allocated stack size of the coroutine.
Coroutine(Coroutine &&) noexcept
Definition of the coroutine execution body.
Definition base_coroutine.hpp:14
MemoryResourceSharedPtr GetDefaultMemoryResource()
std::shared_ptr< MemoryResource > MemoryResourceSharedPtr
Definition memory_resource.hpp:30
fu2::unique_function< void(CoroutineSuspendContext &)> CoroutineBody
The signature for a coroutine's entry point.
Definition coroutine_body.hpp:21
A builder class for creating Coroutine instances with custom configuration.
Definition coroutine.hpp:52
Builder()
Default constructor for Builder.
Builder SetMemoryResource(MemoryResourceSharedPtr resource) &&noexcept
Sets the memory resource for the coroutine to be built.
Builder SetStackSizeInBytes(std::size_t stack_size_bytes) &&noexcept
Sets the stack size for the coroutine to be built.
Coroutine Build(CoroutineBody body) &&
Builds and returns a new Coroutine instance.