From 8679d3a370087b0da2eea508c3dd4892d14536cc Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Mon, 21 Nov 2022 14:54:28 -0800 Subject: [PATCH] Move CancellationFlag to absl::AnyInvocable PiperOrigin-RevId: 490078293 --- Package.resolved | 2 +- internal/platform/BUILD | 2 +- internal/platform/cancellation_flag.cc | 2 +- internal/platform/cancellation_flag.h | 3 ++- .../platform/cancellation_flag_listener.h | 4 ++-- internal/platform/cancellation_flag_test.cc | 19 ++++++++++++------- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Package.resolved b/Package.resolved index e7932fa6..0afb094c 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,7 +6,7 @@ "repositoryURL": "https://github.com/firebase/abseil-cpp-SwiftPM.git", "state": { "branch": "main", - "revision": "d302de612e3d57c6f4afaf087da18fba8eac72a7", + "revision": "da43cbe34db0763a8eaf986ebda8510e7820d7f7", "version": null } }, diff --git a/internal/platform/BUILD b/internal/platform/BUILD index ce83a697..43986d0f 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -136,8 +136,8 @@ cc_library( ], deps = [ ":base", - ":util", "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/synchronization", ], ) diff --git a/internal/platform/cancellation_flag.cc b/internal/platform/cancellation_flag.cc index 2536ff63..5e176efd 100644 --- a/internal/platform/cancellation_flag.cc +++ b/internal/platform/cancellation_flag.cc @@ -48,7 +48,7 @@ void CancellationFlag::Cancel() { listeners = listeners_; } - for (const auto *listener : listeners) { + for (auto *listener : listeners) { (*listener)(); } } diff --git a/internal/platform/cancellation_flag.h b/internal/platform/cancellation_flag.h index f7cc5a34..903572b5 100644 --- a/internal/platform/cancellation_flag.h +++ b/internal/platform/cancellation_flag.h @@ -18,6 +18,7 @@ #include #include "absl/container/flat_hash_set.h" +#include "absl/functional/any_invocable.h" #include "absl/synchronization/mutex.h" namespace location { @@ -28,7 +29,7 @@ namespace nearby { class CancellationFlag { public: // The listener for cancellation. - using CancelListener = std::function; + using CancelListener = absl::AnyInvocable; CancellationFlag(); explicit CancellationFlag(bool cancelled); diff --git a/internal/platform/cancellation_flag_listener.h b/internal/platform/cancellation_flag_listener.h index 06edee58..172d909e 100644 --- a/internal/platform/cancellation_flag_listener.h +++ b/internal/platform/cancellation_flag_listener.h @@ -25,7 +25,7 @@ namespace nearby { class CancellationFlagListener { public: CancellationFlagListener(CancellationFlag* flag, - std::function listener) + CancellationFlag::CancelListener listener) : flag_(flag), listener_(std::move(listener)) { flag_->RegisterOnCancelListener(&listener_); } @@ -34,7 +34,7 @@ class CancellationFlagListener { private: CancellationFlag* flag_; - std::function listener_; + CancellationFlag::CancelListener listener_; }; } // namespace nearby diff --git a/internal/platform/cancellation_flag_test.cc b/internal/platform/cancellation_flag_test.cc index 2ab36e82..807e5ef3 100644 --- a/internal/platform/cancellation_flag_test.cc +++ b/internal/platform/cancellation_flag_test.cc @@ -14,7 +14,9 @@ #include "internal/platform/cancellation_flag.h" +#include #include +#include #include "gmock/gmock.h" #include "protobuf-matchers/protocol-buffer-matchers.h" @@ -96,7 +98,8 @@ TEST_P(CancellationFlagTest, CanCancel) { EXPECT_CALL(mock_cancel_callback, Call) .Times(feature_flags_.enable_cancellation_flag ? 1 : 0); CancellationFlag flag; - CancellationFlagListener cancellation_flag_listener(&flag, cancel_callback); + CancellationFlagListener cancellation_flag_listener( + &flag, std::move(cancel_callback)); flag.Cancel(); // If FeatureFlag is disabled, return as no-op immediately and @@ -117,7 +120,8 @@ TEST_P(CancellationFlagTest, ShouldOnlyCancelOnce) { .Times(feature_flags_.enable_cancellation_flag ? 1 : 0); CancellationFlag flag; - CancellationFlagListener cancellation_flag_listener(&flag, cancel_callback); + CancellationFlagListener cancellation_flag_listener( + &flag, std::move(cancel_callback)); flag.Cancel(); flag.Cancel(); flag.Cancel(); @@ -139,8 +143,8 @@ TEST_P(CancellationFlagTest, CannotCancelAfterUnregister) { EXPECT_CALL(mock_cancel_callback, Call).Times(0); CancellationFlag flag; - auto cancellation_flag_listener = - std::make_unique(&flag, cancel_callback); + auto cancellation_flag_listener = std::make_unique( + &flag, std::move(cancel_callback)); // Release immediately. cancellation_flag_listener.reset(); flag.Cancel(); @@ -162,11 +166,12 @@ TEST(CancellationFlagTest, StrictMock> mock_cancel_callback; CancellationFlag::CancelListener cancel_callback = mock_cancel_callback.AsStdFunction(); + CancellationFlag::CancelListener cancel_callback_copy = + mock_cancel_callback.AsStdFunction(); CancellationFlag::CancelListener* callback_pointer_1 = &cancel_callback; - auto callback_pointer_2 = - std::make_unique(); - *callback_pointer_2 = cancel_callback; + auto callback_pointer_2 = std::make_unique( + std::move(cancel_callback_copy)); EXPECT_NE(callback_pointer_1, callback_pointer_2.get()); EXPECT_CALL(mock_cancel_callback, Call).Times(2);