summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2023-04-24 08:04:04 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2023-04-24 08:04:41 -0700
commit8bd7178b14ea61972f8b04e9a9352bc4a4666cfb (patch)
tree3355edf637fd61458b2b4467e322b89a18769fa1
parent43e07a0ee0ee1111186a69bf32f65a0a2a24e387 (diff)
Adds template specialization for nullptr assertion of absl::AnyInvocable types.
PiperOrigin-RevId: 526644558 Change-Id: I050162c2c0d667c32f7e11f51201054cca52667d
-rw-r--r--absl/functional/BUILD.bazel2
-rw-r--r--absl/functional/CMakeLists.txt1
-rw-r--r--absl/functional/function_ref_test.cc20
-rw-r--r--absl/functional/internal/function_ref.h7
4 files changed, 30 insertions, 0 deletions
diff --git a/absl/functional/BUILD.bazel b/absl/functional/BUILD.bazel
index c4fbce98..4ceac539 100644
--- a/absl/functional/BUILD.bazel
+++ b/absl/functional/BUILD.bazel
@@ -92,6 +92,7 @@ cc_library(
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
+ ":any_invocable",
"//absl/base:base_internal",
"//absl/base:core_headers",
"//absl/meta:type_traits",
@@ -104,6 +105,7 @@ cc_test(
srcs = ["function_ref_test.cc"],
copts = ABSL_TEST_COPTS,
deps = [
+ ":any_invocable",
":function_ref",
"//absl/container:test_instance_tracker",
"//absl/memory",
diff --git a/absl/functional/CMakeLists.txt b/absl/functional/CMakeLists.txt
index c0f6eaaa..628b2ffc 100644
--- a/absl/functional/CMakeLists.txt
+++ b/absl/functional/CMakeLists.txt
@@ -90,6 +90,7 @@ absl_cc_library(
DEPS
absl::base_internal
absl::core_headers
+ absl::any_invocable
absl::meta
PUBLIC
)
diff --git a/absl/functional/function_ref_test.cc b/absl/functional/function_ref_test.cc
index d0923fdd..f91e15e0 100644
--- a/absl/functional/function_ref_test.cc
+++ b/absl/functional/function_ref_test.cc
@@ -20,6 +20,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/internal/test_instance_tracker.h"
+#include "absl/functional/any_invocable.h"
#include "absl/memory/memory.h"
namespace absl {
@@ -157,6 +158,25 @@ TEST(FunctionRef, NullMemberPtrAssertFails) {
EXPECT_DEBUG_DEATH({ FunctionRef<int(const S& s)> ref(mem_ptr); }, "");
}
+TEST(FunctionRef, NullStdFunctionAssertPasses) {
+ std::function<void()> function = []() {};
+ FunctionRef<void()> ref(function);
+}
+
+TEST(FunctionRef, NullStdFunctionAssertFails) {
+ std::function<void()> function = nullptr;
+ EXPECT_DEBUG_DEATH({ FunctionRef<void()> ref(function); }, "");
+}
+
+TEST(FunctionRef, NullAnyInvocableAssertPasses) {
+ AnyInvocable<void() const> invocable = []() {};
+ FunctionRef<void()> ref(invocable);
+}
+TEST(FunctionRef, NullAnyInvocableAssertFails) {
+ AnyInvocable<void() const> invocable = nullptr;
+ EXPECT_DEBUG_DEATH({ FunctionRef<void()> ref(invocable); }, "");
+}
+
#endif // GTEST_HAS_DEATH_TEST
TEST(FunctionRef, CopiesAndMovesPerPassByValue) {
diff --git a/absl/functional/internal/function_ref.h b/absl/functional/internal/function_ref.h
index a1ddbb0c..1cd34a3c 100644
--- a/absl/functional/internal/function_ref.h
+++ b/absl/functional/internal/function_ref.h
@@ -20,6 +20,7 @@
#include <type_traits>
#include "absl/base/internal/invoke.h"
+#include "absl/functional/any_invocable.h"
#include "absl/meta/type_traits.h"
namespace absl {
@@ -90,6 +91,12 @@ void AssertNonNull(const std::function<Sig>& f) {
(void)f;
}
+template <typename Sig>
+void AssertNonNull(const AnyInvocable<Sig>& f) {
+ assert(f != nullptr);
+ (void)f;
+}
+
template <typename F>
void AssertNonNull(const F&) {}