Commit 42f55fa5 authored by Bi Xue's avatar Bi Xue Committed by Facebook Github Bot

Make FunctionRef support bool operator

Summary:
To support following code:
```
void foo(folly::FunctionRef<void(void)> callback = {}) {
  if (callback) {
    callback();
  }
}
```

Reviewed By: yfeldblum

Differential Revision: D4372296

fbshipit-source-id: 7d21e6a44b6f6b046b424f0139465511dbae7b8b
parent e910dd4b
...@@ -780,6 +780,10 @@ class FunctionRef<ReturnType(Args...)> final { ...@@ -780,6 +780,10 @@ class FunctionRef<ReturnType(Args...)> final {
ReturnType operator()(Args... args) const { ReturnType operator()(Args... args) const {
return call_(object_, static_cast<Args&&>(args)...); return call_(object_, static_cast<Args&&>(args)...);
} }
explicit operator bool() const {
return object_;
}
}; };
} // namespace folly } // namespace folly
...@@ -137,11 +137,13 @@ TEST(FunctionRef, OverloadedFunctor) { ...@@ -137,11 +137,13 @@ TEST(FunctionRef, OverloadedFunctor) {
TEST(FunctionRef, DefaultConstructAndAssign) { TEST(FunctionRef, DefaultConstructAndAssign) {
FunctionRef<int(int, int)> fref; FunctionRef<int(int, int)> fref;
EXPECT_FALSE(fref);
EXPECT_THROW(fref(1, 2), std::bad_function_call); EXPECT_THROW(fref(1, 2), std::bad_function_call);
int (*func)(int, int) = [](int x, int y) { return 10 * x + y; }; int (*func)(int, int) = [](int x, int y) { return 10 * x + y; };
fref = func; fref = func;
EXPECT_TRUE(fref);
EXPECT_EQ(42, fref(4, 2)); EXPECT_EQ(42, fref(4, 2));
} }
......
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