problems using .def_readwrite and .property when binding classes

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

problems using .def_readwrite and .property when binding classes

Jonathan Capps
Hello. I've been trying to learn how to use luabind, and I can't get my test program to work as expected. Here's my binding code:

    luabind::module(state)
    [
        luabind::class_<Item>("Item")
            .def(luabind::constructor<const std::string&>() )
            .def("GetText", &Item::GetText)
            .def("SetText", &Item::SetText)
            .def_readwrite("text1", &Item::m_text)
            .property("text2", &Item::GetText, &Item::SetText)
    ];

My problem is that I cannot get the .def_readwrite or the .property line to work as expected. They compile fine, but when I try to access them in lua code as the documentation seems to show I can:

a = new Item("testing")
print(a:GetText())          -- prints "testing"
a:SetText("Hello World")
print(a:GetText())          --prints "Hello World", so SetText works, too
print(a.text1)                  --prints "function: 0014C670"
print(a.text2)                 -- prints "function 0014C800

so it seems that text1 and text2 were exposed as functions? but when I try to call them as such, I get

print(a.texti())                 --prints "luabind: property_tag function can't be called

So, I've been banging my head against this for a while now, and I can't make any headway. I could really use some help getting this working.

Thanks.

Jon


------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: problems using .def_readwrite and .property when binding classes

Nigel Atkinson-2
Hi Jon,

The last two print statements in your script should print "Hello World".
It looks like the meta-table setting isn't working correctly.

Try instead of:

a = new Item("testing")

Just:

a = Item("testing")

Nigel

On Tue, February 8, 2011 9:30 am, Jonathan Capps wrote:

> Hello. I've been trying to learn how to use luabind, and I can't get my
> test
> program to work as expected. Here's my binding code:
>
>     luabind::module(state)
>     [
>         luabind::class_<Item>("Item")
>             .def(luabind::constructor<const std::string&>() )
>             .def("GetText", &Item::GetText)
>             .def("SetText", &Item::SetText)
>             .def_readwrite("text1", &Item::m_text)
>             .property("text2", &Item::GetText, &Item::SetText)
>     ];
>
> My problem is that I cannot get the .def_readwrite or the .property line
> to
> work as expected. They compile fine, but when I try to access them in lua
> code as the documentation seems to show I can:
>
> a = new Item("testing")
> print(a:GetText())          -- prints "testing"
> a:SetText("Hello World")
> print(a:GetText())          --prints "Hello World", so SetText works, too
> print(a.text1)                  --prints "function: 0014C670"
> print(a.text2)                 -- prints "function 0014C800
>
> so it seems that text1 and text2 were exposed as functions? but when I try
> to call them as such, I get
>
> print(a.texti())                 --prints "luabind: property_tag function
> can't be called
>
> So, I've been banging my head against this for a while now, and I can't
> make
> any headway. I could really use some help getting this working.
>
> Thanks.
>
> Jon
> ------------------------------------------------------------------------------
> The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
> Pinpoint memory and threading errors before they happen.
> Find and fix more than 250 security defects in the development cycle.
> Locate bottlenecks in serial and parallel code that limit performance.
> http://p.sf.net/sfu/intel-dev2devfeb_______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>



------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: problems using .def_readwrite and .property when binding classes

Jonathan Capps


On Mon, Feb 7, 2011 at 9:32 PM, Nigel Atkinson <[hidden email]> wrote:
Hi Jon,

The last two print statements in your script should print "Hello World".
It looks like the meta-table setting isn't working correctly.

Try instead of:

a = new Item("testing")

Just:

a = Item("testing")

Nigel

Thanks for your response, Nigel. That "new" was a typo from fingers too used to C++ -- I couldn't seem to copy/paste from the console window, so I had to manually copy what was there. I do in fact use

a = Item("testing")

I've looked over the rest of the code, and it appears to be accurate (but my eyes are tired). Just to see what would happen, I tried

a = new Item("testing")
print(a)                             --prints 'nil', as I expected it would

I had wondered if this issue had something to do with a bad or improperly set metatable, and I'm glad to have some confirmation, but I'm too much of a lua newb to know how to diagnose and correct it. Do you have any other ideas on how to solve this? Google found some other people with the same issue, but no solutions, so I was hopeful that someone on the mailing list might have seen it before.

Jon


------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: problems using .def_readwrite and .property when binding classes

Nigel Atkinson-2
Hi Jon,

This works for me compiled with gcc.  It prints 'Hello World' 3 times.
Perhaps you could try this and see what is different.

---------------------8<---------------------------

#include <luabind/luabind.hpp>
#include <iostream>
#include <lua.hpp>

using namespace luabind;
using namespace std;

class Item
{
    public:

    string name;

    Item( string n ) { name = n; }

    string getString() { return name; }

    void setString( string n ) { name = n; }
};

int main()
{
    lua_State* L = lua_open();

    luaL_openlibs( L );
    luabind::open(L);

    module(L)
    [
        class_<Item>( "Item" )
        .def( constructor<string>() )
        .def( "getString", &Item::getString )
        .def( "setString", &Item::setString )
        .def_readwrite( "name1", &Item::name )
        .property( "name2", &Item::getString, &Item::setString )
    ];

    const char *script =
        "a = Item('testing')\n"
        "a:setString('Hello World')\n"
        "print( a:getString() )\n"
        "print( a.name1 )\n"
        "print( a.name2 )\n";

    if( luaL_dostring( L, script ) )
        cout << lua_tostring( L, -1 ) << endl;

    lua_close( L );

    return 0;
}

---------------------8<---------------------------

On Tue, February 8, 2011 2:55 pm, Jonathan Capps wrote:

> On Mon, Feb 7, 2011 at 9:32 PM, Nigel Atkinson
> <[hidden email]>wrote:
>
>> Hi Jon,
>>
>> The last two print statements in your script should print "Hello World".
>> It looks like the meta-table setting isn't working correctly.
>>
>> Try instead of:
>>
>> a = new Item("testing")
>>
>> Just:
>>
>> a = Item("testing")
>>
>> Nigel
>>
>> Thanks for your response, Nigel. That "new" was a typo from fingers too
> used to C++ -- I couldn't seem to copy/paste from the console window, so I
> had to manually copy what was there. I do in fact use
>
> a = Item("testing")
>
> I've looked over the rest of the code, and it appears to be accurate (but
> my
> eyes are tired). Just to see what would happen, I tried
>
> a = new Item("testing")
> print(a)                             --prints 'nil', as I expected it
> would
>
> I had wondered if this issue had something to do with a bad or improperly
> set metatable, and I'm glad to have some confirmation, but I'm too much of
> a
> lua newb to know how to diagnose and correct it. Do you have any other
> ideas
> on how to solve this? Google found some other people with the same issue,
> but no solutions, so I was hopeful that someone on the mailing list might
> have seen it before.
>
> Jon
> ------------------------------------------------------------------------------
> The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
> Pinpoint memory and threading errors before they happen.
> Find and fix more than 250 security defects in the development cycle.
> Locate bottlenecks in serial and parallel code that limit performance.
> http://p.sf.net/sfu/intel-dev2devfeb_______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>



------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: problems using .def_readwrite and .property when binding classes

Jonathan Capps


On Tue, Feb 8, 2011 at 2:17 AM, Nigel Atkinson <[hidden email]> wrote:
Hi Jon,

This works for me compiled with gcc.  It prints 'Hello World' 3 times.
Perhaps you could try this and see what is different.

Hi, Nigel.

I copied and pasted your code, and for me it printed

Hello World
function: 0020C680
function: 0020C810

I'm using MSVC Express 2008, with Microsoft's compiler. Might that be the issue? I don't think I can switch compilers, since I'm working on a game using DirectX 10, which is ultimately what I want to integrate lua with; and last I chacked, DirectX 10 headers weren't available for gcc.

Jon


------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: problems using .def_readwrite and .property when binding classes

Nigel Atkinson-2
Oh well at least that narrows things down.  I use to use VS2008, and I can't imagine the express version's cl.exe is that different.

Which version of Luabind are you using?

Nigel


On 09/02/2011, at 2:53 PM, Jonathan Capps wrote:



On Tue, Feb 8, 2011 at 2:17 AM, Nigel Atkinson <[hidden email]> wrote:
Hi Jon,

This works for me compiled with gcc.  It prints 'Hello World' 3 times.
Perhaps you could try this and see what is different.

Hi, Nigel.

I copied and pasted your code, and for me it printed

Hello World
function: 0020C680
function: 0020C810

I'm using MSVC Express 2008, with Microsoft's compiler. Might that be the issue? I don't think I can switch compilers, since I'm working on a game using DirectX 10, which is ultimately what I want to integrate lua with; and last I chacked, DirectX 10 headers weren't available for gcc.

Jon

------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: problems using .def_readwrite and .property when binding classes

Jonathan Capps


On Wed, Feb 9, 2011 at 2:52 AM, Nigel J Atkinson <[hidden email]> wrote:
Oh well at least that narrows things down.  I use to use VS2008, and I can't imagine the express version's cl.exe is that different.

Which version of Luabind are you using?

I'm using luabind 0.91 and boost 1.45.0.

I did some more Googling, and found a writeup on CodeProject.com on how to build boost for MSVC. It gives the following command line

bjam toolset=msvc-9.0 variant=debug threading=multi link=shared define=_BIND_TO_CURRENT_VCLIBS_VERSION

The variant and link of course may be changed; it's the define and specifying the version of MSVC appear to be the significant bits here. I'm not sure if one or the other or both were the trick, but I rebuilt luabind with the same options and retried your test code, and it printed "Hello World" three times, as it is supposed to. So I'm willing to chalk my problems up to a bad build.

Thank you so much for your assistance. I was growing very discouraged with this.

Jon


------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: problems using .def_readwrite and .property when binding classes

Nigel Atkinson-2
Ah glad you got it going!  I wondered if it was something like that.
You've possibly saved me a headache too, as I'm about to port my game from
Linux to Windows. :-)

There are also some defines for Luabind, if you are using it as a DLL,
that can cause weird things, but from memory they stop compilation.

Nigel

On Thu, February 10, 2011 12:49 am, Jonathan Capps wrote:

> On Wed, Feb 9, 2011 at 2:52 AM, Nigel J Atkinson
> <[hidden email]>wrote:
>
>> Oh well at least that narrows things down.  I use to use VS2008, and I
>> can't imagine the express version's cl.exe is that different.
>>
>> Which version of Luabind are you using?
>>
>
> I'm using luabind 0.91 and boost 1.45.0.
>
> I did some more Googling, and found a writeup on CodeProject.com on how to
> build boost for MSVC. It gives the following command line
>
> bjam toolset=msvc-9.0 variant=debug threading=multi link=shared
> define=_BIND_TO_CURRENT_VCLIBS_VERSION
>
> The variant and link of course may be changed; it's the define and
> specifying the version of MSVC appear to be the significant bits here. I'm
> not sure if one or the other or both were the trick, but I rebuilt luabind
> with the same options and retried your test code, and it printed "Hello
> World" three times, as it is supposed to. So I'm willing to chalk my
> problems up to a bad build.
>
> Thank you so much for your assistance. I was growing very discouraged with
> this.
>
> Jon
> ------------------------------------------------------------------------------
> The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
> Pinpoint memory and threading errors before they happen.
> Find and fix more than 250 security defects in the development cycle.
> Locate bottlenecks in serial and parallel code that limit performance.
> http://p.sf.net/sfu/intel-dev2devfeb_______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>



------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

C++0x move constructors

Kristoffer Danielsson
Does Luabind support such optimizations?
 
Class(Class && r)
{
    // Move logic here
}
 
VC10 has such optimizations in several STL classes. Wondering if luabind can take advantage of it...

------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user