Lua::check<> fixed (u)int implementation
moved error to seperate header for easier re-use
Dieser Commit ist enthalten in:
Ursprung
d9f02fcbac
Commit
beb4576dc1
@ -26,6 +26,7 @@
|
||||
#include <lua.hpp>
|
||||
#include <type_traits>
|
||||
#include <cmath>
|
||||
#include "error.hpp"
|
||||
|
||||
namespace Lua {
|
||||
|
||||
@ -39,19 +40,18 @@ T check(lua_State* L, int index)
|
||||
}
|
||||
else if constexpr(std::is_integral_v<T>)
|
||||
{
|
||||
if constexpr(std::numeric_limits<T>::min() < LUA_MININTEGER ||
|
||||
std::numeric_limits<T>::max() > LUA_MAXINTEGER)
|
||||
return std::round(luaL_checknumber(L, index));
|
||||
|
||||
const lua_Integer value = luaL_checkinteger(L, index);
|
||||
if constexpr(std::numeric_limits<T>::min() >= LUA_MININTEGER &&
|
||||
std::numeric_limits<T>::max() <= LUA_MAXINTEGER)
|
||||
if constexpr(std::is_unsigned_v<T> && sizeof(T) >= sizeof(value))
|
||||
{
|
||||
if(value >= 0)
|
||||
return static_cast<T>(value);
|
||||
}
|
||||
else if constexpr(std::numeric_limits<T>::min() <= LUA_MININTEGER && std::numeric_limits<T>::max() >= LUA_MAXINTEGER)
|
||||
return value;
|
||||
else if(value >= std::numeric_limits<T>::min() && value <= std::numeric_limits<T>::max())
|
||||
return value;
|
||||
|
||||
luaL_argerror(L, index, "out of range");
|
||||
abort(); // never happens, luaL_error doesn't return
|
||||
errorArgumentOutOfRange(L, index);
|
||||
}
|
||||
else if constexpr(std::is_floating_point_v<T>)
|
||||
return luaL_checknumber(L, index);
|
||||
|
||||
34
server/src/lua/error.hpp
Normale Datei
34
server/src/lua/error.hpp
Normale Datei
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* server/src/lua/error.hpp
|
||||
*
|
||||
* This file is part of the traintastic source code.
|
||||
*
|
||||
* Copyright (C) 2021 Reinder Feenstra
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef TRAINTASTIC_SERVER_LUA_ERROR_HPP
|
||||
#define TRAINTASTIC_SERVER_LUA_ERROR_HPP
|
||||
|
||||
#include <lua.hpp>
|
||||
|
||||
namespace Lua {
|
||||
|
||||
[[noreturn]] inline void errorArgumentOutOfRange(lua_State* L, int arg) { luaL_argerror(L, arg, "out of range"); abort(); }
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Laden…
x
In neuem Issue referenzieren
Einen Benutzer sperren