From 31cfb129cc7a72496956ee16f9979b6501df977a Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Fri, 19 Jun 2026 23:09:16 -0400 Subject: [PATCH] refactor(sonar): fix cpp:S5950 --- src/core/runtime.cpp | 28 ++++---- src/include/libvirtualhid/runtime.hpp | 94 +++++++++++++++++++-------- 2 files changed, 82 insertions(+), 40 deletions(-) diff --git a/src/core/runtime.cpp b/src/core/runtime.cpp index 2970f9a..21c3627 100644 --- a/src/core/runtime.cpp +++ b/src/core/runtime.cpp @@ -291,7 +291,7 @@ namespace lvh { } // namespace - Gamepad::Gamepad(std::shared_ptr device): + Gamepad::Gamepad(detail::RuntimeConstructionToken, std::shared_ptr device): device_ {std::move(device)} {} Gamepad::Gamepad(Gamepad &&) noexcept = default; @@ -411,7 +411,7 @@ namespace lvh { }); } - Keyboard::Keyboard(std::shared_ptr device): + Keyboard::Keyboard(detail::RuntimeConstructionToken, std::shared_ptr device): device_ {std::move(device)} {} Keyboard::Keyboard(Keyboard &&) noexcept = default; @@ -517,7 +517,7 @@ namespace lvh { }); } - Mouse::Mouse(std::shared_ptr device): + Mouse::Mouse(detail::RuntimeConstructionToken, std::shared_ptr device): device_ {std::move(device)} {} Mouse::Mouse(Mouse &&) noexcept = default; @@ -627,7 +627,7 @@ namespace lvh { }); } - Touchscreen::Touchscreen(std::shared_ptr device): + Touchscreen::Touchscreen(detail::RuntimeConstructionToken, std::shared_ptr device): device_ {std::move(device)} {} Touchscreen::Touchscreen(Touchscreen &&) noexcept = default; @@ -729,7 +729,7 @@ namespace lvh { }); } - Trackpad::Trackpad(std::shared_ptr device): + Trackpad::Trackpad(detail::RuntimeConstructionToken, std::shared_ptr device): device_ {std::move(device)} {} Trackpad::Trackpad(Trackpad &&) noexcept = default; @@ -848,7 +848,7 @@ namespace lvh { }); } - PenTablet::PenTablet(std::shared_ptr device): + PenTablet::PenTablet(detail::RuntimeConstructionToken, std::shared_ptr device): device_ {std::move(device)} {} PenTablet::PenTablet(PenTablet &&) noexcept = default; @@ -941,7 +941,7 @@ namespace lvh { }); } - Runtime::Runtime(RuntimeOptions options): + Runtime::Runtime(detail::RuntimeConstructionToken, RuntimeOptions options): state_ {std::make_shared(options)} {} Runtime::Runtime(Runtime &&) noexcept = default; @@ -954,7 +954,7 @@ namespace lvh { } std::unique_ptr Runtime::create(RuntimeOptions options) { - return std::unique_ptr {new Runtime {options}}; + return std::make_unique(detail::RuntimeConstructionToken {}, options); } const BackendCapabilities &Runtime::capabilities() const { @@ -993,7 +993,7 @@ namespace lvh { state_->gamepads.emplace_back(device); } - return {OperationStatus::success(), std::unique_ptr {new Gamepad {std::move(device)}}}; + return {OperationStatus::success(), std::make_unique(detail::RuntimeConstructionToken {}, std::move(device))}; } KeyboardCreationResult Runtime::create_keyboard() { @@ -1024,7 +1024,7 @@ namespace lvh { state_->keyboards.emplace_back(device); } - return {OperationStatus::success(), std::unique_ptr {new Keyboard {std::move(device)}}}; + return {OperationStatus::success(), std::make_unique(detail::RuntimeConstructionToken {}, std::move(device))}; } MouseCreationResult Runtime::create_mouse() { @@ -1055,7 +1055,7 @@ namespace lvh { state_->mice.emplace_back(device); } - return {OperationStatus::success(), std::unique_ptr {new Mouse {std::move(device)}}}; + return {OperationStatus::success(), std::make_unique(detail::RuntimeConstructionToken {}, std::move(device))}; } TouchscreenCreationResult Runtime::create_touchscreen() { @@ -1086,7 +1086,7 @@ namespace lvh { state_->touchscreens.emplace_back(device); } - return {OperationStatus::success(), std::unique_ptr {new Touchscreen {std::move(device)}}}; + return {OperationStatus::success(), std::make_unique(detail::RuntimeConstructionToken {}, std::move(device))}; } TrackpadCreationResult Runtime::create_trackpad() { @@ -1117,7 +1117,7 @@ namespace lvh { state_->trackpads.emplace_back(device); } - return {OperationStatus::success(), std::unique_ptr {new Trackpad {std::move(device)}}}; + return {OperationStatus::success(), std::make_unique(detail::RuntimeConstructionToken {}, std::move(device))}; } PenTabletCreationResult Runtime::create_pen_tablet() { @@ -1148,7 +1148,7 @@ namespace lvh { state_->pen_tablets.emplace_back(device); } - return {OperationStatus::success(), std::unique_ptr {new PenTablet {std::move(device)}}}; + return {OperationStatus::success(), std::make_unique(detail::RuntimeConstructionToken {}, std::move(device))}; } std::size_t Runtime::active_device_count() const { diff --git a/src/include/libvirtualhid/runtime.hpp b/src/include/libvirtualhid/runtime.hpp index f6f1e5f..bd4bced 100644 --- a/src/include/libvirtualhid/runtime.hpp +++ b/src/include/libvirtualhid/runtime.hpp @@ -14,7 +14,19 @@ namespace lvh { + class Runtime; + namespace detail { + /** + * @brief Token used by Runtime to construct runtime-owned handles. + */ + struct RuntimeConstructionToken { + private: + friend class ::lvh::Runtime; + + RuntimeConstructionToken() = default; + }; + struct GamepadDevice; struct KeyboardDevice; struct MouseDevice; @@ -102,6 +114,14 @@ namespace lvh { */ Gamepad &operator=(Gamepad &&other) noexcept; + /** + * @brief Construct a gamepad handle for Runtime-owned state. + * + * @param token Runtime construction token. + * @param device Shared gamepad state. + */ + Gamepad(detail::RuntimeConstructionToken token, std::shared_ptr device); + /** * @brief Destroy the gamepad handle. */ @@ -184,10 +204,6 @@ namespace lvh { std::size_t submit_count() const; private: - friend class Runtime; - - explicit Gamepad(std::shared_ptr device); - std::shared_ptr device_; }; @@ -223,6 +239,14 @@ namespace lvh { */ Keyboard &operator=(Keyboard &&other) noexcept; + /** + * @brief Construct a keyboard handle for Runtime-owned state. + * + * @param token Runtime construction token. + * @param device Shared keyboard state. + */ + Keyboard(detail::RuntimeConstructionToken token, std::shared_ptr device); + /** * @brief Destroy the keyboard handle. */ @@ -300,10 +324,6 @@ namespace lvh { std::size_t submit_count() const; private: - friend class Runtime; - - explicit Keyboard(std::shared_ptr device); - std::shared_ptr device_; }; @@ -339,6 +359,14 @@ namespace lvh { */ Mouse &operator=(Mouse &&other) noexcept; + /** + * @brief Construct a mouse handle for Runtime-owned state. + * + * @param token Runtime construction token. + * @param device Shared mouse state. + */ + Mouse(detail::RuntimeConstructionToken token, std::shared_ptr device); + /** * @brief Destroy the mouse handle. */ @@ -437,10 +465,6 @@ namespace lvh { std::size_t submit_count() const; private: - friend class Runtime; - - explicit Mouse(std::shared_ptr device); - std::shared_ptr device_; }; @@ -476,6 +500,14 @@ namespace lvh { */ Touchscreen &operator=(Touchscreen &&other) noexcept; + /** + * @brief Construct a touchscreen handle for Runtime-owned state. + * + * @param token Runtime construction token. + * @param device Shared touchscreen state. + */ + Touchscreen(detail::RuntimeConstructionToken token, std::shared_ptr device); + /** * @brief Destroy the touchscreen handle. */ @@ -537,10 +569,6 @@ namespace lvh { std::size_t submit_count() const; private: - friend class Runtime; - - explicit Touchscreen(std::shared_ptr device); - std::shared_ptr device_; }; @@ -576,6 +604,14 @@ namespace lvh { */ Trackpad &operator=(Trackpad &&other) noexcept; + /** + * @brief Construct a trackpad handle for Runtime-owned state. + * + * @param token Runtime construction token. + * @param device Shared trackpad state. + */ + Trackpad(detail::RuntimeConstructionToken token, std::shared_ptr device); + /** * @brief Destroy the trackpad handle. */ @@ -645,10 +681,6 @@ namespace lvh { std::size_t submit_count() const; private: - friend class Runtime; - - explicit Trackpad(std::shared_ptr device); - std::shared_ptr device_; }; @@ -684,6 +716,14 @@ namespace lvh { */ PenTablet &operator=(PenTablet &&other) noexcept; + /** + * @brief Construct a pen tablet handle for Runtime-owned state. + * + * @param token Runtime construction token. + * @param device Shared pen tablet state. + */ + PenTablet(detail::RuntimeConstructionToken token, std::shared_ptr device); + /** * @brief Destroy the pen tablet handle. */ @@ -746,10 +786,6 @@ namespace lvh { std::size_t submit_count() const; private: - friend class Runtime; - - explicit PenTablet(std::shared_ptr device); - std::shared_ptr device_; }; @@ -929,6 +965,14 @@ namespace lvh { */ Runtime &operator=(Runtime &&other) noexcept; + /** + * @brief Construct a runtime for Runtime::create. + * + * @param token Runtime construction token. + * @param options Runtime configuration. + */ + Runtime(detail::RuntimeConstructionToken token, RuntimeOptions options); + /** * @brief Destroy the runtime and close any remaining devices. */ @@ -1060,8 +1104,6 @@ namespace lvh { void close_all(); private: - explicit Runtime(RuntimeOptions options); - std::shared_ptr state_; };