Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn already existing objects from C++ to Lua
    primarykey
    data
    text
    <p>In C++, say I have a class that creates a binary tree like structure and I use it something like this:</p> <pre><code>CTreeRoot* root = new CTreeRoot(/* whatever */); CNode* leftNode = root-&gt;getLeftNode(); CNode* rightNode = root-&gt;getRightNOde(); leftNode-&gt;doSomething(); rightNode-&gt;doSomething(); // etc </code></pre> <p>And assume that the left and right nodes have their own left and right nodes (hence, a binary tree). Now I want to expose this to Lua (<strong>not</strong> using luabind) so I can do the same kind of thing:</p> <pre><code>local root = treeroot.new(/* whatever */) local left = root:getLeftNode() local right = root:getRightNode() left:doSomething(); right:doSomething(); </code></pre> <p>I've gotten most of it to work. However, for the getLeftNode() and getRightNode() methods, I'm pretty sure I'm doing it "wrong". Here is how I'm implementing getLeftNode() in C++ for example:</p> <pre><code>int MyLua::TreeRootGetLeftNode(luaState* L) { CTreeRoot* root = (CTreeRoot*)luaL_checkudata(L, 1, "MyLua.treeroot"); CNode* leftNode = root-&gt;getLeftNode(); if (leftNode != NULL) { int size = sizeof(CNode); // create a new copy of the CNode object inplace of the memory Lua // allocated for us new ((CNode*)lua_newuserdata(L,size)) CNode((const CNode&amp;)*leftNode); lua_setmetatable(L, "MyLua.treenode"); } else { lua_pushnil(L); } return 1; } </code></pre> <p>I cast the userdata back to a CTreeRoot object, call getLeftNode(), make sure it exists and then (here's the "wrong part") I create another userdata data object with a copy constructor copying the object I want to return. </p> <p>Is this "standard practice" for this type of scenario? It seems like you would want to avoid creating another copy of the object since what you really want is just a reference to an already existing object. </p> <p>It sounds like this would be the perfect place for lightuserdata since I don't want to create a new object, I would be happy returning the object that already exists. The problem, though, is that lightuserdata has no meta table so the objects would be useless to me once I got them back. Basically, I want to do something like:</p> <pre><code>int MyLua::TreeRootGetLeftNode(luaState* L) { CTreeRoot* root = (CTreeRoot*)luaL_checkudata(L, 1, "MyLua.treeroot"); CNode* leftNode = root-&gt;getLeftNode(); if (leftNode != NULL) { // "WRONG" CODE BUT SHOWS WHAT I WISH I COULD DO lua_pushlightuserdata(L, (void*)leftNode); lua_setmetatable(L, "MyLua.treenode"); } else { lua_pushnil(L); } return 1; } </code></pre> <p>Can someone please tell me how I can have my <code>MyLua::TreeRootGetLeftNode</code> method return to Lua a copy of the object that already exists in such a way that I can use that object as an 'object' in Lua?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload