diff options
author | Robin Gareus <robin@gareus.org> | 2016-07-07 04:44:36 +0200 |
---|---|---|
committer | Robin Gareus <robin@gareus.org> | 2016-07-07 15:37:11 +0200 |
commit | 44a3f042a7a878af45ae893e0361d6a8be315da4 (patch) | |
tree | b7ee5d92f24aca24a7de4084f623dca0ea5eb68a /libs/lua/LuaBridge | |
parent | 225a8a47a419e5e67a27b604bfd912498e3ad2cc (diff) |
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.
Diffstat (limited to 'libs/lua/LuaBridge')
-rw-r--r-- | libs/lua/LuaBridge/detail/LuaRef.h | 5 | ||||
-rw-r--r-- | libs/lua/LuaBridge/detail/Userdata.h | 14 |
2 files changed, 19 insertions, 0 deletions
diff --git a/libs/lua/LuaBridge/detail/LuaRef.h b/libs/lua/LuaBridge/detail/LuaRef.h index 8fae1c038a..46c756445e 100644 --- a/libs/lua/LuaBridge/detail/LuaRef.h +++ b/libs/lua/LuaBridge/detail/LuaRef.h @@ -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. diff --git a/libs/lua/LuaBridge/detail/Userdata.h b/libs/lua/LuaBridge/detail/Userdata.h index e832ef2dc1..feab28f132 100644 --- a/libs/lua/LuaBridge/detail/Userdata.h +++ b/libs/lua/LuaBridge/detail/Userdata.h @@ -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. */ |