Skip to content

Commit

Permalink
Simplify BluetoothAttributeInstanceMap::GetOrCreate*
Browse files Browse the repository at this point in the history
GetOrCreateRemoteGATTService, GetOrCreateRemoteGATTCharacteristic and
GetOrCreateBluetoothRemoteGATTDescriptor use a similar pattern where
they lookup an object by id, and if no object exists it is created.
This pattern uses at() followed by an (optional) insert() - this can be
written as a single insert() which reduces boilerplate.

Change-Id: Ic46c0fe0c3adea404f8844bda147bd89ebf4df75
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2289854
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Commit-Queue: Fredrik Söderquist <fs@opera.com>
Cr-Commit-Position: refs/heads/master@{#788985}
  • Loading branch information
Fredrik Söderqvist authored and Commit Bot committed Jul 16, 2020
1 parent ab7cc5c commit ef20f11
Showing 1 changed file with 16 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ BluetoothAttributeInstanceMap::GetOrCreateRemoteGATTService(
mojom::blink::WebBluetoothRemoteGATTServicePtr remote_gatt_service,
bool is_primary,
const String& device_instance_id) {
String service_instance_id = remote_gatt_service->instance_id;
BluetoothRemoteGATTService* service =
service_id_to_object_.at(service_instance_id);

auto& service =
service_id_to_object_.insert(remote_gatt_service->instance_id, nullptr)
.stored_value->value;
if (!service) {
service = MakeGarbageCollected<BluetoothRemoteGATTService>(
std::move(remote_gatt_service), is_primary, device_instance_id,
device_);
service_id_to_object_.insert(service_instance_id, service);
}

return service;
}

Expand All @@ -45,16 +42,14 @@ BluetoothAttributeInstanceMap::GetOrCreateRemoteGATTCharacteristic(
mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr
remote_gatt_characteristic,
BluetoothRemoteGATTService* service) {
String instance_id = remote_gatt_characteristic->instance_id;
BluetoothRemoteGATTCharacteristic* characteristic =
characteristic_id_to_object_.at(instance_id);

auto& characteristic =
characteristic_id_to_object_
.insert(remote_gatt_characteristic->instance_id, nullptr)
.stored_value->value;
if (!characteristic) {
characteristic = MakeGarbageCollected<BluetoothRemoteGATTCharacteristic>(
context, std::move(remote_gatt_characteristic), service, device_);
characteristic_id_to_object_.insert(instance_id, characteristic);
}

return characteristic;
}

Expand All @@ -65,19 +60,16 @@ bool BluetoothAttributeInstanceMap::ContainsCharacteristic(

BluetoothRemoteGATTDescriptor*
BluetoothAttributeInstanceMap::GetOrCreateBluetoothRemoteGATTDescriptor(
mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor,
mojom::blink::WebBluetoothRemoteGATTDescriptorPtr remote_gatt_descriptor,
BluetoothRemoteGATTCharacteristic* characteristic) {
String instance_id = descriptor->instance_id;
BluetoothRemoteGATTDescriptor* result =
descriptor_id_to_object_.at(instance_id);

if (result)
return result;

result = MakeGarbageCollected<BluetoothRemoteGATTDescriptor>(
std::move(descriptor), characteristic);
descriptor_id_to_object_.insert(instance_id, result);
return result;
auto& descriptor = descriptor_id_to_object_
.insert(remote_gatt_descriptor->instance_id, nullptr)
.stored_value->value;
if (!descriptor) {
descriptor = MakeGarbageCollected<BluetoothRemoteGATTDescriptor>(
std::move(remote_gatt_descriptor), characteristic);
}
return descriptor;
}

bool BluetoothAttributeInstanceMap::ContainsDescriptor(
Expand Down

0 comments on commit ef20f11

Please sign in to comment.