refactor map and vector iteration

This commit is contained in:
Katharine Chui 2025-05-01 16:27:34 +02:00
parent 3568325d39
commit cf52f90db9
2 changed files with 38 additions and 35 deletions

View file

@ -105,11 +105,12 @@ bool usb_device_logitech_g27::open_device()
static void clear_sdl_joysticks(std::map<uint32_t, std::vector<SDL_Joystick*>>& joysticks) static void clear_sdl_joysticks(std::map<uint32_t, std::vector<SDL_Joystick*>>& joysticks)
{ {
for (auto joystick_type = joysticks.begin(); joystick_type != joysticks.end(); joystick_type++) for (auto joystick_type : joysticks)
{ {
for (auto joystick = joystick_type->second.begin(); joystick != joystick_type->second.end(); joystick++) for (auto joystick : joystick_type.second)
{ {
SDL_CloseJoystick(*joystick); if (joystick)
SDL_CloseJoystick(joystick);
} }
} }
joysticks.clear(); joysticks.clear();
@ -164,23 +165,23 @@ static bool sdl_joysticks_equal(std::map<uint32_t, std::vector<SDL_Joystick*>>&
{ {
return false; return false;
} }
for (auto left_joysticks_of_type = left.begin(); left_joysticks_of_type != left.end(); left_joysticks_of_type++) for (auto left_joysticks_of_type : left)
{ {
auto right_joysticks_of_type = right.find(left_joysticks_of_type->first); auto right_joysticks_of_type = right.find(left_joysticks_of_type.first);
if (right_joysticks_of_type == right.end()) if (right_joysticks_of_type == right.end())
{ {
return false; return false;
} }
if (left_joysticks_of_type->second.size() != right_joysticks_of_type->second.size()) if (left_joysticks_of_type.second.size() != right_joysticks_of_type->second.size())
{ {
return false; return false;
} }
for (auto left_joystick = left_joysticks_of_type->second.begin(); left_joystick != left_joysticks_of_type->second.end(); left_joystick++) for (auto left_joystick : left_joysticks_of_type.second)
{ {
bool found = false; bool found = false;
for (auto right_joystick = right_joysticks_of_type->second.begin(); right_joystick != right_joysticks_of_type->second.end(); right_joystick++) for (auto right_joystick : right_joysticks_of_type->second)
{ {
if (*left_joystick == *right_joystick) if (left_joystick == right_joystick)
{ {
found = true; found = true;
break; break;
@ -632,9 +633,9 @@ static int16_t fetch_sdl_axis_avg(std::map<uint32_t, std::vector<SDL_Joystick*>>
// TODO account for deadzone and only pick up active devices // TODO account for deadzone and only pick up active devices
int32_t sdl_joysticks_total_value = 0; int32_t sdl_joysticks_total_value = 0;
for (auto joystick = joysticks_of_type->second.begin(); joystick != joysticks_of_type->second.end(); joystick++) for (auto joystick : joysticks_of_type->second)
{ {
sdl_joysticks_total_value += fetch_sdl_as_axis(*joystick, mapping); sdl_joysticks_total_value += fetch_sdl_as_axis(joystick, mapping);
} }
return sdl_joysticks_total_value / joysticks_of_type->second.size(); return sdl_joysticks_total_value / joysticks_of_type->second.size();
@ -654,9 +655,9 @@ static bool sdl_to_logitech_g27_button(std::map<uint32_t, std::vector<SDL_Joysti
} }
bool pressed = false; bool pressed = false;
for (auto joystick = joysticks_of_type->second.begin(); joystick != joysticks_of_type->second.end(); joystick++) for (auto joystick : joysticks_of_type->second)
{ {
pressed = pressed || fetch_sdl_as_button(*joystick, mapping); pressed = pressed || fetch_sdl_as_button(joystick, mapping);
} }
return pressed; return pressed;
} }

View file

@ -178,9 +178,9 @@ public:
sprintf(text_buf, "Input %s for %s, timeout in %d %s", this->m_is_axis ? "axis" : "button/hat", this->m_name.c_str(), timeout_sec, timeout_sec >= 2 ? "seconds" : "second"); sprintf(text_buf, "Input %s for %s, timeout in %d %s", this->m_is_axis ? "axis" : "button/hat", this->m_name.c_str(), timeout_sec, timeout_sec >= 2 ? "seconds" : "second");
this->m_setting_dialog->set_state_text(text_buf); this->m_setting_dialog->set_state_text(text_buf);
for (auto new_joystick_state = new_joystick_states.begin(); new_joystick_state != new_joystick_states.end(); new_joystick_state++) for (auto new_joystick_state : new_joystick_states)
{ {
auto last_joystick_state = this->m_last_joystick_states.find(new_joystick_state->first); auto last_joystick_state = this->m_last_joystick_states.find(new_joystick_state.first);
if (last_joystick_state == this->m_last_joystick_states.end()) if (last_joystick_state == this->m_last_joystick_states.end())
{ {
continue; continue;
@ -189,20 +189,20 @@ public:
if (this->m_is_axis) if (this->m_is_axis)
{ {
const static int16_t axis_change_threshold = 0x7FFF / 5; const static int16_t axis_change_threshold = 0x7FFF / 5;
if (last_joystick_state->second.axes.size() != new_joystick_state->second.axes.size()) if (last_joystick_state->second.axes.size() != new_joystick_state.second.axes.size())
{ {
logitech_g27_cfg_log.error("during input state change diff, number of axes on %04x:%04x changed", new_joystick_state->first >> 16, new_joystick_state->first & 0xFFFF); logitech_g27_cfg_log.error("during input state change diff, number of axes on %04x:%04x changed", new_joystick_state.first >> 16, new_joystick_state.first & 0xFFFF);
continue; continue;
} }
for (std::vector<int16_t>::size_type i = 0; i < new_joystick_state->second.axes.size(); i++) for (std::vector<int16_t>::size_type i = 0; i < new_joystick_state.second.axes.size(); i++)
{ {
int32_t diff = std::abs(last_joystick_state->second.axes[i] - new_joystick_state->second.axes[i]); int32_t diff = std::abs(last_joystick_state->second.axes[i] - new_joystick_state.second.axes[i]);
if (diff > axis_change_threshold) if (diff > axis_change_threshold)
{ {
this->m_mapping_in_progress = false; this->m_mapping_in_progress = false;
this->m_setting_dialog->set_state_text(DEFAULT_STATUS); this->m_setting_dialog->set_state_text(DEFAULT_STATUS);
this->m_setting_dialog->enable(); this->m_setting_dialog->enable();
this->m_mapping.device_type_id = new_joystick_state->first; this->m_mapping.device_type_id = new_joystick_state.first;
this->m_mapping.type = MAPPING_AXIS; this->m_mapping.type = MAPPING_AXIS;
this->m_mapping.id = i; this->m_mapping.id = i;
this->m_mapping.hat = HAT_NONE; this->m_mapping.hat = HAT_NONE;
@ -212,41 +212,41 @@ public:
} }
else else
{ {
if (last_joystick_state->second.buttons.size() != new_joystick_state->second.buttons.size()) if (last_joystick_state->second.buttons.size() != new_joystick_state.second.buttons.size())
{ {
logitech_g27_cfg_log.error("during input state change diff, number of buttons on %04x:%04x changed", new_joystick_state->first >> 16, new_joystick_state->first & 0xFFFF); logitech_g27_cfg_log.error("during input state change diff, number of buttons on %04x:%04x changed", new_joystick_state.first >> 16, new_joystick_state.first & 0xFFFF);
continue; continue;
} }
if (last_joystick_state->second.hats.size() != new_joystick_state->second.hats.size()) if (last_joystick_state->second.hats.size() != new_joystick_state.second.hats.size())
{ {
logitech_g27_cfg_log.error("during input state change diff, number of hats on %04x:%04x changed", new_joystick_state->first >> 16, new_joystick_state->first & 0xFFFF); logitech_g27_cfg_log.error("during input state change diff, number of hats on %04x:%04x changed", new_joystick_state.first >> 16, new_joystick_state.first & 0xFFFF);
continue; continue;
} }
for (std::vector<int16_t>::size_type i = 0; i < new_joystick_state->second.buttons.size(); i++) for (std::vector<int16_t>::size_type i = 0; i < new_joystick_state.second.buttons.size(); i++)
{ {
if (last_joystick_state->second.buttons[i] != new_joystick_state->second.buttons[i]) if (last_joystick_state->second.buttons[i] != new_joystick_state.second.buttons[i])
{ {
this->m_mapping_in_progress = false; this->m_mapping_in_progress = false;
this->m_setting_dialog->set_state_text(DEFAULT_STATUS); this->m_setting_dialog->set_state_text(DEFAULT_STATUS);
this->m_setting_dialog->enable(); this->m_setting_dialog->enable();
this->m_mapping.device_type_id = new_joystick_state->first; this->m_mapping.device_type_id = new_joystick_state.first;
this->m_mapping.type = MAPPING_BUTTON; this->m_mapping.type = MAPPING_BUTTON;
this->m_mapping.id = i; this->m_mapping.id = i;
this->m_mapping.hat = HAT_NONE; this->m_mapping.hat = HAT_NONE;
break; break;
} }
} }
for (std::vector<int16_t>::size_type i = 0; i < new_joystick_state->second.hats.size(); i++) for (std::vector<int16_t>::size_type i = 0; i < new_joystick_state.second.hats.size(); i++)
{ {
if (last_joystick_state->second.hats[i] != new_joystick_state->second.hats[i] && new_joystick_state->second.hats[i] != HAT_NONE) if (last_joystick_state->second.hats[i] != new_joystick_state.second.hats[i] && new_joystick_state.second.hats[i] != HAT_NONE)
{ {
this->m_mapping_in_progress = false; this->m_mapping_in_progress = false;
this->m_setting_dialog->set_state_text(DEFAULT_STATUS); this->m_setting_dialog->set_state_text(DEFAULT_STATUS);
this->m_setting_dialog->enable(); this->m_setting_dialog->enable();
this->m_mapping.device_type_id = new_joystick_state->first; this->m_mapping.device_type_id = new_joystick_state.first;
this->m_mapping.type = MAPPING_HAT; this->m_mapping.type = MAPPING_HAT;
this->m_mapping.id = i; this->m_mapping.id = i;
this->m_mapping.hat = new_joystick_state->second.hats[i]; this->m_mapping.hat = new_joystick_state.second.hats[i];
break; break;
} }
} }
@ -696,9 +696,10 @@ emulated_logitech_g27_settings_dialog::emulated_logitech_g27_settings_dialog(QWi
emulated_logitech_g27_settings_dialog::~emulated_logitech_g27_settings_dialog() emulated_logitech_g27_settings_dialog::~emulated_logitech_g27_settings_dialog()
{ {
for (auto joystick_handle = m_joystick_handles.begin(); joystick_handle != m_joystick_handles.end(); joystick_handle++) for (auto joystick_handle : m_joystick_handles)
{ {
SDL_CloseJoystick(*joystick_handle); if (joystick_handle)
SDL_CloseJoystick(joystick_handle);
} }
} }
@ -808,9 +809,10 @@ const std::map<uint32_t, joystick_state>& emulated_logitech_g27_settings_dialog:
} }
} }
for (auto joystick_handle = m_joystick_handles.begin(); joystick_handle != m_joystick_handles.end(); joystick_handle++) for (auto joystick_handle : m_joystick_handles)
{ {
SDL_CloseJoystick(*joystick_handle); if (joystick_handle)
SDL_CloseJoystick(joystick_handle);
} }
m_joystick_handles = new_joystick_handles; m_joystick_handles = new_joystick_handles;