Skip to content

Commit e2ce77e

Browse files
committed
Updated manual
1 parent f587d4b commit e2ce77e

1 file changed

Lines changed: 48 additions & 8 deletions

File tree

‎Manual.md‎

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ Contents
3333
* [2.4 - Property Member Proxies](#24---property-member-proxies)
3434
* [2.5 - Function Member Proxies](#25---function-member-proxies)
3535
* [2.5.1 - Partial Application with luabridge::bind_front](#251---partial-application-with-luabridgebind_front)
36-
* [2.5.2 - Function Overloading](#252---function-overloading)
36+
* [2.5.2 - Partial Application with luabridge::bind_back](#252---partial-application-with-luabridgebind_back)
37+
* [2.5.3 - Function Overloading](#253---function-overloading)
3738
* [2.6 - Constructors](#26---constructors)
3839
* [2.6.1 - Constructor Proxies](#261---constructor-proxies)
3940
* [2.6.2 - Constructor Factories](#262---constructor-factories)
@@ -621,20 +622,59 @@ ns.addFunction ("add10", std::function<int(int)> (std::bind_front (&add, 10)));
621622
ns.addFunction ("add10", luabridge::bind_front (&add, 10));
622623
```
623624

624-
It also works with member function pointers, where the bound object is not counted as a Lua-visible parameter:
625+
It also works with member function pointers. The bound object is not counted as a Lua-visible parameter, so `bind_front` turns a member function into a plain callable that Lua sees as a regular function:
625626

626627
```cpp
627-
struct Vec { float coord [3]; };
628-
float getCoord (const Vec* v, int i) { return v->coord [i]; }
628+
struct Vec { float coord [3]; float getCoord (int i) const { return coord [i]; } };
629+
630+
Vec v;
631+
// Bind v as the object; Lua supplies the index
632+
ns.addFunction ("getCoord", luabridge::bind_front (&Vec::getCoord, &v));
633+
// Lua: getCoord(0) == v.coord[0]
634+
```
635+
636+
It also accepts lambdas:
637+
638+
```cpp
639+
int base = 10;
640+
ns.addFunction ("addBase", luabridge::bind_front ([base](int offset, int v) { return base + offset + v; }, 2));
641+
// Lua: addBase(30) == 42
642+
```
643+
644+
### 2.5.2 - Partial Application with luabridge::bind_back
645+
646+
`luabridge::bind_back` is the trailing-argument counterpart to `bind_front`: it pre-binds the **last** arguments of a callable, leaving the **leading** arguments for Lua to supply.
647+
648+
```cpp
649+
int add (int a, int b) { return a + b; }
650+
651+
// bind_back fixes the trailing argument; Lua supplies the leading one
652+
ns.addFunction ("add10", luabridge::bind_back (&add, 10));
653+
// Lua: add10(32) == 42
654+
```
655+
656+
For member function pointers, `bind_back` automatically prepends the class pointer to the remaining (Lua-visible) parameter list so that LuaBridge can dispatch it as a proxy function. This lets you pre-bind a method's trailing arguments while the object still comes from Lua:
657+
658+
```cpp
659+
struct Vec { float coord [3]; float getCoord (int i) const { return coord [i]; } };
629660
630661
ns.beginClass<Vec> ("Vec")
631-
.addFunction ("x", luabridge::bind_front (&getCoord, 0))
632-
.addFunction ("y", luabridge::bind_front (&getCoord, 1))
633-
.addFunction ("z", luabridge::bind_front (&getCoord, 2))
662+
.addFunction ("x", luabridge::bind_back (&Vec::getCoord, 0))
663+
.addFunction ("y", luabridge::bind_back (&Vec::getCoord, 1))
664+
.addFunction ("z", luabridge::bind_back (&Vec::getCoord, 2))
634665
.endClass ();
666+
// Lua: v:x() returns v.coord[0], v:y() returns v.coord[1], etc.
667+
```
668+
669+
`bind_back` also accepts lambdas:
670+
671+
```cpp
672+
int base = 10;
673+
ns.addFunction ("addBase", luabridge::bind_back ([base](int v, int offset) { return base + offset + v; }, 2));
674+
// Lua: addBase(30) == 42
635675
```
636676
637-
### 2.5.2 - Function Overloading
677+
### 2.5.3 - Function Overloading
638678
639679
When specifying more than one method to the `addFunction` or `addStaticFunction` of both `Namespace` and `Class`, those overloads will be invoked in case of a call. Only overloads that have matched arguments arity will be considered, and they will be tried from first to last until the call succeeds.
640680

0 commit comments

Comments
 (0)