You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

74 lines
2.0 KiB

  1. /*
  2. Minetest
  3. Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "helper.h"
  17. #include <cmath>
  18. #include <sstream>
  19. #include "c_types.h"
  20. bool LuaHelper::isNaN(lua_State *L, int idx)
  21. {
  22. return lua_type(L, idx) == LUA_TNUMBER && std::isnan(lua_tonumber(L, idx));
  23. }
  24. /*
  25. * Read template functions
  26. */
  27. template <> bool LuaHelper::readParam(lua_State *L, int index)
  28. {
  29. return lua_toboolean(L, index) != 0;
  30. }
  31. template <> bool LuaHelper::readParam(lua_State *L, int index, const bool &default_value)
  32. {
  33. if (lua_isnil(L, index))
  34. return default_value;
  35. return lua_toboolean(L, index) != 0;
  36. }
  37. template <> float LuaHelper::readParam(lua_State *L, int index)
  38. {
  39. if (isNaN(L, index))
  40. throw LuaError("NaN value is not allowed.");
  41. return (float)luaL_checknumber(L, index);
  42. }
  43. template <> std::string LuaHelper::readParam(lua_State *L, int index)
  44. {
  45. std::string result;
  46. const char *str = luaL_checkstring(L, index);
  47. result.append(str);
  48. return result;
  49. }
  50. template <>
  51. std::string LuaHelper::readParam(
  52. lua_State *L, int index, const std::string &default_value)
  53. {
  54. std::string result;
  55. const char *str = lua_tostring(L, index);
  56. if (str)
  57. result.append(str);
  58. else
  59. result = default_value;
  60. return result;
  61. }