首页 > 其他分享 >UnLua支持TFunction

UnLua支持TFunction

时间:2022-10-09 16:24:16浏览次数:49  
标签:Index return 支持 lua yao TFunction template UnLua

需求

在c++使用TFunction接受Lua传过来的闭包函数,调用后调用Lua函数,并能正确的获得返回。

扩展

Get

//yao
  template<typename R, typename...Args>
  FORCEINLINE TFunction< R(Args...)> Get(lua_State* L, int32 Index, TType<TFunction<R(Args...)>>)
  {
      auto env = FLuaEnv::FindEnv(L);
      FLuaFunction func(env, FLuaValue(env, Index));
      return [](Args... args)->R {return func.Call(args...); };
  }

TType

//yao
template<typename R, typename...Args> struct UnLua::TType<TFunction<R(Args...)>, false>
  {
      static const char* GetName() { return "TFunction"; }
  };

可以实现的更好点,返回的Name包含TFunction的R和参数类型

isType

//yao
    template<typename R, typename...Args>
    FORCEINLINE bool IsType(lua_State* L, int32 Index, TType<TFunction<R(Args...)>>)
    {
        return lua_isfunction(L, Index);
    }

Push

//yao
    template<typename R, typename...Args>
    FORCEINLINE int32 Push(lua_State* L, TFunction<R(Args...)>&& V, bool bCopy = false)
    {
        return 0;
    }

返回nil, 等有明确需求期望再修改

导出

BEGIN_EXPORT_CLASS(Application)
ADD_STATIC_FUNCTION(GetInstance)
ADD_FUNCTION(OnIdle)
END_EXPORT_CLASS()
IMPLEMENT_EXPORTED_CLASS(Application)
class Application :public Singleton<Application>
{
private:
	TFunction<void(float)> _idle;
public:
	void OnIdle(TFunction<void(float)> _onIdle);

	void CallIdle(float e);
};

标签:Index,return,支持,lua,yao,TFunction,template,UnLua
From: https://www.cnblogs.com/marcher/p/16772531.html

相关文章