refactor
This commit is contained in:
parent
f17167f8c9
commit
1c8cafbb68
@ -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<typename T, int(pushfunc)(lua::State*, remove_const_ref_if_primitive_t<const T&>)>
|
||||
std::function<void(const T&)> 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<std::wstring, lua::pushwstring>(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<std::string, lua::pushstring>(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<bool, lua::pushboolean>(env, src, file);
|
||||
}
|
||||
|
||||
doubleconsumer scripting::create_number_consumer(
|
||||
const scriptenv& env, const std::string& src, const std::string& file
|
||||
) {
|
||||
return create_consumer<number_t, lua::pushnumber>(env, src, file);
|
||||
}
|
||||
|
||||
template <typename T, T(tovalueFunc)(lua::State*, int)>
|
||||
std::function<T()> 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<std::wstring, lua::require_wstring>(env, src, file);
|
||||
}
|
||||
|
||||
boolsupplier scripting::create_bool_supplier(
|
||||
const scriptenv& env, const std::string& src, const std::string& file
|
||||
) {
|
||||
return create_supplier<bool, lua::toboolean>(env, src, file);
|
||||
}
|
||||
|
||||
doublesupplier scripting::create_number_supplier(
|
||||
const scriptenv& env, const std::string& src, const std::string& file
|
||||
) {
|
||||
return create_supplier<number_t, lua::tonumber>(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
|
||||
) {
|
||||
|
||||
27
src/util/type_helpers.hpp
Normal file
27
src/util/type_helpers.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
template <typename T>
|
||||
struct remove_const_ref_if_primitive {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct remove_const_ref_if_primitive<const T&> {
|
||||
using stripped_type = typename std::remove_const<typename std::remove_reference<T>::type>::type;
|
||||
using type = typename std::conditional<std::is_fundamental<stripped_type>::value,
|
||||
stripped_type,
|
||||
const T&>::type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct remove_const_ref_if_primitive<const T> {
|
||||
using stripped_type = typename std::remove_const<T>::type;
|
||||
using type = typename std::conditional<std::is_fundamental<stripped_type>::value,
|
||||
stripped_type,
|
||||
const T>::type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using remove_const_ref_if_primitive_t = typename remove_const_ref_if_primitive<T>::type;
|
||||
Loading…
x
Reference in New Issue
Block a user