LuaBind Function Default Values

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

LuaBind Function Default Values

Jcfalcone
[hidden email]Hello People

I have a hard problem on my hands, i need to bind some C++ functions and some functions have default values, in my test luabind don't bind default values. Have some way to put luabind to bind the default values too?

Thanks

--
http://wolfnime.com o futuro do anime esta aqui.

****Eu rejeito a sua Realidade e Substituo pela minha****

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: LuaBind Function Default Values

Tom McCubbin-2

Default values are compile time syntactic sugar...you'll need to use some wrapper facade likely.  Maybe inherit and add multiple signatures that forward to the underlying implementation.  Not sure of any slick boost mpl to get around this :(

On Aug 30, 2010 4:17 PM, "Julio Cesar Cunha" <[hidden email]> wrote:
> <[hidden email]>Hello People
>
> I have a hard problem on my hands, i need to bind some C++ functions and
> some functions have default values, in my test luabind don't bind default
> values. Have some way to put luabind to bind the default values too?
>
> Thanks
>
> --
> http://wolfnime.com o futuro do anime esta aqui.
>
> ****Eu rejeito a sua Realidade e Substituo pela minha****


------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: LuaBind Function Default Values

Jcfalcone
I think that is the solution, but in this way i will redo all.

Strange that luabind don't suporte this feature yet, searching on google i found some binds that do that, like toLua++ and Boost:Python.

Anyway, Don't rasterbar don't say nothing about this feature? maybe in some future update.

2010/8/30 Tom McCubbin <[hidden email]>

Default values are compile time syntactic sugar...you'll need to use some wrapper facade likely.  Maybe inherit and add multiple signatures that forward to the underlying implementation.  Not sure of any slick boost mpl to get around this :(

On Aug 30, 2010 4:17 PM, "Julio Cesar Cunha" <[hidden email]> wrote:
> <[hidden email]>Hello People


>
> I have a hard problem on my hands, i need to bind some C++ functions and
> some functions have default values, in my test luabind don't bind default
> values. Have some way to put luabind to bind the default values too?
>
> Thanks
>
> --
> http://wolfnime.com o futuro do anime esta aqui.
>
> ****Eu rejeito a sua Realidade e Substituo pela minha****

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user




--
http://wolfnime.com o futuro do anime esta aqui.

****Eu rejeito a sua Realidade e Substituo pela minha****

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: LuaBind Function Default Values

Daniel Wallin
On Mon, Aug 30, 2010 at 11:07 PM, Julio Cesar Cunha
<[hidden email]> wrote:
> I think that is the solution, but in this way i will redo all.
>
> Strange that luabind don't suporte this feature yet, searching on google i
> found some binds that do that, like toLua++ and Boost:Python.
>
> Anyway, Don't rasterbar don't say nothing about this feature? maybe in some
> future update.

The problem is that it isn't technically possible to detect and use
the default values. They are not part of the function signature.
Boost.Python supports default values by generating a set of overloads
with different arities using a macro (from docs):

  BOOST_PYTHON_FUNCTION_OVERLOADS(foo_overloads, foo, 1, 4)

I'm planning to support default values in 1.0 by letting the user pass
the values to def(), possibly using this syntax:

  def("f", &f, _2 = "default value")

But for now, the only solution is to define thin wrapper functions.
This is pretty easy to do, but not ideal of course:

  class X
  {
  public:
      void f(int x = 0, int y = 0);
  };

  void f0(X& self)
  {
      self.f();
  }

  void f1(X& self, int x)
  {
      self.f(x);
  }

  void f2(X& self, int x, int y)
  {
      self.f(x, y);
  }

  ...

  class_<X>("X")
      .def("f", &f0)
      .def("f", &f1)
      .def("f", &f2)

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

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: LuaBind Function Default Values

Jcfalcone
I see this solution yesterday, i think i don't have much choise.

Others solution that i find is set many casts like this:

.def("beginScene", (bool (IVideoDriver::*)())&IVideoDriver::beginScene)
.def("beginScene", (bool (IVideoDriver::*)(bool,bool))&IVideoDriver::beginScene)
.def("beginScene", (bool (IVideoDriver::*)(bool,bool,bool,bool))&IVideoDriver::beginScene)

This solution work well with this type of parameter, but if you need to send a point the program crash, because lua send trash to the point.

Thanks Daniel, i'm waiting the 1.0 version.

2010/8/31 Daniel Wallin <[hidden email]>
On Mon, Aug 30, 2010 at 11:07 PM, Julio Cesar Cunha
<[hidden email]> wrote:
> I think that is the solution, but in this way i will redo all.
>
> Strange that luabind don't suporte this feature yet, searching on google i
> found some binds that do that, like toLua++ and Boost:Python.
>
> Anyway, Don't rasterbar don't say nothing about this feature? maybe in some
> future update.

The problem is that it isn't technically possible to detect and use
the default values. They are not part of the function signature.
Boost.Python supports default values by generating a set of overloads
with different arities using a macro (from docs):

 BOOST_PYTHON_FUNCTION_OVERLOADS(foo_overloads, foo, 1, 4)

I'm planning to support default values in 1.0 by letting the user pass
the values to def(), possibly using this syntax:

 def("f", &f, _2 = "default value")

But for now, the only solution is to define thin wrapper functions.
This is pretty easy to do, but not ideal of course:

 class X
 {
 public:
     void f(int x = 0, int y = 0);
 };

 void f0(X& self)
 {
     self.f();
 }

 void f1(X& self, int x)
 {
     self.f(x);
 }

 void f2(X& self, int x, int y)
 {
     self.f(x, y);
 }

 ...

 class_<X>("X")
     .def("f", &f0)
     .def("f", &f1)
     .def("f", &f2)

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

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user



--
http://wolfnime.com o futuro do anime esta aqui.

****Eu rejeito a sua Realidade e Substituo pela minha****

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: LuaBind Function Default Values

Nigel Atkinson-2
The other option is to overload the function in Lua, and use Lua's
optional argument handling.  For example:


// the defaults here are not strictly required for this method to work.
int somefunc( bool p1=true, bool p2=false );

luabind::module(L) [ def( "somefunc", &somefunc ) ];

---------------8<-----------------
-- Lua

oldsomefunc = somefunc
function somefunc( p1, p2 )
        if type(p1) == 'nil' then p1 = true end
        if type(p2) == 'nil' then p2 = false end
        oldsomefunc( p1, p2 )
end

-- Use it.
somefunc()
somefunc( false )
somefunc( true, true )

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

Note that for other types besides boolean, you could get away with:

local p1 = p1 or 'default_value'

This does not work with booleans as 'false' gets replaced with the
default.

HTH

Nigel Atkinson


On Tue, 2010-08-31 at 13:51 -0300, Julio Cesar Cunha wrote:

> I see this solution yesterday, i think i don't have much choise.
>
> Others solution that i find is set many casts like this:
>
> .def("beginScene", (bool
> (IVideoDriver::*)())&IVideoDriver::beginScene)
> .def("beginScene", (bool
> (IVideoDriver::*)(bool,bool))&IVideoDriver::beginScene)
> .def("beginScene", (bool
> (IVideoDriver::*)(bool,bool,bool,bool))&IVideoDriver::beginScene)
>
> This solution work well with this type of parameter, but if you need
> to send a point the program crash, because lua send trash to the
> point.
>
> Thanks Daniel, i'm waiting the 1.0 version.
>
> 2010/8/31 Daniel Wallin <[hidden email]>
>         On Mon, Aug 30, 2010 at 11:07 PM, Julio Cesar Cunha
>         <[hidden email]> wrote:
>        
>         > I think that is the solution, but in this way i will redo
>         all.
>         >
>         > Strange that luabind don't suporte this feature yet,
>         searching on google i
>         > found some binds that do that, like toLua++ and
>         Boost:Python.
>         >
>         > Anyway, Don't rasterbar don't say nothing about this
>         feature? maybe in some
>         > future update.
>        
>        
>         The problem is that it isn't technically possible to detect
>         and use
>         the default values. They are not part of the function
>         signature.
>         Boost.Python supports default values by generating a set of
>         overloads
>         with different arities using a macro (from docs):
>        
>          BOOST_PYTHON_FUNCTION_OVERLOADS(foo_overloads, foo, 1, 4)
>        
>         I'm planning to support default values in 1.0 by letting the
>         user pass
>         the values to def(), possibly using this syntax:
>        
>          def("f", &f, _2 = "default value")
>        
>         But for now, the only solution is to define thin wrapper
>         functions.
>         This is pretty easy to do, but not ideal of course:
>        
>          class X
>          {
>          public:
>              void f(int x = 0, int y = 0);
>          };
>        
>          void f0(X& self)
>          {
>              self.f();
>          }
>        
>          void f1(X& self, int x)
>          {
>              self.f(x);
>          }
>        
>          void f2(X& self, int x, int y)
>          {
>              self.f(x, y);
>          }
>        
>          ...
>        
>          class_<X>("X")
>              .def("f", &f0)
>              .def("f", &f1)
>              .def("f", &f2)
>        
>         --
>         Daniel Wallin
>         BoostPro Computing
>         http://www.boostpro.com
>        
>        
>         ------------------------------------------------------------------------------
>         This SF.net Dev2Dev email is sponsored by:
>        
>         Show off your parallel programming skills.
>         Enter the Intel(R) Threading Challenge 2010.
>         http://p.sf.net/sfu/intel-thread-sfd
>         _______________________________________________
>         luabind-user mailing list
>         [hidden email]
>         https://lists.sourceforge.net/lists/listinfo/luabind-user
>        
>
>
>
> --
> http://wolfnime.com o futuro do anime esta aqui.
>
> ****Eu rejeito a sua Realidade e Substituo pela minha****
> ------------------------------------------------------------------------------
> This SF.net Dev2Dev email is sponsored by:
>
> Show off your parallel programming skills.
> Enter the Intel(R) Threading Challenge 2010.
> http://p.sf.net/sfu/intel-thread-sfd
> _______________________________________________ luabind-user mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/luabind-user



------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user