128
128
129
129
## Registration
130
130
131
- There are four types of objects that LuaBridge can register:
131
+ There are five types of objects that LuaBridge can register:
132
132
133
133
- **Data**: Global variables, static class data members, and class data
134
134
members.
135
135
136
136
- **Functions**: Regular functions, static class members, and class member
137
- functions
137
+ functions.
138
+
139
+ - **CFunctions**: A regular function, static class member function, or class
140
+ member function that uses the `lua_CFunction` calling
141
+ convention.
138
142
139
143
- **Namespaces**: A namespace is simply a table containing registrations of
140
144
functions, data, properties, and other namespaces.
185
189
186
190
The results are accessible to Lua as `test`, `test.detail`, and
187
191
`test.utility`. Here we introduce the `endNamespace` function; it returns an
188
- object representing the original enclosing namespace. All LuaBridge functions which
189
- create registrations return an object upon which subsequent registrations can
190
- be made, allowing for an unlimited number of registrations to be chained
191
- together using the dot operator `.`. Adding two objects with the same name, in
192
- the same namespace, results in undefined behavior (although LuaBridge will
193
- silently accept it).
192
+ object representing the original enclosing namespace. All LuaBridge functions
193
+ which create registrations return an object upon which subsequent
194
+ registrations can be made, allowing for an unlimited number of registrations
195
+ to be chained together using the dot operator `.`. Adding two objects with the
196
+ same name, in the same namespace, results in undefined behavior (although
197
+ LuaBridge will silently accept it).
194
198
195
- A namespace can be re-opened later to add
196
- more functions. This lets you split up the registration between different
197
- source files. These are equivalent:
199
+ A namespace can be re-opened later to add more functions. This lets you split
200
+ up the registration between different source files. These are equivalent:
198
201
199
202
getGlobalNamespace (L)
200
203
.beginNamespace ("test")
215
218
.endNamespace ();
216
219
217
220
218
- ### Data, Properties, and Functions
221
+ ### Data, Properties, Functions, and CFunctions.
219
222
220
223
These are registered into a namespace using `addVariable`, `addProperty`,
221
- and `addFunction`. When registered functions are called by scripts, LuaBridge
222
- automatically takes care of the conversion of arguments into the appropriate
223
- data type when doing so is possible. This automated system works for the
224
- function's return value, and up to 8 parameters although more can be added by
225
- extending the templates. Pointers, references, and objects of class type as
226
- parameters are treated specially, and explained later. If we have:
224
+ `addFunction`, and `addCFunction`. When registered functions are called by
225
+ scripts, LuaBridge automatically takes care of the conversion of arguments
226
+ into the appropriate data type when doing so is possible. This automated
227
+ system works for the function's return value, and up to 8 parameters although
228
+ more can be added by extending the templates. Pointers, references, and
229
+ objects of class type as parameters are treated specially, and explained
230
+ later. If we have:
227
231
228
232
int globalVar;
229
233
static float staticVar;
234
238
235
239
int foo () { return 42; }
236
240
void bar (char const*) { }
241
+ int cFunc (lua_State* L) { return 0; }
237
242
238
- These are registered with:
243
+ These are registered with:
239
244
240
245
getGlobalNamespace (L)
241
246
.beginNamespace ("test")
245
250
.addProperty ("prop2", getString) // read only
246
251
.addFunction ("foo", foo)
247
252
.addFunction ("bar", bar)
253
+ .addCFunction ("cfunc", cFunc)
248
254
.endNamespace ();
249
255
250
256
Variables can be marked _read-only_ by passing `false` in the second optional
259
265
test.prop2 -- a read-only lua_String property
260
266
test.foo -- a function returning a lua_Number
261
267
test.bar -- a function taking a lua_String as a parameter
268
+ test.cfunc -- a function with a variable argument list and multi-return
262
269
263
270
Note that `test.prop1` and `test.prop2` both refer to the same value. However,
264
271
since `test.prop2` is read-only, assignment does not work. These Lua
297
304
static void setStaticProperty (float f) { staticProperty = f; }
298
305
static void staticFunc () { }
299
306
307
+ static int staticCFunc () { return 0; }
308
+
300
309
std::string dataMember;
301
310
302
311
char dataProperty;
305
314
306
315
void func1 () { }
307
316
virtual void virtualFunc () { }
317
+
318
+ int cfunc (lua_State* L) { return 0; }
308
319
};
309
320
310
321
struct B : public A {
326
337
.addStaticData ("staticData", &A::staticData)
327
338
.addStaticProperty ("staticProperty", &A::staticProperty)
328
339
.addStaticMethod ("staticFunc", &A::staticFunc)
340
+ .addStaticCFunction ("staticCFunc", &A::staticCFunc)
329
341
.addData ("data", &A::dataMember)
330
342
.addProperty ("prop", &A::getProperty, &A::setProperty)
331
343
.addMethod ("func1", &A::func1)
332
344
.addMethod ("virtualFunc", &A::virtualFunc)
345
+ .addCFunction ("cfunc", &A::cfunc)
333
346
.endClass ()
334
347
.deriveClass <B, A> ("B")
335
348
.addData ("data", &B::dataMember2)
@@ -3440,7 +3453,7 @@ class Namespace
3440
3453
*/
3441
3454
template <class MemFn ,
3442
3455
class ReturnType = typename FuncTraits <MemFn>::ReturnType>
3443
- struct CallMember
3456
+ struct CallMemberFunction
3444
3457
{
3445
3458
typedef typename FuncTraits <MemFn>::ClassType T;
3446
3459
typedef typename FuncTraits <MemFn>::Params Params;
@@ -3471,7 +3484,7 @@ class Namespace
3471
3484
lua_CFunction to call a class member function with no return value.
3472
3485
*/
3473
3486
template <class MemFn >
3474
- struct CallMember <MemFn, void >
3487
+ struct CallMemberFunction <MemFn, void >
3475
3488
{
3476
3489
typedef typename FuncTraits <MemFn>::ClassType T;
3477
3490
typedef typename FuncTraits <MemFn>::Params Params;
@@ -3495,30 +3508,54 @@ class Namespace
3495
3508
}
3496
3509
};
3497
3510
3511
+ // ----------------------------------------------------------------------------
3512
+ /* *
3513
+ lua_CFunction to call a class member lua_CFunction
3514
+ */
3515
+ template <class T >
3516
+ struct CallMemberCFunction
3517
+ {
3518
+ static int call (lua_State* L)
3519
+ {
3520
+ typedef int (T::*MFP)(lua_State* L);
3521
+ T* const t = Detail::Userdata::get <T> (L, 1 , false );
3522
+ MFP const mfp = *static_cast <MFP*> (lua_touserdata (L, lua_upvalueindex (1 )));
3523
+ return (t->*mfp) (L);
3524
+ }
3525
+
3526
+ static int callConst (lua_State* L)
3527
+ {
3528
+ typedef int (T::*MFP)(lua_State* L);
3529
+ T const * const t = Detail::Userdata::get <T> (L, 1 , true );
3530
+ MFP const mfp = *static_cast <MFP*> (lua_touserdata (L, lua_upvalueindex (1 )));
3531
+ return (t->*mfp) (L);
3532
+ }
3533
+ };
3534
+
3498
3535
// ----------------------------------------------------------------------------
3499
3536
3500
3537
// SFINAE Helpers
3501
3538
3502
3539
template <class MemFn , bool isConst>
3503
- struct CallMemberHelper
3540
+ struct CallMemberFunctionHelper
3504
3541
{
3505
3542
static void add (lua_State* L, char const * name, MemFn mf)
3506
3543
{
3507
3544
new (lua_newuserdata (L, sizeof (MemFn))) MemFn (mf);
3508
- lua_pushcclosure (L, &CallMember <MemFn>::callConst, 1 );
3545
+ lua_pushcclosure (L, &CallMemberFunction <MemFn>::callConst, 1 );
3509
3546
lua_pushvalue (L, -1 );
3510
3547
rawsetfield (L, -5 , name); // const table
3511
3548
rawsetfield (L, -3 , name); // class table
3512
3549
}
3513
3550
};
3514
3551
3515
3552
template <class MemFn >
3516
- struct CallMemberHelper <MemFn, false >
3553
+ struct CallMemberFunctionHelper <MemFn, false >
3517
3554
{
3518
3555
static void add (lua_State* L, char const * name, MemFn mf)
3519
3556
{
3520
3557
new (lua_newuserdata (L, sizeof (MemFn))) MemFn (mf);
3521
- lua_pushcclosure (L, &CallMember <MemFn>::call, 1 );
3558
+ lua_pushcclosure (L, &CallMemberFunction <MemFn>::call, 1 );
3522
3559
rawsetfield (L, -3 , name); // class table
3523
3560
}
3524
3561
};
@@ -4100,6 +4137,17 @@ class Namespace
4100
4137
return *this ;
4101
4138
}
4102
4139
4140
+ // --------------------------------------------------------------------------
4141
+ /* *
4142
+ Add or replace a lua_CFunction.
4143
+ */
4144
+ Class <T>& addStaticCFunction (char const * name, int (*const fp)(lua_State*))
4145
+ {
4146
+ lua_pushcfunction (L, fp);
4147
+ rawsetfield (L, -2 , name);
4148
+ return *this ;
4149
+ }
4150
+
4103
4151
// --------------------------------------------------------------------------
4104
4152
/* *
4105
4153
Add or replace a data member.
@@ -4148,7 +4196,7 @@ class Namespace
4148
4196
rawgetfield (L, -4 , " __propget" );
4149
4197
typedef GT (T::*get_t ) () const ;
4150
4198
new (lua_newuserdata (L, sizeof (get_t ))) get_t (get);
4151
- lua_pushcclosure (L, &CallMember <get_t >::callConst, 1 );
4199
+ lua_pushcclosure (L, &CallMemberFunction <get_t >::callConst, 1 );
4152
4200
lua_pushvalue (L, -1 );
4153
4201
rawsetfield (L, -4 , name);
4154
4202
rawsetfield (L, -2 , name);
@@ -4161,7 +4209,7 @@ class Namespace
4161
4209
assert (lua_istable (L, -1 ));
4162
4210
typedef void (T::* set_t ) (ST);
4163
4211
new (lua_newuserdata (L, sizeof (set_t ))) set_t (set);
4164
- lua_pushcclosure (L, &CallMember <set_t >::call, 1 );
4212
+ lua_pushcclosure (L, &CallMemberFunction <set_t >::call, 1 );
4165
4213
rawsetfield (L, -2 , name);
4166
4214
lua_pop (L, 1 );
4167
4215
}
@@ -4178,7 +4226,7 @@ class Namespace
4178
4226
rawgetfield (L, -4 , " __propget" );
4179
4227
typedef GT (T::*get_t ) () const ;
4180
4228
new (lua_newuserdata (L, sizeof (get_t ))) get_t (get);
4181
- lua_pushcclosure (L, &CallMember <get_t >::callConst, 1 );
4229
+ lua_pushcclosure (L, &CallMemberFunction <get_t >::callConst, 1 );
4182
4230
lua_pushvalue (L, -1 );
4183
4231
rawsetfield (L, -4 , name);
4184
4232
rawsetfield (L, -2 , name);
@@ -4254,7 +4302,37 @@ class Namespace
4254
4302
template <class MemFn >
4255
4303
Class <T>& addMethod (char const * name, MemFn mf)
4256
4304
{
4257
- CallMemberHelper <MemFn, FuncTraits <MemFn>::isConstMemberFunction>::add (L, name, mf);
4305
+ CallMemberFunctionHelper <MemFn, FuncTraits <MemFn>::isConstMemberFunction>::add (L, name, mf);
4306
+ return *this ;
4307
+ }
4308
+
4309
+ // --------------------------------------------------------------------------
4310
+ /* *
4311
+ Add or replace a member lua_CFunction.
4312
+ */
4313
+ Class <T>& addCFunction (char const * name, int (T::*mfp)(lua_State*))
4314
+ {
4315
+ typedef int (T::*MFP)(lua_State*);
4316
+ assert (lua_istable (L, -1 ));
4317
+ new (lua_newuserdata (L, sizeof (mfp))) MFP (mfp);
4318
+ lua_pushcclosure (L, &CallMemberCFunction <T>::call, 1 );
4319
+ rawsetfield (L, -2 , name);
4320
+
4321
+ return *this ;
4322
+ }
4323
+
4324
+ // --------------------------------------------------------------------------
4325
+ /* *
4326
+ Add or replace a const member lua_CFunction.
4327
+ */
4328
+ Class <T>& addCFunction (char const * name, int (T::*mfp)(lua_State*) const )
4329
+ {
4330
+ typedef int (T::*MFP)(lua_State*) const ;
4331
+ assert (lua_istable (L, -1 ));
4332
+ new (lua_newuserdata (L, sizeof (mfp))) MFP (mfp);
4333
+ lua_pushcclosure (L, &CallMemberCFunction <T>::callConst, 1 );
4334
+ rawsetfield (L, -2 , name);
4335
+
4258
4336
return *this ;
4259
4337
}
4260
4338
@@ -4519,6 +4597,17 @@ class Namespace
4519
4597
return *this ;
4520
4598
}
4521
4599
4600
+ // ----------------------------------------------------------------------------
4601
+ /* *
4602
+ Add or replace a lua_CFunction.
4603
+ */
4604
+ Namespace& addCFunction (char const * name, int (*const fp)(lua_State*))
4605
+ {
4606
+ lua_pushcfunction (L, fp);
4607
+ rawsetfield (L, -2 , name);
4608
+ return *this ;
4609
+ }
4610
+
4522
4611
// ----------------------------------------------------------------------------
4523
4612
/* *
4524
4613
Open a new or existing class for registrations.
0 commit comments