LuaFunction.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #pragma once
  23. struct lua_State;
  24. namespace Urho3D
  25. {
  26. class LuaScript;
  27. class LuaScriptInstance;
  28. /// Lua function.
  29. class URHO3D_API LuaFunction
  30. {
  31. public:
  32. /// Construct.
  33. LuaFunction(lua_State* lusState, int functionRef);
  34. /// Destruct.
  35. ~LuaFunction();
  36. /// Check function is valid.
  37. bool IsValid() const;
  38. /// Begin call function.
  39. bool BeginCall();
  40. /// Begin call script object's function.
  41. bool BeginCall(const LuaScriptInstance* instance);
  42. /// End call function.
  43. bool EndCall(int numReturns = 0);
  44. /// Push int to stack.
  45. void PushInt(int value);
  46. /// Push bool to stack.
  47. void PushBool(bool value);
  48. /// Push float to stack.
  49. void PushFloat(float value);
  50. /// Push string to stack.
  51. void PushString(const String& string);
  52. /// Push user type to stack.
  53. void PushUserType(void* userType, const char* typeName);
  54. /// Push user type to stack.
  55. template <typename T> void PushUserType(const T* userType)
  56. {
  57. PushUserType((void*)userType, T::GetTypeName().CString());
  58. }
  59. /// Push user type to stack.
  60. template <typename T> void PushUserType(const T& userType)
  61. {
  62. PushUserType((void*)&userType, T::GetTypeName().CString());
  63. }
  64. /// Push user type to stack.
  65. template <typename T> void PushUserType(const T* userType, const char* typeName)
  66. {
  67. PushUserType((void*)userType, typeName);
  68. }
  69. /// Push user type to stack.
  70. template <typename T> void PushUserType(const T& userType, const char* typeName)
  71. {
  72. PushUserType((void*)&userType, typeName);
  73. }
  74. /// Push variant to stack.
  75. bool PushVariant(const Variant& variant);
  76. /// Push Lua table to stack.
  77. bool PushLuaTable(const String& tableName);
  78. /// Return function ref.
  79. int GetFunctionRef() const { return functionRef_; }
  80. private:
  81. /// Lua state.
  82. lua_State* luaState_;
  83. /// Function ref.
  84. int functionRef_;
  85. /// Lua stack top.
  86. int stackTop_;
  87. /// Number of arguments.
  88. int numArguments_;
  89. };
  90. }