Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
typon-concurrency
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
typon
typon-concurrency
Commits
5f506528
Commit
5f506528
authored
Mar 20, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add preliminary support for generators with new coroutine system
parent
dfbd8f84
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
38 deletions
+25
-38
rt/include/typon/generator.hpp
rt/include/typon/generator.hpp
+25
-38
No files found.
rt/include/typon/generator.hpp
View file @
5f506528
...
...
@@ -12,17 +12,17 @@ namespace typon {
/**
* https://github.com/feabhas/coroutines-blog
*/
template
<
typename
T
>
class
Generator
{
class
Promise
{
template
<
typename
T
>
class
Generator
{
class
Promise
{
public:
using
value_type
=
std
::
optional
<
T
>
;
Promise
()
=
default
;
std
::
suspend_always
initial_suspend
()
{
return
{};
}
std
::
suspend_always
final_suspend
()
noexcept
{
final
=
true
;
return
{};
}
std
::
suspend_always
final_suspend
()
noexcept
{
final
=
true
;
return
{};
}
void
unhandled_exception
()
{
std
::
rethrow_exception
(
std
::
move
(
std
::
current_exception
()));
}
...
...
@@ -36,18 +36,14 @@ class Generator
// this->value = std::move(value);
// }
void
return_void
()
{
this
->
value
=
std
::
nullopt
;
}
void
return_void
()
{
this
->
value
=
std
::
nullopt
;
}
inline
Generator
get_return_object
();
value_type
get_value
()
{
return
std
::
move
(
value
);
}
value_type
get_value
()
{
return
std
::
move
(
value
);
}
bool
finished
()
{
//return !value.has_value();
//
return !value.has_value();
return
final
;
}
...
...
@@ -60,12 +56,12 @@ public:
using
value_type
=
T
;
using
promise_type
=
Promise
;
explicit
Generator
(
std
::
coroutine_handle
<
Promise
>
handle
)
:
handle
(
handle
)
{}
explicit
Generator
(
std
::
coroutine_handle
<
Promise
>
handle
)
:
handle
(
handle
)
{}
~
Generator
()
{
if
(
handle
)
{
handle
.
destroy
();
}
if
(
handle
)
{
handle
.
destroy
();
}
}
Promise
::
value_type
next
()
{
...
...
@@ -74,24 +70,21 @@ public:
handle
.
resume
();
}
return
handle
.
promise
().
get_value
();
}
else
{
}
else
{
return
{};
}
}
struct
end_iterator
{};
class
iterator
{
class
iterator
{
public:
using
value_type
=
Promise
::
value_type
;
using
difference_type
=
std
::
ptrdiff_t
;
using
difference_type
=
std
::
ptrdiff_t
;
using
iterator_category
=
std
::
input_iterator_tag
;
iterator
()
=
default
;
iterator
(
Generator
&
generator
)
:
generator
{
&
generator
}
{}
iterator
(
Generator
&
generator
)
:
generator
{
&
generator
}
{}
value_type
operator
*
()
const
{
if
(
generator
)
{
...
...
@@ -107,26 +100,26 @@ public:
return
{};
}
iterator
&
operator
++
()
{
iterator
&
operator
++
()
{
if
(
generator
&&
generator
->
handle
)
{
generator
->
handle
.
resume
();
}
return
*
this
;
}
iterator
&
operator
++
(
int
)
{
iterator
&
operator
++
(
int
)
{
if
(
generator
&&
generator
->
handle
)
{
generator
->
handle
.
resume
();
}
return
*
this
;
}
bool
operator
==
(
const
end_iterator
&
)
const
{
bool
operator
==
(
const
end_iterator
&
)
const
{
return
generator
?
generator
->
handle
.
promise
().
finished
()
:
true
;
}
private:
Generator
*
generator
{};
Generator
*
generator
{};
};
iterator
begin
()
{
...
...
@@ -134,24 +127,18 @@ public:
return
++
it
;
}
end_iterator
end
()
{
return
end_sentinel
;
}
end_iterator
end
()
{
return
end_sentinel
;
}
std
::
optional
<
value_type
>
py_next
()
{
return
next
();
}
std
::
optional
<
value_type
>
py_next
()
{
return
next
();
}
private:
end_iterator
end_sentinel
{};
std
::
coroutine_handle
<
Promise
>
handle
;
};
template
<
typename
T
>
inline
Generator
<
T
>
Generator
<
T
>::
Promise
::
get_return_object
()
{
return
Generator
{
std
::
coroutine_handle
<
Promise
>::
from_promise
(
*
this
)
};
inline
Generator
<
T
>
Generator
<
T
>::
Promise
::
get_return_object
()
{
return
Generator
{
std
::
coroutine_handle
<
Promise
>::
from_promise
(
*
this
)};
}
}
// namespace typon
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment