LuaFunction.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // Copyright (c) 2008-2017 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 "../Container/RefCounted.h"
  24. struct lua_State;
  25. typedef int (*lua_CFunction) (lua_State *L);
  26. namespace Urho3D
  27. {
  28. class LuaScript;
  29. class LuaScriptInstance;
  30. class Variant;
  31. /// C++ representation of Lua function object.
  32. class URHO3D_API LuaFunction : public RefCounted
  33. {
  34. public:
  35. /// Construct from a Lua function object at the specified stack index.
  36. LuaFunction(lua_State* L, int index);
  37. /// Construct from a C function.
  38. LuaFunction(lua_State* L, lua_CFunction func);
  39. /// Destruct.
  40. ~LuaFunction();
  41. /// Check that function is valid.
  42. bool IsValid() const;
  43. /// Begin function call. When a script object is given then pass it as self argument (first parameter) to the function call.
  44. bool BeginCall(const LuaScriptInstance* instance = 0);
  45. /// End call and actually execute the function. The return values, if any, are still left in the stack when this call returns.
  46. bool EndCall(int numReturns = 0);
  47. /// Push int to stack.
  48. void PushInt(int value);
  49. /// Push bool to stack.
  50. void PushBool(bool value);
  51. /// Push float to stack.
  52. void PushFloat(float value);
  53. /// Push double to stack.
  54. void PushDouble(double value);
  55. /// Push string to stack.
  56. void PushString(const String& string);
  57. /// Push user type to stack.
  58. void PushUserType(void* userType, const char* typeName);
  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)
  66. {
  67. PushUserType((void*)&userType, T::GetTypeName().CString());
  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 user type to stack.
  75. template <typename T> void PushUserType(const T& userType, const char* typeName)
  76. {
  77. PushUserType((void*)&userType, typeName);
  78. }
  79. /// Push variant to stack.
  80. void PushVariant(const Variant& variant, const char* asType = 0);
  81. /// Push Lua table to stack. When the specified table is not found then a nil is pushed instead.
  82. void PushLuaTable(const char* tableName);
  83. /// Push Lua table to stack. When the specified table is not found then a nil is pushed instead.
  84. void PushLuaTable(const String& tableName) { PushLuaTable(tableName.CString()); }
  85. /// Return function ref.
  86. int GetFunctionRef() const { return functionRef_; }
  87. private:
  88. /// Lua state.
  89. lua_State* luaState_;
  90. /// Number of arguments being pushed so far. For internal use only.
  91. int numArguments_;
  92. /// Lua reference to function object.
  93. int functionRef_;
  94. };
  95. }