mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-18 19:23:56 +00:00
75 lines
1017 B
C++
75 lines
1017 B
C++
#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_ = {};
|
|
}
|
|
}
|
|
};
|
|
}
|