LuaFunction.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/RefCounted.h"
  5. struct lua_State;
  6. using lua_CFunction = int (*) (lua_State *L);
  7. namespace Urho3D
  8. {
  9. class LuaScript;
  10. class LuaScriptInstance;
  11. class Variant;
  12. /// C++ representation of Lua function object.
  13. class URHO3D_API LuaFunction : public RefCounted
  14. {
  15. public:
  16. /// Construct from a Lua function object at the specified stack index.
  17. LuaFunction(lua_State* L, int index);
  18. /// Construct from a C function.
  19. LuaFunction(lua_State* L, lua_CFunction func);
  20. /// Destruct.
  21. ~LuaFunction() override;
  22. /// Check that function is valid.
  23. bool IsValid() const;
  24. /// Begin function call. When a script object is given then pass it as self argument (first parameter) to the function call.
  25. bool BeginCall(const LuaScriptInstance* instance = nullptr);
  26. /// End call and actually execute the function. The return values, if any, are still left in the stack when this call returns.
  27. bool EndCall(int numReturns = 0);
  28. /// Push int to stack.
  29. void PushInt(int value);
  30. /// Push bool to stack.
  31. void PushBool(bool value);
  32. /// Push float to stack.
  33. void PushFloat(float value);
  34. /// Push double to stack.
  35. void PushDouble(double value);
  36. /// Push string to stack.
  37. void PushString(const String& string);
  38. /// Push user type to stack.
  39. void PushUserType(void* userType, const char* typeName);
  40. /// Push user type to stack.
  41. template <typename T> void PushUserType(const T* userType)
  42. {
  43. PushUserType((void*)userType, T::GetTypeName().CString());
  44. }
  45. /// Push user type to stack.
  46. template <typename T> void PushUserType(const T& userType)
  47. {
  48. PushUserType((void*)&userType, T::GetTypeName().CString());
  49. }
  50. /// Push user type to stack.
  51. template <typename T> void PushUserType(const T* userType, const char* typeName)
  52. {
  53. PushUserType((void*)userType, typeName);
  54. }
  55. /// Push user type to stack.
  56. template <typename T> void PushUserType(const T& userType, const char* typeName)
  57. {
  58. PushUserType((void*)&userType, typeName);
  59. }
  60. /// Push variant to stack.
  61. void PushVariant(const Variant& variant, const char* asType = nullptr);
  62. /// Push Lua table to stack. When the specified table is not found then a nil is pushed instead.
  63. void PushLuaTable(const char* tableName);
  64. /// Push Lua table to stack. When the specified table is not found then a nil is pushed instead.
  65. void PushLuaTable(const String& tableName) { PushLuaTable(tableName.CString()); }
  66. /// Return function ref.
  67. int GetFunctionRef() const { return functionRef_; }
  68. private:
  69. /// Lua state.
  70. lua_State* luaState_;
  71. /// Number of arguments being pushed so far. For internal use only.
  72. int numArguments_;
  73. /// Lua reference to function object.
  74. int functionRef_;
  75. };
  76. }