ToluaUtils.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Core/Context.h"
  24. #ifdef _WIN32
  25. #include "../Graphics/IndexBuffer.h"
  26. #include "../Graphics/VertexBuffer.h"
  27. #endif
  28. struct lua_State;
  29. using namespace Urho3D;
  30. /// Check is String.
  31. #define tolua_isurho3dstring tolua_isstring
  32. /// Push String.
  33. #define tolua_pushurho3dstring(L, s) tolua_pushstring(L, s.CString())
  34. /// Convert to String.
  35. const char* tolua_tourho3dstring(lua_State* L, int narg, const char* str);
  36. /// Convert to String.
  37. const char* tolua_tourho3dstring(lua_State* L, int narg, const String& str);
  38. /// Set context.
  39. void SetContext(lua_State* L, Context* context);
  40. /// Return context.
  41. Context* GetContext(lua_State* L);
  42. /// Create object.
  43. template <typename T> int ToluaNewObject(lua_State* tolua_S)
  44. {
  45. T* object = Mtolua_new(T(GetContext(tolua_S)));
  46. tolua_pushusertype(tolua_S, (void*)object, T::GetTypeNameStatic().CString());
  47. return 1;
  48. }
  49. /// Create object with garbage collection.
  50. template <typename T> int ToluaNewObjectGC(lua_State* tolua_S)
  51. {
  52. T* object = Mtolua_new(T(GetContext(tolua_S)));
  53. tolua_pushusertype(tolua_S, (void*)object, T::GetTypeNameStatic().CString());
  54. tolua_register_gc(tolua_S, lua_gettop(tolua_S));
  55. return 1;
  56. }
  57. /// Return subsystem.
  58. template <typename T> int ToluaGetSubsystem(lua_State* tolua_S)
  59. {
  60. T* subsystem = GetContext(tolua_S)->GetSubsystem<T>();
  61. tolua_pushusertype(tolua_S, (void*)subsystem, T::GetTypeNameStatic().CString());
  62. return 1;
  63. }
  64. /// Check is Vector<T>.
  65. template <typename T> int ToluaIsVector(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  66. {
  67. return tolua_isusertypearray(L, lo, type, -1, def, err);
  68. }
  69. /// Convert to Vector<T>. This function is not thread-safe.
  70. template <typename T> void* ToluaToVector(lua_State* L, int narg, void* def)
  71. {
  72. if (!lua_istable(L, narg))
  73. return 0;
  74. static Vector<T> result;
  75. result.Clear();
  76. result.Resize((unsigned)lua_objlen(L, narg));
  77. for (unsigned i = 0; i < result.Size(); ++i)
  78. {
  79. lua_rawgeti(L, narg, i + 1); // Lua index starts from 1
  80. result[i] = *static_cast<T*>(tolua_tousertype(L, -1, def));
  81. lua_pop(L, 1);
  82. }
  83. return &result;
  84. }
  85. /// Push Vector<T> to Lua as a table.
  86. template <typename T> int ToluaPushVector(lua_State* L, void* data, const char* type)
  87. {
  88. lua_newtable(L);
  89. Vector<T>& vector = *static_cast<Vector<T>*>(data);
  90. for (unsigned i = 0; i < vector.Size(); ++i)
  91. {
  92. tolua_pushusertype(L, &vector[i], type);
  93. lua_rawseti(L, -2, i + 1);
  94. }
  95. return 1;
  96. }
  97. /// Check is PODVector<T>.
  98. template <typename T> int ToluaIsPODVector(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  99. {
  100. // Whether it is POD or non-POD, on Lua side they are just usertype object, so we can reuse the same function
  101. return ToluaIsVector<T>(L, lo, type, def, err);
  102. }
  103. /// Check is PODVector<T, is_arithmetic<T>>. Use template function overload as non-type partial template specialization is not allowed.
  104. template <typename T> int ToluaIsPODVector(double /*overload*/, lua_State* L, int lo, const char* /*type*/, int def, tolua_Error* err)
  105. {
  106. return tolua_isnumberarray(L, lo, -1, def, err);
  107. }
  108. /// Convert to PODVector<T>. This function is not thread-safe.
  109. template <typename T> void* ToluaToPODVector(lua_State* L, int narg, void* def)
  110. {
  111. return ToluaToVector<T>(L, narg, def);
  112. }
  113. /// Convert to PODVector<T, is_arithmetic<T>>. This function is not thread-safe. Use template function overload as non-type partial template specialization is not allowed.
  114. template <typename T> void* ToluaToPODVector(double /*overload*/, lua_State* L, int narg, void* /*def*/)
  115. {
  116. if (!lua_istable(L, narg))
  117. return 0;
  118. static PODVector<T> result;
  119. result.Clear();
  120. result.Resize((unsigned)lua_objlen(L, narg));
  121. for (unsigned i = 0; i < result.Size(); ++i)
  122. {
  123. lua_rawgeti(L, narg, i + 1);
  124. result[i] = (T)tolua_tonumber(L, -1, 0);
  125. lua_pop(L, 1);
  126. }
  127. return &result;
  128. }
  129. /// Push PODVector<T> to Lua as a table.
  130. template <typename T> int ToluaPushPODVector(lua_State* L, void* data, const char* type)
  131. {
  132. return ToluaPushVector<T>(L, data, type);
  133. }
  134. /// Push PODVector<T, is_pointer<T>> to Lua as a table. Use template function overload as non-type partial template specialization is not allowed.
  135. template <typename T> int ToluaPushPODVector(const char* /*overload*/, lua_State* L, void* data, const char* type)
  136. {
  137. lua_newtable(L);
  138. const PODVector<T>& vector = *static_cast<const PODVector<T>*>(data);
  139. for (unsigned i = 0; i < vector.Size(); ++i)
  140. {
  141. tolua_pushusertype(L, vector[i], type);
  142. lua_rawseti(L, -2, i + 1);
  143. }
  144. return 1;
  145. }
  146. /// Push PODVector<T, is_arithmetic<T>> to Lua as a table. Use template function overload as non-type partial template specialization is not allowed.
  147. template <typename T> int ToluaPushPODVector(double /*overload*/, lua_State* L, void* data, const char* /*type*/)
  148. {
  149. lua_newtable(L);
  150. const PODVector<T>& vector = *static_cast<const PODVector<T>*>(data);
  151. for (unsigned i = 0; i < vector.Size(); ++i)
  152. {
  153. lua_pushnumber(L, vector[i]);
  154. lua_rawseti(L, -2, i + 1);
  155. }
  156. return 1;
  157. }
  158. // GCC and Clang does not follow the C++ standard in expecting explicit template specialization shall be declared before first use,
  159. // both compilers are able to avoid the multiple definitions of template instantiation symbol during linking by using weak symbol
  160. // MSVC and MinGW, however, follow the standard strictly, hence we need to declare all the explicit template specializations below
  161. // to keep these two compilers happy
  162. #ifdef _WIN32
  163. template <> int ToluaIsVector<String>(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
  164. template <> void* ToluaToVector<String>(lua_State* L, int narg, void* def);
  165. template <> int ToluaPushVector<String>(lua_State* L, void* data, const char* type);
  166. template <> int ToluaIsPODVector<bool>(double /*overload*/, lua_State* L, int lo, const char* type, int def, tolua_Error* err);
  167. template <> void* ToluaToPODVector<bool>(double /*overload*/, lua_State* L, int narg, void* def);
  168. template <> int ToluaPushPODVector<bool>(double /*overload*/, lua_State* L, void* data, const char* type);
  169. template <> void* ToluaToVector<SharedPtr<IndexBuffer> >(lua_State* L, int narg, void* def);
  170. template <> void* ToluaToVector<SharedPtr<VertexBuffer> >(lua_State* L, int narg, void* def);
  171. #endif
  172. /// Convert object at the given index and store it in Variant. This function is not thread-safe.
  173. void ToluaToVariant(lua_State* L, int narg, void* def, Variant& variant);
  174. /// Push object stored in a Variant to stack. Empty variant value is pushed as nil.
  175. void ToluaPushVariant(lua_State* L, const Variant* variant, const char* type = 0);
  176. /// Push a registered Lua user type to stack. If the specified type is not yet registered, a nil is pushed instead.
  177. void ToluaPushRegisteredUserType(lua_State* L, void* data, const char* type);
  178. /// Push Object to Lua.
  179. void ToluaPushObject(lua_State* L, void* data, const char* type);