Utils/File: Properly handle move assignment/construction

Ensures that whenever a file handle is transferred anywhere, that the
moved from instance won't end up closing the file handle when the
destructor runs.
This commit is contained in:
Lioncache committed 2026-09-01 16:00:11 -04:00
1 parent a6e74cdbe6
commit aaca8772bd
1 file changed
+29
+29
View File
@@ -7,6 +7,7 @@
#include <chrono>
#include <thread>
#include <utility>
#ifndef _WIN32
#include <fcntl.h>
@@ -69,6 +70,34 @@ public:
ShouldClose = IsValidHandle;
}
File(File&& Other) noexcept
: ShouldClose(std::exchange(Other.ShouldClose, false))
, IsValidHandle(std::exchange(Other.IsValidHandle, false))
#ifdef _WIN32
, Handle(std::exchange(Other.Handle, INVALID_HANDLE_VALUE))
#else
, Handle(std::exchange(Other.Handle, -1))
#endif
, Seekable(std::exchange(Other.Seekable, false))
, Locked(std::exchange(Other.Locked, false)) {
}
File& operator=(File&& Other) noexcept {
if (this == &Other) {
return *this;
}
std::swap(ShouldClose, Other.ShouldClose);
std::swap(IsValidHandle, Other.IsValidHandle);
std::swap(Handle, Other.Handle);
std::swap(Seekable, Other.Seekable);
std::swap(Locked, Other.Locked);
return *this;
}
File(const File&) = delete;
File& operator=(const File&) = delete;
FileHandleType GetHandle() {
return Handle;
}