Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std smart pointers, type checking for Stack and LuaRef and adding free functions as members #46

Merged
merged 8 commits into from
Oct 19, 2013

Conversation

progschj
Copy link
Contributor

These are various changes/additions I made based on my use cases:

std::/boost::shared_ptr and similar can be used as container via an additional trait (see the manual).

Stack and LuaRef have convenience functions to check for user defined types. I used this to circumvent the lack of overload support (specifically for constructors) by just taking the lua_State and then sorting out the parameter count and types manually which often requires checking if some lua object is of a specific, potentially custom, type.

Adding free functions as class members similar to the mechanism already present for properties. This allows adding dummy functions to do the aforementioned resolution of overloads without changing the C++ class itself. It also allows adding functions like __tostring __add etc. to types that need to be adapted without intrusion.

The changes are (mostly) documented in the Manual.html.

So far these changes worked for me but there might still be issues that just haven't come up in my use cases. Overall I think they are useful and shouldn't mess with backwards compatibility or performance if not used.

The introduction of an additional trait allows to specify how to obtain
a container from a raw pointer which allows types that provide that
functionality for shared_ptr via shared_from_this to be managed by a
shared_ptr.
The is_a function provides an equivalent to the
lua_isnumber, lua_isstring etc. functions for registered
custom types.
Added the functionality to use free functions with a fitting
first parameter as member functions.
for consistency with the Stack::is_a function
a is<T> member function has also be added to
LuaRef to check for custom types.
The manual now also mentions the possibility of
adding free functions with a "this argument" to
classes.
the tostring function was using a absolute index
instead of -1 which lead to the wrong value being
read to a string and potentially resulting in a
exception when assigning NULL to std::string.
There was a case where the checkClass (used by Stack::is_a
and LuaRef::is) would unbalance the stack.
@merlinblack
Copy link

Tried out the "Allow free functions as member functions". Works a treat. I'm using it to give some Ogre3d methods some default parameters, without having to bother with them in Lua. Like you mention it makes making a __tostring "method" easier too!

@vinniefalco
Copy link
Owner

This looks like good stuff, should I just merge it? I haven't been actively developing LuaBridge so its a bit cold on my side.

@merlinblack
Copy link

I guess it won't hurt to merge them into develop and try them out. I've merged them into my copy of LuaBridge that I'm hacking around for my game engine. So far so good, although I haven't exercised the is_a stuff ;-)

@merlinblack
Copy link

@progschj How are you doing your constructor overloads or overload handlers in general. Have you found some way of taking some of the grind out of writing them? You can see my first go at
https://github.com/merlinblack/Game-Engine-Testbed/blob/master/src/ogrebind.cpp
as Vector3Constructor. It works great but writing a lot of these will be a hassle! I'm thinking some template or macro magic but that could get messy too!

@progschj
Copy link
Contributor Author

progschj commented Jul 4, 2013

I do them very similar to what you are doing there. I don't think there is much you can do about it. I guess maybe some form of visitor style thing where you switch on like templated argument lists or so. But I think I'll just stick to "handcoding" them.

@merlinblack
Copy link

Thanks progschj, think I'll stick to hand writing these too for now - hopefully I wont need to many! :-)

vinniefalco added a commit that referenced this pull request Oct 19, 2013
std smart pointers, type checking for Stack and LuaRef and adding free functions as members
@vinniefalco vinniefalco merged commit 5bec6da into vinniefalco:develop Oct 19, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants