dynamic::Value simplified

This commit is contained in:
MihailRis
2024-05-07 18:39:12 +03:00
parent 8e83a07094
commit a4c21984d5
26 changed files with 230 additions and 297 deletions
+2 -2
View File
@@ -134,7 +134,7 @@ static void erase_pack_indices(dynamic::Map* root, const std::string& id) {
if (name.find(prefix) != 0)
continue;
auto value = blocks->getValueWriteable(i);
value->value = CORE_AIR;
*value = CORE_AIR;
}
auto items = root->list("items");
@@ -143,7 +143,7 @@ static void erase_pack_indices(dynamic::Map* root, const std::string& id) {
if (name.find(prefix) != 0)
continue;
auto value = items->getValueWriteable(i);
value->value = CORE_EMPTY;
*value = CORE_EMPTY;
}
}
+3 -5
View File
@@ -104,7 +104,7 @@ bool files::write_binary_json(fs::path filename, const dynamic::Map* obj, bool c
return files::write_bytes(filename, bytes.data(), bytes.size());
}
std::unique_ptr<dynamic::Map> files::read_json(fs::path filename) {
std::shared_ptr<dynamic::Map> files::read_json(fs::path filename) {
std::string text = files::read_string(filename);
try {
auto obj = json::parse(filename.string(), text);
@@ -115,12 +115,10 @@ std::unique_ptr<dynamic::Map> files::read_json(fs::path filename) {
}
}
std::unique_ptr<dynamic::Map> files::read_binary_json(fs::path file) {
std::shared_ptr<dynamic::Map> files::read_binary_json(fs::path file) {
size_t size;
std::unique_ptr<ubyte[]> bytes (files::read_bytes(file, size));
return std::unique_ptr<dynamic::Map>(
json::from_binary(bytes.get(), size)
);
return json::from_binary(bytes.get(), size);
}
std::vector<std::string> files::read_list(fs::path filename) {
+2 -2
View File
@@ -64,8 +64,8 @@ namespace files {
/// @brief Read JSON or BJSON file
/// @param file *.json or *.bjson file
std::unique_ptr<dynamic::Map> read_json(fs::path file);
std::unique_ptr<dynamic::Map> read_binary_json(fs::path file);
std::shared_ptr<dynamic::Map> read_json(fs::path file);
std::shared_ptr<dynamic::Map> read_binary_json(fs::path file);
std::vector<std::string> read_list(fs::path file);
}
+9 -9
View File
@@ -77,20 +77,20 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) {
builder.add("do-write-lights", &settings.debug.doWriteLights);
}
std::unique_ptr<dynamic::Value> SettingsHandler::getValue(const std::string& name) const {
dynamic::Value SettingsHandler::getValue(const std::string& name) const {
auto found = map.find(name);
if (found == map.end()) {
throw std::runtime_error("setting '"+name+"' does not exist");
}
auto setting = found->second;
if (auto number = dynamic_cast<NumberSetting*>(setting)) {
return dynamic::value_of((number_t)number->get());
return static_cast<number_t>(number->get());
} else if (auto integer = dynamic_cast<IntegerSetting*>(setting)) {
return dynamic::value_of((integer_t)integer->get());
return static_cast<integer_t>(integer->get());
} else if (auto flag = dynamic_cast<FlagSetting*>(setting)) {
return dynamic::value_of(flag->get());
return flag->get();
} else if (auto string = dynamic_cast<StringSetting*>(setting)) {
return dynamic::value_of(string->get());
return string->get();
} else {
throw std::runtime_error("type is not implemented for '"+name+"'");
}
@@ -119,18 +119,18 @@ bool SettingsHandler::has(const std::string& name) const {
template<class T>
static void set_numeric_value(T* setting, const dynamic::Value& value) {
if (auto num = std::get_if<integer_t>(&value.value)) {
if (auto num = std::get_if<integer_t>(&value)) {
setting->set(*num);
} else if (auto num = std::get_if<number_t>(&value.value)) {
} else if (auto num = std::get_if<number_t>(&value)) {
setting->set(*num);
} else if (auto flag = std::get_if<bool>(&value.value)) {
} else if (auto flag = std::get_if<bool>(&value)) {
setting->set(*flag);
} else {
throw std::runtime_error("type error, numeric value expected");
}
}
void SettingsHandler::setValue(const std::string& name, const dynamic::valvalue& value) {
void SettingsHandler::setValue(const std::string& name, const dynamic::Value& value) {
auto found = map.find(name);
if (found == map.end()) {
throw std::runtime_error("setting '"+name+"' does not exist");
+2 -2
View File
@@ -22,8 +22,8 @@ class SettingsHandler {
public:
SettingsHandler(EngineSettings& settings);
std::unique_ptr<dynamic::Value> getValue(const std::string& name) const;
void setValue(const std::string& name, const dynamic::valvalue& value);
dynamic::Value getValue(const std::string& name) const;
void setValue(const std::string& name, const dynamic::Value& value);
std::string toString(const std::string& name) const;
Setting* getSetting(const std::string& name) const;
bool has(const std::string& name) const;