mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Move CancellationFlag to absl::AnyInvocable
PiperOrigin-RevId: 490078293
This commit is contained in:
committed by
Copybara-Service
parent
b1d1019541
commit
8679d3a370
+1
-1
@@ -6,7 +6,7 @@
|
||||
"repositoryURL": "https://github.com/firebase/abseil-cpp-SwiftPM.git",
|
||||
"state": {
|
||||
"branch": "main",
|
||||
"revision": "d302de612e3d57c6f4afaf087da18fba8eac72a7",
|
||||
"revision": "da43cbe34db0763a8eaf986ebda8510e7820d7f7",
|
||||
"version": null
|
||||
}
|
||||
},
|
||||
|
||||
@@ -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",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -48,7 +48,7 @@ void CancellationFlag::Cancel() {
|
||||
listeners = listeners_;
|
||||
}
|
||||
|
||||
for (const auto *listener : listeners) {
|
||||
for (auto *listener : listeners) {
|
||||
(*listener)();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <memory>
|
||||
|
||||
#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<void()>;
|
||||
using CancelListener = absl::AnyInvocable<void()>;
|
||||
|
||||
CancellationFlag();
|
||||
explicit CancellationFlag(bool cancelled);
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace nearby {
|
||||
class CancellationFlagListener {
|
||||
public:
|
||||
CancellationFlagListener(CancellationFlag* flag,
|
||||
std::function<void()> listener)
|
||||
CancellationFlag::CancelListener listener)
|
||||
: flag_(flag), listener_(std::move(listener)) {
|
||||
flag_->RegisterOnCancelListener(&listener_);
|
||||
}
|
||||
@@ -34,7 +34,7 @@ class CancellationFlagListener {
|
||||
|
||||
private:
|
||||
CancellationFlag* flag_;
|
||||
std::function<void()> listener_;
|
||||
CancellationFlag::CancelListener listener_;
|
||||
};
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -14,7 +14,9 @@
|
||||
|
||||
#include "internal/platform/cancellation_flag.h"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#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<CancellationFlagListener>(&flag, cancel_callback);
|
||||
auto cancellation_flag_listener = std::make_unique<CancellationFlagListener>(
|
||||
&flag, std::move(cancel_callback));
|
||||
// Release immediately.
|
||||
cancellation_flag_listener.reset();
|
||||
flag.Cancel();
|
||||
@@ -162,11 +166,12 @@ TEST(CancellationFlagTest,
|
||||
StrictMock<MockFunction<void()>> 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<CancellationFlag::CancelListener>();
|
||||
*callback_pointer_2 = cancel_callback;
|
||||
auto callback_pointer_2 = std::make_unique<CancellationFlag::CancelListener>(
|
||||
std::move(cancel_callback_copy));
|
||||
|
||||
EXPECT_NE(callback_pointer_1, callback_pointer_2.get());
|
||||
EXPECT_CALL(mock_cancel_callback, Call).Times(2);
|
||||
|
||||
Reference in New Issue
Block a user