Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions modules/Thread/SharedLocker.mpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ export namespace CppUtils::Thread
m_value{std::forward<decltype(args)>(args)...}
{}

SharedLocker(const SharedLocker&) = delete;
auto operator=(const SharedLocker&) -> SharedLocker& = delete;

inline SharedLocker(SharedLocker&& other) noexcept(std::is_nothrow_move_constructible_v<T>):
m_value{std::move(other.uniqueAccess().value())}
{}

inline auto operator=(SharedLocker&& other) noexcept(std::is_nothrow_move_assignable_v<T>) -> SharedLocker&
{
if (this != std::addressof(other))
{
auto guard = std::scoped_lock{m_mutex, other.m_mutex};
m_value = std::move(other.m_value);
}
return *this;
}

[[nodiscard]] inline auto uniqueAccess() -> UniqueAccessor
{
return Accessor{m_mutex, m_value};
Expand Down
20 changes: 20 additions & 0 deletions tests/Thread/SharedLocker.mpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,25 @@ namespace CppUtils::UnitTest::Thread::SharedLocker
suite.expectEqual(std::get<1>(accessor.values), "Bar");
}
});

suite.addTest("Move constructor", [&] {
auto source = CppUtils::Thread::SharedLocker<std::unique_ptr<int>>{std::make_unique<int>(42)};
auto destination = CppUtils::Thread::SharedLocker<std::unique_ptr<int>>{std::move(source)};

suite.expect(source.uniqueAccess().value() == nullptr);
suite.expect(destination.sharedAccess().value() != nullptr);
suite.expectEqual(*destination.sharedAccess().value(), 42);
});

suite.addTest("Move assignment", [&] {
auto source = CppUtils::Thread::SharedLocker<std::unique_ptr<int>>{std::make_unique<int>(100)};
auto destination = CppUtils::Thread::SharedLocker<std::unique_ptr<int>>{std::make_unique<int>(200)};

destination = std::move(source);

suite.expect(source.uniqueAccess().value() == nullptr);
suite.expect(destination.sharedAccess().value() != nullptr);
suite.expectEqual(*destination.sharedAccess().value(), 100);
});
}};
}
Loading