c_call¶
templated type to transport functions through templates
template <typename Function, Function f>
int c_call (lua_State* L);
template <typename... Functions>
int c_call (lua_State* L);
The goal of sol::c_call<...> is to provide a way to wrap a function and transport it through a compile-time context. This enables faster speed at the cost of a much harder to read / poorer interface, and can alleviate some template compilation speed issues. sol::c_call expects a type for its first template argument, and a value of the previously provided type for the second template argument. To make a compile-time transported overloaded function, specify multiple functions in the same type, value pairing, but put it inside of a sol::wrap.
Note
This can also be placed into the argument list for a usertype as well.
This pushes a raw lua_CFunction into whatever you pass the resulting c_call function pointer into, whether it be a table or a userdata or whatever else using sol2’s API. The resulting lua_CFunction can also be used directly with the lua API, just like many of sol2’s types can be intermingled with Lua’s API if you know what you’re doing.
It is advisable for the user to consider making a macro to do the necessary decltype( &function_name, ), function_name. sol does not provide one because many codebases already have one similar to this.
Here’s an example below of various ways to use sol::c_call:
1#define SOL_ALL_SAFETIES_ON 1
2#include <sol/sol.hpp>
3
4int f1(int) {
5 return 32;
6}
7
8int f2(int, int) {
9 return 1;
10}
11
12struct fer {
13 double f3(int, int) {
14 return 2.5;
15 }
16};
17
18int main() {
19
20 sol::state lua;
21 // overloaded function f
22 lua.set("f",
23 sol::c_call<sol::wrap<decltype(&f1), &f1>,
24 sol::wrap<decltype(&f2), &f2>,
25 sol::wrap<decltype(&fer::f3), &fer::f3>>);
26 // singly-wrapped function
27 lua.set("g", sol::c_call<sol::wrap<decltype(&f1), &f1>>);
28 // without the 'sol::wrap' boilerplate
29 lua.set("h", sol::c_call<decltype(&f2), &f2>);
30 // object used for the 'fer' member function call
31 lua.set("obj", fer());
32
33 // call them like any other bound function
34 lua.script("r1 = f(1)");
35 lua.script("r2 = f(1, 2)");
36 lua.script("r3 = f(obj, 1, 2)");
37 lua.script("r4 = g(1)");
38 lua.script("r5 = h(1, 2)");
39
40 // get the results and see
41 // if it worked out
42 int r1 = lua["r1"];
43 SOL_ASSERT(r1 == 32);
44 int r2 = lua["r2"];
45 SOL_ASSERT(r2 == 1);
46 double r3 = lua["r3"];
47 SOL_ASSERT(r3 == 2.5);
48 int r4 = lua["r4"];
49 SOL_ASSERT(r4 == 32);
50 int r5 = lua["r5"];
51 SOL_ASSERT(r5 == 1);
52
53 return 0;
54}