https://github.com/ubpa/uluapp
Ubpa Lua++ (Lua & C++)
https://github.com/ubpa/uluapp
Last synced: over 1 year ago
JSON representation
Ubpa Lua++ (Lua & C++)
- Host: GitHub
- URL: https://github.com/ubpa/uluapp
- Owner: Ubpa
- License: mit
- Created: 2020-07-22T22:04:57.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-11-23T13:29:51.000Z (over 5 years ago)
- Last Synced: 2025-03-24T20:38:11.272Z (over 1 year ago)
- Language: C++
- Size: 51.8 KB
- Stars: 33
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
```
_ _ _ ____________
| | | | | | ___ \ ___ \
| | | | | _ _ __ _| |_/ / |_/ /
| | | | | | | | |/ _` | __/| __/
| |_| | |___| |_| | (_| | | | |
\___/\_____/\__,_|\__,_\_| \_|
```
[](https://github.com/Ubpa/ULuaPP/archive/master.zip) [](https://github.com/Ubpa/ULuaPP/tags) [](LICENSE)
⭐ Star us on GitHub — it helps!
# ULuaPP
Ubpa Lua++ (Lua & C++)
Auto register C++ class to Lua with [sol2](https://github.com/ThePhD/sol2) and [USRefl](https://github.com/Ubpa/USRefl)
## Example
Suppose you have a class `Vec`, what you need to do are
- write `TypeInfo` (you can use `USRefl::AutoRefl` to generate)
- register : `Ubpa::ULuaPP::Register(lua_State*)`
That's all.
```c++
#include
#include
struct Vec {
Vec(float x, float y) : x{ x }, y{ y } {}
float x;
float y;
void Add(float dx = 1.f, float dy = 1.f) {
x += dx;
y += dy;
}
};
template<>
struct Ubpa::USRefl::TypeInfo :
TypeInfoBase
{
#ifdef UBPA_USREFL_NOT_USE_NAMEOF
static constexpr char name[4] = "Vec";
#endif
static constexpr AttrList attrs = {};
static constexpr FieldList fields = {
Field {TSTR(UMeta::constructor), WrapConstructor()},
Field {TSTR("x"), &Type::x},
Field {TSTR("y"), &Type::y},
Field {TSTR("Add"), &Type::Add, AttrList {
Attr {TSTR(UMeta::default_functions), std::tuple {
[](Type* __this, float dx) { return __this->Add(std::forward(dx)); },
[](Type* __this) { return __this->Add(); }
}},
}},
};
};
int main() {
lua_State* L = luaL_newstate(); /* opens Lua */
luaL_openlibs(L); /* opens the standard libraries */
// you just need to write a line of code
Ubpa::ULuaPP::Register(L);
{
sol::state_view lua(L);
const char code[] = R"(
v = Vec.new(1, 2)
print(v.x, v.y)
v.x = 3
v.y = 4
print(v.x, v.y)
v:Add(1, 2)
v:Add(3)
v:Add()
print(v.x, v.y)
)";
lua.script(code);
}
lua_close(L);
return 0;
}
```