how to do a custom object_cast?

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

how to do a custom object_cast?

anders langlands
I'm trying to object_cast the following class that I've passed in a table from lua to my c function:

template < typename T >
class Vec3
{
public:
   T x, y, z;
};


I've bound to lua, and all was fine and dandy, but now I want to pass a table of them to a function. In the function, I check that a table has been passed and then do the following:

         iterator i(obj), end;
       
        for( ; i != end; ++i )
        {
            T t = object_cast<T>(*i);
            v.push_back(  t  );
        }

I'm doing the same thing with simple types (uint, float etc) and that works fine.

How can I tell luabind how to cast my class? I looked through the examples and saw the converters examples for boost::any and intrusive_ptr, do I want to do something like that?

Cheers,

Anders
Reply | Threaded
Open this post in threaded view
|

Re: how to do a custom object_cast?

Arvid Norberg-2

On Mar 26, 2006, at 15:43, anders langlands wrote:

> I'm trying to object_cast the following class that I've passed in a  
> table from lua to my c function:
>
> template < typename T >
> class Vec3
> {
> public:
>    T x, y, z;
> };
>
>
> I've bound to lua, and all was fine and dandy, but now I want to  
> pass a table of them to a function. In the function, I check that a  
> table has been passed and then do the following:
>
>          iterator i(obj), end;
>
>         for( ; i != end; ++i )
>         {
>             T t = object_cast<T>(*i);
>             v.push_back(  t  );
>         }
>
> I'm doing the same thing with simple types (uint, float etc) and  
> that works fine.
>
> How can I tell luabind how to cast my class? I looked through the  
> examples and saw the converters examples for boost::any and  
> intrusive_ptr, do I want to do something like that?

No, you simply have to bind your Vec3 type to lua with luabind. This  
will make luabind recognize it and be able to do conversions. I  
presume you have done this already.

What error do you get?

You may have to do the object cast to a pointer type (T*) and then  
copying it by dereferencing the resulting pointer.

T t = *object_cast<T*>(*i);

--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: how to do a custom object_cast?

anders langlands
Thanks for the reply Arvid.

I tried doing the pointer cast way already but to no avail. The error I get is simply "unable to make cast".

At least I know now that it's supposed to work and that I must be doing something wrong somewhere else. I'll have another try tonight.

Incidentally, I can;t make sense of the custom error message documentation. The function "set_pcall_callback", is that defined somwehere, or do I have to write it myself? I couldn't find it documented anywhere.

A

On 3/26/06, Arvid Norberg <[hidden email]> wrote:

On Mar 26, 2006, at 15:43, anders langlands wrote:

> I'm trying to object_cast the following class that I've passed in a
> table from lua to my c function:
>
> template < typename T >
> class Vec3
> {
> public:
>    T x, y, z;
> };
>
>
> I've bound to lua, and all was fine and dandy, but now I want to
> pass a table of them to a function. In the function, I check that a
> table has been passed and then do the following:
>
>          iterator i(obj), end;
>
>         for( ; i != end; ++i )
>         {
>             T t = object_cast<T>(*i);
>             v.push_back(  t  );
>         }
>
> I'm doing the same thing with simple types (uint, float etc) and
> that works fine.
>
> How can I tell luabind how to cast my class? I looked through the
> examples and saw the converters examples for boost::any and
> intrusive_ptr, do I want to do something like that?

No, you simply have to bind your Vec3 type to lua with luabind. This
will make luabind recognize it and be able to do conversions. I
presume you have done this already.

What error do you get?

You may have to do the object cast to a pointer type (T*) and then
copying it by dereferencing the resulting pointer.

T t = *object_cast<T*>(*i);

--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

Reply | Threaded
Open this post in threaded view
|

Re: how to do a custom object_cast?

anders langlands
In reply to this post by Arvid Norberg-2
Hi Arvid, just got it to work. The problem seems to have been that I was binding the Vec3 type in a different translation unit from the TypedArray<Vec3> type. I'm doing this thing as plugins with my own loadlib() function and I was registering each type in a seperate plugin.

I tried making sure that the plugin that registers the Vec3 type was loaded first, but that didn't work, so I just put the registration of the Vec3 type into the same plugin as the TypedArray type.

Is this the expected behaviour? If so, can you explain why (this may be a silly question i realise)

Cheers,

Anders

On 3/27/06, Arvid Norberg <[hidden email]> wrote:

On Mar 26, 2006, at 15:43, anders langlands wrote:

> I'm trying to object_cast the following class that I've passed in a
> table from lua to my c function:
>
> template < typename T >
> class Vec3
> {
> public:
>    T x, y, z;
> };
>
>
> I've bound to lua, and all was fine and dandy, but now I want to
> pass a table of them to a function. In the function, I check that a
> table has been passed and then do the following:
>
>          iterator i(obj), end;
>
>         for( ; i != end; ++i )
>         {
>             T t = object_cast<T>(*i);
>             v.push_back(  t  );
>         }
>
> I'm doing the same thing with simple types (uint, float etc) and
> that works fine.
>
> How can I tell luabind how to cast my class? I looked through the
> examples and saw the converters examples for boost::any and
> intrusive_ptr, do I want to do something like that?

No, you simply have to bind your Vec3 type to lua with luabind. This
will make luabind recognize it and be able to do conversions. I
presume you have done this already.

What error do you get?

You may have to do the object cast to a pointer type (T*) and then
copying it by dereferencing the resulting pointer.

T t = *object_cast<T*>(*i);

--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

Reply | Threaded
Open this post in threaded view
|

Re: how to do a custom object_cast?

Arvid Norberg-2

On Mar 27, 2006, at 21:19, anders langlands wrote:

> Hi Arvid, just got it to work. The problem seems to have been that  
> I was binding the Vec3 type in a different translation unit from  
> the TypedArray<Vec3> type. I'm doing this thing as plugins with my  
> own loadlib() function and I was registering each type in a  
> seperate plugin.
>
> I tried making sure that the plugin that registers the Vec3 type  
> was loaded first, but that didn't work, so I just put the  
> registration of the Vec3 type into the same plugin as the  
> TypedArray type.
>
> Is this the expected behaviour? If so, can you explain why (this  
> may be a silly question i realise)

As long as both translation units operate on the same lua state, it  
shouldn't matter what is bound in which one. But it has to be bound  
before it can be used in a cast.

--
Arvid Norberg


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: how to do a custom object_cast?

Arvid Norberg-2
In reply to this post by anders langlands

On Mar 27, 2006, at 10:41, anders langlands wrote:

> Thanks for the reply Arvid.
>
> I tried doing the pointer cast way already but to no avail. The  
> error I get is simply "unable to make cast".
>
> At least I know now that it's supposed to work and that I must be  
> doing something wrong somewhere else. I'll have another try tonight.
>
> Incidentally, I can;t make sense of the custom error message  
> documentation. The function "set_pcall_callback", is that defined  
> somwehere, or do I have to write it myself? I couldn't find it  
> documented anywhere.

set_pcall_callback() is used to give luabind your custom function  
that is called when an error occurs inside lua. Documentation about  
that callback function (that you may implement) is found in the lua  
manual:

http://www.lua.org/manual/5.0/manual.html#3.15

--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user