mirror of
https://github.com/LizardByte/libvirtualhid.git
synced 2026-12-04 22:22:32 +09:00
fix(runtime): close replaced devices on move assignment (#236)
Co-authored-by: salty-sugar-art <254115885+salty-sugar-art@users.noreply.github.com> Co-authored-by: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com>
This commit is contained in:
3 files changed
+46
-2
No files matched your search
@@ -1186,7 +1186,13 @@ namespace lvh {
|
||||
state_ {std::make_shared<detail::RuntimeState>(options)} {}
|
||||
|
||||
Runtime::Runtime(Runtime &&) noexcept = default;
|
||||
Runtime &Runtime::operator=(Runtime &&) noexcept = default;
|
||||
|
||||
Runtime &Runtime::operator=(Runtime &&other) noexcept {
|
||||
Runtime replacement {std::move(other)};
|
||||
state_.swap(replacement.state_);
|
||||
// replacement now owns the old state and closes its devices on scope exit.
|
||||
return *this;
|
||||
}
|
||||
|
||||
Runtime::~Runtime() {
|
||||
if (state_) {
|
||||
|
||||
@@ -988,7 +988,7 @@ namespace lvh {
|
||||
Runtime(Runtime &&other) noexcept;
|
||||
|
||||
/**
|
||||
* @brief Move assign a runtime.
|
||||
* @brief Move assign a runtime and close the devices owned by the replaced runtime.
|
||||
*
|
||||
* @param other Runtime to move from.
|
||||
* @return This runtime.
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
// local includes
|
||||
@@ -38,6 +39,43 @@ TEST(RuntimeTest, FakeBackendReportsCapabilities) {
|
||||
EXPECT_FALSE(runtime->capabilities().requires_installed_driver);
|
||||
}
|
||||
|
||||
TEST(RuntimeTest, MoveAssignmentClosesReplacedDevices) {
|
||||
auto runtime = lvh::Runtime::create();
|
||||
auto old_gamepad = runtime->create_gamepad(lvh::profiles::xbox_360());
|
||||
auto old_keyboard = runtime->create_keyboard();
|
||||
ASSERT_TRUE(old_gamepad);
|
||||
ASSERT_TRUE(old_keyboard);
|
||||
|
||||
auto replacement = lvh::Runtime::create();
|
||||
auto new_gamepad = replacement->create_gamepad(lvh::profiles::xbox_360());
|
||||
ASSERT_TRUE(new_gamepad);
|
||||
|
||||
*runtime = std::move(*replacement);
|
||||
|
||||
EXPECT_FALSE(old_gamepad.gamepad->is_open());
|
||||
EXPECT_FALSE(old_keyboard.keyboard->is_open());
|
||||
EXPECT_EQ(old_gamepad.gamepad->submit({}).code(), lvh::ErrorCode::device_closed);
|
||||
EXPECT_EQ(runtime->active_device_count(), 1U);
|
||||
EXPECT_TRUE(new_gamepad.gamepad->submit({}).ok());
|
||||
|
||||
*replacement = std::move(*runtime);
|
||||
EXPECT_EQ(replacement->active_device_count(), 1U);
|
||||
replacement.reset();
|
||||
EXPECT_FALSE(new_gamepad.gamepad->is_open());
|
||||
}
|
||||
|
||||
TEST(RuntimeTest, SelfMoveAssignmentKeepsDevicesOpen) {
|
||||
auto runtime = lvh::Runtime::create();
|
||||
auto created = runtime->create_gamepad(lvh::profiles::xbox_360());
|
||||
ASSERT_TRUE(created);
|
||||
|
||||
auto &same_runtime = *runtime;
|
||||
*runtime = std::move(same_runtime);
|
||||
|
||||
EXPECT_EQ(runtime->active_device_count(), 1U);
|
||||
EXPECT_TRUE(created.gamepad->submit({}).ok());
|
||||
}
|
||||
|
||||
TEST(RuntimeTest, RoutesDiagnosticsToConsumerCallback) {
|
||||
struct Diagnostic {
|
||||
lvh::LogLevel level;
|
||||
|
||||
Reference in new issue
Block a user