From 43da3e7693038971fe4eea7d5228cdb400099394 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 19 Nov 2024 13:56:57 +0000 Subject: [PATCH] Codechange: Use std::visit for ScriptDataVariant. --- src/script/script_instance.cpp | 74 ++++++++++++++++------------------ 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/src/script/script_instance.cpp b/src/script/script_instance.cpp index e26d29cdf6..91a15acb6e 100644 --- a/src/script/script_instance.cpp +++ b/src/script/script_instance.cpp @@ -613,52 +613,46 @@ bool ScriptInstance::IsPaused() ScriptDataVariant value = data->front(); data->pop_front(); - if (std::holds_alternative(value)) { - sq_pushinteger(vm, std::get(value)); - return true; - } + struct visitor { + HSQUIRRELVM vm; + ScriptData *data; - if (std::holds_alternative(value)) { - sq_pushstring(vm, std::get(value), -1); - return true; - } + bool operator()(const SQInteger &value) { sq_pushinteger(this->vm, value); return true; } + bool operator()(const std::string &value) { sq_pushstring(this->vm, value, -1); return true; } + bool operator()(const SQBool &value) { sq_pushbool(this->vm, value); return true; } + bool operator()(const SQSaveLoadType &type) + { + switch (type) { + case SQSL_ARRAY: + sq_newarray(this->vm, 0); + while (LoadObjects(this->vm, this->data)) { + sq_arrayappend(this->vm, -2); + /* The value is popped from the stack by squirrel. */ + } + return true; - if (std::holds_alternative(value)) { - sq_pushbool(vm, std::get(value)); - return true; - } + case SQSL_TABLE: + sq_newtable(this->vm); + while (LoadObjects(this->vm, this->data)) { + LoadObjects(this->vm, this->data); + sq_rawset(this->vm, -3); + /* The key (-2) and value (-1) are popped from the stack by squirrel. */ + } + return true; - switch (std::get(value)) { - case SQSL_ARRAY: { - sq_newarray(vm, 0); - while (LoadObjects(vm, data)) { - sq_arrayappend(vm, -2); - /* The value is popped from the stack by squirrel. */ + case SQSL_NULL: + sq_pushnull(this->vm); + return true; + + case SQSL_ARRAY_TABLE_END: + return false; + + default: NOT_REACHED(); } - return true; } + }; - case SQSL_TABLE: { - sq_newtable(vm); - while (LoadObjects(vm, data)) { - LoadObjects(vm, data); - sq_rawset(vm, -3); - /* The key (-2) and value (-1) are popped from the stack by squirrel. */ - } - return true; - } - - case SQSL_NULL: { - sq_pushnull(vm); - return true; - } - - case SQSL_ARRAY_TABLE_END: { - return false; - } - - default: NOT_REACHED(); - } + return std::visit(visitor{vm, data}, value); } /* static */ void ScriptInstance::LoadEmpty()