1 #ifndef INCLUDE_AL_TOML_HPP
2 #define INCLUDE_AL_TOML_HPP
11 #include "al/io/al_File.hpp"
21 std::shared_ptr<cpptoml::table> root;
22 std::string mFilename;
26 TomlLoader(
const std::string& filename) { setFile(filename); }
28 void setFile(
const std::string& filename) {
30 std::cout <<
"TomlLoader creating config file: " << filename << std::endl;
32 if (configFile.
open(filename,
"w")) {
35 std::cout <<
"ERROR: Unable to create config file: " << filename
40 root = cpptoml::parse_file(filename);
50 if (configFile.
open()) {
51 std::ostringstream ss;
53 configFile.
write(ss.str());
56 std::cout <<
"ERROR: Opening toml file '" << filename <<
"' for writing."
62 void setDefaultValue(
const std::string& keyName,
const T& value) {
64 throw std::runtime_error(
65 "al::TomlLoader::setDefaultValue, toml file not set");
67 auto val = root->get_as<T>(keyName);
69 root->insert(keyName, value);
73 void setDefaultValueVector(
const std::string& keyName,
74 const std::vector<T>& value) {
76 throw std::runtime_error(
77 "al::TomlLoader::setDefaultValue, toml file not set");
79 auto val = root->get_array_of<T>(keyName);
81 auto array = cpptoml::make_array();
82 for (
auto& elem : value) {
83 array->push_back(elem);
85 root->insert(keyName, array);
90 bool hasKey(
const std::string& keyName) {
92 auto val = root->get_as<T>(keyName);
100 template <
typename T>
101 void set(
const std::string& key,
const T& value) {
103 throw std::runtime_error(
"al::TomlLoader::get, toml file not set");
105 root->insert(key, value);
112 template <
typename T>
113 T get(
const std::string& key) {
115 throw std::runtime_error(
"al::TomlLoader::get, toml file not set");
117 auto val = root->get_as<T>(key);
119 throw std::runtime_error(std::string(
"al::TomlLoader::get, key '") + key +
120 std::string(
"' not found"));
127 template <
typename T>
128 T get(
const std::string& table_key,
const std::string& key) {
130 throw std::runtime_error(
"al::TomlLoader::get, toml file not set");
132 auto table = root->get_table(table_key);
134 throw std::runtime_error(std::string(
"al::TomlLoader::get, table '") +
135 table_key + std::string(
"' not found"));
137 auto val = table->get_as<T>(key);
139 throw std::runtime_error(std::string(
"al::TomlLoader::get, key '") + key +
140 std::string(
"' not found"));
145 double getd(
const std::string& key) {
return get<double>(key); }
147 int64_t geti(
const std::string& key) {
return get<int64_t>(key); }
149 std::string gets(
const std::string& key) {
return get<std::string>(key); }
151 bool getb(
const std::string& key) {
return get<bool>(key); }
153 double getd(
const std::string& table,
const std::string& key) {
154 return get<double>(table, key);
157 int64_t geti(
const std::string& table,
const std::string& key) {
158 return get<int64_t>(table, key);
161 std::string gets(
const std::string& table,
const std::string& key) {
162 return get<std::string>(table, key);
165 bool getb(
const std::string& table,
const std::string& key) {
166 return get<bool>(table, key);
169 template <
typename T>
170 std::vector<T> getVector(
const std::string& key) {
172 throw std::runtime_error(
"al::TomlLoader::get, toml file not set");
174 auto vals = root->get_array_of<T>(key);
175 std::vector<T> outVector;
176 for (
const T& val : *vals) {
177 outVector.push_back(val);
182 template <
typename T>
183 void setVector(
const std::string& key,
const std::vector<T>& vec) {
185 throw std::runtime_error(
"al::TomlLoader::get, toml file not set");
187 auto array = cpptoml::make_array();
188 for (
auto& elem : vec) {
189 array->push_back(elem);
191 root->insert(key, array);
196 void setFile(
const std::string& file);
197 double getd(
const std::string& key);
198 int64_t geti(
const std::string& key);
199 std::string gets(
const std::string& key);
200 bool getb(
const std::string& key);
201 double getd(
const std::string& table,
const std::string& key);
202 int64_t geti(
const std::string& table,
const std::string& key);
203 std::string gets(
const std::string& table,
const std::string& key);
204 bool getb(
const std::string& table,
const std::string& key);
static bool exists(const std::string &path)
Returns whether a file or directory exists.
int write(const std::string &v)
Write string to file.
bool open(const std::string &path, const std::string &mode="r")
Open file.
void writeFile(std::string filename="")
Updates file on disk.