ToluaUtils.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // Copyright (c) 2008-2015 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. struct lua_State;
  25. using namespace Urho3D;
  26. /// Check is String.
  27. #define tolua_isurho3dstring tolua_isstring
  28. /// Push String.
  29. #define tolua_pushurho3dstring(L, s) tolua_pushstring(L, s.CString())
  30. /// Convert to String.
  31. const char* tolua_tourho3dstring(lua_State* L, int narg, const char* str);
  32. /// Convert to String.
  33. const char* tolua_tourho3dstring(lua_State* L, int narg, const String& str);
  34. /// Set context.
  35. void SetContext(lua_State* L, Context* context);
  36. /// Return context.
  37. Context* GetContext(lua_State* L);
  38. /// Create object.
  39. template <typename T> int ToluaNewObject(lua_State* tolua_S)
  40. {
  41. T* object = Mtolua_new(T(GetContext(tolua_S)));
  42. tolua_pushusertype(tolua_S, (void*)object, T::GetTypeNameStatic().CString());
  43. return 1;
  44. }
  45. /// Create object with garbage collection.
  46. template <typename T> int ToluaNewObjectGC(lua_State* tolua_S)
  47. {
  48. T* object = Mtolua_new(T(GetContext(tolua_S)));
  49. tolua_pushusertype(tolua_S, (void*)object, T::GetTypeNameStatic().CString());
  50. tolua_register_gc(tolua_S, lua_gettop(tolua_S));
  51. return 1;
  52. }
  53. /// Return subsystem.
  54. template <typename T> int ToluaGetSubsystem(lua_State* tolua_S)
  55. {
  56. T* subsystem = GetContext(tolua_S)->GetSubsystem<T>();
  57. tolua_pushusertype(tolua_S, (void*)subsystem, T::GetTypeNameStatic().CString());
  58. return 1;
  59. }
  60. /// Check is Vector<T>.
  61. template <typename T> int ToluaIsVector(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  62. {
  63. return tolua_isusertypearray(L, lo, type, -1, def, err);
  64. }
  65. /// Convert to Vector<T>. This function is not thread-safe.
  66. template <typename T> void* ToluaToVector(lua_State* L, int narg, void* def)
  67. {
  68. if (!lua_istable(L, narg))
  69. return 0;
  70. static Vector<T> result;
  71. result.Clear();
  72. result.Resize((unsigned)lua_objlen(L, narg));
  73. for (unsigned i = 0; i < result.Size(); ++i)
  74. {
  75. lua_rawgeti(L, narg, i + 1); // Lua index starts from 1
  76. result[i] = *static_cast<T*>(tolua_tousertype(L, -1, def));
  77. lua_pop(L, 1);
  78. }
  79. return &result;
  80. }
  81. /// Push Vector<T> to Lua as a table.
  82. template <typename T> int ToluaPushVector(lua_State* L, void* data, const char* type)
  83. {
  84. lua_newtable(L);
  85. Vector<T>& vector = *static_cast<Vector<T>*>(data);
  86. for (unsigned i = 0; i < vector.Size(); ++i)
  87. {
  88. tolua_pushusertype(L, &vector[i], type);
  89. lua_rawseti(L, -2, i + 1);
  90. }
  91. return 1;
  92. }
  93. /// Check is PODVector<T>.
  94. template <typename T> int ToluaIsPODVector(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  95. {
  96. // Whether it is POD or non-POD, on Lua side they are just usertype object, so we can reuse the same function
  97. return ToluaIsVector<T>(L, lo, type, def, err);
  98. }
  99. /// Check is PODVector<T, is_arithmetic<T>>. Use template function overload as non-type partial template specialization is not allowed.
  100. template <typename T> int ToluaIsPODVector(double /*overload*/, lua_State* L, int lo, const char* /*type*/, int def, tolua_Error* err)
  101. {
  102. return tolua_isnumberarray(L, lo, -1, def, err);
  103. }
  104. /// Convert to PODVector<T>. This function is not thread-safe.
  105. template <typename T> void* ToluaToPODVector(lua_State* L, int narg, void* def)
  106. {
  107. return ToluaToVector<T>(L, narg, def);
  108. }
  109. /// 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.
  110. template <typename T> void* ToluaToPODVector(double /*overload*/, lua_State* L, int narg, void* /*def*/)
  111. {
  112. if (!lua_istable(L, narg))
  113. return 0;
  114. static PODVector<T> result;
  115. result.Clear();
  116. result.Resize((unsigned)lua_objlen(L, narg));
  117. for (unsigned i = 0; i < result.Size(); ++i)
  118. {
  119. lua_rawgeti(L, narg, i + 1);
  120. result[i] = (T)tolua_tonumber(L, -1, 0);
  121. lua_pop(L, 1);
  122. }
  123. return &result;
  124. }
  125. /// Push PODVector<T> to Lua as a table.
  126. template <typename T> int ToluaPushPODVector(lua_State* L, void* data, const char* type)
  127. {
  128. return ToluaPushVector<T>(L, data, type);
  129. }
  130. /// Push PODVector<T, is_pointer<T>> to Lua as a table. Use template function overload as non-type partial template specialization is not allowed.
  131. template <typename T> int ToluaPushPODVector(const char* /*overload*/, lua_State* L, void* data, const char* type)
  132. {
  133. lua_newtable(L);
  134. const PODVector<T>& vector = *static_cast<const PODVector<T>*>(data);
  135. for (unsigned i = 0; i < vector.Size(); ++i)
  136. {
  137. tolua_pushusertype(L, vector[i], type);
  138. lua_rawseti(L, -2, i + 1);
  139. }
  140. return 1;
  141. }
  142. /// Push PODVector<T, is_arithmetic<T>> to Lua as a table. Use template function overload as non-type partial template specialization is not allowed.
  143. template <typename T> int ToluaPushPODVector(double /*overload*/, lua_State* L, void* data, const char* /*type*/)
  144. {
  145. lua_newtable(L);
  146. const PODVector<T>& vector = *static_cast<const PODVector<T>*>(data);
  147. for (unsigned i = 0; i < vector.Size(); ++i)
  148. {
  149. lua_pushnumber(L, vector[i]);
  150. lua_rawseti(L, -2, i + 1);
  151. }
  152. return 1;
  153. }
  154. // GCC and Clang does not follow the C++ standard in expecting explicit template specialization shall be declared before first use,
  155. // both compilers are able to avoid the multiple definitions of template instantiation symbol during linking by using weak symbol
  156. // MSVC and MinGW, however, follow the standard strictly, hence we need to declare all the explicit template specializations below
  157. // to keep these two compilers happy
  158. // We do not use #ifdef MSVC/MINGW here because GCC and Clang are happy to comply with the C++ standard too
  159. template <> int ToluaIsVector<String>(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
  160. template <> void* ToluaToVector<String>(lua_State* L, int narg, void* def);
  161. template <> int ToluaPushVector<String>(lua_State* L, void* data, const char* type);
  162. template <> int ToluaIsPODVector<bool>(double /*overload*/, lua_State* L, int lo, const char* type, int def, tolua_Error* err);
  163. template <> void* ToluaToPODVector<bool>(double /*overload*/, lua_State* L, int narg, void* def);
  164. template <> int ToluaPushPODVector<bool>(double /*overload*/, lua_State* L, void* data, const char* type);
  165. /// Convert object at the given index and store it in Variant. This function is not thread-safe.
  166. void ToluaToVariant(lua_State* L, int narg, void* def, Variant& variant);
  167. /// Push object stored in a Variant to stack. Empty variant value is pushed as nil.
  168. void ToluaPushVariant(lua_State* L, const Variant* variant, const char* type = 0);
  169. /// Push a registered Lua user type to stack. If the specified type is not yet registered, a nil is pushed instead.
  170. void ToluaPushRegisteredUserType(lua_State* L, void* data, const char* type);
  171. /// Push Object to Lua.
  172. void ToluaPushObject(lua_State* L, void* data, const char* type);