mirror of
https://github.com/FEX-Emu/FEX.git
synced 2026-10-06 14:00:16 +02:00
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:
1 parent
a6e74cdbe6
commit
aaca8772bd
1 file changed
+29
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user