'dynamic' namespace refactor (step 2)
This commit is contained in:
parent
d33edd4cd9
commit
8e83a07094
@ -207,13 +207,13 @@ std::unique_ptr<Value> Parser::parseValue() {
|
||||
if (is_identifier_start(next)) {
|
||||
std::string literal = parseName();
|
||||
if (literal == "true") {
|
||||
return Value::boolean(true);
|
||||
return dynamic::value_of(true);
|
||||
} else if (literal == "false") {
|
||||
return Value::boolean(false);
|
||||
return dynamic::value_of(false);
|
||||
} else if (literal == "inf") {
|
||||
return Value::of(INFINITY);
|
||||
return dynamic::value_of(INFINITY);
|
||||
} else if (literal == "nan") {
|
||||
return Value::of(NAN);
|
||||
return dynamic::value_of(NAN);
|
||||
}
|
||||
throw error("invalid literal ");
|
||||
}
|
||||
|
||||
@ -47,7 +47,11 @@ class Reader : public BasicParser {
|
||||
number_u num;
|
||||
parseNumber(1, num);
|
||||
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 == '+') {
|
||||
int sign = c == '-' ? -1 : 1;
|
||||
@ -55,25 +59,29 @@ class Reader : public BasicParser {
|
||||
number_u num;
|
||||
parseNumber(sign, num);
|
||||
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)) {
|
||||
std::string identifier = parseName();
|
||||
if (handler.has(name)) {
|
||||
if (identifier == "true" || identifier == "false") {
|
||||
bool flag = identifier == "true";
|
||||
handler.setValue(name, *dynamic::Value::boolean(flag));
|
||||
handler.setValue(name, flag);
|
||||
} else if (identifier == "inf") {
|
||||
handler.setValue(name, *dynamic::Value::of(INFINITY));
|
||||
handler.setValue(name, INFINITY);
|
||||
} else if (identifier == "nan") {
|
||||
handler.setValue(name, *dynamic::Value::of(NAN));
|
||||
handler.setValue(name, NAN);
|
||||
}
|
||||
}
|
||||
} else if (c == '"' || c == '\'') {
|
||||
pos++;
|
||||
std::string str = parseString(c);
|
||||
if (handler.has(name)) {
|
||||
handler.setValue(name, *dynamic::Value::of(str));
|
||||
handler.setValue(name, str);
|
||||
}
|
||||
} else {
|
||||
throw error("feature is not supported");
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
std::unique_ptr<Value> Value::of(number_u 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) {
|
||||
std::unique_ptr<Value> dynamic::value_of(std::unique_ptr<Map> value) {
|
||||
return std::make_unique<Value>(value.release());
|
||||
}
|
||||
|
||||
@ -34,12 +34,9 @@ namespace dynamic {
|
||||
valvalue value;
|
||||
Value(valvalue 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 {
|
||||
public:
|
||||
@ -121,7 +118,7 @@ namespace dynamic {
|
||||
return put(key, std::make_unique<Value>(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);
|
||||
|
||||
@ -84,13 +84,13 @@ std::unique_ptr<dynamic::Value> SettingsHandler::getValue(const std::string& nam
|
||||
}
|
||||
auto setting = found->second;
|
||||
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)) {
|
||||
return dynamic::Value::of((integer_t)integer->get());
|
||||
return dynamic::value_of((integer_t)integer->get());
|
||||
} 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)) {
|
||||
return dynamic::Value::of(string->get());
|
||||
return dynamic::value_of(string->get());
|
||||
} else {
|
||||
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);
|
||||
if (found == map.end()) {
|
||||
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)) {
|
||||
set_numeric_value(flag, value);
|
||||
} 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));
|
||||
} 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));
|
||||
} else if (auto flag = std::get_if<bool>(&value.value)) {
|
||||
} else if (auto flag = std::get_if<bool>(&value)) {
|
||||
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);
|
||||
} else {
|
||||
throw std::runtime_error("not implemented for type");
|
||||
|
||||
@ -23,7 +23,7 @@ public:
|
||||
SettingsHandler(EngineSettings& settings);
|
||||
|
||||
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;
|
||||
Setting* getSetting(const std::string& name) const;
|
||||
bool has(const std::string& name) const;
|
||||
|
||||
@ -380,7 +380,7 @@ void Hud::closeInventory() {
|
||||
}
|
||||
|
||||
void Hud::add(HudElement element) {
|
||||
using dynamic::Value;
|
||||
using namespace dynamic;
|
||||
|
||||
gui->add(element.getNode());
|
||||
auto invview = std::dynamic_pointer_cast<InventoryView>(element.getNode());
|
||||
@ -388,9 +388,9 @@ void Hud::add(HudElement element) {
|
||||
if (document) {
|
||||
auto inventory = invview ? invview->getInventory() : nullptr;
|
||||
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++) {
|
||||
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(
|
||||
element.getDocument(),
|
||||
|
||||
@ -53,7 +53,7 @@ gui::page_loader_func menus::create_page_loader(Engine* engine) {
|
||||
auto value = parser.readUntil('&');
|
||||
map->put(key, value);
|
||||
}
|
||||
args.push_back(Value::of(std::move(map)));
|
||||
args.push_back(value_of(std::move(map)));
|
||||
} else {
|
||||
name = query;
|
||||
}
|
||||
@ -89,8 +89,8 @@ void menus::show_process_panel(Engine* engine, std::shared_ptr<Task> task, std::
|
||||
|
||||
auto menu = engine->getGUI()->getMenu();
|
||||
menu->reset();
|
||||
std::vector<std::unique_ptr<dynamic::Value>> args;
|
||||
args.emplace_back(Value::of(util::wstr2str_utf8(langs::get(text))));
|
||||
std::vector<std::unique_ptr<Value>> args;
|
||||
args.emplace_back(value_of(util::wstr2str_utf8(langs::get(text))));
|
||||
auto doc = menus::show(engine, "process", std::move(args));
|
||||
std::dynamic_pointer_cast<Container>(doc->getRoot())->listenInterval(0.01f, [=]() {
|
||||
task->update();
|
||||
|
||||
@ -311,18 +311,18 @@ std::unique_ptr<dynamic::Value> lua::LuaState::tovalue(int idx) {
|
||||
case LUA_TNONE:
|
||||
return std::make_unique<Value>(std::monostate());
|
||||
case LUA_TBOOLEAN:
|
||||
return Value::boolean(lua_toboolean(L, idx) == 1);
|
||||
return dynamic::value_of(lua_toboolean(L, idx) == 1);
|
||||
case LUA_TNUMBER: {
|
||||
auto number = lua_tonumber(L, idx);
|
||||
auto integer = lua_tointeger(L, idx);
|
||||
if (number == (lua_Number)integer) {
|
||||
return Value::of(integer);
|
||||
return dynamic::value_of(integer);
|
||||
} else {
|
||||
return Value::of(number);
|
||||
return dynamic::value_of(number);
|
||||
}
|
||||
}
|
||||
case LUA_TSTRING:
|
||||
return Value::of(lua_tostring(L, idx));
|
||||
return dynamic::value_of(lua_tostring(L, idx));
|
||||
case LUA_TTABLE: {
|
||||
int len = lua_objlen(L, idx);
|
||||
if (len) {
|
||||
|
||||
@ -117,7 +117,7 @@ static int l_get_setting(lua_State* L) {
|
||||
static int l_set_setting(lua_State* L) {
|
||||
auto name = lua_tostring(L, 1);
|
||||
const auto value = scripting::state->tovalue(2);
|
||||
scripting::engine->getSettingsHandler().setValue(name, *value);
|
||||
scripting::engine->getSettingsHandler().setValue(name, value->value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user