Skip to content

Commit c884fce

Browse files
authored
Merge pull request #5 from Madokakaroto/master
void function support for then
2 parents b974d13 + 4f36cbe commit c884fce

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

future/future.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,13 @@ template <typename T> class Future {
5151
}
5252

5353
template <typename F>
54-
Future<absl::result_of_t<
55-
typename std::decay<F>::type(typename TryWrapper<T>::type)>>
54+
Future<typename function_traits<F>::return_type>
5655
Then(Lauch policy, F &&fn) {
5756
return ThenImpl(policy, (EmptyExecutor *)nullptr, std::forward<F>(fn));
5857
}
5958

6059
template <typename F, typename Ex>
61-
Future<absl::result_of_t<
62-
typename std::decay<F>::type(typename TryWrapper<T>::type)>>
60+
Future<typename function_traits<F>::return_type>
6361
Then(Ex *executor, F &&fn) {
6462
return ThenImpl(Lauch::Async, executor, std::forward<F>(fn));
6563
}
@@ -146,12 +144,23 @@ template <typename T> class Future {
146144
return std::move(shared_state_->value_.Value());
147145
}
148146

149-
template <typename R, typename U, typename F, typename Arg>
147+
template <typename R, typename F, typename Arg>
148+
static try_type_t<R> InvokeVoid(F fn, Arg arg, std::true_type/* R is same with void*/) {
149+
fn();
150+
return try_type_t<R>{};
151+
}
152+
153+
template <typename R, typename F, typename Arg>
154+
static try_type_t<R> InvokeVoid(F fn, Arg arg, std::false_type/* R is not void type */) {
155+
return try_type_t<R>(fn());
156+
}
157+
158+
template <typename U, typename F, typename Arg>
150159
static auto Invoke(F fn, Arg arg) ->
151160
typename std::enable_if<!IsTry<U>::value && std::is_same<void, U>::value,
152161
try_type_t<decltype(fn())>>::type {
153-
using type = decltype(fn());
154-
return try_type_t<type>(fn());
162+
using type = decltype(fn());
163+
return InvokeVoid<type>(fn, arg, std::is_same<void, type>{});
155164
}
156165

157166
template <typename U, typename F, typename Arg>

tests/future_test.cc

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,22 @@ TEST(future_then, async_then)
4444
TEST(future_then, void_then) {
4545
auto future = Async([] {});
4646

47-
future.Then(Lauch::Callback, [](Try<void> t) { EXPECT_TRUE(t.HasValue()); });
47+
auto lambda = [] {};
48+
using type = decltype(lambda);
49+
static_assert(std::is_same<function_traits<type>::first_arg_t, void>::value, "");
50+
51+
int a = 0;
52+
53+
auto future1 = future.Then(Lauch::Callback,
54+
[](Try<void> t) {
55+
EXPECT_TRUE(t.HasValue());
56+
}).Then([&a] {
57+
a = 2020;
58+
return;
59+
});
60+
61+
future1.Wait();
62+
EXPECT_EQ(a, 2020);
4863
}
4964

5065
TEST(when_any, any)
@@ -310,7 +325,6 @@ TEST(when_all, when_all_variadic_get){
310325
p2.SetValue();
311326

312327
auto result = future.Get();
313-
assert(std::get<0>(future.Get()).HasValue());
314328
assert(std::get<0>(result).HasValue());
315329
auto& r1 = std::get<0>(result);
316330
auto& r2 = std::get<1>(result);
@@ -628,5 +642,6 @@ TEST(future_then, finally){
628642

629643
int main(int argc, char **argv) {
630644
::testing::InitGoogleTest(&argc, argv);
631-
return RUN_ALL_TESTS();
645+
auto result = RUN_ALL_TESTS();
646+
return result;
632647
}

0 commit comments

Comments
 (0)