44 lines
991 B
C++
44 lines
991 B
C++
#pragma once
|
|
|
|
#include "./utils/logger.hpp"
|
|
#include <fstream>
|
|
#include "./utils/toml.hpp"
|
|
|
|
namespace config {
|
|
|
|
constexpr auto DEFAULT_FILE = "config.toml";
|
|
|
|
/// Standalone options
|
|
template<typename O>
|
|
struct options_part {
|
|
public:
|
|
/// Load from path
|
|
options_part(const std::string &path): path(path) {
|
|
auto config = toml::table({{"c", [&] {
|
|
try {
|
|
return toml::parse_file(path);
|
|
} catch(const toml::parse_error& e) {
|
|
LOG_E("Failed to read config at " << path << ". " << e.description());
|
|
return toml::table();
|
|
}
|
|
}()}});
|
|
val = new O(config["c"]);
|
|
}
|
|
/// Write to path
|
|
void save() {
|
|
std::ofstream out;
|
|
out.open(path, std::ios::out | std::ios::trunc);
|
|
out << val->save() << "\n\n";
|
|
out.close();
|
|
}
|
|
~options_part() {
|
|
delete val;
|
|
}
|
|
|
|
O &get() { return *val; }
|
|
|
|
private:
|
|
std::string path;
|
|
owner<O*> val;
|
|
};
|
|
} |