feat: add smart FILE pointer.
- use std::unique_ptr and custom deleter to implement smart FILE pointer for convenient auto free.
This commit is contained in:
@ -49,7 +49,7 @@ namespace YYCC::ConfigManager {
|
||||
Reset();
|
||||
|
||||
// get file handle
|
||||
auto fs = this->GetFileHandle(YYCC_U8("rb"));
|
||||
IOHelper::SmartStdFile fs(IOHelper::UTF8FOpen(m_CfgFilePath.c_str(), YYCC_U8("rb")));
|
||||
if (fs.get() == nullptr) {
|
||||
// if we fail to get, it means that we do not have corresponding cfg file.
|
||||
// all settings should be reset to default value.
|
||||
@ -112,7 +112,7 @@ namespace YYCC::ConfigManager {
|
||||
|
||||
bool CoreManager::Save() {
|
||||
// get file handle
|
||||
auto fs = this->GetFileHandle(YYCC_U8("wb"));
|
||||
IOHelper::SmartStdFile fs(IOHelper::UTF8FOpen(m_CfgFilePath.c_str(), YYCC_U8("wb")));
|
||||
// if we fail to get, return false.
|
||||
if (fs == nullptr) return false;
|
||||
|
||||
@ -157,15 +157,6 @@ namespace YYCC::ConfigManager {
|
||||
}
|
||||
}
|
||||
|
||||
CoreManager::FileHandleGuard_t CoreManager::GetFileHandle(const yycc_char8_t* mode) const {
|
||||
return CoreManager::FileHandleGuard_t(
|
||||
IOHelper::UTF8FOpen(this->m_CfgFilePath.c_str(), mode),
|
||||
[](FILE* fs) -> void {
|
||||
if (fs != nullptr) std::fclose(fs);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
}
|
||||
|
@ -103,9 +103,6 @@ namespace YYCC::ConfigManager {
|
||||
void Reset();
|
||||
|
||||
private:
|
||||
using FileHandleGuard_t = std::unique_ptr<FILE, std::function<void(FILE*)>>;
|
||||
FileHandleGuard_t GetFileHandle(const yycc_char8_t* mode) const;
|
||||
|
||||
yycc_u8string m_CfgFilePath;
|
||||
uint64_t m_VersionIdentifier;
|
||||
std::map<yycc_u8string, AbstractSetting*> m_Settings;
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <memory>
|
||||
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
#include "WinImportPrefix.hpp"
|
||||
@ -13,8 +14,8 @@
|
||||
#endif
|
||||
|
||||
namespace YYCC::IOHelper {
|
||||
|
||||
FILE* UTF8FOpen(const yycc_char8_t* u8_filepath, const yycc_char8_t* u8_mode) {
|
||||
|
||||
std::FILE* UTF8FOpen(const yycc_char8_t* u8_filepath, const yycc_char8_t* u8_mode) {
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
|
||||
// convert mode and file path to wchar
|
||||
|
@ -27,6 +27,19 @@ namespace YYCC::IOHelper {
|
||||
#else
|
||||
#error "Not supported pointer size."
|
||||
#endif
|
||||
|
||||
/// @brief C++ standard deleter for std::FILE*
|
||||
class StdFileDeleter {
|
||||
public:
|
||||
StdFileDeleter() {}
|
||||
void operator() (std::FILE* ptr) {
|
||||
if (ptr != nullptr) {
|
||||
std::fclose(ptr);
|
||||
}
|
||||
}
|
||||
};
|
||||
/// @brief Smart unique pointer of \c std::FILE*
|
||||
using SmartStdFile = std::unique_ptr<std::FILE, StdFileDeleter>;
|
||||
|
||||
/**
|
||||
* @brief The UTF8 version of \c std::fopen.
|
||||
@ -37,6 +50,6 @@ namespace YYCC::IOHelper {
|
||||
* On other platforms, this function will delegate request directly to std::fopen.
|
||||
* @return \c FILE* of the file to be opened, or nullptr if failed.
|
||||
*/
|
||||
FILE* UTF8FOpen(const yycc_char8_t* u8_filepath, const yycc_char8_t* u8_mode);
|
||||
std::FILE* UTF8FOpen(const yycc_char8_t* u8_filepath, const yycc_char8_t* u8_mode);
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user