Nz::ranged_cast

- Moved ranged_cast template function to namespace Nz in file
LuaInstance.hpp
- Added comments


Former-commit-id: 9af0a680b693208e582eb4edbe5a65f996d70a4a
This commit is contained in:
GigAnon 2016-01-21 20:12:37 +01:00
parent 69a1be70ba
commit 8bfd879d87
2 changed files with 20 additions and 19 deletions

View File

@ -7,14 +7,6 @@
#include <Nazara/Renderer.hpp> #include <Nazara/Renderer.hpp>
#include <Nazara/Lua/LuaClass.hpp> #include <Nazara/Lua/LuaClass.hpp>
#include <limits>
// WIP: I don't know where to put it...
template <class T> T ranged_cast(long long a, T high = std::numeric_limits<T>::max(), T low = std::numeric_limits<T>::min())
{
return static_cast<T>(std::min(static_cast<long long>(high), std::max(a, static_cast<long long>(low))));
}
namespace Ndk namespace Ndk
{ {
void LuaAPI::Register_Renderer(Nz::LuaInstance& instance) void LuaAPI::Register_Renderer(Nz::LuaInstance& instance)
@ -41,7 +33,7 @@ namespace Ndk
unsigned int argCount = std::min(lua.GetStackTop(), 2U); unsigned int argCount = std::min(lua.GetStackTop(), 2U);
if (argCount == 1 && lua.IsOfType(1, Nz::LuaType_Number)) if (argCount == 1 && lua.IsOfType(1, Nz::LuaType_Number))
memory = abstractImage->GetMemoryUsage(ranged_cast<Nz::UInt8>(lua.CheckInteger(1))); memory = abstractImage->GetMemoryUsage(Nz::ranged_cast<Nz::UInt8>(lua.CheckInteger(1)));
else else
memory = abstractImage->GetMemoryUsage(); memory = abstractImage->GetMemoryUsage();
@ -81,15 +73,15 @@ namespace Ndk
virtual bool Update(const UInt8* pixels, unsigned int srcWidth = 0, unsigned int srcHeight = 0, UInt8 level = 0) virtual bool Update(const UInt8* pixels, unsigned int srcWidth = 0, unsigned int srcHeight = 0, UInt8 level = 0)
*/ */
unsigned int srcWidth = ranged_cast<unsigned int>(lua.CheckInteger(2)); unsigned int srcWidth = Nz::ranged_cast<unsigned int>(lua.CheckInteger(2));
unsigned int srcHeight = 0; unsigned int srcHeight = 0;
Nz::UInt8 level = 0; Nz::UInt8 level = 0;
if (argCount >= 3) if (argCount >= 3)
{ {
srcHeight = ranged_cast<unsigned int>(lua.CheckInteger(3)); srcHeight = Nz::ranged_cast<unsigned int>(lua.CheckInteger(3));
if (argCount >= 4) if (argCount >= 4)
level = ranged_cast<Nz::UInt8>(lua.CheckInteger(3)); level = Nz::ranged_cast<Nz::UInt8>(lua.CheckInteger(3));
} }
rValue = abstractImage->Update(pixels, srcWidth, srcHeight, level); rValue = abstractImage->Update(pixels, srcWidth, srcHeight, level);
@ -107,12 +99,12 @@ namespace Ndk
if (argCount >= 3) if (argCount >= 3)
{ {
srcWidth = ranged_cast<unsigned int>(lua.CheckInteger(3)); srcWidth = Nz::ranged_cast<unsigned int>(lua.CheckInteger(3));
if (argCount >= 4) if (argCount >= 4)
{ {
srcHeight = ranged_cast<unsigned int>(lua.CheckInteger(4)); srcHeight = Nz::ranged_cast<unsigned int>(lua.CheckInteger(4));
if (argCount >= 5) if (argCount >= 5)
level = ranged_cast<Nz::UInt8>(lua.CheckInteger(5)); level = Nz::ranged_cast<Nz::UInt8>(lua.CheckInteger(5));
} }
} }
rValue = abstractImage->Update(pixels, box, srcWidth, srcHeight, level); rValue = abstractImage->Update(pixels, box, srcWidth, srcHeight, level);
@ -131,15 +123,15 @@ namespace Ndk
if (argCount >= 3) if (argCount >= 3)
{ {
z = ranged_cast<unsigned int>(lua.CheckInteger(3)); z = Nz::ranged_cast<unsigned int>(lua.CheckInteger(3));
if (argCount >= 4) if (argCount >= 4)
{ {
srcWidth = ranged_cast<unsigned int>(lua.CheckInteger(4)); srcWidth = Nz::ranged_cast<unsigned int>(lua.CheckInteger(4));
if (argCount >= 5) if (argCount >= 5)
{ {
srcHeight = ranged_cast<unsigned int>(lua.CheckInteger(5)); srcHeight = Nz::ranged_cast<unsigned int>(lua.CheckInteger(5));
if (argCount >= 6) if (argCount >= 6)
level = ranged_cast<Nz::UInt8>(lua.CheckInteger(6)); level = Nz::ranged_cast<Nz::UInt8>(lua.CheckInteger(6));
} }
} }
} }

View File

@ -15,6 +15,7 @@
#include <Nazara/Lua/Enums.hpp> #include <Nazara/Lua/Enums.hpp>
#include <cstddef> #include <cstddef>
#include <functional> #include <functional>
#include <limits>
struct lua_Debug; struct lua_Debug;
struct lua_State; struct lua_State;
@ -189,6 +190,14 @@ namespace Nz
lua_State* m_state; lua_State* m_state;
unsigned int m_level; unsigned int m_level;
}; };
// Performs a 'ranged cast' to type T, i.e. a cast guaranteed to end up within the numerical limits of the type (or the limits provided in argument),
// without over- or under-flowing.
// Values beyond that range will be truncated to the type minimum (resp. maximum) if they are greater (resp. lesser) than the initial value.
template <class T> T ranged_cast(long long a, T high = std::numeric_limits<T>::max(), T low = std::numeric_limits<T>::min())
{
return static_cast<T>(std::min(static_cast<long long>(high), std::max(a, static_cast<long long>(low))));
}
} }
#include <Nazara/Lua/LuaInstance.inl> #include <Nazara/Lua/LuaInstance.inl>