luabridge绑定c++返回指针的函数,和绑定c++返回对象的函数区别

示例

复制代码
返回对象写法
MatTest createMatTest(string txt) {
	MatTest res(txt);
	return res;
};

//返回指针写法
MatTest* createMatTest1(string txt) {
	return new MatTest(txt);
};

返回对象的写法lua自动释放内存

返回指针的对象自己释放内存

指针存储对象

复制代码
/**
  Wraps a pointer to a class object inside a Lua userdata.

  The lifetime of the object is managed by C++.
*/
class UserdataPtr : public Userdata
{
private:
    UserdataPtr(UserdataPtr const&);
    UserdataPtr operator=(UserdataPtr const&);

private:
    /** Push a pointer to object using metatable key.
     */
    static void push(lua_State* L, const void* p, void const* const key)
    {
        new (lua_newuserdata(L, sizeof(UserdataPtr))) UserdataPtr(const_cast<void*>(p));
        lua_rawgetp(L, LUA_REGISTRYINDEX, key);
        if (!lua_istable(L, -1))
        {
            lua_pop(L, 1); // possibly: a nil
            throw std::logic_error("The class is not registered in LuaBridge");
        }
        lua_setmetatable(L, -2);
    }

返回对象

复制代码
template<class T>
class UserdataValue : public Userdata
{
private:
    UserdataValue(UserdataValue<T> const&);
    UserdataValue<T> operator=(UserdataValue<T> const&);

    char m_storage[sizeof(T)];

private:
    /**
      Used for placement construction.
    */
    UserdataValue() { m_p = 0; }

    ~UserdataValue()
    {
        if (getPointer() != 0)
        {
            getObject()->~T();
        }
    }

public:
    /**
      Push a T via placement new.

      The caller is responsible for calling placement new using the
      returned uninitialized storage.

      @param L A Lua state.
      @returns An object referring to the newly created userdata value.
    */
    static UserdataValue<T>* place(lua_State* const L)
    {
        UserdataValue<T>* const ud =
            new (lua_newuserdata(L, sizeof(UserdataValue<T>))) UserdataValue<T>();
        lua_rawgetp(L, LUA_REGISTRYINDEX, detail::getClassRegistryKey<T>());
        if (!lua_istable(L, -1))
        {
            throw std::logic_error("The class is not registered in LuaBridge");
        }
        lua_setmetatable(L, -2);
        return ud;
    }

    /**
      Push T via copy construction from U.

      @tparam U A container type.
      @param  L A Lua state.
      @param  u A container object reference.
    */
    template<class U>
    static inline void push(lua_State* const L, U const& u)
    {
        UserdataValue<T>* ud = place(L);
        new (ud->getObject()) U(u);
        ud->commit();
    }
相关推荐
weixin_4241836110 小时前
开放—封闭原则实战(下):基于策略的流程编排与编译期多态
c++·分布式·系统架构
wuminyu11 小时前
C++协程实现接收端的零拷贝Buffer管理原理剖析
java·linux·c语言·jvm·c++
m0_3807438711 小时前
C++中的freopen的用法实例详解
开发语言·c++
jimy112 小时前
polymorph.cpp里面的“析构函数、智能指针”问题
开发语言·c++
朝朝辞暮i12 小时前
C++ 第 6 课:switch —— 多状态选择
开发语言·c++·算法
朝朝辞暮i13 小时前
C++ 第 4 课:if / else —— 让程序自己做决定
开发语言·c++
苏打豆13 小时前
Pinocchio源码阅读——刚体运动物理量空间变换及求导
c++·线性代数·算法·矩阵·机器人
m0_3807438713 小时前
Qt中导航栏实现的详细指南
开发语言·c++
估值探索者13 小时前
【Python量化策略实战 #02】多因子合成mom和vol两因子打分合成
开发语言·c++·python·数据挖掘·c#
思麟呀13 小时前
位运算:从一个整数到一个集合
c语言·数据结构·c++·算法