Some ideas of a promisify helper

This commit is contained in:
Denis Blank 2018-02-25 17:37:30 +01:00
parent 9be06f4bcc
commit 9c66b53f23
2 changed files with 59 additions and 3 deletions

View File

@ -137,6 +137,31 @@ template <typename T, typename... Args>
is_invokable, std::forward<T>(callable), std::forward<Args>(args)...); is_invokable, std::forward<T>(callable), std::forward<Args>(args)...);
} }
/// Invokes the given callable object with the given arguments
template <typename Callable, typename... Args>
constexpr auto invoke(Callable&& callable, Args&&... args) noexcept(
noexcept(std::forward<Callable>(callable)(std::forward<Args>(args)...)))
-> decltype(std::forward<Callable>(callable)(std::forward<Args>(args)...)) {
return std::forward<Callable>(callable)(std::forward<Args>(args)...);
}
/// Invokes the given member function pointer by reference
template <typename T, typename Type, typename Self, typename... Args>
constexpr auto invoke(Type T::*member, Self&& self, Args&&... args) noexcept(
noexcept((std::forward<Self>(self).*member)(std::forward<Args>(args)...)))
-> decltype((std::forward<Self>(self).*
member)(std::forward<Args>(args)...)) {
return (std::forward<Self>(self).*member)(std::forward<Args>(args)...);
}
/// Invokes the given member function pointer by pointer
template <typename T, typename Type, typename Self, typename... Args>
constexpr auto invoke(Type T::*member, Self&& self, Args&&... args) noexcept(
noexcept((std::forward<Self>(self)->*member)(std::forward<Args>(args)...)))
-> decltype(
(std::forward<Self>(self)->*member)(std::forward<Args>(args)...)) {
return (std::forward<Self>(self)->*member)(std::forward<Args>(args)...);
}
// Class for making child classes non copyable // Class for making child classes non copyable
struct non_copyable { struct non_copyable {
constexpr non_copyable() = default; constexpr non_copyable() = default;

View File

@ -253,7 +253,34 @@ struct future_result {
} // namespace detail } // namespace detail
} // namespace cti } // namespace cti
using namespace cti::detail::remapping; using namespace cti::detail;
using namespace remapping;
template <typename... Result>
struct promisify {
template <typename Callable, typename... Args>
static auto from(Callable&& callable, Args&&... args) {
return cti::make_continuable<Result...>([args = std::make_tuple(
std::forward<Callable>(
callable),
std::forward<Args>(args)...)](
auto&& promise) mutable {
traits::unpack(std::move(args), [promise =
std::forward<decltype(promise)>(
promise)](
auto&&... args) mutable {
util::invoke(
std::forward<decltype(args)>(args)..., [promise =
std::move(promise)](
auto&&... /*result*/){
});
});
});
}
};
int main(int, char**) { int main(int, char**) {
@ -270,7 +297,7 @@ int main(int, char**) {
auto r = create_result_pack(std::move(p)); auto r = create_result_pack(std::move(p));
auto i = create_index_pack(std::move(p)); auto i = create_index_pack(std::move(p));
relocate_index_pack( /*relocate_index_pack(
[](auto index, auto result) [](auto index, auto result)
-> std::enable_if_t<is_indexed_continuable< -> std::enable_if_t<is_indexed_continuable<
std::remove_pointer_t<std::decay_t<decltype(index)>>>::value> { std::remove_pointer_t<std::decay_t<decltype(index)>>>::value> {
@ -280,7 +307,11 @@ int main(int, char**) {
return; return;
}, },
&i, &r); &i, &r);*/
auto t = [](auto&&...) {};
promisify<int>::from(t, "");
return 0; return 0;
} }