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();
    }
相关推荐
大白的编程日记.1 小时前
【C++笔记】C++编译器拷贝优化和内存管理
java·开发语言·c++·笔记
CrazyZ1262 小时前
c++primer第九章内存模型和名称空间学习笔记
c++·笔记·学习
小灰灰爱代码3 小时前
C++——将数组a[5]={-1,2,9,-5,7}中小于0的元素置成0。并将其结果输出(要求:用数组名作为函数的参数来实现)
数据结构·c++·算法
冰红茶兑滴水3 小时前
Linux 线程互斥
linux·c++
菜♕卷4 小时前
SpringCloud-07 GateWay01 网关技术
spring·spring cloud·junit
咩咩大主教5 小时前
Linux下的简单TCP客户端和服务器
linux·服务器·c语言·开发语言·c++·tcp/ip·网络编程
玄【学生党】6 小时前
【c++】介绍
开发语言·c++
polebugzhuzhu6 小时前
7-50 畅通工程之局部最小花费问题 (kruskal)
c++·算法·图论
玉蜉蝣7 小时前
PAT甲级-1083 List Grades
c++·算法·list·pat甲
ephemerals__7 小时前
【c++】STL简介
开发语言·c++