LuaFunction.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // Copyright (c) 2008-2015 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. namespace Urho3D
  26. {
  27. class LuaScript;
  28. class LuaScriptInstance;
  29. class Variant;
  30. /// Lua function.
  31. class URHO3D_API LuaFunction : public RefCounted
  32. {
  33. public:
  34. /// Construct.
  35. LuaFunction(lua_State* lusState, int functionRef, bool needUnref = true);
  36. /// Destruct.
  37. ~LuaFunction();
  38. /// Check that function is valid.
  39. bool IsValid() const;
  40. /// Begin function call. When a script object is given then pass it as self argument (first parameter) to the function call.
  41. bool BeginCall(const LuaScriptInstance* instance = 0);
  42. /// End call and actually execute the 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 double to stack.
  51. void PushDouble(double value);
  52. /// Push string to stack.
  53. void PushString(const String& string);
  54. /// Push user type to stack.
  55. void PushUserType(void* userType, const char* typeName);
  56. /// Push user type to stack.
  57. template <typename T> void PushUserType(const T* userType)
  58. {
  59. PushUserType((void*)userType, T::GetTypeName().CString());
  60. }
  61. /// Push user type to stack.
  62. template <typename T> void PushUserType(const T& userType)
  63. {
  64. PushUserType((void*)&userType, T::GetTypeName().CString());
  65. }
  66. /// Push user type to stack.
  67. template <typename T> void PushUserType(const T* userType, const char* typeName)
  68. {
  69. PushUserType((void*)userType, typeName);
  70. }
  71. /// Push user type to stack.
  72. template <typename T> void PushUserType(const T& userType, const char* typeName)
  73. {
  74. PushUserType((void*)&userType, typeName);
  75. }
  76. /// Push variant to stack.
  77. void PushVariant(const Variant& variant, const char* asType = 0);
  78. /// Push Lua table to stack. When the specified table is not found then a nil is pushed instead.
  79. void PushLuaTable(const String& tableName);
  80. /// Return function ref.
  81. int GetFunctionRef() const { return functionRef_; }
  82. private:
  83. /// Lua state.
  84. lua_State* luaState_;
  85. /// Function ref.
  86. int functionRef_;
  87. /// Need unref.
  88. bool needUnref_;
  89. /// Number of arguments being pushed so far. For internal use only.
  90. int numArguments_;
  91. };
  92. }