1
0
Fork 0
forked from suyu/suyu

input_engine: Simplify PreSet* family of functions

We can make use of try_emplace() to insert values only if they don't
already exist.
This commit is contained in:
Lioncash 2021-12-13 10:18:02 -05:00
parent 4d4a234476
commit a9d39b6895
2 changed files with 14 additions and 24 deletions

View file

@ -10,41 +10,31 @@ namespace InputCommon {
void InputEngine::PreSetController(const PadIdentifier& identifier) { void InputEngine::PreSetController(const PadIdentifier& identifier) {
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
if (!controller_list.contains(identifier)) { controller_list.try_emplace(identifier);
controller_list.insert_or_assign(identifier, ControllerData{});
}
} }
void InputEngine::PreSetButton(const PadIdentifier& identifier, int button) { void InputEngine::PreSetButton(const PadIdentifier& identifier, int button) {
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
ControllerData& controller = controller_list.at(identifier); ControllerData& controller = controller_list.at(identifier);
if (!controller.buttons.contains(button)) { controller.buttons.try_emplace(button, false);
controller.buttons.insert_or_assign(button, false);
}
} }
void InputEngine::PreSetHatButton(const PadIdentifier& identifier, int button) { void InputEngine::PreSetHatButton(const PadIdentifier& identifier, int button) {
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
ControllerData& controller = controller_list.at(identifier); ControllerData& controller = controller_list.at(identifier);
if (!controller.hat_buttons.contains(button)) { controller.hat_buttons.try_emplace(button, u8{0});
controller.hat_buttons.insert_or_assign(button, u8{0});
}
} }
void InputEngine::PreSetAxis(const PadIdentifier& identifier, int axis) { void InputEngine::PreSetAxis(const PadIdentifier& identifier, int axis) {
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
ControllerData& controller = controller_list.at(identifier); ControllerData& controller = controller_list.at(identifier);
if (!controller.axes.contains(axis)) { controller.axes.try_emplace(axis, 0.0f);
controller.axes.insert_or_assign(axis, 0.0f);
}
} }
void InputEngine::PreSetMotion(const PadIdentifier& identifier, int motion) { void InputEngine::PreSetMotion(const PadIdentifier& identifier, int motion) {
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
ControllerData& controller = controller_list.at(identifier); ControllerData& controller = controller_list.at(identifier);
if (!controller.motions.contains(motion)) { controller.motions.try_emplace(motion);
controller.motions.insert_or_assign(motion, BasicMotion{});
}
} }
void InputEngine::SetButton(const PadIdentifier& identifier, int button, bool value) { void InputEngine::SetButton(const PadIdentifier& identifier, int button, bool value) {

View file

@ -23,15 +23,15 @@ struct PadIdentifier {
friend constexpr bool operator==(const PadIdentifier&, const PadIdentifier&) = default; friend constexpr bool operator==(const PadIdentifier&, const PadIdentifier&) = default;
}; };
// Basic motion data containing data from the sensors and a timestamp in microsecons // Basic motion data containing data from the sensors and a timestamp in microseconds
struct BasicMotion { struct BasicMotion {
float gyro_x; float gyro_x{};
float gyro_y; float gyro_y{};
float gyro_z; float gyro_z{};
float accel_x; float accel_x{};
float accel_y; float accel_y{};
float accel_z; float accel_z{};
u64 delta_timestamp; u64 delta_timestamp{};
}; };
// Stages of a battery charge // Stages of a battery charge
@ -202,7 +202,7 @@ private:
std::unordered_map<int, u8> hat_buttons; std::unordered_map<int, u8> hat_buttons;
std::unordered_map<int, float> axes; std::unordered_map<int, float> axes;
std::unordered_map<int, BasicMotion> motions; std::unordered_map<int, BasicMotion> motions;
BatteryLevel battery; BatteryLevel battery{};
}; };
void TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value); void TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value);