From: Joel Rosdahl Date: Fri, 30 Dec 2022 21:00:17 +0000 (+0100) Subject: enhance: Make util::LockFile movable X-Git-Tag: v4.8~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=958cbfee8f4f8da6cc3af0787120f5a2713560f4;p=thirdparty%2Fccache.git enhance: Make util::LockFile movable --- diff --git a/src/util/LockFile.cpp b/src/util/LockFile.cpp index 26b38e6b9..2e5bef320 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 bdb3f8cb9..67d1cb043 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();