ToluaUtils.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include "../Precompiled.h"
  23. #include <toluapp/tolua++.h>
  24. #include "../LuaScript/ToluaUtils.h"
  25. const char* tolua_tourho3dstring(lua_State* L, int narg, const char* str)
  26. {
  27. return tolua_tostring(L, narg, str);
  28. }
  29. const char* tolua_tourho3dstring(lua_State* L, int narg, const String& str)
  30. {
  31. return tolua_tourho3dstring(L, narg, str.CString());
  32. }
  33. void SetContext(lua_State* L, Context* context)
  34. {
  35. assert(L && context);
  36. tolua_pushusertype(L, static_cast<void*>(context), "Context");
  37. lua_setglobal(L, ".context"); // This property is internal, the exposed 'context' property is obtained via GetContext() call
  38. }
  39. Context* GetContext(lua_State* L)
  40. {
  41. lua_getglobal(L, ".context");
  42. if (lua_isnil(L, -1))
  43. {
  44. lua_State* L1 = lua_getmainthread(L);
  45. return (L == L1) ? 0 : GetContext(L1);
  46. }
  47. tolua_Error error; // Ensure we are indeed getting a Context object before casting
  48. return tolua_isusertype(L, -1, "Context", 0, &error) ? static_cast<Context*>(tolua_tousertype(L, -1, 0)) : 0;
  49. }
  50. // Explicit template specialization only required for StringVector
  51. template <> int ToluaIsVector<String>(lua_State* L, int lo, const char* /*type*/, int def, tolua_Error* err)
  52. {
  53. return tolua_isstringarray(L, lo, -1, def, err);
  54. }
  55. template <> void* ToluaToVector<String>(lua_State* L, int narg, void* /*def*/)
  56. {
  57. if (!lua_istable(L, narg))
  58. return 0;
  59. static Vector<String> result;
  60. result.Clear();
  61. result.Resize((unsigned)lua_objlen(L, narg));
  62. for (unsigned i = 0; i < result.Size(); ++i)
  63. {
  64. lua_rawgeti(L, narg, i + 1);
  65. result[i] = tolua_tourho3dstring(L, -1, "");
  66. lua_pop(L, 1);
  67. }
  68. return &result;
  69. }
  70. template <> int ToluaPushVector<String>(lua_State* L, void* data, const char* /*type*/)
  71. {
  72. lua_newtable(L);
  73. const Vector<String>& vector = *static_cast<const Vector<String>*>(data);
  74. for (unsigned i = 0; i < vector.Size(); ++i)
  75. {
  76. tolua_pushurho3dstring(L, vector[i]);
  77. lua_rawseti(L, -2, i + 1);
  78. }
  79. return 1;
  80. }
  81. // Explicit template specialization only required for boolean
  82. template <> int ToluaIsPODVector<bool>(double /*overload*/, lua_State* L, int lo, const char* /*type*/, int def, tolua_Error* err)
  83. {
  84. return tolua_isbooleanarray(L, lo, -1, def, err);
  85. }
  86. template <> void* ToluaToPODVector<bool>(double /*overload*/, lua_State* L, int narg, void* /*def*/)
  87. {
  88. if (!lua_istable(L, narg))
  89. return 0;
  90. static PODVector<bool> result;
  91. result.Clear();
  92. result.Resize((unsigned)lua_objlen(L, narg));
  93. for (unsigned i = 0; i < result.Size(); ++i)
  94. {
  95. lua_rawgeti(L, narg, i + 1);
  96. result[i] = (bool)tolua_toboolean(L, -1, 0);
  97. lua_pop(L, 1);
  98. }
  99. return &result;
  100. }
  101. template <> int ToluaPushPODVector<bool>(double /*overload*/, lua_State* L, void* data, const char* /*type*/)
  102. {
  103. lua_newtable(L);
  104. const PODVector<bool>& vector = *static_cast<const PODVector<bool>*>(data);
  105. for (unsigned i = 0; i < vector.Size(); ++i)
  106. {
  107. lua_pushboolean(L, vector[i]);
  108. lua_rawseti(L, -2, i + 1);
  109. }
  110. return 1;
  111. }
  112. void ToluaPushObject(lua_State* L, void* data, const char* type)
  113. {
  114. tolua_pushusertype(L, data, data ? static_cast<Object*>(data)->GetTypeName().CString() : type);
  115. }