ToluaUtils.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Copyright (c) 2008-2014 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 "Context.h"
  24. #include "OctreeQuery.h"
  25. #include "PhysicsWorld.h"
  26. #include "Vector2.h"
  27. #include "Vector3.h"
  28. struct lua_State;
  29. namespace Urho3D
  30. {
  31. class Context;
  32. class SoundSource;
  33. class UIElement;
  34. }
  35. using namespace Urho3D;
  36. /// Check is String.
  37. #define tolua_isurho3dstring tolua_isstring
  38. /// Push String.
  39. #define tolua_pushurho3dstring(x, y) tolua_pushstring(x, y.CString())
  40. /// Convert to String.
  41. const char* tolua_tourho3dstring(lua_State* L, int narg, const char* str);
  42. /// Convert to String.
  43. const char* tolua_tourho3dstring(lua_State* L, int narg, const String& str);
  44. /// Set context.
  45. void SetContext(lua_State* L, Context* context);
  46. /// Return context.
  47. Context* GetContext(lua_State* L);
  48. /// Create object.
  49. template<typename T> int ToluaNewObject(lua_State* tolua_S)
  50. {
  51. T* object = Mtolua_new(T(GetContext(tolua_S)));
  52. tolua_pushusertype(tolua_S, (void*)object,T::GetTypeNameStatic().CString());
  53. return 1;
  54. }
  55. /// Create object with garbage collection.
  56. template<typename T> int ToluaNewObjectGC(lua_State* tolua_S)
  57. {
  58. T* object = Mtolua_new(T(GetContext(tolua_S)));
  59. tolua_pushusertype(tolua_S, (void*)object,T::GetTypeNameStatic().CString());
  60. tolua_register_gc(tolua_S, lua_gettop(tolua_S));
  61. return 1;
  62. }
  63. /// Return subsystem.
  64. template<typename T> int ToluaGetSubsystem(lua_State* tolua_S)
  65. {
  66. T* subsystem = GetContext(tolua_S)->GetSubsystem<T>();
  67. tolua_pushusertype(tolua_S, (void*)subsystem, T::GetTypeNameStatic().CString());
  68. return 1;
  69. }
  70. /// Check is Vector<T>.
  71. template<typename T> int ToluaIsVector(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
  72. /// Check is Vector<String>.
  73. template<> int ToluaIsVector<String>(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
  74. /// Convert to Vector<T>.
  75. template<typename T> void* ToluaToVector(lua_State* L, int narg, void* def);
  76. /// Convert to Vector<String>.
  77. template<> void* ToluaToVector<String>(lua_State* L, int narg, void* def);
  78. /// Push Vector<T> to Lua as a table.
  79. template<typename T> int ToluaPushVector(lua_State*L, void* data, const char* type);
  80. /// Push Vector<String> to Lua as a table.
  81. template<> int ToluaPushVector<String>(lua_State* L, void* data, const char* type);
  82. /// Check is PODVector<T>.
  83. template<typename T> int ToluaIsPODVector(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
  84. /// Check is PODVector<unsigned>.
  85. template<> int ToluaIsPODVector<unsigned>(lua_State* L, int lo, const char* type, int def, tolua_Error* err);
  86. /// Convert to PODVector<T>.
  87. template<typename T> void* ToluaToPODVector(lua_State* L, int narg, void* def);
  88. /// Convert PODVector<unsigned>.
  89. template<> void* ToluaToPODVector<unsigned>(lua_State* L, int narg, void* def);
  90. /// Push PODVector<T> to Lua as a table.
  91. template<typename T> int ToluaPushPODVector(lua_State* L, void* data, const char* type);
  92. /// Push PODVector<int> to Lua as a table.
  93. template<> int ToluaPushPODVector<int>(lua_State* L, void* data, const char* type);
  94. /// Push PODVector<unsigned> to Lua as a table.
  95. template<> int ToluaPushPODVector<unsigned>(lua_State* L, void* data, const char* type);
  96. /// Push PODVector<SoundSource*> to Lua as a table.
  97. template<> int ToluaPushPODVector<SoundSource*>(lua_State* L, void* data, const char* type);
  98. /// Push PODVector<UIElement*> to Lua as a table.
  99. template<> int ToluaPushPODVector<UIElement*>(lua_State* L, void* data, const char* type);
  100. /// Push PODVector<Vector3> to Lua as a table.
  101. template<> int ToluaPushPODVector<Vector3>(lua_State* L, void* data, const char* type);
  102. /// Push PODVector<IntVector2> to Lua as a table.
  103. template<> int ToluaPushPODVector<IntVector2>(lua_State* L, void* data, const char* type);
  104. /// Push PODVector<OctreeQueryResult> to Lua as a table.
  105. template<> int ToluaPushPODVector<OctreeQueryResult>(lua_State* L, void* data, const char* type);
  106. /// Push PODVector<PhysicsRaycastResult> to Lua as a table.
  107. template<> int ToluaPushPODVector<PhysicsRaycastResult>(lua_State* L, void* data, const char* type);
  108. /// Push PODVector<RayQueryResult> to Lua as a table.
  109. template<> int ToluaPushPODVector<RayQueryResult>(lua_State* L, void* data, const char* type);