mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-19 19:53:56 +00:00
Basic working file writing
This commit is contained in:
74
src/common/utils/file_handle.hpp
Normal file
74
src/common/utils/file_handle.hpp
Normal 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_ = {};
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user