Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
typon
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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tom Niget
typon
Commits
1e4aa258
Commit
1e4aa258
authored
Apr 07, 2022
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WIP
parent
a429ac16
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
147 additions
and
108 deletions
+147
-108
rt/promise.hpp
rt/promise.hpp
+8
-89
rt/result.hpp
rt/result.hpp
+115
-0
rt/task.hpp
rt/task.hpp
+24
-19
No files found.
rt/promise.hpp
View file @
1e4aa258
...
...
@@ -2,102 +2,29 @@
#define __PROMISE_HPP__
#include <cassert>
#include <exception>
#include <memory>
#include <type_traits>
#include <result.hpp>
namespace
typon
::
detail
namespace
typon
{
template
<
typename
T
>
struct
promise
{
enum
{
Exception
,
Value
}
_tag
=
Exception
;
union
{
T
_value
;
std
::
exception_ptr
_exception
=
nullptr
;
};
~
promise
()
{
switch
(
_tag
)
{
case
Value
:
std
::
destroy_at
(
std
::
addressof
(
_value
));
break
;
case
Exception
:
std
::
destroy_at
(
std
::
addressof
(
_exception
));
break
;
}
}
result
<
T
>*
_result
;
template
<
typename
U
>
void
return_value
(
U
&&
expr
)
noexcept
(
std
::
is_nothrow_constructible_v
<
T
,
U
&&>
)
{
std
::
construct_at
(
std
::
addressof
(
_value
),
std
::
forward
<
U
>
(
expr
));
_tag
=
Value
;
}
void
unhandled_exception
()
noexcept
{
_exception
=
std
::
current_exception
();
_tag
=
Exception
;
}
T
&
get
()
&
{
switch
(
_tag
)
{
case
Value
:
return
_value
;
case
Exception
:
assert
(
_exception
)
std
::
rethrow_exception
(
_exception
);
}
}
T
&&
get
()
&&
{
switch
(
_tag
)
{
case
Value
:
return
std
::
move
(
_value
);
case
Exception
:
std
::
rethrow_exception
(
_exception
);
}
}
};
template
<
typename
T
>
struct
promise
<
T
&>
{
T
*
_value
;
std
::
exception_ptr
_exception
;
void
return_value
(
T
&
expr
)
noexcept
{
_value
=
std
::
addressof
(
expr
);
_result
->
return_value
(
std
::
forward
<
U
>
(
expr
));
}
void
unhandled_exception
()
noexcept
{
_exception
=
std
::
current_exception
();
}
T
&
get
()
&
{
if
(
_exception
)
{
std
::
rethrow_exception
(
_exception
);
}
return
*
_value
;
_result
->
unhandled_exception
();
}
};
...
...
@@ -107,21 +34,13 @@ namespace typon::detail
struct
promise
<
void
>
{
std
::
exception_ptr
_exception
;
result
<
void
>*
_result
;
void
return_void
()
noexcept
{}
void
unhandled_exception
()
noexcept
{
_exception
=
std
::
current_exception
();
}
void
get
()
{
if
(
_exception
)
{
std
::
rethrow_exception
(
_exception
);
}
_result
->
unhandled_exception
();
}
};
...
...
rt/result.hpp
0 → 100644
View file @
1e4aa258
#ifndef __RESULT_HPP__
#define __RESULT_HPP__
#include <exception>
#include <memory>
#include <type_traits>
namespace
typon
{
template
<
typename
T
>
struct
result
{
std
::
exception_ptr
_exception
;
union
{
T
_value
;
};
~
result
()
{
if
(
!
_exception
)
{
std
::
destroy_at
(
std
::
addressof
(
_value
));
}
}
template
<
typename
U
>
void
return_value
(
U
&&
expr
)
noexcept
(
std
::
is_nothrow_constructible_v
<
T
,
U
&&>
)
{
std
::
construct_at
(
std
::
addressof
(
_value
),
std
::
forward
<
U
>
(
expr
));
}
void
unhandled_exception
()
noexcept
{
_exception
=
std
::
current_exception
();
}
T
&
get
()
&
{
if
(
_exception
)
{
std
::
rethrow_exception
(
_exception
);
}
return
_value
;
}
T
&&
get
()
&&
{
if
(
_exception
)
{
std
::
rethrow_exception
(
_exception
);
}
return
std
::
move
(
_value
);
}
};
template
<
typename
T
>
struct
result
<
T
&>
{
T
*
_value
;
std
::
exception_ptr
_exception
;
void
return_value
(
T
&
expr
)
noexcept
{
_value
=
std
::
addressof
(
expr
);
}
void
unhandled_exception
()
noexcept
{
_exception
=
std
::
current_exception
();
}
T
&
get
()
&
{
if
(
_exception
)
{
std
::
rethrow_exception
(
_exception
);
}
return
*
_value
;
}
};
template
<
>
struct
result
<
void
>
{
std
::
exception_ptr
_exception
;
void
unhandled_exception
()
noexcept
{
_exception
=
std
::
current_exception
();
}
void
get
()
{
if
(
_exception
)
{
std
::
rethrow_exception
(
_exception
);
}
}
};
}
#endif // __RESULT_HPP__
rt/task.hpp
View file @
1e4aa258
...
...
@@ -4,23 +4,31 @@
#include <coroutine>
#include <promise.hpp>
#include <result.hpp>
namespace
typon
{
template
<
typename
T
=
void
>
struct
[[
nodiscard
]]
task
//struct [[nodiscard]] task
struct
task
{
struct
promise_type
;
std
::
coroutine_handle
<
promise_type
>
_coroutine
;
result
<
T
>
_result
;
template
<
typename
T
>
struct
promise_type
:
promise
<
T
>
{
std
::
coroutine_handle
_continuation
;
// std::coroutine_handle<>
_continuation;
task
get_return_object
()
noexcept
{
return
task
{
std
::
coroutine_handle
<
promise
<
T
>>::
from_promise
(
*
this
)
};
task
_task
{
std
::
coroutine_handle
<
promise_type
>::
from_promise
(
*
this
),
result
<
T
>
{}
};
this
->
_result
=
std
::
addressof
(
_task
.
_result
);
return
_task
;
}
std
::
suspend_never
initial_suspend
()
noexcept
...
...
@@ -28,26 +36,23 @@ namespace typon
return
{};
}
auto
final_suspend
()
noexcept
{
struct
awaitable
:
std
::
suspend_always
std
::
suspend_never
final_suspend
()
noexcept
{
template
<
typename
Promise
>
void
await_suspend
(
std
::
coroutine_handle
<
Promise
>
coroutine
)
noexcept
{
return
coroutine
.
promise
().
_continuation
;
}
void
await_resume
()
noexcept
{
/*unreachable*/
};
}
return
awaitable
{};
return
{};
}
};
auto
operator
co_await
()
const
&&
noexcept
{
struct
awaitable
:
std
::
suspend_always
}
// auto operator co_await() const&& noexcept
// {
// struct awaitable
// {
// bool await_ready() noexcept
// {
// }
// };
// }
};
...
...
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