'dynamic' namespace refactor (step 2)

This commit is contained in:
MihailRis
2024-05-07 17:03:04 +03:00
parent d33edd4cd9
commit 8e83a07094
10 changed files with 44 additions and 51 deletions
+4 -4
View File
@@ -207,13 +207,13 @@ std::unique_ptr<Value> Parser::parseValue() {
if (is_identifier_start(next)) { if (is_identifier_start(next)) {
std::string literal = parseName(); std::string literal = parseName();
if (literal == "true") { if (literal == "true") {
return Value::boolean(true); return dynamic::value_of(true);
} else if (literal == "false") { } else if (literal == "false") {
return Value::boolean(false); return dynamic::value_of(false);
} else if (literal == "inf") { } else if (literal == "inf") {
return Value::of(INFINITY); return dynamic::value_of(INFINITY);
} else if (literal == "nan") { } else if (literal == "nan") {
return Value::of(NAN); return dynamic::value_of(NAN);
} }
throw error("invalid literal "); throw error("invalid literal ");
} }
+14 -6
View File
@@ -47,7 +47,11 @@ class Reader : public BasicParser {
number_u num; number_u num;
parseNumber(1, num); parseNumber(1, num);
if (handler.has(name)) { if (handler.has(name)) {
handler.setValue(name, *dynamic::Value::of(num)); if (std::holds_alternative<integer_t>(num)) {
handler.setValue(name, std::get<integer_t>(num));
} else {
handler.setValue(name, std::get<number_t>(num));
}
} }
} else if (c == '-' || c == '+') { } else if (c == '-' || c == '+') {
int sign = c == '-' ? -1 : 1; int sign = c == '-' ? -1 : 1;
@@ -55,25 +59,29 @@ class Reader : public BasicParser {
number_u num; number_u num;
parseNumber(sign, num); parseNumber(sign, num);
if (handler.has(name)) { if (handler.has(name)) {
handler.setValue(name, *dynamic::Value::of(num)); if (std::holds_alternative<integer_t>(num)) {
handler.setValue(name, std::get<integer_t>(num));
} else {
handler.setValue(name, std::get<number_t>(num));
}
} }
} else if (is_identifier_start(c)) { } else if (is_identifier_start(c)) {
std::string identifier = parseName(); std::string identifier = parseName();
if (handler.has(name)) { if (handler.has(name)) {
if (identifier == "true" || identifier == "false") { if (identifier == "true" || identifier == "false") {
bool flag = identifier == "true"; bool flag = identifier == "true";
handler.setValue(name, *dynamic::Value::boolean(flag)); handler.setValue(name, flag);
} else if (identifier == "inf") { } else if (identifier == "inf") {
handler.setValue(name, *dynamic::Value::of(INFINITY)); handler.setValue(name, INFINITY);
} else if (identifier == "nan") { } else if (identifier == "nan") {
handler.setValue(name, *dynamic::Value::of(NAN)); handler.setValue(name, NAN);
} }
} }
} else if (c == '"' || c == '\'') { } else if (c == '"' || c == '\'') {
pos++; pos++;
std::string str = parseString(c); std::string str = parseString(c);
if (handler.has(name)) { if (handler.has(name)) {
handler.setValue(name, *dynamic::Value::of(str)); handler.setValue(name, str);
} }
} else { } else {
throw error("feature is not supported"); throw error("feature is not supported");
+2 -14
View File
@@ -246,22 +246,10 @@ Value::~Value() {
} }
} }
std::unique_ptr<Value> Value::boolean(bool value) { std::unique_ptr<Value> dynamic::value_of(const valvalue& value) {
return std::make_unique<Value>(value); return std::make_unique<Value>(value);
} }
std::unique_ptr<Value> Value::of(number_u value) { std::unique_ptr<Value> dynamic::value_of(std::unique_ptr<Map> value) {
if (std::holds_alternative<integer_t>(value)) {
return std::make_unique<Value>(std::get<integer_t>(value));
} else {
return std::make_unique<Value>(std::get<number_t>(value));
}
}
std::unique_ptr<Value> Value::of(const std::string& value) {
return std::make_unique<Value>(value);
}
std::unique_ptr<Value> Value::of(std::unique_ptr<Map> value) {
return std::make_unique<Value>(value.release()); return std::make_unique<Value>(value.release());
} }
+3 -6
View File
@@ -34,12 +34,9 @@ namespace dynamic {
valvalue value; valvalue value;
Value(valvalue value); Value(valvalue value);
~Value(); ~Value();
static std::unique_ptr<Value> boolean(bool value);
static std::unique_ptr<Value> of(number_u value);
static std::unique_ptr<Value> of(const std::string& value);
static std::unique_ptr<Value> of(std::unique_ptr<Map> value);
}; };
std::unique_ptr<Value> value_of(const valvalue& value);
std::unique_ptr<Value> value_of(std::unique_ptr<Map> value);
class List { class List {
public: public:
@@ -121,7 +118,7 @@ namespace dynamic {
return put(key, std::make_unique<Value>(value)); return put(key, std::make_unique<Value>(value));
} }
Map& put(std::string key, uint64_t value) { Map& put(std::string key, uint64_t value) {
return put(key, Value::of(static_cast<integer_t>(value))); return put(key, value_of(static_cast<integer_t>(value)));
} }
Map& put(std::string key, std::unique_ptr<Value> value); Map& put(std::string key, std::unique_ptr<Value> value);
+9 -9
View File
@@ -84,13 +84,13 @@ std::unique_ptr<dynamic::Value> SettingsHandler::getValue(const std::string& nam
} }
auto setting = found->second; auto setting = found->second;
if (auto number = dynamic_cast<NumberSetting*>(setting)) { if (auto number = dynamic_cast<NumberSetting*>(setting)) {
return dynamic::Value::of((number_t)number->get()); return dynamic::value_of((number_t)number->get());
} else if (auto integer = dynamic_cast<IntegerSetting*>(setting)) { } else if (auto integer = dynamic_cast<IntegerSetting*>(setting)) {
return dynamic::Value::of((integer_t)integer->get()); return dynamic::value_of((integer_t)integer->get());
} else if (auto flag = dynamic_cast<FlagSetting*>(setting)) { } else if (auto flag = dynamic_cast<FlagSetting*>(setting)) {
return dynamic::Value::boolean(flag->get()); return dynamic::value_of(flag->get());
} else if (auto string = dynamic_cast<StringSetting*>(setting)) { } else if (auto string = dynamic_cast<StringSetting*>(setting)) {
return dynamic::Value::of(string->get()); return dynamic::value_of(string->get());
} else { } else {
throw std::runtime_error("type is not implemented for '"+name+"'"); throw std::runtime_error("type is not implemented for '"+name+"'");
} }
@@ -130,7 +130,7 @@ static void set_numeric_value(T* setting, const dynamic::Value& value) {
} }
} }
void SettingsHandler::setValue(const std::string& name, const dynamic::Value& value) { void SettingsHandler::setValue(const std::string& name, const dynamic::valvalue& value) {
auto found = map.find(name); auto found = map.find(name);
if (found == map.end()) { if (found == map.end()) {
throw std::runtime_error("setting '"+name+"' does not exist"); throw std::runtime_error("setting '"+name+"' does not exist");
@@ -143,13 +143,13 @@ void SettingsHandler::setValue(const std::string& name, const dynamic::Value& va
} else if (auto flag = dynamic_cast<FlagSetting*>(setting)) { } else if (auto flag = dynamic_cast<FlagSetting*>(setting)) {
set_numeric_value(flag, value); set_numeric_value(flag, value);
} else if (auto string = dynamic_cast<StringSetting*>(setting)) { } else if (auto string = dynamic_cast<StringSetting*>(setting)) {
if (auto num = std::get_if<integer_t>(&value.value)) { if (auto num = std::get_if<integer_t>(&value)) {
string->set(std::to_string(*num)); string->set(std::to_string(*num));
} else if (auto num = std::get_if<number_t>(&value.value)) { } else if (auto num = std::get_if<number_t>(&value)) {
string->set(std::to_string(*num)); string->set(std::to_string(*num));
} else if (auto flag = std::get_if<bool>(&value.value)) { } else if (auto flag = std::get_if<bool>(&value)) {
string->set(*flag ? "true" : "false"); string->set(*flag ? "true" : "false");
} else if (auto str = std::get_if<std::string>(&value.value)) { } else if (auto str = std::get_if<std::string>(&value)) {
string->set(*str); string->set(*str);
} else { } else {
throw std::runtime_error("not implemented for type"); throw std::runtime_error("not implemented for type");
+1 -1
View File
@@ -23,7 +23,7 @@ public:
SettingsHandler(EngineSettings& settings); SettingsHandler(EngineSettings& settings);
std::unique_ptr<dynamic::Value> getValue(const std::string& name) const; std::unique_ptr<dynamic::Value> getValue(const std::string& name) const;
void setValue(const std::string& name, const dynamic::Value& value); void setValue(const std::string& name, const dynamic::valvalue& value);
std::string toString(const std::string& name) const; std::string toString(const std::string& name) const;
Setting* getSetting(const std::string& name) const; Setting* getSetting(const std::string& name) const;
bool has(const std::string& name) const; bool has(const std::string& name) const;
+3 -3
View File
@@ -380,7 +380,7 @@ void Hud::closeInventory() {
} }
void Hud::add(HudElement element) { void Hud::add(HudElement element) {
using dynamic::Value; using namespace dynamic;
gui->add(element.getNode()); gui->add(element.getNode());
auto invview = std::dynamic_pointer_cast<InventoryView>(element.getNode()); auto invview = std::dynamic_pointer_cast<InventoryView>(element.getNode());
@@ -388,9 +388,9 @@ void Hud::add(HudElement element) {
if (document) { if (document) {
auto inventory = invview ? invview->getInventory() : nullptr; auto inventory = invview ? invview->getInventory() : nullptr;
std::vector<std::unique_ptr<Value>> args; std::vector<std::unique_ptr<Value>> args;
args.push_back(Value::of(inventory ? inventory.get()->getId() : 0)); args.push_back(value_of(inventory ? inventory.get()->getId() : 0));
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
args.push_back(Value::of(static_cast<integer_t>(blockPos[i]))); args.push_back(value_of(static_cast<integer_t>(blockPos[i])));
} }
scripting::on_ui_open( scripting::on_ui_open(
element.getDocument(), element.getDocument(),
+3 -3
View File
@@ -53,7 +53,7 @@ gui::page_loader_func menus::create_page_loader(Engine* engine) {
auto value = parser.readUntil('&'); auto value = parser.readUntil('&');
map->put(key, value); map->put(key, value);
} }
args.push_back(Value::of(std::move(map))); args.push_back(value_of(std::move(map)));
} else { } else {
name = query; name = query;
} }
@@ -89,8 +89,8 @@ void menus::show_process_panel(Engine* engine, std::shared_ptr<Task> task, std::
auto menu = engine->getGUI()->getMenu(); auto menu = engine->getGUI()->getMenu();
menu->reset(); menu->reset();
std::vector<std::unique_ptr<dynamic::Value>> args; std::vector<std::unique_ptr<Value>> args;
args.emplace_back(Value::of(util::wstr2str_utf8(langs::get(text)))); args.emplace_back(value_of(util::wstr2str_utf8(langs::get(text))));
auto doc = menus::show(engine, "process", std::move(args)); auto doc = menus::show(engine, "process", std::move(args));
std::dynamic_pointer_cast<Container>(doc->getRoot())->listenInterval(0.01f, [=]() { std::dynamic_pointer_cast<Container>(doc->getRoot())->listenInterval(0.01f, [=]() {
task->update(); task->update();
+4 -4
View File
@@ -311,18 +311,18 @@ std::unique_ptr<dynamic::Value> lua::LuaState::tovalue(int idx) {
case LUA_TNONE: case LUA_TNONE:
return std::make_unique<Value>(std::monostate()); return std::make_unique<Value>(std::monostate());
case LUA_TBOOLEAN: case LUA_TBOOLEAN:
return Value::boolean(lua_toboolean(L, idx) == 1); return dynamic::value_of(lua_toboolean(L, idx) == 1);
case LUA_TNUMBER: { case LUA_TNUMBER: {
auto number = lua_tonumber(L, idx); auto number = lua_tonumber(L, idx);
auto integer = lua_tointeger(L, idx); auto integer = lua_tointeger(L, idx);
if (number == (lua_Number)integer) { if (number == (lua_Number)integer) {
return Value::of(integer); return dynamic::value_of(integer);
} else { } else {
return Value::of(number); return dynamic::value_of(number);
} }
} }
case LUA_TSTRING: case LUA_TSTRING:
return Value::of(lua_tostring(L, idx)); return dynamic::value_of(lua_tostring(L, idx));
case LUA_TTABLE: { case LUA_TTABLE: {
int len = lua_objlen(L, idx); int len = lua_objlen(L, idx);
if (len) { if (len) {
+1 -1
View File
@@ -117,7 +117,7 @@ static int l_get_setting(lua_State* L) {
static int l_set_setting(lua_State* L) { static int l_set_setting(lua_State* L) {
auto name = lua_tostring(L, 1); auto name = lua_tostring(L, 1);
const auto value = scripting::state->tovalue(2); const auto value = scripting::state->tovalue(2);
scripting::engine->getSettingsHandler().setValue(name, *value); scripting::engine->getSettingsHandler().setValue(name, value->value);
return 0; return 0;
} }