Re: LuaBind Function Default Values
Nigel Atkinson <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <1283311603.1682.16.camel@octavo> |
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 <[email protected]>
> On Mon, Aug 30, 2010 at 11:07 PM, Julio Cesar Cunha
> <[email protected]> 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
> [email protected]
> 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 [email protected] 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