From 2014ea40b91a4631160d8c8b1055fe7f231c96b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <3397065-ZehMatt@users.noreply.gitlab.com> Date: Mon, 18 Jul 2022 21:13:41 +0300 Subject: [PATCH] Use ScopedHandle for File::Handle --- components/files/constrainedfilestreambuf.hpp | 2 +- components/platform/file.hpp | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/components/files/constrainedfilestreambuf.hpp b/components/files/constrainedfilestreambuf.hpp index 0808ba6a9c..bb9d6ca89e 100644 --- a/components/files/constrainedfilestreambuf.hpp +++ b/components/files/constrainedfilestreambuf.hpp @@ -22,7 +22,7 @@ namespace Files private: std::size_t mOrigin; std::size_t mSize; - Platform::File::Handle mFile{ Platform::File::Handle::Invalid }; + Platform::File::ScopedHandle mFile; char mBuffer[8192]{ 0 }; }; } diff --git a/components/platform/file.hpp b/components/platform/file.hpp index d0d5eb0399..3456cdc113 100644 --- a/components/platform/file.hpp +++ b/components/platform/file.hpp @@ -29,7 +29,17 @@ namespace Platform::File { size_t read(Handle handle, void* data, size_t size); - + class ScopedHandle + { + Handle mHandle{ Handle::Invalid }; + + public: + ScopedHandle() = default; + ScopedHandle(Handle handle) : mHandle(handle) {} + ~ScopedHandle() { close(mHandle); } + + operator Handle() const { return mHandle; } + }; } #endif // OPENMW_COMPONENTS_PLATFORM_FILE_HPP