mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-28 08:11:34 +00:00
Merge branch 'finite_number' into 'master'
Reduce code duplication for finite number and add tests See merge request OpenMW/openmw!4707
This commit is contained in:
commit
1c242425b0
6 changed files with 30 additions and 43 deletions
|
@ -88,7 +88,7 @@ namespace MWLua
|
|||
|
||||
sol::table initAnimationPackage(const Context& context)
|
||||
{
|
||||
using FiniteFloat = Misc::FiniteFloat;
|
||||
using Misc::FiniteFloat;
|
||||
|
||||
auto view = context.sol();
|
||||
auto mechanics = MWBase::Environment::get().getMechanicsManager();
|
||||
|
|
|
@ -16,7 +16,7 @@ namespace MWLua
|
|||
|
||||
sol::table initCameraPackage(sol::state_view lua)
|
||||
{
|
||||
using FiniteFloat = Misc::FiniteFloat;
|
||||
using Misc::FiniteFloat;
|
||||
|
||||
MWRender::Camera* camera = MWBase::Environment::get().getWorld()->getCamera();
|
||||
MWRender::RenderingManager* renderingManager = MWBase::Environment::get().getWorld()->getRenderingManager();
|
||||
|
|
|
@ -57,7 +57,7 @@ namespace MWLua
|
|||
|
||||
static void addWorldTimeBindings(sol::table& api, const Context& context)
|
||||
{
|
||||
using FiniteFloat = Misc::FiniteFloat;
|
||||
using Misc::FiniteFloat;
|
||||
|
||||
MWWorld::DateTimeManager* timeManager = MWBase::Environment::get().getWorld()->getTimeManager();
|
||||
|
||||
|
|
|
@ -5,69 +5,48 @@
|
|||
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace Misc
|
||||
{
|
||||
struct FiniteDouble
|
||||
template <class T>
|
||||
struct FiniteNumber
|
||||
{
|
||||
double mValue;
|
||||
FiniteDouble(double v)
|
||||
T mValue;
|
||||
|
||||
FiniteNumber(T v)
|
||||
{
|
||||
if (!std::isfinite(v))
|
||||
throw std::invalid_argument("Value must be a finite number");
|
||||
mValue = v;
|
||||
}
|
||||
operator double() const { return mValue; }
|
||||
|
||||
operator T() const { return mValue; }
|
||||
};
|
||||
|
||||
struct FiniteFloat
|
||||
{
|
||||
float mValue;
|
||||
FiniteFloat(float v)
|
||||
{
|
||||
if (!std::isfinite(v))
|
||||
throw std::invalid_argument("Value must be a finite number");
|
||||
mValue = v;
|
||||
}
|
||||
operator float() const { return mValue; }
|
||||
};
|
||||
using FiniteDouble = FiniteNumber<double>;
|
||||
|
||||
using FiniteFloat = FiniteNumber<float>;
|
||||
}
|
||||
|
||||
namespace sol
|
||||
{
|
||||
using FiniteDouble = Misc::FiniteDouble;
|
||||
using FiniteFloat = Misc::FiniteFloat;
|
||||
|
||||
template <typename Handler>
|
||||
template <class Handler, class T>
|
||||
bool sol_lua_check(
|
||||
sol::types<FiniteDouble>, lua_State* L, int index, Handler&& handler, sol::stack::record& tracking)
|
||||
types<Misc::FiniteNumber<T>>, lua_State* state, int index, Handler&& handler, stack::record& tracking)
|
||||
{
|
||||
bool success = sol::stack::check<double>(L, lua_absindex(L, index), handler);
|
||||
bool success = stack::check<T>(state, lua_absindex(state, index), std::forward<Handler>(handler));
|
||||
tracking.use(1);
|
||||
return success;
|
||||
}
|
||||
|
||||
static FiniteDouble sol_lua_get(sol::types<FiniteDouble>, lua_State* L, int index, sol::stack::record& tracking)
|
||||
template <class T>
|
||||
static Misc::FiniteNumber<T> sol_lua_get(
|
||||
types<Misc::FiniteNumber<T>>, lua_State* state, int index, stack::record& tracking)
|
||||
{
|
||||
double val = sol::stack::get<double>(L, lua_absindex(L, index));
|
||||
T value = stack::get<T>(state, lua_absindex(state, index));
|
||||
tracking.use(1);
|
||||
return FiniteDouble(val);
|
||||
}
|
||||
|
||||
template <typename Handler>
|
||||
bool sol_lua_check(
|
||||
sol::types<FiniteFloat>, lua_State* L, int index, Handler&& handler, sol::stack::record& tracking)
|
||||
{
|
||||
bool success = sol::stack::check<float>(L, lua_absindex(L, index), handler);
|
||||
tracking.use(1);
|
||||
return success;
|
||||
}
|
||||
|
||||
static FiniteFloat sol_lua_get(sol::types<FiniteFloat>, lua_State* L, int index, sol::stack::record& tracking)
|
||||
{
|
||||
float val = sol::stack::get<float>(L, lua_absindex(L, index));
|
||||
tracking.use(1);
|
||||
return FiniteFloat(val);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -344,6 +344,13 @@ testing.registerGlobalTest('load while teleporting - teleport', function()
|
|||
landracer:teleport(player.cell, player.position)
|
||||
end)
|
||||
|
||||
testing.registerGlobalTest('nan', function()
|
||||
local nan = 0.0 / 0.0
|
||||
local ok, err = pcall(function() world.setGameTimeScale(nan) end)
|
||||
testing.expectEqual(ok, false)
|
||||
testing.expectEqual(err, 'Value must be a finite number')
|
||||
end)
|
||||
|
||||
return {
|
||||
engineHandlers = {
|
||||
onUpdate = testing.updateGlobal,
|
||||
|
|
|
@ -72,6 +72,7 @@ registerGlobalTest('memory limit')
|
|||
registerGlobalTest('vfs')
|
||||
registerGlobalTest('commit crime')
|
||||
registerGlobalTest('record model property')
|
||||
registerGlobalTest('nan', 'world.setGameTimeScale should not accept nan')
|
||||
|
||||
registerGlobalTest('player yaw rotation', 'rotating player with controls.yawChange should change rotation')
|
||||
registerGlobalTest('player pitch rotation', 'rotating player with controls.pitchChange should change rotation')
|
||||
|
|
Loading…
Reference in a new issue