Skip to content

Commit

Permalink
Use static_cast<> rather than reinterpret_cast<> where possible.
Browse files Browse the repository at this point in the history
These are places where dynamic_cast<> would have been used
if rtti were used.
  • Loading branch information
natersoz authored and nersoz-impinj committed May 22, 2022
1 parent e2d6b20 commit 03d0368
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions ble/gatt_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ attribute const* service::find_attribute(uint16_t handle) const
{
for (attribute const& attr : this->characteristic_list)
{
characteristic const& chr = reinterpret_cast<characteristic const&>(attr);
characteristic const& chr = static_cast<characteristic const&>(attr);
attribute const* attr_found = chr.find_attribute(handle);
if (attr_found) { return attr_found; }
}
Expand All @@ -52,7 +52,7 @@ characteristic const* service::find_characteristic(ble::att::uuid const& chr_uui
{
for (attribute const& attr : this->characteristic_list)
{
characteristic const& chr = reinterpret_cast<characteristic const&>(attr);
characteristic const& chr = static_cast<characteristic const&>(attr);
if (chr.uuid == chr_uuid) { return &chr; }
}

Expand Down
2 changes: 1 addition & 1 deletion ble/gatt_service_container.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ ble::gatt::characteristic const*
ble::gatt::attribute const* attribute = service.find_attribute(handle);
if (attribute)
{
return reinterpret_cast<characteristic const*>(attribute);
return static_cast<characteristic const*>(attribute);
}
}

Expand Down
2 changes: 1 addition & 1 deletion ble/gatt_service_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class service_container: public service_list_type

iterator_node(ble::gatt::service& svc, ble::gatt::attribute& chr)
: service(svc),
characteristic(reinterpret_cast<ble::gatt::characteristic&>(chr))
characteristic(static_cast<ble::gatt::characteristic&>(chr))
{
}

Expand Down
8 changes: 4 additions & 4 deletions ble/gatt_write_ostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,25 @@ void attribute_write(io::output_stream& os, ble::gatt::attribute const& attribut
if (attribute.decl.attribute_type == ble::gatt::attribute_type::characteristic)
{
ble::gatt::characteristic const& characteristic =
reinterpret_cast<ble::gatt::characteristic const&>(attribute);
static_cast<ble::gatt::characteristic const&>(attribute);
characteristic_write(os, characteristic);
}
else if (attribute.decl.attribute_type == ble::gatt::attribute_type::cccd)
{
ble::gatt::cccd const& descriptor =
reinterpret_cast<ble::gatt::cccd const&>(attribute);
static_cast<ble::gatt::cccd const&>(attribute);
descriptor_cccd_write(os, descriptor);
}
else if (attribute.decl.attribute_type == ble::gatt::attribute_type::cpfd)
{
ble::gatt::cpfd const& descriptor =
reinterpret_cast<ble::gatt::cpfd const&>(attribute);
static_cast<ble::gatt::cpfd const&>(attribute);
descriptor_cpfd_write(os, descriptor);
}
else if (attribute.decl.attribute_type == ble::gatt::attribute_type::cud)
{
ble::gatt::cud const& descriptor =
reinterpret_cast<ble::gatt::cud const&>(attribute);
static_cast<ble::gatt::cud const&>(attribute);
descriptor_cud_write(os, descriptor);
}
else
Expand Down
4 changes: 2 additions & 2 deletions ble/gattc_service_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void service_builder::characteristic_discovered(
this->free_list.characteristics.pop_front();

ble::gatt::characteristic& characteristic =
reinterpret_cast<ble::gatt::characteristic&>(list_node);
static_cast<ble::gatt::characteristic&>(list_node);

// Note that the default ctor for ble::gatt::characteristic has
// set the attribute_type properly. Doing it here anyway.
Expand Down Expand Up @@ -376,7 +376,7 @@ void service_builder::descriptor_discovered(
this->free_list.characteristics.pop_front();

ble::gatt::descriptor_base& descriptor =
reinterpret_cast<ble::gatt::descriptor_base&>(list_node);
static_cast<ble::gatt::descriptor_base&>(list_node);
descriptor.decl.handle = gatt_handle_desciptor;

ble::gatt::service_container::discovery_iterator::iterator_node
Expand Down
22 changes: 11 additions & 11 deletions ble/nordic_ble_gatts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,20 @@ static uint32_t gatts_characteristic_add(uint16_t service_handle,
for (ble::gatt::attribute &node : characteristic.descriptor_list)
{
// Note: with --rtti turned off in the compiler we have to use
// reinterpret_cast<> instead of dynamic_cast<>.
// static_cast<> instead of dynamic_cast<>.
switch (node.decl.attribute_type)
{
case ble::gatt::attribute_type::characteristic_user_description:
userd = reinterpret_cast<ble::gatt::characteristic_user_descriptor*>(&node);
userd = static_cast<ble::gatt::characteristic_user_descriptor*>(&node);
break;
case ble::gatt::attribute_type::client_characteristic_configuration:
cccd = reinterpret_cast<ble::gatt::client_characteristic_configuration_descriptor*>(&node);
cccd = static_cast<ble::gatt::client_characteristic_configuration_descriptor*>(&node);
break;
case ble::gatt::attribute_type::server_characteristic_configuration:
sccd = reinterpret_cast<ble::gatt::server_characteristic_configuration_descriptor*>(&node);
sccd = static_cast<ble::gatt::server_characteristic_configuration_descriptor*>(&node);
break;
case ble::gatt::attribute_type::characteristic_presentation_format:
presd = reinterpret_cast<ble::gatt::characteristic_presentation_format_descriptor*>(&node);
presd = static_cast<ble::gatt::characteristic_presentation_format_descriptor*>(&node);
break;
default:
break;
Expand Down Expand Up @@ -272,7 +272,7 @@ static uint32_t gatts_characteristic_add(uint16_t service_handle,
.init_len = characteristic.data_length(),
.init_offs = u16_zero,
.max_len = characteristic.data_length_max(),
.p_value = reinterpret_cast<uint8_t*>(characteristic.data_pointer()),
.p_value = static_cast<uint8_t*>(characteristic.data_pointer()),
};

ble_gatts_char_handles_t gatt_handles;
Expand Down Expand Up @@ -329,7 +329,7 @@ static uint32_t nordic_add_gap_service(ble::gatt::service const& service)
uint32_t error = NRF_SUCCESS;

ble::gatt::characteristic const& node =
reinterpret_cast<ble::gatt::characteristic const&>(attr_node);
static_cast<ble::gatt::characteristic const&>(attr_node);
auto const uuid = static_cast<ble::gatt::characteristic_type>(node.uuid.get_u16());
switch(uuid)
{
Expand All @@ -341,7 +341,7 @@ static uint32_t nordic_add_gap_service(ble::gatt::service const& service)
.lv = 0u,
};

uint8_t const* utf8_ptr = reinterpret_cast<uint8_t const *>(node.data_pointer());
uint8_t const* utf8_ptr = static_cast<uint8_t const *>(node.data_pointer());
uint16_t utf8_len = node.data_length();
error = sd_ble_gap_device_name_set(&security_mode, utf8_ptr, utf8_len);

Expand All @@ -354,7 +354,7 @@ static uint32_t nordic_add_gap_service(ble::gatt::service const& service)

case ble::gatt::characteristic_type::appearance:
{
uint16_t const *appearance_ptr = reinterpret_cast<uint16_t const*>(node.data_pointer());
uint16_t const *appearance_ptr = static_cast<uint16_t const*>(node.data_pointer());
if (node.data_length() != sizeof(uint16_t))
{
logger.error("invalid appearance length: %u", node.data_length());
Expand All @@ -373,7 +373,7 @@ static uint32_t nordic_add_gap_service(ble::gatt::service const& service)
case ble::gatt::characteristic_type::ppcp:
{
ble::gap::connection_parameters const *connection_parameters_ptr =
reinterpret_cast<ble::gap::connection_parameters const*>(node.data_pointer());
static_cast<ble::gap::connection_parameters const*>(node.data_pointer());
if (node.data_length() != sizeof(ble::gap::connection_parameters))
{
logger.error("invalid connection_parameters length: %u", node.data_length());
Expand Down Expand Up @@ -461,7 +461,7 @@ uint32_t gatts_service_add(ble::gatt::service& service)
for (ble::gatt::attribute &attr_node : service.characteristic_list)
{
ble::gatt::characteristic& node =
reinterpret_cast<ble::gatt::characteristic&>(attr_node);
static_cast<ble::gatt::characteristic&>(attr_node);

error = gatts_characteristic_add(service.decl.handle, node);
if (error != NRF_SUCCESS)
Expand Down
6 changes: 4 additions & 2 deletions ble/nordic_ble_stack_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ static void event_notify(ble_evt_t const* ble_event_ptr)
extern uint32_t __start_sdh_ble_observers;
extern uint32_t __stop_sdh_ble_observers;

nrf_sdh_ble_evt_observer_t *observers_begin = reinterpret_cast<nrf_sdh_ble_evt_observer_t *>(&__start_sdh_ble_observers);
nrf_sdh_ble_evt_observer_t *observers_end = reinterpret_cast<nrf_sdh_ble_evt_observer_t *>(&__stop_sdh_ble_observers);
nrf_sdh_ble_evt_observer_t *observers_begin =
reinterpret_cast<nrf_sdh_ble_evt_observer_t *>(&__start_sdh_ble_observers);
nrf_sdh_ble_evt_observer_t *observers_end =
reinterpret_cast<nrf_sdh_ble_evt_observer_t *>(&__stop_sdh_ble_observers);

for (nrf_sdh_ble_evt_observer_t *observer = observers_begin;
observer <= observers_end; ++observer)
Expand Down

0 comments on commit 03d0368

Please sign in to comment.