-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
234 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
|
||
#include <concepts> | ||
|
||
|
||
namespace asyncpp { | ||
|
||
// clang-format off | ||
|
||
template <class T> | ||
concept directly_awaitable = requires(std::remove_reference_t<T>& t) { | ||
{ t.await_ready() } -> std::convertible_to<bool>; | ||
{ t.await_resume() }; | ||
}; | ||
|
||
|
||
template <class T> | ||
concept indirectly_awaitable = requires(std::remove_reference_t<T>& t) { | ||
{ t.operator co_await() } -> directly_awaitable; | ||
}; | ||
|
||
|
||
template <class T> | ||
concept awaitable = directly_awaitable<T> || indirectly_awaitable<T>; | ||
|
||
// clang-format on | ||
|
||
template <class T> | ||
struct await_result {}; | ||
|
||
|
||
template <directly_awaitable T> | ||
struct await_result<T> { | ||
using type = decltype(std::declval<T>().await_resume()); | ||
}; | ||
|
||
|
||
template <indirectly_awaitable T> | ||
struct await_result<T> { | ||
using type = decltype(std::declval<T>().operator co_await().await_resume()); | ||
}; | ||
|
||
|
||
template <class T> | ||
using await_result_t = typename await_result<T>::type; | ||
|
||
|
||
}; // namespace asyncpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#pragma once | ||
|
||
#include "concepts.hpp" | ||
#include "interleaving/sequence_point.hpp" | ||
#include "promise.hpp" | ||
|
||
#include <coroutine> | ||
#include <exception> | ||
#include <future> | ||
|
||
|
||
namespace asyncpp { | ||
|
||
namespace impl_join { | ||
|
||
template <class T> | ||
struct joiner; | ||
|
||
template <class T> | ||
struct basic_promise : impl::resumable_promise { | ||
std::promise<T> m_promise; | ||
|
||
joiner<T> get_return_object() { | ||
return { m_promise.get_future() }; | ||
} | ||
|
||
constexpr auto initial_suspend() const noexcept { | ||
return std::suspend_never{}; | ||
} | ||
|
||
void unhandled_exception() noexcept { | ||
m_promise.set_exception(std::current_exception()); | ||
} | ||
|
||
constexpr auto final_suspend() const noexcept { | ||
return std::suspend_never{}; | ||
} | ||
}; | ||
|
||
template <class T> | ||
struct promise : basic_promise<T> { | ||
void return_value(T value) noexcept { | ||
this->m_promise.set_value(std::forward<T>(value)); | ||
} | ||
|
||
void resume() noexcept override { | ||
std::coroutine_handle<promise>::from_promise(*this).resume(); | ||
} | ||
}; | ||
|
||
template <> | ||
struct promise<void> : basic_promise<void> { | ||
void return_void() noexcept { | ||
m_promise.set_value(); | ||
} | ||
|
||
void resume() noexcept override { | ||
std::coroutine_handle<promise>::from_promise(*this).resume(); | ||
} | ||
}; | ||
|
||
template <class T> | ||
struct joiner { | ||
using promise_type = promise<T>; | ||
|
||
std::future<T> future; | ||
}; | ||
|
||
} // namespace impl_join | ||
|
||
|
||
template <awaitable Awaitable> | ||
auto join(Awaitable&& object) -> await_result_t<std::remove_reference_t<Awaitable>> { | ||
using T = await_result_t<std::remove_reference_t<Awaitable>>; | ||
auto joiner_ = [&object]() -> impl_join::joiner<T> { | ||
co_return co_await object; | ||
}(); | ||
if constexpr (std::is_void_v<T> || std::is_reference_v<T>) { | ||
return INTERLEAVED_ACQUIRE(joiner_.future.get()); | ||
} | ||
else { | ||
return std::forward<T>(INTERLEAVED_ACQUIRE(joiner_.future.get())); | ||
} | ||
} | ||
|
||
} // namespace asyncpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.