mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-03-30 16:20:21 +00:00
Merge branch 'fix-fileclose' into 'master'
Use ScopedHandle for File::Handle See merge request OpenMW/openmw!2159
This commit is contained in:
commit
4a15fae588
@ -22,7 +22,7 @@ namespace Files
|
|||||||
private:
|
private:
|
||||||
std::size_t mOrigin;
|
std::size_t mOrigin;
|
||||||
std::size_t mSize;
|
std::size_t mSize;
|
||||||
Platform::File::Handle mFile{ Platform::File::Handle::Invalid };
|
Platform::File::ScopedHandle mFile;
|
||||||
char mBuffer[8192]{ 0 };
|
char mBuffer[8192]{ 0 };
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,36 @@ namespace Platform::File {
|
|||||||
|
|
||||||
size_t read(Handle handle, void* data, size_t size);
|
size_t read(Handle handle, void* data, size_t size);
|
||||||
|
|
||||||
|
class ScopedHandle
|
||||||
|
{
|
||||||
|
Handle mHandle{ Handle::Invalid };
|
||||||
|
|
||||||
|
public:
|
||||||
|
ScopedHandle() noexcept = default;
|
||||||
|
ScopedHandle(ScopedHandle& other) = delete;
|
||||||
|
ScopedHandle(Handle handle) noexcept : mHandle(handle) {}
|
||||||
|
ScopedHandle(ScopedHandle&& other) noexcept
|
||||||
|
: mHandle(other.mHandle)
|
||||||
|
{
|
||||||
|
other.mHandle = Handle::Invalid;
|
||||||
|
}
|
||||||
|
ScopedHandle& operator=(const ScopedHandle& other) = delete;
|
||||||
|
ScopedHandle& operator=(ScopedHandle&& other) noexcept
|
||||||
|
{
|
||||||
|
if (mHandle != Handle::Invalid)
|
||||||
|
close(mHandle);
|
||||||
|
mHandle = other.mHandle;
|
||||||
|
other.mHandle = Handle::Invalid;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
~ScopedHandle()
|
||||||
|
{
|
||||||
|
if(mHandle != Handle::Invalid)
|
||||||
|
close(mHandle);
|
||||||
|
}
|
||||||
|
|
||||||
|
operator Handle() const { return mHandle; }
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // OPENMW_COMPONENTS_PLATFORM_FILE_HPP
|
#endif // OPENMW_COMPONENTS_PLATFORM_FILE_HPP
|
||||||
|
Loading…
x
Reference in New Issue
Block a user