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 "../../raw_type.hpp"
13//#include "../../string.hpp"
14#include <coroutine>
15#include <exception>
16
18namespace xtd {
20 namespace collections {
22 namespace generic {
44 template<typename type_t>
45 class enumerable_generator : public xtd::object, public xtd::collections::generic::ienumerable<type_t> {
46 public:
50 struct promise_type {
51 using value_type = xtd::raw_type<type_t>;
53 value_type current_value;
54
56 std::exception_ptr exception;
57
58
61 enumerable_generator get_return_object() {return enumerable_generator {std::coroutine_handle<promise_type>::from_promise(*this)};}
62
66 std::suspend_always initial_suspend() noexcept {return {};}
67
71 std::suspend_always final_suspend() noexcept {return {};}
72
74 void return_void() noexcept {}
75
77 void unhandled_exception() noexcept {exception = std::current_exception();}
78
83 template<typename value_t>
84 std::suspend_always yield_value(value_t&& value) noexcept {
85 // The following must be refactor.
86 current_value = (decltype(current_value)&)std::forward<value_t>(value);
87 return {};
88 }
89 };
90
93 enumerable_generator& operator =(const enumerable_generator& other) = delete;
94 enumerable_generator(enumerable_generator&& other) noexcept : handle_(std::exchange(other.handle_, {})) {}
95 enumerable_generator& operator =(enumerable_generator&& other) noexcept {
96 if (handle_) handle_.destroy();
97 handle_ = std::exchange(other.handle_, {});
98 return *this;
99 }
100 ~enumerable_generator() override {if (handle_) handle_.destroy();}
102
104
109 struct generator_enumerator final : xtd::collections::generic::ienumerator<type_t> {
110 explicit generator_enumerator(std::coroutine_handle<promise_type> handle) : handle_(handle) {}
111 const type_t& current() const override {
113 return handle_.promise().current_value;
114 }
115 bool move_next() override {
116 started_ = true;
117 if (!handle_ || handle_.done()) return false;
118
119 handle_.resume();
120
121 if (handle_.promise().exception)
122 std::rethrow_exception(handle_.promise().exception);
123
124 return !handle_.done();
125 }
127
128 private:
129 bool started_ = false;
130 std::coroutine_handle<promise_type> handle_;
131 };
132
133 return {new_ptr<generator_enumerator>(handle_)};
134 }
135
138 [[nodiscard]] auto to_string() const -> xtd::string override; // Defined in xtd/string.hpp
140
141 private:
142 explicit enumerable_generator(std::coroutine_handle<promise_type> handle) : handle_(handle) {}
143
144 mutable std::coroutine_handle<promise_type> handle_;
145 };
146 }
147 }
148}
Represents an enumerable generator that supports deferred, lazy iteration over a collection of a spec...
Definition enumerable_generator.hpp:45
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:108
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:40
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
std::remove_cvref_t< value_t > raw_type
Represents a raw type alias equivalent to std::remove_cvref_t<value_t>.
Definition raw_type.hpp:25
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.
Contains xtd::raw_type alias.
The promise type required by the C++20 coroutine standard to manage the state and lifecycle of the en...
Definition enumerable_generator.hpp:50
std::suspend_always yield_value(value_t &&value) noexcept
Captures the value emitted by a co_yield expression and suspends the coroutine execution flow.
Definition enumerable_generator.hpp:84
enumerable_generator get_return_object()
Creates and returns the public instance of the xtd::collections::generic::enumerable_generator linked...
Definition enumerable_generator.hpp:61
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:74
std::suspend_always initial_suspend() noexcept
Defines the initial suspension behavior of the coroutine.
Definition enumerable_generator.hpp:66
value_type current_value
Represents the current value yielded by the coroutine execution state.
Definition enumerable_generator.hpp:53
std::exception_ptr exception
Represents the current exception if exception occured.
Definition enumerable_generator.hpp:56
std::suspend_always final_suspend() noexcept
Defines the final suspension behavior of the coroutine upon completion.
Definition enumerable_generator.hpp:71
void unhandled_exception() noexcept
Catches any unhandled exceptions escaped from the coroutine body.
Definition enumerable_generator.hpp:77
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:39
Contains xtd::helpers::throw_helper class.