From 958cbfee8f4f8da6cc3af0787120f5a2713560f4 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Fri, 30 Dec 2022 22:00:17 +0100 Subject: enhance: Make util::LockFile movable --- src/util/LockFile.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/util/LockFile.hpp | 3 +++ 2 files changed, 40 insertions(+) diff --git a/src/util/LockFile.cpp b/src/util/LockFile.cpp index 26b38e6b..2e5bef32 100644 --- a/src/util/LockFile.cpp +++ b/src/util/LockFile.cpp @@ -82,6 +82,43 @@ LockFile::LockFile(const std::string& path) { } +LockFile::LockFile(LockFile&& other) noexcept + : m_lock_file(std::move(other.m_lock_file)), +#ifndef _WIN32 + m_lock_manager(other.m_lock_manager), + m_alive_file(std::move(other.m_alive_file)), + m_acquired(other.m_acquired) +#else + m_handle(other.m_handle) +#endif +{ +#ifndef _WIN32 + other.m_lock_manager = nullptr; + other.m_acquired = false; +#else + other.m_handle = INVALID_HANDLE_VALUE; +#endif +} + +LockFile& +LockFile::operator=(LockFile&& other) noexcept +{ + if (&other != this) { + m_lock_file = std::move(other.m_lock_file); +#ifndef _WIN32 + m_lock_manager = other.m_lock_manager; + other.m_lock_manager = nullptr; + m_alive_file = std::move(other.m_alive_file); + m_acquired = other.m_acquired; + other.m_acquired = false; +#else + m_handle = other.m_handle; + other.m_handle = INVALID_HANDLE_VALUE; +#endif + } + return *this; +} + void LockFile::make_long_lived( [[maybe_unused]] LongLivedLockFileManager& lock_manager) diff --git a/src/util/LockFile.hpp b/src/util/LockFile.hpp index bdb3f8cb..67d1cb04 100644 --- a/src/util/LockFile.hpp +++ b/src/util/LockFile.hpp @@ -34,6 +34,9 @@ class LockFile : NonCopyable { public: explicit LockFile(const std::string& path); + LockFile(LockFile&& other) noexcept; + + LockFile& operator=(LockFile&& other) noexcept; // Release the lock if previously acquired. ~LockFile(); -- cgit v1.2.1