diff --git a/src/logic/scripting/scripting_functional.cpp b/src/logic/scripting/scripting_functional.cpp index 554bfffe..a6ef42a4 100644 --- a/src/logic/scripting/scripting_functional.cpp +++ b/src/logic/scripting/scripting_functional.cpp @@ -3,6 +3,7 @@ #include "coders/json.hpp" #include "debug/Logger.hpp" #include "util/stringutil.hpp" +#include "util/type_helpers.hpp" #include "lua/lua_engine.hpp" using namespace scripting; @@ -58,29 +59,44 @@ key_handler scripting::create_key_handler( }; } -wstringconsumer scripting::create_wstring_consumer( +template)> +std::function create_consumer( const scriptenv& env, const std::string& src, const std::string& file ) { - return [=](const std::wstring& x) { + return [=](const T& x) { if (auto L = process_callback(env, src, file)) { - lua::pushwstring(L, x); + pushfunc(L, x); lua::call_nothrow(L, 1); } }; } +wstringconsumer scripting::create_wstring_consumer( + const scriptenv& env, const std::string& src, const std::string& file +) { + return create_consumer(env, src, file); +} + stringconsumer scripting::create_string_consumer( const scriptenv& env, const std::string& src, const std::string& file ) { - return [=](const std::string& x) { - if (auto L = process_callback(env, src, file)) { - lua::pushstring(L, x); - lua::call_nothrow(L, 1); - } - }; + return create_consumer(env, src, file); } -wstringsupplier scripting::create_wstring_supplier( +boolconsumer scripting::create_bool_consumer( + const scriptenv& env, const std::string& src, const std::string& file +) { + return create_consumer(env, src, file); +} + +doubleconsumer scripting::create_number_consumer( + const scriptenv& env, const std::string& src, const std::string& file +) { + return create_consumer(env, src, file); +} + +template +std::function create_supplier( const scriptenv& env, const std::string& src, const std::string& file ) { return [=]() { @@ -88,14 +104,32 @@ wstringsupplier scripting::create_wstring_supplier( if (lua::isfunction(L, -1)) { lua::call_nothrow(L, 0); } - auto str = lua::require_wstring(L, -1); + auto str = tovalueFunc(L, -1); lua::pop(L); return str; } - return std::wstring(); + return T {}; }; } +wstringsupplier scripting::create_wstring_supplier( + const scriptenv& env, const std::string& src, const std::string& file +) { + return create_supplier(env, src, file); +} + +boolsupplier scripting::create_bool_supplier( + const scriptenv& env, const std::string& src, const std::string& file +) { + return create_supplier(env, src, file); +} + +doublesupplier scripting::create_number_supplier( + const scriptenv& env, const std::string& src, const std::string& file +) { + return create_supplier(env, src, file); +} + wstringchecker scripting::create_wstring_validator( const scriptenv& env, const std::string& src, const std::string& file ) { @@ -108,60 +142,6 @@ wstringchecker scripting::create_wstring_validator( }; } -boolconsumer scripting::create_bool_consumer( - const scriptenv& env, const std::string& src, const std::string& file -) { - return [=](bool x) { - if (auto L = process_callback(env, src, file)) { - lua::pushboolean(L, x); - lua::call_nothrow(L, 1); - } - }; -} - -boolsupplier scripting::create_bool_supplier( - const scriptenv& env, const std::string& src, const std::string& file -) { - return [=]() { - if (auto L = process_callback(env, src, file)) { - if (lua::isfunction(L, -1)) { - lua::call_nothrow(L, 0); - } - bool x = lua::toboolean(L, -1); - lua::pop(L); - return x; - } - return false; - }; -} - -doubleconsumer scripting::create_number_consumer( - const scriptenv& env, const std::string& src, const std::string& file -) { - return [=](double x) { - if (auto L = process_callback(env, src, file)) { - lua::pushnumber(L, x); - lua::call_nothrow(L, 1); - } - }; -} - -doublesupplier scripting::create_number_supplier( - const scriptenv& env, const std::string& src, const std::string& file -) { - return [=]() { - if (auto L = process_callback(env, src, file)) { - if (lua::isfunction(L, -1)) { - lua::call_nothrow(L, 0); - } - auto x = lua::tonumber(L, -1); - lua::pop(L); - return x; - } - return 0.0; - }; -} - int_array_consumer scripting::create_int_array_consumer( const scriptenv& env, const std::string& src, const std::string& file ) { diff --git a/src/util/type_helpers.hpp b/src/util/type_helpers.hpp new file mode 100644 index 00000000..0ff2654b --- /dev/null +++ b/src/util/type_helpers.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include + +template +struct remove_const_ref_if_primitive { + using type = T; +}; + +template +struct remove_const_ref_if_primitive { + using stripped_type = typename std::remove_const::type>::type; + using type = typename std::conditional::value, + stripped_type, + const T&>::type; +}; + +template +struct remove_const_ref_if_primitive { + using stripped_type = typename std::remove_const::type; + using type = typename std::conditional::value, + stripped_type, + const T>::type; +}; + +template +using remove_const_ref_if_primitive_t = typename remove_const_ref_if_primitive::type;