Skip to content

Commit

Permalink
Reword and simplify readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
davits committed Oct 3, 2023
1 parent 333233f commit 52ed6be
Showing 1 changed file with 27 additions and 40 deletions.
67 changes: 27 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Introduction
Luabind is a small header only library aiming to provide easy means to expose C++ functionality in Lua.
Currently it supports inheritance, binding of class member functions, member variables and global functions.
Currently it supports inheritance, binding of class member functions, static functions, member variables and global functions.

It uses variadic template magic to generate code for you, which handles function arguments transition from Lua to C++, function call, and transition result back to Lua.

## Example
```cpp
Expand All @@ -11,19 +13,22 @@ public:
void credit(int amount);
void debit(int amount);

private:
public: // public for demonstative purposes
int balance;
};

class SpecialAccount : public Account {
public:
SpecialAccount(int balance, int overdraft);

int getOverdraft() const;

private:
int overdraft;
};
```
Binding to lua
```cpp
lua_State* L = luaL_newstate();
luaL_openlibs(L);
Expand All @@ -35,17 +40,20 @@ luabind::class_<Account>(L, "Account")
luabind::class_<SpecialAccount, Account>(L, "SpecialAccount")
.constructor<int, int>("new")
.property_readonly<&SpecialAccount::overdraft>("overdraft");
.construct_shared<int, int>("create")
.property_readonly<&SpecialAccount::getOverdraft>("overdraft");
luaL_dostring(L, R"--(
a = Account:new(10)
a:credit(3)
a:debit(2)
s = SpecialAccount:new(50, 10)
s = SpecialAccount:new(50, 10) " Memory is allocated on lua stack.
s:credit(20)
s = SpecialAccount:create(50, 10) " Represented by C++ shared_ptr on lua stack.
s:credit(20)
)--");
```
## How to install, confugure, build and run
## How to install, configure, build and run

Ordinary prcedure when developing and testing `luabind` library.

Expand All @@ -61,42 +69,21 @@ cmake --build --preset luabind
ctest --preset unit_tests
```

There are some specifications when you want to use `luabind` as submodule. As a library it consist of submodules such as: `lua` and `googletest`. You can encounter to issues like `'multiple definition of googletest'` etc.. To avoid this conflicts on CMake level variables are defined to prevent such problems.

| CMake variable | Purpose |
| ------ | ------ |
| LUABIND_LUA_LIB_NAME (STRING) | variable to link specific library (luabind_lua is set as default) |
| LUABIND_LUA_CPP (BOOL) | variable to add/remove extern "C" when including lua headers |
| LUABIND_TESTS (BOOL) | variable for inclusion/exclusion of test into build phase |
## Integrate to CMake project


## Command line build steps

- To use custom `lua` library one can run with the following command.
```sh
cmake --preset luabind -DLUA_LIB_NAME=[your-custom-lua-library]
```
> Note: `-DLUA_LIB_NAME=luabind_lua` is set by default.

- To exclude `luabind` tests and `googletest` library from build phase run the following command.
```sh
cmake --preset luabind -DLUABIND_TEST=OFF
```
> Note: `-DLUABIND_TEST=ON` is set by default.
- To wrap/unwrap lua includes with `extern "C"` use the variable to do so.
```sh
cmake --preset luabind -DINCLUDE_LUA_LIB_WITH_EXTERN_C=OFF
```
> Note: `-DINCLUDE_LUA_LIB_WITH_EXTERN_C=ON` is set by default.
## Integrate to CMake

In this example `luabind` is used as submodule.
```
set(LUABIND_LUA_LIB_NAME lua)
add_subdirectory(luabind)
Simple example to use `luabind` as a submodule to a project which already has Lua integrated.
``` cmake
# Given that "lua" is the cmake name of Lua library in the top project
# And it has lua's path added to include path, such as:
# target_include_directories(lua SYSTEM INTERFACE ${LUA_SOURCE_DIR})
set(LUABIND_LUA_LIB_NAME lua)
add_subdirectory(luabind)
```

Here are some control variables, which will help to fine tune integration:

| CMake variable | Description |
| ------ | ------ |
| LUABIND_LUA_LIB_NAME (STRING) | cmake name of the Lua library to use (default: luabind_lua) |
| LUABIND_LUA_CPP (BOOL) | option indicating whether Lua headers should be included as C++ code. (default: OFF) |
| LUABIND_TESTS (BOOL) | option to enable luabind tests (default: OFF) |

0 comments on commit 52ed6be

Please sign in to comment.