Refactor std::function usage to absl::AnyInvocable for ZoneTransitionCallback

PiperOrigin-RevId: 550959839
This commit is contained in:
Joy Babafemi
2023-07-25 12:16:20 -07:00
committed by Copybara-Service
parent 99c5e19f3f
commit 5b605b2426
6 changed files with 46 additions and 27 deletions
+1
View File
@@ -40,6 +40,7 @@ cc_test(
srcs = ["fpp_manager_test.cc"],
deps = [
":fpp_manager",
"//presence/implementation:sensor_fusion",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/status",
"@com_google_googletest//:gtest_main",
+7 -4
View File
@@ -17,10 +17,12 @@
#include <cstdint>
#include <optional>
#include <string>
#include <utility>
#include "absl/status/status.h"
#include "internal/platform/logging.h"
#include "presence/fpp/fpp_c_ffi/include/presence_detector.h"
#include "presence/implementation/sensor_fusion.h"
#include "presence/presence_zone.h"
namespace nearby {
@@ -110,7 +112,7 @@ absl::Status FppManager::UpdateBleScanResult(uint64_t device_id,
void FppManager::RegisterZoneTransitionListener(
uint64_t callback_id, ZoneTransitionCallback callback) {
zone_transition_callbacks_[callback_id] = callback;
zone_transition_callbacks_[callback_id] = std::move(callback);
}
void FppManager::UnregisterZoneTransitionListener(uint64_t callback_id) {
@@ -147,9 +149,10 @@ void FppManager::CheckPresenceZoneChanged(uint64_t device_id,
NEARBY_LOG(WARNING,
"Updating zone transition callbacks with new zone. Zone=%p",
new_estimate.proximity_state);
for (const auto& pair : zone_transition_callbacks_) {
pair.second(device_id, ConvertProximityStateToRangeType(
new_estimate.proximity_state));
for (auto& pair : zone_transition_callbacks_) {
pair.second.on_proximity_zone_changed(
device_id,
ConvertProximityStateToRangeType(new_estimate.proximity_state));
}
}
}
-1
View File
@@ -31,7 +31,6 @@ namespace presence {
// between fpp and NP sensor fusion
class FppManager {
public:
using ZoneTransitionCallback = SensorFusion::ZoneTransitionCallback;
using RangeType = PresenceZone::DistanceBoundary::RangeType;
FppManager() { presence_detector_handle_ = presence_detector_create(); }
+25 -16
View File
@@ -19,6 +19,7 @@
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "presence/implementation/sensor_fusion.h"
namespace nearby {
namespace presence {
@@ -33,10 +34,12 @@ TEST(FppManager, UpdateBleScanResultSuccess) {
bool callback_called = false;
manager.RegisterZoneTransitionListener(
kCallbackId,
[&callback_called](uint64_t device_id,
PresenceZone::DistanceBoundary::RangeType range_type) {
callback_called = true;
});
{.on_proximity_zone_changed =
[&callback_called](
uint64_t device_id,
PresenceZone::DistanceBoundary::RangeType range_type) {
callback_called = true;
}});
EXPECT_OK(manager.UpdateBleScanResult(kDeviceId, /*txPower=*/absl::nullopt,
kReachRssi,
/*elapsed_real_time_millis=*/0));
@@ -56,10 +59,12 @@ TEST(FppManager, ZoneTransitionDetected) {
bool callback_called = false;
manager.RegisterZoneTransitionListener(
kCallbackId,
[&callback_called](uint64_t device_id,
PresenceZone::DistanceBoundary::RangeType range_type) {
callback_called = true;
});
{.on_proximity_zone_changed =
[&callback_called](
uint64_t device_id,
PresenceZone::DistanceBoundary::RangeType range_type) {
callback_called = true;
}});
// ProximityEstimate is only computed after consecutive scans is fulfilled
EXPECT_OK(manager.UpdateBleScanResult(kDeviceId, /*txPower=*/absl::nullopt,
kReachRssi,
@@ -139,10 +144,12 @@ TEST(FppManager, UpdateBleScanResultWithTxPowerSuccess) {
bool callback_called = false;
manager.RegisterZoneTransitionListener(
kCallbackId,
[&callback_called](uint64_t device_id,
PresenceZone::DistanceBoundary::RangeType range_type) {
callback_called = true;
});
{.on_proximity_zone_changed =
[&callback_called](
uint64_t device_id,
PresenceZone::DistanceBoundary::RangeType range_type) {
callback_called = true;
}});
EXPECT_OK(manager.UpdateBleScanResult(kDeviceId, /*txPower=*/20, kReachRssi,
/*elapsed_real_time_millis=*/0));
EXPECT_OK(manager.UpdateBleScanResult(kDeviceId, /*txPower=*/20, kReachRssi,
@@ -159,10 +166,12 @@ TEST(FppManager, UnregisterZoneTransitionListener) {
bool callback_called = false;
manager.RegisterZoneTransitionListener(
kCallbackId,
[&callback_called](uint64_t device_id,
PresenceZone::DistanceBoundary::RangeType range_type) {
callback_called = true;
});
{.on_proximity_zone_changed =
[&callback_called](
uint64_t device_id,
PresenceZone::DistanceBoundary::RangeType range_type) {
callback_called = true;
}});
EXPECT_OK(manager.UpdateBleScanResult(kDeviceId, /*txPower=*/absl::nullopt,
kReachRssi,
/*elapsed_real_time_millis=*/0));
+1
View File
@@ -126,6 +126,7 @@ cc_library(
],
deps = [
"//presence:types",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/types:optional",
],
)
+12 -6
View File
@@ -19,6 +19,7 @@
#include <functional>
#include <vector>
#include "absl/functional/any_invocable.h"
#include "absl/types/optional.h"
#include "presence/device_motion.h"
#include "presence/presence_zone.h"
@@ -58,16 +59,21 @@ struct RangingData {
std::vector<DeviceMotion> device_motions;
};
struct ZoneTransitionCallback {
absl::AnyInvocable<void(
uint64_t device_id,
PresenceZone::DistanceBoundary::RangeType proximity_zone)>
on_proximity_zone_changed =
[](uint64_t device_id,
PresenceZone::DistanceBoundary::RangeType proximity_zone) {};
absl::AnyInvocable<void(uint64_t callback_id)> on_callback_id_generated =
[](uint64_t callback_id) {};
};
class SensorFusion {
public:
virtual ~SensorFusion() = default;
// Called when the proximity zone to a nearby peer device has changed.
typedef std::function<void(
uint64_t device_id,
PresenceZone::DistanceBoundary::RangeType proximity_zone)>
ZoneTransitionCallback;
// Called when a device motion gesture is detected.
typedef std::function<void(DeviceMotion::MotionType detected_device_motion)>
DeviceMotionCallback;