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"
8#include <coroutine>
9#include <exception>
10
12namespace xtd {
14 namespace collections {
16 namespace generic {
38 template<typename type_t>
39 class enumerable_generator : public xtd::collections::generic::ienumerable<type_t> {
40 public:
44 struct promise_type {
47
50 enumerable_generator get_return_object() {return enumerable_generator {std::coroutine_handle<promise_type>::from_promise(*this)};}
51
55 std::suspend_always initial_suspend() noexcept {return {};}
56
60 std::suspend_always final_suspend() noexcept {return {};}
61
63 void return_void() noexcept {}
64
67 void unhandled_exception() {std::terminate();}
68
73 std::suspend_always yield_value(type_t value) noexcept {
74 current_value = std::move(value);
75 return {};
76 }
77 };
78
81 enumerable_generator& operator =(const enumerable_generator& other) = delete;
82 enumerable_generator(enumerable_generator&& other) noexcept : handle_(std::exchange(other.handle_, {})) {}
83 enumerable_generator& operator =(enumerable_generator&& other) noexcept {
84 handle_ = std::exchange(other.handle_, {});
85 return *this;
86 }
87 ~enumerable_generator() override {if (handle_) handle_.destroy();}
89
91
96 struct generator_enumerator final : xtd::collections::generic::ienumerator<type_t> {
97 explicit generator_enumerator(std::coroutine_handle<promise_type> handle) : handle_(handle) {}
98 const type_t& current() const override {
100 return handle_.promise().current_value;
101 }
102 bool move_next() override {
103 started_ = true;
104 if (!handle_ || handle_.done()) return false;
105 handle_.resume();
106 return !handle_.done();
107 }
109
110 private:
111 bool started_ = false;
112 std::coroutine_handle<promise_type> handle_;
113 };
114
115 return {new_ptr<generator_enumerator>(handle_)};
116 }
117
118
119 private:
120 explicit enumerable_generator(std::coroutine_handle<promise_type> handle) : handle_(handle) {}
121
122 mutable std::coroutine_handle<promise_type> handle_;
123 };
124 }
125 }
126}
Represents an enumerable generator that supports deferred, lazy iteration over a collection of a spec...
Definition enumerable_generator.hpp:39
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:95
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.
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
The promise type required by the C++20 coroutine standard to manage the state and lifecycle of the en...
Definition enumerable_generator.hpp:44
void unhandled_exception()
Catches any unhandled exceptions escaped from the coroutine body.
Definition enumerable_generator.hpp:67
enumerable_generator get_return_object()
Creates and returns the public instance of the xtd::collections::generic::enumerable_generator linked...
Definition enumerable_generator.hpp:50
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:73
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:63
type_t current_value
Represents the current value yielded by the coroutine execution state.
Definition enumerable_generator.hpp:46
std::suspend_always initial_suspend() noexcept
Defines the initial suspension behavior of the coroutine.
Definition enumerable_generator.hpp:55
std::suspend_always final_suspend() noexcept
Defines the final suspension behavior of the coroutine upon completion.
Definition enumerable_generator.hpp:60
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:39
Contains xtd::helpers::throw_helper class.