wwa-coro 0.0.1
Yet Another C++20 Coroutine Library
sync_generator_adapter.h
Go to the documentation of this file.
1#ifndef C6A6302D_1533_460C_84B8_A465FEABCAF6
2#define C6A6302D_1533_460C_84B8_A465FEABCAF6
3
15
16#include <cstddef>
17#include <iterator>
18#include <ranges>
19#include <utility>
20
21#include "async_generator.h"
22#include "eager_task.h"
23
24namespace wwa::coro {
25
38template<typename Result>
39class [[nodiscard]] sync_generator_adapter : public std::ranges::view_interface<sync_generator_adapter<Result>> {
40public:
47 struct iterator {
49 using iterator_concept = std::input_iterator_tag;
50
55 using difference_type = std::ptrdiff_t;
56
58
63 iterator() noexcept : m_op(nullptr) {}
64
70 explicit iterator(const async_generator<Result>::iterator::advance_op& op) : m_op(std::move(op))
71 {
72 this->advance();
73 }
74
78 ~iterator() = default;
80
89 iterator(const iterator& other) noexcept = default;
90
98 iterator(iterator&& other) noexcept = default;
99
109 iterator& operator=(const iterator& other) noexcept = default;
110
119 iterator& operator=(iterator&& other) noexcept = default;
120
126 iterator& operator++() { return this->advance(); }
127
133 void operator++(int) { this->operator++(); }
134
141
148 bool operator==(const iterator& other) const noexcept { return this->m_it == other.m_it; }
149
150 private:
153
161 iterator& advance()
162 {
163 [](iterator* self) -> eager_task { self->m_it = co_await self->m_op; }(this);
164 return *this;
165 }
166 };
167
175 explicit sync_generator_adapter(async_generator<Result> gen) noexcept : m_gen(std::move(gen)) {}
176
185
190
193 sync_generator_adapter& operator=(const sync_generator_adapter&) = delete;
194 sync_generator_adapter& operator=(sync_generator_adapter&&) = delete;
196
207 iterator begin() { return iterator(this->m_gen.begin()); }
208
217 iterator end() noexcept { return {}; }
218
219private:
221};
222
227
235template<typename Result>
237
238} // namespace wwa::coro
239
240#endif /* C6A6302D_1533_460C_84B8_A465FEABCAF6 */
Asynchronous generator.
An input iterator that asynchronously produces values of type Result.
An asynchronous generator that produces values of type Result.
Eager coroutine.
Definition eager_task.h:32
Adapter for converting asynchronous generators to synchronous generators.
~sync_generator_adapter()=default
Destructor.
sync_generator_adapter(async_generator< Result > gen) noexcept
Class constructor.
sync_generator_adapter(sync_generator_adapter &&other) noexcept=default
Move constructor.
iterator begin()
Returns an iterator to the current item of the generator.
iterator end() noexcept
Returns a sentinel iterator that marks the end of the generator.
Eager coroutine.
Library namespace.
sync_generator_adapter(async_generator< Result >) -> sync_generator_adapter< Result >
Deduction guide for sync_generator_adapter.
Iterator for the synchronous generator adapter.
bool operator==(const iterator &other) const noexcept
Compares two iterators for equality.
iterator & operator=(const iterator &other) noexcept=default
Default copy assignment operator.
std::input_iterator_tag iterator_concept
The strongest iterator concept supported by the iterator.
async_generator< Result >::iterator::reference operator*() const
Dereferences the iterator to access the current element.
iterator & operator++()
Advances the iterator to the next element.
void operator++(int)
Advances the iterator to the next element.
iterator(const iterator &other) noexcept=default
Default copy constructor.
iterator(iterator &&other) noexcept=default
Default move constructor.
std::ptrdiff_t difference_type
Difference between the addresses of any two elements in the controlled sequence.
iterator & operator=(iterator &&other) noexcept=default
Default move assignment operator.