Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions future/future.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@ template <typename T> class Future {
}

template <typename F>
Future<absl::result_of_t<
typename std::decay<F>::type(typename TryWrapper<T>::type)>>
Future<typename function_traits<F>::return_type>
Then(Lauch policy, F &&fn) {
return ThenImpl(policy, (EmptyExecutor *)nullptr, std::forward<F>(fn));
}

template <typename F, typename Ex>
Future<absl::result_of_t<
typename std::decay<F>::type(typename TryWrapper<T>::type)>>
Future<typename function_traits<F>::return_type>
Then(Ex *executor, F &&fn) {
return ThenImpl(Lauch::Async, executor, std::forward<F>(fn));
}
Expand Down Expand Up @@ -146,12 +144,23 @@ template <typename T> class Future {
return std::move(shared_state_->value_.Value());
}

template <typename R, typename U, typename F, typename Arg>
template <typename R, typename F, typename Arg>
static try_type_t<R> InvokeVoid(F fn, Arg arg, std::true_type/* R is same with void*/) {
fn();
return try_type_t<R>{};
}

template <typename R, typename F, typename Arg>
static try_type_t<R> InvokeVoid(F fn, Arg arg, std::false_type/* R is not void type */) {
return try_type_t<R>(fn());
}

template <typename U, typename F, typename Arg>
static auto Invoke(F fn, Arg arg) ->
typename std::enable_if<!IsTry<U>::value && std::is_same<void, U>::value,
try_type_t<decltype(fn())>>::type {
using type = decltype(fn());
return try_type_t<type>(fn());
using type = decltype(fn());
return InvokeVoid<type>(fn, arg, std::is_same<void, type>{});
}

template <typename U, typename F, typename Arg>
Expand Down
21 changes: 18 additions & 3 deletions tests/future_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,22 @@ TEST(future_then, async_then)
TEST(future_then, void_then) {
auto future = Async([] {});

future.Then(Lauch::Callback, [](Try<void> t) { EXPECT_TRUE(t.HasValue()); });
auto lambda = [] {};
using type = decltype(lambda);
static_assert(std::is_same<function_traits<type>::first_arg_t, void>::value, "");

int a = 0;

auto future1 = future.Then(Lauch::Callback,
[](Try<void> t) {
EXPECT_TRUE(t.HasValue());
}).Then([&a] {
a = 2020;
return;
});

future1.Wait();
EXPECT_EQ(a, 2020);
}

TEST(when_any, any)
Expand Down Expand Up @@ -310,7 +325,6 @@ TEST(when_all, when_all_variadic_get){
p2.SetValue();

auto result = future.Get();
assert(std::get<0>(future.Get()).HasValue());
assert(std::get<0>(result).HasValue());
auto& r1 = std::get<0>(result);
auto& r2 = std::get<1>(result);
Expand Down Expand Up @@ -628,5 +642,6 @@ TEST(future_then, finally){

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
auto result = RUN_ALL_TESTS();
return result;
}