ToluaUtils.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. // Lua state to context mapping
  34. static HashMap<void*, Context*> contextMapping;
  35. void SetContext(lua_State* L, Context* context)
  36. {
  37. if (context == 0)
  38. contextMapping.Erase(L);
  39. else
  40. contextMapping[L] = context;
  41. }
  42. Context* GetContext(lua_State* L)
  43. {
  44. HashMap<void*, Context*>::ConstIterator i = contextMapping.Find(L);
  45. if (i == contextMapping.End())
  46. {
  47. lua_State* L1 = lua_getmainthread(L);
  48. return (L == L1) ? 0 : GetContext(L1);
  49. }
  50. return i->second_;
  51. }
  52. // Explicit template specialization only required for StringVector
  53. template <> int ToluaIsVector<String>(lua_State* L, int lo, const char* /*type*/, int def, tolua_Error* err)
  54. {
  55. return tolua_isstringarray(L, lo, -1, def, err);
  56. }
  57. template <> void* ToluaToVector<String>(lua_State* L, int narg, void* /*def*/)
  58. {
  59. if (!lua_istable(L, narg))
  60. return 0;
  61. static Vector<String> result;
  62. result.Clear();
  63. result.Resize((unsigned)lua_objlen(L, narg));
  64. for (unsigned i = 0; i < result.Size(); ++i)
  65. {
  66. lua_rawgeti(L, narg, i + 1);
  67. result[i] = tolua_tourho3dstring(L, -1, "");
  68. lua_pop(L, 1);
  69. }
  70. return &result;
  71. }
  72. template <> int ToluaPushVector<String>(lua_State* L, void* data, const char* /*type*/)
  73. {
  74. lua_newtable(L);
  75. const Vector<String>& vector = *static_cast<const Vector<String>*>(data);
  76. for (unsigned i = 0; i < vector.Size(); ++i)
  77. {
  78. tolua_pushurho3dstring(L, vector[i]);
  79. lua_rawseti(L, -2, i + 1);
  80. }
  81. return 1;
  82. }
  83. // Explicit template specialization only required for boolean
  84. template <> int ToluaIsPODVector<bool>(double /*overload*/, lua_State* L, int lo, const char* /*type*/, int def, tolua_Error* err)
  85. {
  86. return tolua_isbooleanarray(L, lo, -1, def, err);
  87. }
  88. template <> void* ToluaToPODVector<bool>(double /*overload*/, lua_State* L, int narg, void* /*def*/)
  89. {
  90. if (!lua_istable(L, narg))
  91. return 0;
  92. static PODVector<bool> result;
  93. result.Clear();
  94. result.Resize((unsigned)lua_objlen(L, narg));
  95. for (unsigned i = 0; i < result.Size(); ++i)
  96. {
  97. lua_rawgeti(L, narg, i + 1);
  98. result[i] = (bool)tolua_toboolean(L, -1, 0);
  99. lua_pop(L, 1);
  100. }
  101. return &result;
  102. }
  103. template <> int ToluaPushPODVector<bool>(double /*overload*/, lua_State* L, void* data, const char* /*type*/)
  104. {
  105. lua_newtable(L);
  106. const PODVector<bool>& vector = *static_cast<const PODVector<bool>*>(data);
  107. for (unsigned i = 0; i < vector.Size(); ++i)
  108. {
  109. lua_pushboolean(L, vector[i]);
  110. lua_rawseti(L, -2, i + 1);
  111. }
  112. return 1;
  113. }
  114. void ToluaPushObject(lua_State* L, void* data, const char* type)
  115. {
  116. tolua_pushusertype(L, data, data ? static_cast<Object*>(data)->GetTypeName().CString() : type);
  117. }