mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-03-10 16:14:26 +00:00
Add function LuaUtil::cast
This commit is contained in:
parent
957318d0b9
commit
61578ca39b
@ -100,6 +100,23 @@ return {
|
|||||||
EXPECT_EQ(LuaUtil::toString(sol::make_object(mLua.sol(), "something")), "\"something\"");
|
EXPECT_EQ(LuaUtil::toString(sol::make_object(mLua.sol(), "something")), "\"something\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(LuaStateTest, Cast)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(LuaUtil::cast<int>(sol::make_object(mLua.sol(), 3.14)), 3);
|
||||||
|
EXPECT_ERROR(
|
||||||
|
LuaUtil::cast<int>(sol::make_object(mLua.sol(), "3.14")), "Value \"\"3.14\"\" can not be casted to int");
|
||||||
|
EXPECT_ERROR(LuaUtil::cast<std::string_view>(sol::make_object(mLua.sol(), sol::nil)),
|
||||||
|
"Value \"nil\" can not be casted to string");
|
||||||
|
EXPECT_ERROR(LuaUtil::cast<std::string>(sol::make_object(mLua.sol(), sol::nil)),
|
||||||
|
"Value \"nil\" can not be casted to string");
|
||||||
|
EXPECT_ERROR(LuaUtil::cast<sol::table>(sol::make_object(mLua.sol(), sol::nil)),
|
||||||
|
"Value \"nil\" can not be casted to sol::table");
|
||||||
|
EXPECT_ERROR(LuaUtil::cast<sol::function>(sol::make_object(mLua.sol(), "3.14")),
|
||||||
|
"Value \"\"3.14\"\" can not be casted to sol::function");
|
||||||
|
EXPECT_ERROR(LuaUtil::cast<sol::protected_function>(sol::make_object(mLua.sol(), "3.14")),
|
||||||
|
"Value \"\"3.14\"\" can not be casted to sol::function");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(LuaStateTest, ErrorHandling)
|
TEST_F(LuaStateTest, ErrorHandling)
|
||||||
{
|
{
|
||||||
EXPECT_ERROR(mLua.runInNewSandbox("invalid.lua"), "[string \"invalid.lua\"]:1:");
|
EXPECT_ERROR(mLua.runInNewSandbox("invalid.lua"), "[string \"invalid.lua\"]:1:");
|
||||||
|
@ -420,4 +420,25 @@ namespace LuaUtil
|
|||||||
return call(sol::state_view(obj.lua_state())["tostring"], obj);
|
return call(sol::state_view(obj.lua_state())["tostring"], obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string internal::formatCastingError(const sol::object& obj, const std::type_info& t)
|
||||||
|
{
|
||||||
|
const char* typeName = t.name();
|
||||||
|
if (t == typeid(int))
|
||||||
|
typeName = "int";
|
||||||
|
else if (t == typeid(unsigned))
|
||||||
|
typeName = "uint32";
|
||||||
|
else if (t == typeid(size_t))
|
||||||
|
typeName = "size_t";
|
||||||
|
else if (t == typeid(float))
|
||||||
|
typeName = "float";
|
||||||
|
else if (t == typeid(double))
|
||||||
|
typeName = "double";
|
||||||
|
else if (t == typeid(sol::table))
|
||||||
|
typeName = "sol::table";
|
||||||
|
else if (t == typeid(sol::function) || t == typeid(sol::protected_function))
|
||||||
|
typeName = "sol::function";
|
||||||
|
else if (t == typeid(std::string) || t == typeid(std::string_view))
|
||||||
|
typeName = "string";
|
||||||
|
return std::string("Value \"") + toString(obj) + std::string("\" can not be casted to ") + typeName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
#ifndef COMPONENTS_LUA_LUASTATE_H
|
#ifndef COMPONENTS_LUA_LUASTATE_H
|
||||||
#define COMPONENTS_LUA_LUASTATE_H
|
#define COMPONENTS_LUA_LUASTATE_H
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <typeinfo>
|
||||||
|
|
||||||
#include <sol/sol.hpp>
|
#include <sol/sol.hpp>
|
||||||
|
|
||||||
#include <filesystem>
|
|
||||||
|
|
||||||
#include "configuration.hpp"
|
#include "configuration.hpp"
|
||||||
|
|
||||||
namespace VFS
|
namespace VFS
|
||||||
@ -247,15 +247,25 @@ namespace LuaUtil
|
|||||||
// String representation of a Lua object. Should be used for debugging/logging purposes only.
|
// String representation of a Lua object. Should be used for debugging/logging purposes only.
|
||||||
std::string toString(const sol::object&);
|
std::string toString(const sol::object&);
|
||||||
|
|
||||||
|
namespace internal
|
||||||
|
{
|
||||||
|
std::string formatCastingError(const sol::object& obj, const std::type_info&);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
decltype(auto) cast(const sol::object& obj)
|
||||||
|
{
|
||||||
|
if (!obj.is<T>())
|
||||||
|
throw std::runtime_error(internal::formatCastingError(obj, typeid(T)));
|
||||||
|
return obj.as<T>();
|
||||||
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
T getValueOrDefault(const sol::object& obj, const T& defaultValue)
|
T getValueOrDefault(const sol::object& obj, const T& defaultValue)
|
||||||
{
|
{
|
||||||
if (obj == sol::nil)
|
if (obj == sol::nil)
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
if (obj.is<T>())
|
return cast<T>(obj);
|
||||||
return obj.as<T>();
|
|
||||||
else
|
|
||||||
throw std::logic_error(std::string("Value \"") + toString(obj) + std::string("\" has unexpected type"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Makes a table read only (when accessed from Lua) by wrapping it with an empty userdata.
|
// Makes a table read only (when accessed from Lua) by wrapping it with an empty userdata.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user