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.
 
 
 
 
 
 

148 regels
5.0 KiB

  1. //
  2. // Base Lua class for Lua script loading
  3. //
  4. // Copyright: (c) 2009-2015 Sam Hocevar <sam@hocevar.net>
  5. // 2009-2015 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  6. //
  7. // This program is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #include "lua/lua.hpp"
  14. #include "lua/luawrapper.hpp"
  15. #pragma once
  16. namespace lol
  17. {
  18. //-----------------------------------------------------------------------------
  19. // Class available to link C++ class to Lua methods
  20. //--
  21. class LuaObject
  22. {
  23. protected:
  24. template<typename T, const char* name, const luaL_Reg* statics, const luaL_Reg* methods, T* (*ctor)(lua_State*)>
  25. struct LuaLibrary
  26. {
  27. LuaLibrary() { }
  28. void LoadTo(lua_State* l)
  29. {
  30. #define LOLUA_WRAPPER 1
  31. #if LOLUA_WRAPPER
  32. luaW_register<T>(l, name, statics, methods, ctor);
  33. #else
  34. luaL_newlib(L, statics);
  35. luaL_newlib(L, methods);
  36. #endif
  37. }
  38. };
  39. };
  40. //-----------------------------------------------------------------------------
  41. //
  42. //--
  43. struct LuaFunction
  44. {
  45. LuaFunction(lua_State* l, const char* name, int (*function)(lua_State*))
  46. {
  47. lua_pushcfunction(l, function);
  48. lua_setglobal(l, name);
  49. }
  50. };
  51. //-----------------------------------------------------------------------------
  52. template<typename T>
  53. struct LuaVar
  54. {
  55. private:
  56. T m_value = T(0);
  57. public:
  58. LuaVar() { }
  59. LuaVar(T value) { m_value = value; }
  60. LuaVar(lua_State* l, int index) { InnerGet(l, index); }
  61. inline T& V() { return m_value; }
  62. inline T& operator=(const T& value) { m_value = value; }
  63. inline int Return(lua_State* l) { InnerPush(l); return 1; }
  64. private:
  65. void InnerGet(lua_State* l, int index) { ASSERT(false); }
  66. void InnerPush(lua_State* l) { ASSERT(false); }
  67. };
  68. //-----------------------------------------------------------------------------
  69. template<typename T>
  70. struct LuaPtr
  71. {
  72. private:
  73. T* m_value = nullptr;
  74. public:
  75. LuaPtr() { }
  76. LuaPtr(T* value) { m_value = value; }
  77. LuaPtr(lua_State* l, int index) { InnerGet(l, index); }
  78. inline T* V() { return m_value; }
  79. inline T* operator=(T* value) { m_value = value; }
  80. inline int Return(lua_State* l) { InnerPush(l); return 1; }
  81. private:
  82. void InnerGet(lua_State* l, int index) { m_value = luaW_check<T>(l, index); }
  83. void InnerPush(lua_State* l) { luaW_push<T>(l, m_value); }
  84. };
  85. //-----------------------------------------------------------------------------
  86. template<> inline void LuaVar<String> ::InnerGet(lua_State* l, int index) { m_value = lua_tostring(l, index); }
  87. template<> inline void LuaVar<char const*>::InnerGet(lua_State* l, int index) { m_value = lua_tostring(l, index); }
  88. template<> inline void LuaVar<double> ::InnerGet(lua_State* l, int index) { m_value = lua_tonumber(l, index); }
  89. template<> inline void LuaVar<float> ::InnerGet(lua_State* l, int index) { m_value = (float)lua_tonumber(l, index); }
  90. template<> inline void LuaVar<int32_t> ::InnerGet(lua_State* l, int index) { m_value = (int32_t)lua_tointeger(l, index); }
  91. template<> inline void LuaVar<int64_t> ::InnerGet(lua_State* l, int index) { m_value = lua_tointeger(l, index); }
  92. template<> inline void LuaVar<uint32_t> ::InnerGet(lua_State* l, int index) { m_value = lua_tounsigned(l, index); }
  93. template<> inline void LuaVar<String> ::InnerPush(lua_State* l) { lua_pushstring(l, m_value.C()); }
  94. template<> inline void LuaVar<char const*>::InnerPush(lua_State* l) { lua_pushstring(l, m_value); }
  95. template<> inline void LuaVar<double> ::InnerPush(lua_State* l) { lua_pushnumber(l, m_value); }
  96. template<> inline void LuaVar<float> ::InnerPush(lua_State* l) { lua_pushnumber(l, m_value); }
  97. template<> inline void LuaVar<int32_t> ::InnerPush(lua_State* l) { lua_pushinteger(l, m_value); }
  98. template<> inline void LuaVar<int64_t> ::InnerPush(lua_State* l) { lua_pushinteger(l, m_value); }
  99. template<> inline void LuaVar<uint32_t> ::InnerPush(lua_State* l) { lua_pushunsigned(l, m_value); }
  100. //-----------------------------------------------------------------------------
  101. class LuaLoader
  102. {
  103. public:
  104. LuaLoader();
  105. virtual ~LuaLoader();
  106. bool ExecLua(String const &lua);
  107. template<typename T>
  108. T GetVar(String const &name)
  109. {
  110. lua_getglobal(m_lua_state, name.C());
  111. LuaVar<T> var(m_lua_state, -1);
  112. lua_pop(m_lua_state, 1);
  113. return var.V();
  114. }
  115. template<typename T>
  116. T* GetPtr(String const &name)
  117. {
  118. lua_getglobal(m_lua_state, name.C());
  119. LuaPtr<T> var(m_lua_state, -1);
  120. lua_pop(m_lua_state, 1);
  121. return var.V();
  122. }
  123. protected:
  124. lua_State* GetLuaState();
  125. private:
  126. lua_State* m_lua_state;
  127. };
  128. } /* namespace lol */