mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-20 01:41:36 +00:00
Reduce code duplication for finite number
This commit is contained in:
parent
178f216317
commit
267ce1ec9b
4 changed files with 22 additions and 43 deletions
|
@ -100,7 +100,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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue