Cleanup: remove unused FunctionRef::call_safe

It's better to be more explicit in the calling code about what
happens when there is no function.
This commit is contained in:
Jacques Lucke
2024-09-03 18:06:12 +02:00
parent fc7de7c92f
commit 3393af16da
2 changed files with 0 additions and 48 deletions

View File

@@ -132,29 +132,6 @@ template<typename Ret, typename... Params> class FunctionRef<Ret(Params...)> {
return callback_(callable_, std::forward<Params>(params)...);
}
using OptionalReturnValue = std::conditional_t<std::is_void_v<Ret>, void, std::optional<Ret>>;
/**
* Calls the referenced function if it is available.
* The return value is of type `std::optional<Ret>` if `Ret` is not `void`.
* Otherwise the return type is `void`.
*/
OptionalReturnValue call_safe(Params... params) const
{
if constexpr (std::is_void_v<Ret>) {
if (callback_ == nullptr) {
return;
}
callback_(callable_, std::forward<Params>(params)...);
}
else {
if (callback_ == nullptr) {
return {};
}
return callback_(callable_, std::forward<Params>(params)...);
}
}
/**
* Returns true, when the `FunctionRef` references a function currently.
* If this returns false, the `FunctionRef` must not be called.

View File

@@ -101,31 +101,6 @@ TEST(function_ref, ReferenceAnotherFunctionRef)
EXPECT_EQ(y(), 2);
}
TEST(function_ref, CallSafe)
{
FunctionRef<int()> f;
EXPECT_FALSE(f.call_safe().has_value());
auto func = []() { return 10; };
f = func;
EXPECT_TRUE(f.call_safe().has_value());
EXPECT_EQ(*f.call_safe(), 10);
f = {};
EXPECT_FALSE(f.call_safe().has_value());
BLI_STATIC_ASSERT((std::is_same_v<decltype(f.call_safe()), std::optional<int>>), "");
}
TEST(function_ref, CallSafeVoid)
{
FunctionRef<void()> f;
BLI_STATIC_ASSERT((std::is_same_v<decltype(f.call_safe()), void>), "");
f.call_safe();
int value = 0;
auto func = [&]() { value++; };
f = func;
f.call_safe();
EXPECT_EQ(value, 1);
}
TEST(function_ref, InitializeWithNull)
{
FunctionRef<int(int, int)> f{nullptr};