Refactor bluetooth code to use shared pointers instead of references.

This commit is contained in:
Vibhav Pant
2023-09-05 19:45:10 +05:30
parent df5800fc70
commit 4bf11c8db0
11 changed files with 85 additions and 94 deletions
@@ -24,21 +24,19 @@
namespace nearby {
namespace linux {
std::optional<std::reference_wrapper<BluetoothDevice>>
BluetoothDevices::get_device_by_path(
std::shared_ptr<BluetoothDevice> BluetoothDevices::get_device_by_path(
const sdbus::ObjectPath &device_object_path) {
absl::ReaderMutexLock l(&devices_by_path_lock_);
if (devices_by_path_.count(device_object_path) == 0) {
return std::nullopt;
return nullptr;
}
auto &device = devices_by_path_.at(device_object_path);
return *device;
return devices_by_path_[device_object_path];
}
std::optional<std::reference_wrapper<BluetoothDevice>>
BluetoothDevices::get_device_by_address(const std::string &addr) {
std::shared_ptr<BluetoothDevice> BluetoothDevices::get_device_by_address(
const std::string &addr) {
auto device_object_path =
bluez::device_object_path(adapter_object_path_, addr);
return get_device_by_path(device_object_path);
@@ -51,14 +49,14 @@ void BluetoothDevices::remove_device_by_path(
devices_by_path_.erase(device_object_path);
}
BluetoothDevice &BluetoothDevices::add_new_device(
std::shared_ptr<BluetoothDevice> BluetoothDevices::add_new_device(
sdbus::ObjectPath device_object_path) {
absl::MutexLock l(&devices_by_path_lock_);
auto pair = devices_by_path_.emplace(
std::string(device_object_path),
std::make_unique<MonitoredBluetoothDevice>(
system_bus_, std::move(device_object_path), observers_));
return *pair.first->second;
return pair.first->second;
}
} // namespace linux
} // namespace nearby