tolua++urho3d.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // Copyright (c) 2008-2013 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 "tolua++urho3d.h"
  24. #include "HttpRequest.h"
  25. #include "Ptr.h"
  26. const char* tolua_tourho3dstring(lua_State* L, int narg, const char* def)
  27. {
  28. const char* s = tolua_tostring(L, narg, def);
  29. return s ? s : "";
  30. }
  31. const char* tolua_tourho3dstring(lua_State* L, int narg, const String& def)
  32. {
  33. return tolua_tourho3dstring(L, narg, def.CString());
  34. }
  35. void* tolua_tourho3dhttprequest(lua_State* L, int reg, void* def)
  36. {
  37. void* userdata = tolua_tousertype(L, reg, def);
  38. /// Use data type may be SharedPtr<HttpRequest>.
  39. const char* type = tolua_typename(L, reg);
  40. if (strcmp(type, "SharedPtr<HttpRequest>") == 0)
  41. return *static_cast<SharedPtr<HttpRequest>*>(userdata);
  42. return userdata;
  43. }
  44. int tolua_pushurho3dconstpodvectorintvector2(lua_State* L, void* data, const char* type)
  45. {
  46. #ifndef TOLUA_RELEASE
  47. assert(strcmp(type, "const PODVector<IntVector2>") == 0);
  48. #endif
  49. const PODVector<IntVector2>& vector = *((const PODVector<IntVector2>*)data);
  50. lua_newtable(L);
  51. for (unsigned i = 0; i < vector.Size(); ++i)
  52. {
  53. void* tolua_obj = Mtolua_new((IntVector2)(vector[i]));
  54. tolua_pushusertype(L,tolua_obj,"IntVector2");
  55. tolua_register_gc(L,lua_gettop(L));
  56. lua_rawseti(L, -2, i + 1);
  57. }
  58. return 1;
  59. }
  60. int tolua_pushurho3dpodvectoruielement(lua_State* L, void* data, const char* type)
  61. {
  62. #ifndef TOLUA_RELEASE
  63. assert(strcmp(type, "const PODVector<UIElement*>") == 0);
  64. #endif
  65. const PODVector<UIElement*>& vector = *((const PODVector<UIElement*>*)data);
  66. lua_newtable(L);
  67. for (unsigned i = 0; i < vector.Size(); ++i)
  68. {
  69. tolua_pushusertype(L, vector[i], "UIElement");
  70. lua_rawseti(L, -2, i + 1);
  71. }
  72. return 1;
  73. }
  74. int tolua_isurho3dconstpodvectorunsigned(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  75. {
  76. if (lua_istable(L, lo))
  77. {
  78. int length = lua_objlen(L, lo);
  79. for (int i = 1; i <= length; ++i)
  80. {
  81. lua_pushinteger(L, i);
  82. lua_gettable(L, lo);
  83. if (!lua_isnumber(L, -1))
  84. {
  85. lua_pop(L, 1);
  86. err->index = lo;
  87. err->array = 0;
  88. err->type = type;
  89. return 0;
  90. }
  91. lua_pop(L, 1);
  92. }
  93. return 1;
  94. }
  95. err->index = lo;
  96. err->array = 0;
  97. err->type = type;
  98. return 0;
  99. }
  100. void* tolua_tourho3dconstpodvectorunsigned(lua_State* L, int narg, void* def)
  101. {
  102. if (!lua_istable(L, narg))
  103. return 0;
  104. static Vector<unsigned> result;
  105. result.Clear();
  106. int length = lua_objlen(L, narg);
  107. for (int i = 1; i <= length; ++i)
  108. {
  109. lua_pushinteger(L, i);
  110. lua_gettable(L, narg);
  111. if (!lua_isnumber(L, -1))
  112. {
  113. lua_pop(L, 1);
  114. return 0;
  115. }
  116. unsigned value = (unsigned)tolua_tonumber(L, -1, 0);
  117. result.Push(value);
  118. lua_pop(L, 1);
  119. }
  120. return &result;
  121. }
  122. int tolua_pushurho3dconstpodvectorunsigned(lua_State* L, void* data, const char* type)
  123. {
  124. #ifndef TOLUA_RELEASE
  125. assert(strcmp(type, "const PODVector<unsigned>") == 0);
  126. #endif
  127. const PODVector<unsigned>& vector = *((const PODVector<unsigned>*)data);
  128. lua_newtable(L);
  129. for (unsigned i = 0; i < vector.Size(); ++i)
  130. {
  131. lua_pushinteger(L, vector[i]);
  132. lua_rawseti(L, -2, i + 1);
  133. }
  134. return 1;
  135. }
  136. int tolua_isurho3dconstvectorstring(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  137. {
  138. if (lua_istable(L, lo))
  139. {
  140. int length = lua_objlen(L, lo);
  141. for (int i = 1; i <= length; ++i)
  142. {
  143. lua_pushinteger(L, i);
  144. lua_gettable(L, lo);
  145. if (!lua_isstring(L, -1))
  146. {
  147. lua_pop(L, 1);
  148. err->index = lo;
  149. err->array = 0;
  150. err->type = type;
  151. return 0;
  152. }
  153. lua_pop(L, 1);
  154. }
  155. return 1;
  156. }
  157. err->index = lo;
  158. err->array = 0;
  159. err->type = type;
  160. return 0;
  161. }
  162. void* tolua_tourho3dconstvectorstring(lua_State* L, int narg, void* def)
  163. {
  164. if (!lua_istable(L, narg))
  165. return 0;
  166. static Vector<String> result;
  167. result.Clear();
  168. int length = lua_objlen(L, narg);
  169. for (int i = 1; i <= length; ++i)
  170. {
  171. lua_pushinteger(L, i);
  172. lua_gettable(L, narg);
  173. if (!lua_isstring(L, -1))
  174. {
  175. lua_pop(L, 1);
  176. return 0;
  177. }
  178. String string = tolua_tourho3dstring(L, -1, "");
  179. result.Push(string);
  180. lua_pop(L, 1);
  181. }
  182. return &result;
  183. }
  184. int tolua_pushurho3dconstvectorstring(lua_State*L, void* data, const char* type)
  185. {
  186. #ifndef TOLUA_RELEASE
  187. assert(strcmp(type, "const Vector<String>") == 0);
  188. #endif
  189. const Vector<String>& vectorstring = *((const Vector<String>*)data);
  190. lua_newtable(L);
  191. for (unsigned i = 0; i < vectorstring.Size(); ++i)
  192. {
  193. tolua_pushurho3dstring(L, vectorstring[i]);
  194. lua_rawseti(L, -2, i + 1);
  195. }
  196. return 1;
  197. }