prepare sharing C++ class instances across lua-interpreters

in particular: lua-lifefime (!) C++ instances.
This allows for dynamic allocation of custom user-data, bound to
the lifetime of the allocating lua-context.
This commit is contained in:
Robin Gareus
2016-07-07 04:44:36 +02:00
parent 225a8a47a4
commit 44a3f042a7
5 changed files with 217 additions and 1 deletions

View File

@@ -180,6 +180,11 @@ private:
return *this;
}
// the implementation needs UserdataPtr, which
// is not yet defined here.
// -> libs/ardour/lua_api.cc
Proxy& clone_instance (const void* key, void* p);
//--------------------------------------------------------------------------
/**
Assign a new value to this table key.

View File

@@ -302,6 +302,11 @@ ud __parent (nil)
public:
virtual ~Userdata () { }
static void* get_ptr (lua_State* L, int index) {
Userdata* ud = static_cast <Userdata*> (lua_touserdata (L, index));
return ud->m_p;
}
//--------------------------------------------------------------------------
/**
Returns the Userdata* if the class on the Lua stack matches.
@@ -457,6 +462,15 @@ private:
assert (m_p != 0);
}
friend class LuaRef;
static inline void push_raw (lua_State* const L, void* p, const void* classkey)
{
new (lua_newuserdata (L, sizeof (UserdataPtr))) UserdataPtr (p);
lua_rawgetp (L, LUA_REGISTRYINDEX, classkey);
assert (lua_istable (L, -1));
lua_setmetatable (L, -2);
}
public:
/** Push non-const pointer to object.
*/