native converter questions

View: New views
4 Messages — Rating Filter:   Alert me  

native converter questions

by David Raulo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Suppose you have a class vec2, and a function do_stuff(const Vec2&).
I'd like to have native converters between vec2 and lua tables, so I
could write for example :

  v1=Vec2{1,2}
  do_stuff{x,y}

I specialized luabind::default_converter<Vec2> to achieve this, which
worked, except that I can not use my vec2 bindings anymore, e.g :

  =v1
  error calling 'print' (No matching overload found, candidates:
  void __tostring(lua_State*,Vec2 const&))
  =v1.x
  No matching overload found, candidates:
  custom [f] <unknown>(Vec2 const&)

(both worked before I wrote the native converter). Does anyone see what
I did wrong? I can give more details if needed.

As an alternative, I thought of writing "light wrappers" instead of
custom converters, but this lead me to 2 problems:

- I have a couple of classes like vec2 above, and a lot of functions
  that take them. I'd need to write wrapper functions (taking
  lua tables) by-hand, for all of them. That's going to be painfull to
  write, review and maintain.

- I could not find a way to simulate a custom constructor allowing to
  write e.g  v1=Vec2{x,y}
  (without touching the original c++ api that is).  The closest I got
  to was adding pseudo class-methods acting like factories:
    v1=Vec2.New{x,y}
 
Is there a better way? Thanks!

- David

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user

Re: native converter questions

by Kristoffer Danielsson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
What's wrong with this?
v1 = Vec2(1,2)
 
All you have to do is to bind a constructor of type (int, int).
 

> Date: Mon, 17 Aug 2009 11:49:50 +0200
> From: david.raulo@...
> To: luabind-user@...
> Subject: [luabind] native converter questions
>
> Hi,
>
> Suppose you have a class vec2, and a function do_stuff(const Vec2&).
> I'd like to have native converters between vec2 and lua tables, so I
> could write for example :
>
> v1=Vec2{1,2}
> do_stuff{x,y}
>
> I specialized luabind::default_converter<Vec2> to achieve this, which
> worked, except that I can not use my vec2 bindings anymore, e.g :
>
> =v1
> error calling 'print' (No matching overload found, candidates:
> void __tostring(lua_State*,Vec2 const&))
> =v1.x
> No matching overload found, candidates:
> custom [f] <unknown>(Vec2 const&)
>
> (both worked before I wrote the native converter). Does anyone see what
> I did wrong? I can give more details if needed.
>
> As an alternative, I thought of writing "light wrappers" instead of
> custom converters, but this lead me to 2 problems:
>
> - I have a couple of classes like vec2 above, and a lot of functions
> that take them. I'd need to write wrapper functions (taking
> lua tables) by-hand, for all of them. That's going to be painfull to
> write, review and maintain.
>
> - I could not find a way to simulate a custom constructor allowing to
> write e.g v1=Vec2{x,y}
> (without touching the original c++ api that is). The closest I got
> to was adding pseudo class-methods acting like factories:
> v1=Vec2.New{x,y}
>
> Is there a better way? Thanks!
>
> - David
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now. http://p.sf.net/sfu/bobj-july
> _______________________________________________
> luabind-user mailing list
> luabind-user@...
> https://lists.sourceforge.net/lists/listinfo/luabind-user


kolla in resten av Windows LiveT. Inte bara e-post - Windows LiveT är mycket mer än din inkorg. Mer än bara meddelanden
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user

Re: native converter questions

by David Raulo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, 17 Aug 2009 12:25:51 +0200
Kristoffer Danielsson <kristoffer.danielsson@...> wrote:

> What's wrong with this?
>
> v1 = Vec2(1,2)
>  
> All you have to do is to bind a constructor of type (int, int).

You still won't be able to do :
  t = {1,2}
  v1 = Vec2(t)
  do_stuff(t)
  do_stuff{1,2}

(and see my remark in first post about having to write thin
wrappers for each and every function similar to do_stuff).

Besides Vec2 was only an example; The point is to allow native
conversion from/to lua tables without modifying the c++ code.  Maybe a
second example could make things clearer? Consider a 4x4 matrix. The
c++ class will probably have a constructor from float*, which you'll
want to convert to a constructor from table. Adding a custom
constructor taking 16 float parameters is out of the question:

- you'd have to change the c++ library (unless you know a way to
  do this outside of the matrix class?)

- such a constructor could not be bound with a standard build of
  luabind (which accepts 10 parameters max IIRC).

- this still would not allow the usage I'm looking for.


-David


> > Date: Mon, 17 Aug 2009 11:49:50 +0200
> > From: david.raulo@...
> > To: luabind-user@...
> > Subject: [luabind] native converter questions
> >
> > Hi,
> >
> > Suppose you have a class vec2, and a function do_stuff(const Vec2&).
> > I'd like to have native converters between vec2 and lua tables, so I
> > could write for example :
> >
> > v1=Vec2{1,2}
> > do_stuff{x,y}
> >
> > I specialized luabind::default_converter<Vec2> to achieve this,
> > which worked, except that I can not use my vec2 bindings anymore,
> > e.g :
> >
> > =v1
> > error calling 'print' (No matching overload found, candidates:
> > void __tostring(lua_State*,Vec2 const&))
> > =v1.x
> > No matching overload found, candidates:
> > custom [f] <unknown>(Vec2 const&)
> >
> > (both worked before I wrote the native converter). Does anyone see
> > what I did wrong? I can give more details if needed.
> >
> > As an alternative, I thought of writing "light wrappers" instead of
> > custom converters, but this lead me to 2 problems:
> >
> > - I have a couple of classes like vec2 above, and a lot of functions
> > that take them. I'd need to write wrapper functions (taking
> > lua tables) by-hand, for all of them. That's going to be painfull to
> > write, review and maintain.
> >
> > - I could not find a way to simulate a custom constructor allowing
> > to write e.g v1=Vec2{x,y}
> > (without touching the original c++ api that is). The closest I got
> > to was adding pseudo class-methods acting like factories:
> > v1=Vec2.New{x,y}
> >
> > Is there a better way? Thanks!
> >
> > - David
> >

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user

Re: native converter questions

by David Raulo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, 17 Aug 2009 11:49:50 +0200
David Raulo <david.raulo@...> wrote:

> Hi,
>
> Suppose you have a class vec2, and a function do_stuff(const Vec2&).
> I'd like to have native converters between vec2 and lua tables, so I
> could write for example :
>
>   v1=Vec2{1,2}
>   do_stuff{x,y}
>
> I specialized luabind::default_converter<Vec2> to achieve this, which
> worked, except that I can not use my vec2 bindings anymore, e.g :
>
>   =v1
>   error calling 'print' (No matching overload found, candidates:
>   void __tostring(lua_State*,Vec2 const&))
>   =v1.x
>   No matching overload found, candidates:
>   custom [f] <unknown>(Vec2 const&)
>
> (both worked before I wrote the native converter). Does anyone see
> what I did wrong? I can give more details if needed.
>
> As an alternative, I thought of writing "light wrappers" instead of
> custom converters, but this lead me to 2 problems:
>
> - I have a couple of classes like vec2 above, and a lot of functions
>   that take them. I'd need to write wrapper functions (taking
>   lua tables) by-hand, for all of them. That's going to be painfull to
>   write, review and maintain.
>
> - I could not find a way to simulate a custom constructor allowing to
>   write e.g  v1=Vec2{x,y}
>   (without touching the original c++ api that is).  The closest I got
>   to was adding pseudo class-methods acting like factories:
>     v1=Vec2.New{x,y}
>  
> Is there a better way? Thanks!

Anyone?

To reformulate my questions:
- is there a way to have native converter for a class while keeping its bindings (so its methods are still accessible)?
- is it possible to add custom constructors to a class' bindings, without changing the original code?

Thanks!

- David

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user