Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/user1095108/lualite
a one header library for creating Lua bindings to C++
https://github.com/user1095108/lualite
bindings cpp14 lua
Last synced: about 2 months ago
JSON representation
a one header library for creating Lua bindings to C++
- Host: GitHub
- URL: https://github.com/user1095108/lualite
- Owner: user1095108
- License: unlicense
- Archived: true
- Created: 2015-04-28T07:46:25.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-06-06T14:50:57.000Z (over 3 years ago)
- Last Synced: 2024-08-04T02:10:57.642Z (5 months ago)
- Topics: bindings, cpp14, lua
- Language: C++
- Homepage:
- Size: 430 KB
- Stars: 79
- Watchers: 8
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- AwesomeCppGameDev - lualite
README
# Description
A C++ library for generating Lua bindings.Supported features:
* functions,
* constructors,
* inheritance,
* member functions,
* properties,
* standard containers,
* user types.`lualite` is now the stuff of legends. History became legend. Legend became myth.
# Example
```c++
lualite::module{L,
lualite::class_("testbase")
.constant("__classname", "testbase")
.constant("__b", true)
.constant("__pi", 3.1459)
.def("dummy"),
lualite::class_("testclass")
.constructor("defaultNew")
.constructor()
.inherits() // you can add more classes to inherit from
.enum_("smell", 9)
.def (testclass::*)(int), &testclass::print>("print")
.def (testclass::*)(std::string) const, &testclass::print>("print_")
.def("pointer")
.def("reference")
.property("a")
.def("test_array"),
lualite::scope("subscope",
lualite::class_("testclass")
.constructor<>("defaultNew")
.constructor()
.enum_("smell", 10)
.def("testfunc")
.def (testclass::*)(int), &testclass::print>("print")
.def (testclass::*)(std::string) const, &testclass::print>("print_")
)
}
.enum_("apple", 1)
.def("testfunc")
.def("testpair")
.def("testtuple");luaL_dostring(
L,
"local a = testfunc(3, 2, 1)\n"
"r = { \"my\", \"pair\" }\n"
"testpair(r)\n"
"r[3] = 3\n"
"testtuple(r)\n"
"print(a.y)\n"
"print(apple)\n"
"print(testclass.smell)\n"
"print(testbase.__classname)\n"
"print(testbase.__b)\n"
"print(testbase.__pi)\n"
"local b = testclass.defaultNew()\n"
"print(\"---\")\n"
"print(b.a)\n"
"b:reference().a = 888\n"
"print(b.dummy)\n"
"print(b.a .. \" \" .. b:dummy(\"test\"))\n"
"local tmp1, tmp2, tmp3 = b:pointer():print(100)\n"
"print(tmp1 .. \" \" .. tmp2 .. \" \" .. tmp3)\n"
"b:reference():print_(\"msg1\")\n"
"local a = subscope.testclass.new(1111)\n"
"print(subscope.testclass.smell)\n"
"subscope.testclass.testfunc(200, 0, 1)\n"
"local c = a:reference():print_(\"msg2\")\n"
"print(c[10])\n"
"r = {}"
"for i = 1, 10 do\n"
" r[i] = 7\n"
"end\n"
"print(a:test_array(r))\n"
);
```
# FAQ
**Q:** Why is there no support for introspection in lualite:**A:** There is, but it is not exposed to lua by default. The idea is not to pollute the lua namespace and not to make users pay for something they potentially don't want. Example:
```
void Object::init()
{
lualite::module(L,
lualite::class_("Object")
.constructor()
.def_func::inherits>("inherits")
);
}
```
Adds the ability to query whether an object's class inherits from another class. By default, an object's class always inherits from itself. That is, `obj.inherits("Object")` always returns true. Similar mechanisms can be implemented for querying property names and the types of objects they expose.**Q:** Why can't I return `char*`:
**A:** Try returning `char const*`. Don't return references or pointers to non-const objects, if you don't provide wrappers for them (returning a non-const reference or pointer implies the ability to change the referred-to object within a Lua script, which is not possible without also writing a wrapper class for the type of the referred-to object).
**Q:** Why am I getting:
`Assertion failed: (sizeof...(A) + O - 1 == lua_gettop(L)), function member_stub, file ../src/Script/LuaLite.h, line 1172.`
whenever I try to call a member function declared here:
```
class TestClass {
public:
TestClass();
virtual ~TestClass();void sayHello();
static void registerFunc(lua_State* L) {
lualite::module(L,
lualite::class_("TestClass")
.constructor("new")
.def("sayHello"));
}
};
```using this lua code:
```
luaL_dostring(luaState,
"local test = TestClass.new()\n"
"test.sayHello()");
```**A:** Try `test:sayHello()`. Or try:
```
.def_func("sayHello"));
```