Commit 3fd7ef23 authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[base] Add ResultType to base::Callback

This change adds a public ResultType alias to base::Callback, specifying
the return type of `Run()`. This is analogous to std::function's nested
result_type.

Bug: 554299
Change-Id: Ie478345b68051b03cd5b064b0833d528d47990b4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2436536
Auto-Submit: Jan Wilken Dörrie <jdoerrie@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812087}
parent 353d9f59
...@@ -57,6 +57,7 @@ namespace base { ...@@ -57,6 +57,7 @@ namespace base {
template <typename R, typename... Args> template <typename R, typename... Args>
class OnceCallback<R(Args...)> : public internal::CallbackBase { class OnceCallback<R(Args...)> : public internal::CallbackBase {
public: public:
using ResultType = R;
using RunType = R(Args...); using RunType = R(Args...);
using PolymorphicInvoke = R (*)(internal::BindStateBase*, using PolymorphicInvoke = R (*)(internal::BindStateBase*,
internal::PassingType<Args>...); internal::PassingType<Args>...);
...@@ -103,6 +104,7 @@ class OnceCallback<R(Args...)> : public internal::CallbackBase { ...@@ -103,6 +104,7 @@ class OnceCallback<R(Args...)> : public internal::CallbackBase {
template <typename R, typename... Args> template <typename R, typename... Args>
class RepeatingCallback<R(Args...)> : public internal::CallbackBaseCopyable { class RepeatingCallback<R(Args...)> : public internal::CallbackBaseCopyable {
public: public:
using ResultType = R;
using RunType = R(Args...); using RunType = R(Args...);
using PolymorphicInvoke = R (*)(internal::BindStateBase*, using PolymorphicInvoke = R (*)(internal::BindStateBase*,
internal::PassingType<Args>...); internal::PassingType<Args>...);
......
...@@ -58,6 +58,24 @@ class CallbackTest : public ::testing::Test { ...@@ -58,6 +58,24 @@ class CallbackTest : public ::testing::Test {
RepeatingCallback<void()> null_callback_; RepeatingCallback<void()> null_callback_;
}; };
TEST_F(CallbackTest, Types) {
static_assert(std::is_same<void, OnceClosure::ResultType>::value, "");
static_assert(std::is_same<void(), OnceClosure::RunType>::value, "");
using OnceCallbackT = OnceCallback<double(int, char)>;
static_assert(std::is_same<double, OnceCallbackT::ResultType>::value, "");
static_assert(std::is_same<double(int, char), OnceCallbackT::RunType>::value,
"");
static_assert(std::is_same<void, RepeatingClosure::ResultType>::value, "");
static_assert(std::is_same<void(), RepeatingClosure::RunType>::value, "");
using RepeatingCallbackT = RepeatingCallback<bool(float, short)>;
static_assert(std::is_same<bool, RepeatingCallbackT::ResultType>::value, "");
static_assert(
std::is_same<bool(float, short), RepeatingCallbackT::RunType>::value, "");
}
// Ensure we can create unbound callbacks. We need this to be able to store // Ensure we can create unbound callbacks. We need this to be able to store
// them in class members that can be initialized later. // them in class members that can be initialized later.
TEST_F(CallbackTest, DefaultConstruction) { TEST_F(CallbackTest, DefaultConstruction) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment