Basic working file writing

This commit is contained in:
momo5502
2024-10-22 20:09:33 +02:00
parent 4b2adb0d63
commit af529a62d7
5 changed files with 197 additions and 73 deletions

View File

@@ -0,0 +1,74 @@
#pragma once
#include <cstdio>
#include <type_traits>
namespace utils
{
class file_handle
{
public:
file_handle() = default;
file_handle(FILE* file)
: file_(file)
{
}
~file_handle()
{
this->release();
}
file_handle(const file_handle&) = delete;
file_handle& operator=(const file_handle&) = delete;
file_handle(file_handle&& obj) noexcept
: file_handle()
{
this->operator=(std::move(obj));
}
file_handle& operator=(file_handle&& obj) noexcept
{
if (this != &obj)
{
this->release();
this->file_ = obj.file_;
obj.file_ = {};
}
return *this;
}
file_handle& operator=(FILE* file) noexcept
{
this->release();
this->file_ = file;
return *this;
}
[[nodiscard]] operator bool() const
{
return this->file_;
}
[[nodiscard]] operator FILE*() const
{
return this->file_;
}
private:
FILE* file_{};
void release()
{
if (this->file_)
{
(void)fclose(this->file_);
this->file_ = {};
}
}
};
}