Pass promises to type erased continuables instead of the raw callbacks

This commit is contained in:
Denis Blank 2017-10-03 01:21:32 +02:00
parent a685d9234a
commit 6b9efad602
3 changed files with 17 additions and 26 deletions

View File

@ -52,24 +52,15 @@ namespace cti {
template <template <typename...> class CallbackWrapper,
template <typename...> class ContinuationWrapper, typename... Args>
struct continuable_trait {
/// The callback type which is passed to continuations
// TODO This should be void(promise<...>) I think
using callback =
CallbackWrapper<void(Args...), void(detail::types::dispatch_error_tag,
detail::types::error_type)>;
/// The continuation type which is used to erase the internal data.
using continuation = ContinuationWrapper<void(
CallbackWrapper<void(Args...), void(detail::types::dispatch_error_tag,
detail::types::error_type)>)>;
/// The promise type which is used to resolve continuations
using promise =
promise_base<callback, detail::hints::signature_hint_tag<Args...>>;
using promise = promise_base<
CallbackWrapper<void(Args...), void(detail::types::dispatch_error_tag,
detail::types::error_type)>,
detail::hints::signature_hint_tag<Args...>>;
/// The continuable type for the given parameters.
using continuable =
continuable_base<continuation,
continuable_base<ContinuationWrapper<void(promise)>,
detail::hints::signature_hint_tag<Args...>>;
};
} // namespace cti

View File

@ -41,7 +41,7 @@ namespace cti {
namespace detail {
template<typename... Args>
using trait_of = continuable_trait<
fu2::function,
fu2::unique_function,
fu2::function,
Args...
>;

View File

@ -35,16 +35,6 @@ static cti::continuable<std::string> http_request(std::string url) {
};
}
static cti::continuable<std::string> http_request3(std::string url) {
return [url = std::move(url)](auto promise) {
if (false) {
promise.set_value("");
promise("");
}
promise.set_exception(std::error_condition{});
};
}
static auto http_request2(std::string url) {
return cti::make_continuable<std::string>(
// ...
@ -57,6 +47,16 @@ static auto http_request2(std::string url) {
});
}
static cti::continuable<std::string> http_request3(std::string url) {
return [url = std::move(url)](auto&& promise) {
if (false) {
promise.set_value("");
promise("");
}
promise.set_exception(std::error_condition{});
};
}
int main(int, char**) {
http_request("github.com")
.then([](std::string) {
@ -82,7 +82,7 @@ int main(int, char**) {
// ...
});
(http_request("github.com") && http_request("github.com"))
(http_request("github.com") && http_request3("github.com"))
.then([](std::string, std::string) {
// ...
})