xtd 1.0.0
Loading...
Searching...
No Matches
enumerable_generator.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "ienumerable.hpp"
6#include "ienumerator.hpp"
7#define __XTD_CORE_INTERNAL__
9#undef __XTD_CORE_INTERNAL__
11#include "../../object.hpp"
12//#include "../../string.hpp"
13#include <coroutine>
14#include <exception>
15
17namespace xtd {
19 namespace collections {
21 namespace generic {
43 template<typename type_t>
44 class enumerable_generator : public xtd::object, public xtd::collections::generic::ienumerable<type_t> {
45 public:
49 struct promise_type {
52
54 std::exception_ptr exception;
55
56
59 enumerable_generator get_return_object() {return enumerable_generator {std::coroutine_handle<promise_type>::from_promise(*this)};}
60
64 std::suspend_always initial_suspend() noexcept {return {};}
65
69 std::suspend_always final_suspend() noexcept {return {};}
70
72 void return_void() noexcept {}
73
76 void unhandled_exception() noexcept {exception = std::current_exception();}
77
82 std::suspend_always yield_value(const type_t& value) noexcept {
83 current_value = value;
84 return {};
85 }
86
90 std::suspend_always yield_value(type_t& value) noexcept {
91 current_value = value;
92 return {};
93 }
94
98 std::suspend_always yield_value(type_t&& value) noexcept {
99 current_value = std::move(value);
100 return {};
101 }
102 };
103
106 enumerable_generator& operator =(const enumerable_generator& other) = delete;
107 enumerable_generator(enumerable_generator&& other) noexcept : handle_(std::exchange(other.handle_, {})) {}
108 enumerable_generator& operator =(enumerable_generator&& other) noexcept {
109 handle_ = std::exchange(other.handle_, {});
110 return *this;
111 }
112 ~enumerable_generator() override {if (handle_) handle_.destroy();}
114
116
121 struct generator_enumerator final : xtd::collections::generic::ienumerator<type_t> {
122 explicit generator_enumerator(std::coroutine_handle<promise_type> handle) : handle_(handle) {}
123 const type_t& current() const override {
125 return handle_.promise().current_value;
126 }
127 bool move_next() override {
128 started_ = true;
129 if (!handle_ || handle_.done()) return false;
130
131 handle_.resume();
132
133 if (handle_.promise().exception)
134 std::rethrow_exception(handle_.promise().exception);
135
136 return !handle_.done();
137 }
139
140 private:
141 bool started_ = false;
142 std::coroutine_handle<promise_type> handle_;
143 };
144
145 return {new_ptr<generator_enumerator>(handle_)};
146 }
147
150 [[nodiscard]] auto to_string() const -> xtd::string override; // Defined in xtd/string.hpp
152
153 private:
154 explicit enumerable_generator(std::coroutine_handle<promise_type> handle) : handle_(handle) {}
155
156 mutable std::coroutine_handle<promise_type> handle_;
157 };
158 }
159 }
160}
Represents an enumerable generator that supports deferred, lazy iteration over a collection of a spec...
Definition enumerable_generator.hpp:44
xtd::collections::generic::enumerator< type_t > get_enumerator() const override
Returns an enumerator that iterates through the xtd::collections::generic::enumerable_generator.
Definition enumerable_generator.hpp:120
auto to_string() const -> xtd::string override
Returns a xtd::string that represents the current object.
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:40
Supports a simple iteration over a generic collection.
Definition ienumerator.hpp:58
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
Contains xtd::collections::generic::ienumerable <type_t> interface.
Contains xtd::collections::generic::ienumerator <type_t> interface.
@ not_supported
The method or operation is not supported.
Definition exception_case.hpp:77
@ invalid_operation
The operation is not valid.
Definition exception_case.hpp:65
auto new_ptr(args_t &&... args) -> xtd::ptr< type_t >
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
@ other
The operating system is other.
Definition platform_id.hpp:60
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd::collections namespace contains interfaces and classes that define various collections of obj...
Definition any_pair.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::object class.
The promise type required by the C++20 coroutine standard to manage the state and lifecycle of the en...
Definition enumerable_generator.hpp:49
enumerable_generator get_return_object()
Creates and returns the public instance of the xtd::collections::generic::enumerable_generator linked...
Definition enumerable_generator.hpp:59
void return_void() noexcept
Handles the completion of a coroutine function that does not return a final value (using co_return; o...
Definition enumerable_generator.hpp:72
type_t current_value
Represents the current value yielded by the coroutine execution state.
Definition enumerable_generator.hpp:51
std::suspend_always yield_value(type_t &value) noexcept
Captures the value emitted by a co_yield expression and suspends the coroutine execution flow.
Definition enumerable_generator.hpp:90
std::suspend_always initial_suspend() noexcept
Defines the initial suspension behavior of the coroutine.
Definition enumerable_generator.hpp:64
std::suspend_always yield_value(type_t &&value) noexcept
Captures the value emitted by a co_yield expression and suspends the coroutine execution flow.
Definition enumerable_generator.hpp:98
std::exception_ptr exception
Represents the current exception if exception occured.
Definition enumerable_generator.hpp:54
std::suspend_always final_suspend() noexcept
Defines the final suspension behavior of the coroutine upon completion.
Definition enumerable_generator.hpp:69
std::suspend_always yield_value(const type_t &value) noexcept
Captures the value emitted by a co_yield expression and suspends the coroutine execution flow.
Definition enumerable_generator.hpp:82
void unhandled_exception() noexcept
Catches any unhandled exceptions escaped from the coroutine body.
Definition enumerable_generator.hpp:76
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:39
Contains xtd::helpers::throw_helper class.