Tomasu / LuaGlue

C++11 Lua 5.2 Binding Library
zlib License
79 stars 22 forks source link

Does LuaGlue support default parameters? #68

Open amerkoleci opened 10 years ago

amerkoleci commented 10 years ago

I have the method:

static CameraPtr CreatePerspective(float fieldOfView = MATH_PIOVER4, float aspectRatio = 1.33333333333333f, float nearPlane = 0.2f, float farPlane = 100000.0f);

CameraPtr is shared_ptr

Will this work and can I omit default params in Lua?

Tomasu commented 10 years ago

Due to the way call's are generated at compile time, it may not work. but please test it if you can. I've had some ideas on how to make it work (if it doesn't).

Tomasu commented 10 years ago

It looks like no, as is it isn't supported. The magic C++ template magic is not able to detect default arguments. I'm not actually sure how to support variable number of arguments at the moment because of the way we bind to functions using stored function pointers that pull their type from the passed function.

I have an idea, but i'm not sure if it'll actually work. I will try to implement it, but I give no guarantees.

JMLX42 commented 10 years ago

Yet you know how many arguments you have in the lua stack. So "reading" only those arguments and calling the function with only those doesn't work ?

On Wed, Sep 3, 2014 at 9:02 PM, Thomas Fjellstrom notifications@github.com wrote:

It looks like no, as is it isn't supported. The magic C++ template magic is not able to detect default arguments. I'm not actually sure how to support variable number of arguments at the moment because of the way we bind to functions using stored function pointers that pull their type from the passed function.

I have an idea, but i'm not sure if it'll actually work. I will try to implement it, but I give no guarantees.

— Reply to this email directly or view it on GitHub https://github.com/Tomasu/LuaGlue/issues/68#issuecomment-54348703.

Jean-Marc Le Roux

Founder and CEO of Aerys (http://aerys.in)

Blog: http://blogs.aerys.in/jeanmarc-leroux Cell: (+33)6 20 56 45 78 Phone: (+33)9 72 40 17 58

Tomasu commented 10 years ago

Yes, we do know how many items are in the stack when our internal call method is called, the main problem is when storing function pointers, they store ALL of the parameters as part of the function pointer type, not just the required ones. So we actually have to pass ALL of the parameters or the compiler will complain when we try to call the function through that function pointer.

A separate set of code that stores the function pointer as a var-arg function pointer might work, but thats a bunch of code duplication, and may be less safe. I'm also not convinced it'll end up working properly either.