From a55baaff7547c92389b15817d80075f1d5bd1973 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Fri, 19 Jun 2026 23:22:01 -0400 Subject: [PATCH 1/2] refactor(sonar): fix cpp:S8379 --- src/core/runtime.cpp | 155 +++++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 81 deletions(-) diff --git a/src/core/runtime.cpp b/src/core/runtime.cpp index 21c3627..0c1697d 100644 --- a/src/core/runtime.cpp +++ b/src/core/runtime.cpp @@ -18,7 +18,19 @@ namespace lvh::detail { - struct GamepadDevice { + class SynchronizedState { + public: + template + decltype(auto) with_lock(Func &&func) const { + std::lock_guard lock {mutex_}; + return std::forward(func)(); + } + + private: + mutable std::mutex mutex_; + }; + + struct GamepadDevice: SynchronizedState { explicit GamepadDevice( DeviceId device_id, CreateGamepadOptions create_options, @@ -36,10 +48,9 @@ namespace lvh::detail { std::vector last_report; std::size_t submitted_reports = 0; OutputCallback output_callback; - mutable std::mutex mutex; }; - struct KeyboardDevice { + struct KeyboardDevice: SynchronizedState { explicit KeyboardDevice( DeviceId device_id, CreateKeyboardOptions create_options, @@ -56,10 +67,9 @@ namespace lvh::detail { KeyboardEvent last_event; KeyboardTextEvent last_text_event; std::size_t submitted_events = 0; - mutable std::mutex mutex; }; - struct MouseDevice { + struct MouseDevice: SynchronizedState { explicit MouseDevice(DeviceId device_id, CreateMouseOptions create_options, std::unique_ptr backend_mouse): id {device_id}, options {std::move(create_options)}, @@ -71,10 +81,9 @@ namespace lvh::detail { bool open = true; MouseEvent last_event; std::size_t submitted_events = 0; - mutable std::mutex mutex; }; - struct TouchscreenDevice { + struct TouchscreenDevice: SynchronizedState { explicit TouchscreenDevice( DeviceId device_id, CreateTouchscreenOptions create_options, @@ -90,10 +99,9 @@ namespace lvh::detail { bool open = true; TouchContact last_contact; std::size_t submitted_events = 0; - mutable std::mutex mutex; }; - struct TrackpadDevice { + struct TrackpadDevice: SynchronizedState { explicit TrackpadDevice( DeviceId device_id, CreateTrackpadOptions create_options, @@ -109,10 +117,9 @@ namespace lvh::detail { bool open = true; TouchContact last_contact; std::size_t submitted_events = 0; - mutable std::mutex mutex; }; - struct PenTabletDevice { + struct PenTabletDevice: SynchronizedState { explicit PenTabletDevice( DeviceId device_id, CreatePenTabletOptions create_options, @@ -128,10 +135,9 @@ namespace lvh::detail { bool open = true; PenToolState last_tool; std::size_t submitted_events = 0; - mutable std::mutex mutex; }; - class RuntimeState { + class RuntimeState: public SynchronizedState { public: explicit RuntimeState(RuntimeOptions runtime_options): options {runtime_options}, @@ -148,7 +154,6 @@ namespace lvh::detail { std::vector> touchscreens; std::vector> trackpads; std::vector> pen_tablets; - mutable std::mutex mutex; }; } // namespace lvh::detail @@ -259,8 +264,9 @@ namespace lvh { template auto with_device(const auto &device, Func &&func) { - std::lock_guard lock {device->mutex}; - return func(*device); + return device->with_lock([&]() { + return func(*device); + }); } template @@ -268,7 +274,9 @@ namespace lvh { std::size_t count = 0; for (const auto &weak_device : devices) { if (const auto device = weak_device.lock()) { - if (device->open) { + if (device->with_lock([&]() { + return device->open; + })) { ++count; } } @@ -280,11 +288,12 @@ namespace lvh { void close_devices(DeviceList &devices) { for (const auto &weak_device : devices) { if (const auto device = weak_device.lock()) { - std::lock_guard device_lock {device->mutex}; - if (device->backend) { - static_cast(device->backend->close()); - } - device->open = false; + device->with_lock([&]() { + if (device->backend) { + static_cast(device->backend->close()); + } + device->open = false; + }); } } } @@ -976,11 +985,9 @@ namespace lvh { return {validation, nullptr}; } - DeviceId id; - { - std::lock_guard lock {state_->mutex}; - id = state_->next_device_id++; - } + const auto id = state_->with_lock([&]() { + return state_->next_device_id++; + }); auto backend_result = state_->backend->create_gamepad(id, options); if (!backend_result) { @@ -988,10 +995,9 @@ namespace lvh { } auto device = std::make_shared(id, options, std::move(backend_result.gamepad)); - { - std::lock_guard lock {state_->mutex}; + state_->with_lock([&]() { state_->gamepads.emplace_back(device); - } + }); return {OperationStatus::success(), std::make_unique(detail::RuntimeConstructionToken {}, std::move(device))}; } @@ -1007,11 +1013,9 @@ namespace lvh { return {validation, nullptr}; } - DeviceId id; - { - std::lock_guard lock {state_->mutex}; - id = state_->next_device_id++; - } + const auto id = state_->with_lock([&]() { + return state_->next_device_id++; + }); auto backend_result = state_->backend->create_keyboard(id, options); if (!backend_result) { @@ -1019,10 +1023,9 @@ namespace lvh { } auto device = std::make_shared(id, options, std::move(backend_result.keyboard)); - { - std::lock_guard lock {state_->mutex}; + state_->with_lock([&]() { state_->keyboards.emplace_back(device); - } + }); return {OperationStatus::success(), std::make_unique(detail::RuntimeConstructionToken {}, std::move(device))}; } @@ -1038,11 +1041,9 @@ namespace lvh { return {validation, nullptr}; } - DeviceId id; - { - std::lock_guard lock {state_->mutex}; - id = state_->next_device_id++; - } + const auto id = state_->with_lock([&]() { + return state_->next_device_id++; + }); auto backend_result = state_->backend->create_mouse(id, options); if (!backend_result) { @@ -1050,10 +1051,9 @@ namespace lvh { } auto device = std::make_shared(id, options, std::move(backend_result.mouse)); - { - std::lock_guard lock {state_->mutex}; + state_->with_lock([&]() { state_->mice.emplace_back(device); - } + }); return {OperationStatus::success(), std::make_unique(detail::RuntimeConstructionToken {}, std::move(device))}; } @@ -1069,11 +1069,9 @@ namespace lvh { return {validation, nullptr}; } - DeviceId id; - { - std::lock_guard lock {state_->mutex}; - id = state_->next_device_id++; - } + const auto id = state_->with_lock([&]() { + return state_->next_device_id++; + }); auto backend_result = state_->backend->create_touchscreen(id, options); if (!backend_result) { @@ -1081,10 +1079,9 @@ namespace lvh { } auto device = std::make_shared(id, options, std::move(backend_result.touchscreen)); - { - std::lock_guard lock {state_->mutex}; + state_->with_lock([&]() { state_->touchscreens.emplace_back(device); - } + }); return {OperationStatus::success(), std::make_unique(detail::RuntimeConstructionToken {}, std::move(device))}; } @@ -1100,11 +1097,9 @@ namespace lvh { return {validation, nullptr}; } - DeviceId id; - { - std::lock_guard lock {state_->mutex}; - id = state_->next_device_id++; - } + const auto id = state_->with_lock([&]() { + return state_->next_device_id++; + }); auto backend_result = state_->backend->create_trackpad(id, options); if (!backend_result) { @@ -1112,10 +1107,9 @@ namespace lvh { } auto device = std::make_shared(id, options, std::move(backend_result.trackpad)); - { - std::lock_guard lock {state_->mutex}; + state_->with_lock([&]() { state_->trackpads.emplace_back(device); - } + }); return {OperationStatus::success(), std::make_unique(detail::RuntimeConstructionToken {}, std::move(device))}; } @@ -1131,11 +1125,9 @@ namespace lvh { return {validation, nullptr}; } - DeviceId id; - { - std::lock_guard lock {state_->mutex}; - id = state_->next_device_id++; - } + const auto id = state_->with_lock([&]() { + return state_->next_device_id++; + }); auto backend_result = state_->backend->create_pen_tablet(id, options); if (!backend_result) { @@ -1143,29 +1135,30 @@ namespace lvh { } auto device = std::make_shared(id, options, std::move(backend_result.pen_tablet)); - { - std::lock_guard lock {state_->mutex}; + state_->with_lock([&]() { state_->pen_tablets.emplace_back(device); - } + }); return {OperationStatus::success(), std::make_unique(detail::RuntimeConstructionToken {}, std::move(device))}; } std::size_t Runtime::active_device_count() const { - std::lock_guard lock {state_->mutex}; - return count_open_devices(state_->gamepads) + count_open_devices(state_->keyboards) + count_open_devices(state_->mice) + - count_open_devices(state_->touchscreens) + count_open_devices(state_->trackpads) + - count_open_devices(state_->pen_tablets); + return state_->with_lock([&]() { + return count_open_devices(state_->gamepads) + count_open_devices(state_->keyboards) + count_open_devices(state_->mice) + + count_open_devices(state_->touchscreens) + count_open_devices(state_->trackpads) + + count_open_devices(state_->pen_tablets); + }); } void Runtime::close_all() { - std::lock_guard lock {state_->mutex}; - close_devices(state_->gamepads); - close_devices(state_->keyboards); - close_devices(state_->mice); - close_devices(state_->touchscreens); - close_devices(state_->trackpads); - close_devices(state_->pen_tablets); + state_->with_lock([&]() { + close_devices(state_->gamepads); + close_devices(state_->keyboards); + close_devices(state_->mice); + close_devices(state_->touchscreens); + close_devices(state_->trackpads); + close_devices(state_->pen_tablets); + }); } } // namespace lvh From c2e799e5fdab1e2ce305f526cd2cd7f93324aea2 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Fri, 19 Jun 2026 23:26:34 -0400 Subject: [PATCH 2/2] Additional sonar fixes --- src/core/runtime.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/core/runtime.cpp b/src/core/runtime.cpp index 0c1697d..fcf7d19 100644 --- a/src/core/runtime.cpp +++ b/src/core/runtime.cpp @@ -264,7 +264,7 @@ namespace lvh { template auto with_device(const auto &device, Func &&func) { - return device->with_lock([&]() { + return device->with_lock([&device, &func]() { return func(*device); }); } @@ -274,7 +274,7 @@ namespace lvh { std::size_t count = 0; for (const auto &weak_device : devices) { if (const auto device = weak_device.lock()) { - if (device->with_lock([&]() { + if (device->with_lock([device]() { return device->open; })) { ++count; @@ -288,7 +288,7 @@ namespace lvh { void close_devices(DeviceList &devices) { for (const auto &weak_device : devices) { if (const auto device = weak_device.lock()) { - device->with_lock([&]() { + device->with_lock([device]() { if (device->backend) { static_cast(device->backend->close()); } @@ -985,7 +985,7 @@ namespace lvh { return {validation, nullptr}; } - const auto id = state_->with_lock([&]() { + const auto id = state_->with_lock([this]() { return state_->next_device_id++; }); @@ -995,7 +995,7 @@ namespace lvh { } auto device = std::make_shared(id, options, std::move(backend_result.gamepad)); - state_->with_lock([&]() { + state_->with_lock([this, &device]() { state_->gamepads.emplace_back(device); }); @@ -1013,7 +1013,7 @@ namespace lvh { return {validation, nullptr}; } - const auto id = state_->with_lock([&]() { + const auto id = state_->with_lock([this]() { return state_->next_device_id++; }); @@ -1023,7 +1023,7 @@ namespace lvh { } auto device = std::make_shared(id, options, std::move(backend_result.keyboard)); - state_->with_lock([&]() { + state_->with_lock([this, &device]() { state_->keyboards.emplace_back(device); }); @@ -1041,7 +1041,7 @@ namespace lvh { return {validation, nullptr}; } - const auto id = state_->with_lock([&]() { + const auto id = state_->with_lock([this]() { return state_->next_device_id++; }); @@ -1051,7 +1051,7 @@ namespace lvh { } auto device = std::make_shared(id, options, std::move(backend_result.mouse)); - state_->with_lock([&]() { + state_->with_lock([this, &device]() { state_->mice.emplace_back(device); }); @@ -1069,7 +1069,7 @@ namespace lvh { return {validation, nullptr}; } - const auto id = state_->with_lock([&]() { + const auto id = state_->with_lock([this]() { return state_->next_device_id++; }); @@ -1079,7 +1079,7 @@ namespace lvh { } auto device = std::make_shared(id, options, std::move(backend_result.touchscreen)); - state_->with_lock([&]() { + state_->with_lock([this, &device]() { state_->touchscreens.emplace_back(device); }); @@ -1097,7 +1097,7 @@ namespace lvh { return {validation, nullptr}; } - const auto id = state_->with_lock([&]() { + const auto id = state_->with_lock([this]() { return state_->next_device_id++; }); @@ -1107,7 +1107,7 @@ namespace lvh { } auto device = std::make_shared(id, options, std::move(backend_result.trackpad)); - state_->with_lock([&]() { + state_->with_lock([this, &device]() { state_->trackpads.emplace_back(device); }); @@ -1125,7 +1125,7 @@ namespace lvh { return {validation, nullptr}; } - const auto id = state_->with_lock([&]() { + const auto id = state_->with_lock([this]() { return state_->next_device_id++; }); @@ -1135,7 +1135,7 @@ namespace lvh { } auto device = std::make_shared(id, options, std::move(backend_result.pen_tablet)); - state_->with_lock([&]() { + state_->with_lock([this, &device]() { state_->pen_tablets.emplace_back(device); }); @@ -1143,7 +1143,7 @@ namespace lvh { } std::size_t Runtime::active_device_count() const { - return state_->with_lock([&]() { + return state_->with_lock([this]() { return count_open_devices(state_->gamepads) + count_open_devices(state_->keyboards) + count_open_devices(state_->mice) + count_open_devices(state_->touchscreens) + count_open_devices(state_->trackpads) + count_open_devices(state_->pen_tablets); @@ -1151,7 +1151,7 @@ namespace lvh { } void Runtime::close_all() { - state_->with_lock([&]() { + state_->with_lock([this]() { close_devices(state_->gamepads); close_devices(state_->keyboards); close_devices(state_->mice);