diff options
| author | 2019-04-01 12:29:59 -0400 | |
|---|---|---|
| committer | 2019-04-01 12:53:47 -0400 | |
| commit | 781ab8407b50d303197ab6fb888ed35ecbcce23a (patch) | |
| tree | eaf2aabd5471c13fe89ac5f7da247b3bf1248e83 /src/input_common | |
| parent | Merge pull request #2304 from lioncash/memsize (diff) | |
| download | yuzu-781ab8407b50d303197ab6fb888ed35ecbcce23a.tar.gz yuzu-781ab8407b50d303197ab6fb888ed35ecbcce23a.tar.xz yuzu-781ab8407b50d303197ab6fb888ed35ecbcce23a.zip | |
general: Use deducation guides for std::lock_guard and std::unique_lock
Since C++17, the introduction of deduction guides for locking facilities
means that we no longer need to hardcode the mutex type into the locks
themselves, making it easier to switch mutex types, should it ever be
necessary in the future.
Diffstat (limited to 'src/input_common')
| -rw-r--r-- | src/input_common/keyboard.cpp | 8 | ||||
| -rw-r--r-- | src/input_common/motion_emu.cpp | 10 | ||||
| -rw-r--r-- | src/input_common/sdl/sdl_impl.cpp | 26 |
3 files changed, 23 insertions, 21 deletions
diff --git a/src/input_common/keyboard.cpp b/src/input_common/keyboard.cpp index 525fe6abc..078374be5 100644 --- a/src/input_common/keyboard.cpp +++ b/src/input_common/keyboard.cpp | |||
| @@ -36,18 +36,18 @@ struct KeyButtonPair { | |||
| 36 | class KeyButtonList { | 36 | class KeyButtonList { |
| 37 | public: | 37 | public: |
| 38 | void AddKeyButton(int key_code, KeyButton* key_button) { | 38 | void AddKeyButton(int key_code, KeyButton* key_button) { |
| 39 | std::lock_guard<std::mutex> guard(mutex); | 39 | std::lock_guard guard{mutex}; |
| 40 | list.push_back(KeyButtonPair{key_code, key_button}); | 40 | list.push_back(KeyButtonPair{key_code, key_button}); |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | void RemoveKeyButton(const KeyButton* key_button) { | 43 | void RemoveKeyButton(const KeyButton* key_button) { |
| 44 | std::lock_guard<std::mutex> guard(mutex); | 44 | std::lock_guard guard{mutex}; |
| 45 | list.remove_if( | 45 | list.remove_if( |
| 46 | [key_button](const KeyButtonPair& pair) { return pair.key_button == key_button; }); | 46 | [key_button](const KeyButtonPair& pair) { return pair.key_button == key_button; }); |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | void ChangeKeyStatus(int key_code, bool pressed) { | 49 | void ChangeKeyStatus(int key_code, bool pressed) { |
| 50 | std::lock_guard<std::mutex> guard(mutex); | 50 | std::lock_guard guard{mutex}; |
| 51 | for (const KeyButtonPair& pair : list) { | 51 | for (const KeyButtonPair& pair : list) { |
| 52 | if (pair.key_code == key_code) | 52 | if (pair.key_code == key_code) |
| 53 | pair.key_button->status.store(pressed); | 53 | pair.key_button->status.store(pressed); |
| @@ -55,7 +55,7 @@ public: | |||
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | void ChangeAllKeyStatus(bool pressed) { | 57 | void ChangeAllKeyStatus(bool pressed) { |
| 58 | std::lock_guard<std::mutex> guard(mutex); | 58 | std::lock_guard guard{mutex}; |
| 59 | for (const KeyButtonPair& pair : list) { | 59 | for (const KeyButtonPair& pair : list) { |
| 60 | pair.key_button->status.store(pressed); | 60 | pair.key_button->status.store(pressed); |
| 61 | } | 61 | } |
diff --git a/src/input_common/motion_emu.cpp b/src/input_common/motion_emu.cpp index 6d96d4019..868251628 100644 --- a/src/input_common/motion_emu.cpp +++ b/src/input_common/motion_emu.cpp | |||
| @@ -39,7 +39,7 @@ public: | |||
| 39 | void Tilt(int x, int y) { | 39 | void Tilt(int x, int y) { |
| 40 | auto mouse_move = Common::MakeVec(x, y) - mouse_origin; | 40 | auto mouse_move = Common::MakeVec(x, y) - mouse_origin; |
| 41 | if (is_tilting) { | 41 | if (is_tilting) { |
| 42 | std::lock_guard<std::mutex> guard(tilt_mutex); | 42 | std::lock_guard guard{tilt_mutex}; |
| 43 | if (mouse_move.x == 0 && mouse_move.y == 0) { | 43 | if (mouse_move.x == 0 && mouse_move.y == 0) { |
| 44 | tilt_angle = 0; | 44 | tilt_angle = 0; |
| 45 | } else { | 45 | } else { |
| @@ -51,13 +51,13 @@ public: | |||
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | void EndTilt() { | 53 | void EndTilt() { |
| 54 | std::lock_guard<std::mutex> guard(tilt_mutex); | 54 | std::lock_guard guard{tilt_mutex}; |
| 55 | tilt_angle = 0; | 55 | tilt_angle = 0; |
| 56 | is_tilting = false; | 56 | is_tilting = false; |
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | std::tuple<Common::Vec3<float>, Common::Vec3<float>> GetStatus() { | 59 | std::tuple<Common::Vec3<float>, Common::Vec3<float>> GetStatus() { |
| 60 | std::lock_guard<std::mutex> guard(status_mutex); | 60 | std::lock_guard guard{status_mutex}; |
| 61 | return status; | 61 | return status; |
| 62 | } | 62 | } |
| 63 | 63 | ||
| @@ -93,7 +93,7 @@ private: | |||
| 93 | old_q = q; | 93 | old_q = q; |
| 94 | 94 | ||
| 95 | { | 95 | { |
| 96 | std::lock_guard<std::mutex> guard(tilt_mutex); | 96 | std::lock_guard guard{tilt_mutex}; |
| 97 | 97 | ||
| 98 | // Find the quaternion describing current 3DS tilting | 98 | // Find the quaternion describing current 3DS tilting |
| 99 | q = Common::MakeQuaternion( | 99 | q = Common::MakeQuaternion( |
| @@ -115,7 +115,7 @@ private: | |||
| 115 | 115 | ||
| 116 | // Update the sensor state | 116 | // Update the sensor state |
| 117 | { | 117 | { |
| 118 | std::lock_guard<std::mutex> guard(status_mutex); | 118 | std::lock_guard guard{status_mutex}; |
| 119 | status = std::make_tuple(gravity, angular_rate); | 119 | status = std::make_tuple(gravity, angular_rate); |
| 120 | } | 120 | } |
| 121 | } | 121 | } |
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index b132d77f5..5949ecbae 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp | |||
| @@ -55,22 +55,22 @@ public: | |||
| 55 | : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, deleter} {} | 55 | : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, deleter} {} |
| 56 | 56 | ||
| 57 | void SetButton(int button, bool value) { | 57 | void SetButton(int button, bool value) { |
| 58 | std::lock_guard<std::mutex> lock(mutex); | 58 | std::lock_guard lock{mutex}; |
| 59 | state.buttons[button] = value; | 59 | state.buttons[button] = value; |
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | bool GetButton(int button) const { | 62 | bool GetButton(int button) const { |
| 63 | std::lock_guard<std::mutex> lock(mutex); | 63 | std::lock_guard lock{mutex}; |
| 64 | return state.buttons.at(button); | 64 | return state.buttons.at(button); |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | void SetAxis(int axis, Sint16 value) { | 67 | void SetAxis(int axis, Sint16 value) { |
| 68 | std::lock_guard<std::mutex> lock(mutex); | 68 | std::lock_guard lock{mutex}; |
| 69 | state.axes[axis] = value; | 69 | state.axes[axis] = value; |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | float GetAxis(int axis) const { | 72 | float GetAxis(int axis) const { |
| 73 | std::lock_guard<std::mutex> lock(mutex); | 73 | std::lock_guard lock{mutex}; |
| 74 | return state.axes.at(axis) / 32767.0f; | 74 | return state.axes.at(axis) / 32767.0f; |
| 75 | } | 75 | } |
| 76 | 76 | ||
| @@ -92,12 +92,12 @@ public: | |||
| 92 | } | 92 | } |
| 93 | 93 | ||
| 94 | void SetHat(int hat, Uint8 direction) { | 94 | void SetHat(int hat, Uint8 direction) { |
| 95 | std::lock_guard<std::mutex> lock(mutex); | 95 | std::lock_guard lock{mutex}; |
| 96 | state.hats[hat] = direction; | 96 | state.hats[hat] = direction; |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | bool GetHatDirection(int hat, Uint8 direction) const { | 99 | bool GetHatDirection(int hat, Uint8 direction) const { |
| 100 | std::lock_guard<std::mutex> lock(mutex); | 100 | std::lock_guard lock{mutex}; |
| 101 | return (state.hats.at(hat) & direction) != 0; | 101 | return (state.hats.at(hat) & direction) != 0; |
| 102 | } | 102 | } |
| 103 | /** | 103 | /** |
| @@ -140,7 +140,7 @@ private: | |||
| 140 | * Get the nth joystick with the corresponding GUID | 140 | * Get the nth joystick with the corresponding GUID |
| 141 | */ | 141 | */ |
| 142 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { | 142 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { |
| 143 | std::lock_guard<std::mutex> lock(joystick_map_mutex); | 143 | std::lock_guard lock{joystick_map_mutex}; |
| 144 | const auto it = joystick_map.find(guid); | 144 | const auto it = joystick_map.find(guid); |
| 145 | if (it != joystick_map.end()) { | 145 | if (it != joystick_map.end()) { |
| 146 | while (it->second.size() <= port) { | 146 | while (it->second.size() <= port) { |
| @@ -161,7 +161,8 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& g | |||
| 161 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) { | 161 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) { |
| 162 | auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id); | 162 | auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id); |
| 163 | const std::string guid = GetGUID(sdl_joystick); | 163 | const std::string guid = GetGUID(sdl_joystick); |
| 164 | std::lock_guard<std::mutex> lock(joystick_map_mutex); | 164 | |
| 165 | std::lock_guard lock{joystick_map_mutex}; | ||
| 165 | auto map_it = joystick_map.find(guid); | 166 | auto map_it = joystick_map.find(guid); |
| 166 | if (map_it != joystick_map.end()) { | 167 | if (map_it != joystick_map.end()) { |
| 167 | auto vec_it = std::find_if(map_it->second.begin(), map_it->second.end(), | 168 | auto vec_it = std::find_if(map_it->second.begin(), map_it->second.end(), |
| @@ -198,8 +199,9 @@ void SDLState::InitJoystick(int joystick_index) { | |||
| 198 | LOG_ERROR(Input, "failed to open joystick {}", joystick_index); | 199 | LOG_ERROR(Input, "failed to open joystick {}", joystick_index); |
| 199 | return; | 200 | return; |
| 200 | } | 201 | } |
| 201 | std::string guid = GetGUID(sdl_joystick); | 202 | const std::string guid = GetGUID(sdl_joystick); |
| 202 | std::lock_guard<std::mutex> lock(joystick_map_mutex); | 203 | |
| 204 | std::lock_guard lock{joystick_map_mutex}; | ||
| 203 | if (joystick_map.find(guid) == joystick_map.end()) { | 205 | if (joystick_map.find(guid) == joystick_map.end()) { |
| 204 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick); | 206 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick); |
| 205 | joystick_map[guid].emplace_back(std::move(joystick)); | 207 | joystick_map[guid].emplace_back(std::move(joystick)); |
| @@ -221,7 +223,7 @@ void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) { | |||
| 221 | std::string guid = GetGUID(sdl_joystick); | 223 | std::string guid = GetGUID(sdl_joystick); |
| 222 | std::shared_ptr<SDLJoystick> joystick; | 224 | std::shared_ptr<SDLJoystick> joystick; |
| 223 | { | 225 | { |
| 224 | std::lock_guard<std::mutex> lock(joystick_map_mutex); | 226 | std::lock_guard lock{joystick_map_mutex}; |
| 225 | // This call to guid is safe since the joystick is guaranteed to be in the map | 227 | // This call to guid is safe since the joystick is guaranteed to be in the map |
| 226 | auto& joystick_guid_list = joystick_map[guid]; | 228 | auto& joystick_guid_list = joystick_map[guid]; |
| 227 | const auto joystick_it = | 229 | const auto joystick_it = |
| @@ -274,7 +276,7 @@ void SDLState::HandleGameControllerEvent(const SDL_Event& event) { | |||
| 274 | } | 276 | } |
| 275 | 277 | ||
| 276 | void SDLState::CloseJoysticks() { | 278 | void SDLState::CloseJoysticks() { |
| 277 | std::lock_guard<std::mutex> lock(joystick_map_mutex); | 279 | std::lock_guard lock{joystick_map_mutex}; |
| 278 | joystick_map.clear(); | 280 | joystick_map.clear(); |
| 279 | } | 281 | } |
| 280 | 282 | ||