Re: native converter questions

David Raulo <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
On Mon, 17 Aug 2009 12:25:51 +0200
Kristoffer Danielsson <[email protected]> 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: [email protected]
> > To: [email protected]
> > 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.