From 9dcf7e7f08c045e5f1272647567348e73fb758ea Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 21 Aug 2019 19:28:10 -0400 Subject: [PATCH] hecl/hecl: Add smart pointer variant of Fopen Provides a wrapper API over Fopen in order to prevent resource leaks. --- hecl/include/hecl/hecl.hpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/hecl/include/hecl/hecl.hpp b/hecl/include/hecl/hecl.hpp index f3e9fdb58..e679699af 100644 --- a/hecl/include/hecl/hecl.hpp +++ b/hecl/include/hecl/hecl.hpp @@ -275,6 +275,16 @@ inline FILE* Fopen(const SystemChar* path, const SystemChar* mode, FileLockType return fp; } +struct UniqueFileDeleter { + void operator()(FILE* file) const noexcept { std::fclose(file); } +}; +using UniqueFilePtr = std::unique_ptr; + +inline UniqueFilePtr FopenUnique(const SystemChar* path, const SystemChar* mode, + FileLockType lock = FileLockType::None) { + return UniqueFilePtr{Fopen(path, mode, lock)}; +} + inline int FSeek(FILE* fp, int64_t offset, int whence) { #if _WIN32 return _fseeki64(fp, offset, whence);