Experiments in using Lua with sol2 and how best to structure the data. Contains a shoot out of SOA, vs AOS vs. MOA. SOA = Struct of Arrays AOS = Array of Structs MOA = Many Array of Structs (kind of a hybrid)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sol2_test/src/guecs/sfml/config.cpp

47 lines
1.3 KiB

#include "guecs/sfml/config.hpp"
#include <fmt/core.h>
using nlohmann::json;
using fmt::format;
std::filesystem::path Config::BASE_DIR{"."};
Config::Config(const std::string src_path) : $src_path(src_path) {
std::ifstream infile(Config::path_to($src_path));
$config = json::parse(infile);
}
json &Config::operator[](const std::string &key) {
assert($config.contains(key) &&
"ERROR in config, key doesn't exist.");
return $config[key];
}
std::wstring Config::wstring(const std::string main_key, const std::string sub_key) {
assert($config.contains(main_key) && "ERROR wstring main/key in config, main_key doesn't exist.");
assert($config[main_key].contains(sub_key) && "ERROR wstring in config, main_key/key doesn't exist.");
const std::string& str_val = $config[main_key][sub_key];
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
return $converter.from_bytes(str_val);
}
std::vector<std::string> Config::keys() {
std::vector<std::string> the_fucking_keys;
for(auto& [key, value] : $config.items()) {
the_fucking_keys.push_back(key);
}
return the_fucking_keys;
}
void Config::set_base_dir(const char *optarg) {
Config::BASE_DIR.assign(optarg);
}
std::filesystem::path Config::path_to(const std::string& path) {
return Config::BASE_DIR / path;
}