Copying a container on function call... (Was Re: return_stl_iterator acting as const?)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Copying a container on function call... (Was Re: return_stl_iterator acting as const?)

John Smith-124
So I'm still having a problem with this, looking into it more it seems that the problem is the Copy Construct is being called via:
     server.exe!luabind::detail::value_converter::apply<ContainedObject>(lua_State * L=0x00f45ca8, const ContainedObject& ref={...})  Line 299 + 0x2f bytes    C++
     server.exe!luabind::detail::convert_to_lua<ContainedObject>(lua_State * L=0x00f45ca8, const ContainedObject& v={...})  Line 74    C++
     server.exe!luabind::detail::iterator<std::_Vector_iterator<ContainedObject,std::allocator<ContainedObject> > >::next(lua_State * L=0x00f45ca8)  Line 24 + 0x12 bytes    C++
specifically:
object_ptr = new T(ref);

So we're deffinetly making a copy of the vector when we go into the function exec.  Is there anyway to specify to pass the member variable by reference and not by copy?  Due to some constraints of other APIs I'm using, I have to store some objects I'm trying to expose to lua in the container, I can't just pass them by a pointer. 

Any thoughts as to things to try?


On Tue, Mar 3, 2009 at 2:04 AM, John Smith <[hidden email]> wrote:
I hate replying to myself but I also noticed that this problem is only in the case where the vector is storing an object not a pointer to an object.  (Which is why I think I'm hitting a copy constructor at some point instead of having it pass by reference)

If I was to modify the container to be std::vector<ContainedObject *> vec;, and modify the test code to be:
ContainerObject cont;
ContainedObject* Obj = new ContainedObject;


Obj->value = 10;

cont.push_back(Obj);

Then when I come out of the call_function Obj has the expected value of 5. 

Anyone have other ideas/thoughts?


On Tue, Mar 3, 2009 at 1:57 AM, John Smith <[hidden email]> wrote:
I don't believe so, changing that to be : gave the following errors:
print(cont:containervalue)
function arguments expected near ')' for that line


for things in cont:vec
gave the error:
 function arguments expected near 'do'

My understanding of the : notation was that it was a shorthand of:
x:y is equivilant to x.y(x)

Therefore, trying:
for things in cont:vec()

gave a luabind exception of 'No such operator defined'



On Tue, Mar 3, 2009 at 1:26 AM, James Porter <[hidden email]> wrote:
John Smith wrote:
> Lua code:
> function LuaFunction( cont )
>     print (cont.containervalue)
>     cont.containervalue = 30
>     for things in cont.vec
>     do
>         print (things.value)
>         things.vlaue = 5
>     end
> end

It looks like you're using the wrong syntax for getting at your members.
In Lua, "." is similar to "::" in C++, while ":" in Lua is similar to
"." in C++. You want cont:containervalue, cont:vec, and so on. The
former is working because you're defining a new static variable,
essentially.

- Jim

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user




------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Copying a container on function call... (Was Re: return_stl_iterator acting as const?)

John Smith-124
Well another 2 days on this and still no closer to figuring it out...  I tried to wrap a custom iterator around the class:

typedef std::vector<ContainedObject> ContainedObjectVec;

class ContainedObjectVectorIterator
{
public:
    ContainedObjectVectorIterator(ContainedObjectVec* pVec)
        : m_pVec(pVec)
    {
    }

    struct iterator : boost::iterator_adaptor<iterator, ContainedObject*>
    {
        iterator(ContainedObject* p) : iterator::iterator_adaptor_(p)
        {

        }

        iterator(iterator const& other) : iterator::iterator_adaptor_(other)
        {

        }
    };

    iterator begin()
    {
        return iterator( m_pVec->begin()._Myptr );
    }

    iterator end()
    {
        return iterator( m_pVec->end()._Myptr);
    }

    ContainedObjectVec* m_pVec;
};

made that a member variable of ContainerObject and still the same result.

I'm really amazed that it is so difficult, and no one seems to know how to pass a vector of objects to LUA and have LUA modify member variables of those objects.  It would seem to be the type of problem that luabind was be designed to solve.  However every example I can find on the web, the only use of a container of objects is to read the values not modify them.  So I'm completely at a loss here...


On Tue, Mar 3, 2009 at 10:49 PM, John Smith <[hidden email]> wrote:
So I'm still having a problem with this, looking into it more it seems that the problem is the Copy Construct is being called via:
     server.exe!luabind::detail::value_converter::apply<ContainedObject>(lua_State * L=0x00f45ca8, const ContainedObject& ref={...})  Line 299 + 0x2f bytes    C++
     server.exe!luabind::detail::convert_to_lua<ContainedObject>(lua_State * L=0x00f45ca8, const ContainedObject& v={...})  Line 74    C++
     server.exe!luabind::detail::iterator<std::_Vector_iterator<ContainedObject,std::allocator<ContainedObject> > >::next(lua_State * L=0x00f45ca8)  Line 24 + 0x12 bytes    C++
specifically:
object_ptr = new T(ref);

So we're deffinetly making a copy of the vector when we go into the function exec.  Is there anyway to specify to pass the member variable by reference and not by copy?  Due to some constraints of other APIs I'm using, I have to store some objects I'm trying to expose to lua in the container, I can't just pass them by a pointer. 

Any thoughts as to things to try?


On Tue, Mar 3, 2009 at 2:04 AM, John Smith <[hidden email]> wrote:
I hate replying to myself but I also noticed that this problem is only in the case where the vector is storing an object not a pointer to an object.  (Which is why I think I'm hitting a copy constructor at some point instead of having it pass by reference)

If I was to modify the container to be std::vector<ContainedObject *> vec;, and modify the test code to be:
ContainerObject cont;
ContainedObject* Obj = new ContainedObject;


Obj->value = 10;

cont.push_back(Obj);

Then when I come out of the call_function Obj has the expected value of 5. 

Anyone have other ideas/thoughts?


On Tue, Mar 3, 2009 at 1:57 AM, John Smith <[hidden email]> wrote:
I don't believe so, changing that to be : gave the following errors:
print(cont:containervalue)
function arguments expected near ')' for that line


for things in cont:vec
gave the error:
 function arguments expected near 'do'

My understanding of the : notation was that it was a shorthand of:
x:y is equivilant to x.y(x)

Therefore, trying:
for things in cont:vec()

gave a luabind exception of 'No such operator defined'



On Tue, Mar 3, 2009 at 1:26 AM, James Porter <[hidden email]> wrote:
John Smith wrote:
> Lua code:
> function LuaFunction( cont )
>     print (cont.containervalue)
>     cont.containervalue = 30
>     for things in cont.vec
>     do
>         print (things.value)
>         things.vlaue = 5
>     end
> end

It looks like you're using the wrong syntax for getting at your members.
In Lua, "." is similar to "::" in C++, while ":" in Lua is similar to
"." in C++. You want cont:containervalue, cont:vec, and so on. The
former is working because you're defining a new static variable,
essentially.

- Jim

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user




------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user



------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Copying a container on function call... (Was Re: return_stl_iterator acting as const?)

Daniel Wallin
John Smith wrote:
> Well another 2 days on this and still no closer to figuring it out...  I
> tried to wrap a custom iterator around the class:
>
[...]
> I'm really amazed that it is so difficult, and no one seems to know how to
> pass a vector of objects to LUA and have LUA modify member variables of
> those objects.  It would seem to be the type of problem that luabind was be
> designed to solve.  However every example I can find on the web, the only
> use of a container of objects is to read the values not modify them.  So I'm
> completely at a loss here...

Unfortunately it can't be done with "return_stl_iterator". That policy
is awful and really needs to be redone properly, but in the meantime the
simplest solution would be to create a new policy using the code in
iterator_policy.hpp, and change the line that says:

    convert_to_lua(L, *self->first);

to

    convert_to_lua(L, &*self->first);

or

    convert_to_lua(L, boost::ref(*self->first));

That should do it.

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Copying a container on function call... (Was Re: return_stl_iterator acting as const?)

James Porter-2
In reply to this post by John Smith-124
John Smith wrote:
> I'm really amazed that it is so difficult, and no one seems to know how
> to pass a vector of objects to LUA and have LUA modify member variables
> of those objects.  It would seem to be the type of problem that luabind
> was be designed to solve.  However every example I can find on the web,
> the only use of a container of objects is to read the values not modify
> them.  So I'm completely at a loss here...

Have you tried using a smart pointer wrapper?
http://www.rasterbar.com/products/luabind/docs.html#smart-pointers

I'd try this myself, but my Luabind install is currently fubar and I
need to fix it.

- Jim

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Copying a container on function call... (Was Re: return_stl_iterator acting as const?)

John Smith-124
In reply to this post by Daniel Wallin
Thank you, that worked with a newly created  'return_stl_iterator_pointer' policy.

On Thu, Mar 5, 2009 at 12:46 AM, Daniel Wallin <[hidden email]> wrote:
John Smith wrote:
> Well another 2 days on this and still no closer to figuring it out...  I
> tried to wrap a custom iterator around the class:
>
[...]
> I'm really amazed that it is so difficult, and no one seems to know how to
> pass a vector of objects to LUA and have LUA modify member variables of
> those objects.  It would seem to be the type of problem that luabind was be
> designed to solve.  However every example I can find on the web, the only
> use of a container of objects is to read the values not modify them.  So I'm
> completely at a loss here...

Unfortunately it can't be done with "return_stl_iterator". That policy
is awful and really needs to be redone properly, but in the meantime the
simplest solution would be to create a new policy using the code in
iterator_policy.hpp, and change the line that says:

   convert_to_lua(L, *self->first);

to

   convert_to_lua(L, &*self->first);

or

   convert_to_lua(L, boost::ref(*self->first));

That should do it.

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user