Skip to content

Commit 1c24242

Browse files
committed
Merge branch 'finite_number' into 'master'
Reduce code duplication for finite number and add tests See merge request OpenMW/openmw!4707
2 parents 85742a1 + 267ce1e commit 1c24242

File tree

6 files changed

+30
-43
lines changed

6 files changed

+30
-43
lines changed

apps/openmw/mwlua/animationbindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ namespace MWLua
8888

8989
sol::table initAnimationPackage(const Context& context)
9090
{
91-
using FiniteFloat = Misc::FiniteFloat;
91+
using Misc::FiniteFloat;
9292

9393
auto view = context.sol();
9494
auto mechanics = MWBase::Environment::get().getMechanicsManager();

apps/openmw/mwlua/camerabindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace MWLua
1616

1717
sol::table initCameraPackage(sol::state_view lua)
1818
{
19-
using FiniteFloat = Misc::FiniteFloat;
19+
using Misc::FiniteFloat;
2020

2121
MWRender::Camera* camera = MWBase::Environment::get().getWorld()->getCamera();
2222
MWRender::RenderingManager* renderingManager = MWBase::Environment::get().getWorld()->getRenderingManager();

apps/openmw/mwlua/worldbindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace MWLua
5757

5858
static void addWorldTimeBindings(sol::table& api, const Context& context)
5959
{
60-
using FiniteFloat = Misc::FiniteFloat;
60+
using Misc::FiniteFloat;
6161

6262
MWWorld::DateTimeManager* timeManager = MWBase::Environment::get().getWorld()->getTimeManager();
6363

components/misc/finitenumbers.hpp

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,48 @@
55

66
#include <cmath>
77
#include <stdexcept>
8+
#include <utility>
89

910
namespace Misc
1011
{
11-
struct FiniteDouble
12+
template <class T>
13+
struct FiniteNumber
1214
{
13-
double mValue;
14-
FiniteDouble(double v)
15-
{
16-
if (!std::isfinite(v))
17-
throw std::invalid_argument("Value must be a finite number");
18-
mValue = v;
19-
}
20-
operator double() const { return mValue; }
21-
};
15+
T mValue;
2216

23-
struct FiniteFloat
24-
{
25-
float mValue;
26-
FiniteFloat(float v)
17+
FiniteNumber(T v)
2718
{
2819
if (!std::isfinite(v))
2920
throw std::invalid_argument("Value must be a finite number");
3021
mValue = v;
3122
}
32-
operator float() const { return mValue; }
23+
24+
operator T() const { return mValue; }
3325
};
26+
27+
using FiniteDouble = FiniteNumber<double>;
28+
29+
using FiniteFloat = FiniteNumber<float>;
3430
}
3531

3632
namespace sol
3733
{
38-
using FiniteDouble = Misc::FiniteDouble;
39-
using FiniteFloat = Misc::FiniteFloat;
40-
41-
template <typename Handler>
42-
bool sol_lua_check(
43-
sol::types<FiniteDouble>, lua_State* L, int index, Handler&& handler, sol::stack::record& tracking)
44-
{
45-
bool success = sol::stack::check<double>(L, lua_absindex(L, index), handler);
46-
tracking.use(1);
47-
return success;
48-
}
49-
50-
static FiniteDouble sol_lua_get(sol::types<FiniteDouble>, lua_State* L, int index, sol::stack::record& tracking)
51-
{
52-
double val = sol::stack::get<double>(L, lua_absindex(L, index));
53-
tracking.use(1);
54-
return FiniteDouble(val);
55-
}
56-
57-
template <typename Handler>
34+
template <class Handler, class T>
5835
bool sol_lua_check(
59-
sol::types<FiniteFloat>, lua_State* L, int index, Handler&& handler, sol::stack::record& tracking)
36+
types<Misc::FiniteNumber<T>>, lua_State* state, int index, Handler&& handler, stack::record& tracking)
6037
{
61-
bool success = sol::stack::check<float>(L, lua_absindex(L, index), handler);
38+
bool success = stack::check<T>(state, lua_absindex(state, index), std::forward<Handler>(handler));
6239
tracking.use(1);
6340
return success;
6441
}
6542

66-
static FiniteFloat sol_lua_get(sol::types<FiniteFloat>, lua_State* L, int index, sol::stack::record& tracking)
43+
template <class T>
44+
static Misc::FiniteNumber<T> sol_lua_get(
45+
types<Misc::FiniteNumber<T>>, lua_State* state, int index, stack::record& tracking)
6746
{
68-
float val = sol::stack::get<float>(L, lua_absindex(L, index));
47+
T value = stack::get<T>(state, lua_absindex(state, index));
6948
tracking.use(1);
70-
return FiniteFloat(val);
49+
return value;
7150
}
7251
}
7352

scripts/data/integration_tests/test_lua_api/global.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ testing.registerGlobalTest('load while teleporting - teleport', function()
344344
landracer:teleport(player.cell, player.position)
345345
end)
346346

347+
testing.registerGlobalTest('nan', function()
348+
local nan = 0.0 / 0.0
349+
local ok, err = pcall(function() world.setGameTimeScale(nan) end)
350+
testing.expectEqual(ok, false)
351+
testing.expectEqual(err, 'Value must be a finite number')
352+
end)
353+
347354
return {
348355
engineHandlers = {
349356
onUpdate = testing.updateGlobal,

scripts/data/integration_tests/test_lua_api/menu.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ registerGlobalTest('memory limit')
7272
registerGlobalTest('vfs')
7373
registerGlobalTest('commit crime')
7474
registerGlobalTest('record model property')
75+
registerGlobalTest('nan', 'world.setGameTimeScale should not accept nan')
7576

7677
registerGlobalTest('player yaw rotation', 'rotating player with controls.yawChange should change rotation')
7778
registerGlobalTest('player pitch rotation', 'rotating player with controls.pitchChange should change rotation')

0 commit comments

Comments
 (0)