Comments (3)
For Lua operators, you can just bind the function to special meta name, for example:
LuaBinding(L).beginClass<MyClass>("MyClass")
...
.addFunction("__add", &MyClass::add)
...
.endClass();
For a complete list of Lua operator meta methods you can see Lua reference, under Metatables and Metamethods section.
Lua function overloads on the other hand will never work too well. For one thing, the Lua type conversion for number and string will make automatically bind to overloaded functions nearly impossible, and there is no way to enumerate C++ overloaded functions. Combine with other difficulty, I don't think there is a way to do that in compile time.
So if you plan to add a Lua overloads function (a function that do different things based on argument types), the best way is to use CFunction
convention, and dispatch by yourself.
If you are talking about adding overloaded C++ function to Lua (pick one the overloaded function), you just need to static_cast to the proper function type before adding it. For example:
static int test(string, int);
static string test(string);
LuaBinding(L).beginModule("utils")
// this will bind int test(string, int)
.addFunction("test_1", static_cast<int(*)(string, int)>(&test))
// this will bind string test(string), by using our LUA_FN macro
// LUA_FN(return_type, func_name, func_arg_types...)
.addFunction("test_2", LUA_FN(string, test, string))
.endModule();
from lua-intf.
Thanks for the quick response Steve. This was very helpful. I recognize it's a bit tricky to support overloading automatically so I appreciate that it probably doesn't make sense. I'm glad to hear that I can access the metaclass when adding operators - other libraries I was using were not working with the metaclass directly.
I'm currently writing an automatic binding generator for your project using libclang. If you are interested, I'd be happy to share the source when I'm further along.
from lua-intf.
Thanks for contribution. Yeah, I think it is interesting topic, and want to take a look at it.
from lua-intf.
Related Issues (20)
- Why LuaRef::call is not const? HOT 1
- Passing a C++ shared_ptr to LUA HOT 1
- How to make exported module non-global? HOT 6
- Can't bind lua_State* or Lambda to getter/setter HOT 3
- Getting pointer to the object from the Lua stack with LuaRef? HOT 1
- unprotected error in call to lua api when passing std::vector as argument in luaref calls
- How do I work on a returning pointer in Lua? HOT 1
- LUA_FN issues
- Bunch of warnings with /W4 in Visual C++
- How to start with the wrapper? HOT 7
- How can I know the function name from LuaRef HOT 4
- error: no member named 'clear' in 'LuaIntf::LuaRef'
- Comparison of userdata in scripts HOT 1
- Luajit support
- mutithread error handle
- lua_isinteger is missing in LuaState
- Support Lua 5.4
- Svar: Consider using a unified binding across languages
- pass args from lua to cpp problem in coroutines HOT 1
- Calling c++ function dosen't return value correctly in lua coroutine function, it just returned function's input arguments HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lua-intf.