LuaFunction.h 3.3 KB

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