UnLua支持TFunction

需求

在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...); };
  }

TIsPrimitiveType

 template<typename R, typename...Args> struct TIsPrimitiveType<TFunction<R(Args...)>> { enum { Value = true }; };

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);
};

posted on 2022-10-09 16:12  marcher  阅读(112)  评论(0编辑  收藏  举报

导航