-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluawrapperutil.hpp
844 lines (775 loc) · 26.8 KB
/
luawrapperutil.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/*
* Copyright (c) 2010-2013 Alexander Ames
* See Copyright Notice at the end of this file
*/
// API Summary:
//
// This is a set of utility functions that add to the functionalit of
// LuaWrapper. Over time I found myself repeating a few patterns, such as
// writing many trvial getter and setter functions, and wanting pass ownership
// of one object to another and have the Lua script properly handle that case.
//
// This file contains the additional functions that I've added but that do
// not really belong in the core API.
#ifndef LUAWRAPPERUTILS_HPP_
#define LUAWRAPPERUTILS_HPP_
#include "luawrapper.hpp"
#ifndef LUAW_NO_CXX11
#include <type_traits>
#endif
#ifndef LUAW_STD
#define LUAW_STD std
#endif
#ifndef LUAW_NO_CXX11
////////////////////////////////////////////////////////////////////////////////
//
// This template removes reference and const qualifier from the type
//
template <typename T>
struct luaW_remove_cr
{
typedef typename std::remove_const<typename std::remove_reference<T>::type>::type type;
};
#endif
////////////////////////////////////////////////////////////////////////////////
//
// A set of templated luaL_check and lua_push functions for use in the getters
// and setters below
//
// It is often useful to override luaU_check, luaU_to and/or luaU_push to
// operate on your own simple types rather than register your type with
// LuaWrapper, especially with small objects.
//
template<typename U, typename = void>
struct luaU_Impl
{
static U luaU_check(lua_State* L, int index);
static U luaU_to (lua_State* L, int index);
static void luaU_push (lua_State* L, const U& value);
};
template<typename U> U luaU_check(lua_State* L, int index) { return luaU_Impl<U>::luaU_check(L, index); }
template<typename U> U luaU_to (lua_State* L, int index) { return luaU_Impl<U>::luaU_to (L, index); }
template<typename U> void luaU_push (lua_State* L, const U& value) { luaU_Impl<U>::luaU_push (L, value); }
////////////////////////////////////////////////////////////////////////////////
//
// This is slightly different than the previous three functions in that you
// shouldn't need to write your own version of it, since it uses luaU_check
// automatically.
//
template<typename U>
U luaU_opt(lua_State* L, int index, const U& fallback = U())
{
if (lua_isnil(L, index))
return fallback;
else
return luaU_Impl<U>::luaU_check(L, index);
}
template<>
struct luaU_Impl<bool>
{
static bool luaU_check(lua_State* L, int index) { return lua_toboolean (L, index) != 0; }
static bool luaU_to (lua_State* L, int index) { return lua_toboolean (L, index) != 0; }
static void luaU_push (lua_State* L, const bool& value) { lua_pushboolean(L, value); }
};
template<>
struct luaU_Impl<const char*>
{
static const char* luaU_check(lua_State* L, int index) { return luaL_checkstring(L, index); }
static const char* luaU_to (lua_State* L, int index) { return lua_tostring (L, index); }
static void luaU_push (lua_State* L, const char* const& value) { lua_pushstring (L, value); }
};
template<>
struct luaU_Impl<unsigned int>
{
static unsigned int luaU_check(lua_State* L, int index) { return static_cast<unsigned int>(luaL_checkinteger(L, index)); }
static unsigned int luaU_to (lua_State* L, int index) { return static_cast<unsigned int>(lua_tointeger (L, index)); }
static void luaU_push (lua_State* L, const unsigned int& value) { lua_pushinteger (L, value); }
};
template<>
struct luaU_Impl<int>
{
static int luaU_check(lua_State* L, int index) { return static_cast<int>(luaL_checkinteger(L, index)); }
static int luaU_to (lua_State* L, int index) { return static_cast<int>(lua_tointeger (L, index)); }
static void luaU_push (lua_State* L, const int& value) { lua_pushinteger (L, value); }
};
template<>
struct luaU_Impl<unsigned char>
{
static unsigned char luaU_check(lua_State* L, int index) { return static_cast<unsigned char>(luaL_checkinteger(L, index)); }
static unsigned char luaU_to (lua_State* L, int index) { return static_cast<unsigned char>(lua_tointeger (L, index)); }
static void luaU_push (lua_State* L, const unsigned char& value) { lua_pushinteger (L, value); }
};
template<>
struct luaU_Impl<char>
{
static char luaU_check(lua_State* L, int index) { return static_cast<char>(luaL_checkinteger(L, index)); }
static char luaU_to (lua_State* L, int index) { return static_cast<char>(lua_tointeger (L, index)); }
static void luaU_push (lua_State* L, const char& value) { lua_pushinteger (L, value); }
};
template<>
struct luaU_Impl<float>
{
static float luaU_check(lua_State* L, int index) { return static_cast<float>(luaL_checknumber(L, index)); }
static float luaU_to (lua_State* L, int index) { return static_cast<float>(lua_tonumber (L, index)); }
static void luaU_push (lua_State* L, const float& value) { lua_pushnumber (L, value); }
};
template<>
struct luaU_Impl<double>
{
static double luaU_check(lua_State* L, int index) { return static_cast<double>(luaL_checknumber(L, index)); }
static double luaU_to (lua_State* L, int index) { return static_cast<double>(lua_tonumber (L, index)); }
static void luaU_push (lua_State* L, const double& value) { lua_pushnumber (L, value); }
};
#ifndef LUAW_NO_CXX11
template<typename T>
struct luaU_Impl<T, typename LUAW_STD::enable_if<LUAW_STD::is_enum<T>::value>::type>
{
static T luaU_check(lua_State* L, int index) { return static_cast<T>(luaL_checkinteger (L, index)); }
static T luaU_to (lua_State* L, int index) { return static_cast<T>(lua_tointeger (L, index)); }
static void luaU_push (lua_State* L, const T& value) { lua_pushnumber(L, static_cast<int>(value )); }
};
template<typename T>
struct luaU_Impl<T*, typename LUAW_STD::enable_if<LUAW_STD::is_class<T>::value>::type>
{
static T* luaU_check( lua_State* L, int index) { return luaW_check<T>(L, index); }
static T* luaU_to ( lua_State* L, int index) { return luaW_to <T>(L, index); }
static void luaU_push ( lua_State* L, T*& value) { luaW_push <T>(L, value); }
static void luaU_push ( lua_State* L, T* value) { luaW_push <T>(L, value); }
};
#endif
///////////////////////////////////////////////////////////////////////////////
//
// These are just some functions I've always felt should exist
//
template <typename U>
inline U luaU_getfield(lua_State* L, int index, const char* field)
{
#ifndef LUAW_NO_CXX11
static_assert(!LUAW_STD::is_same<U, const char*>::value,
"luaU_getfield is not safe to use on const char*'s. (The string will be popped from the stack.)");
#endif
lua_getfield(L, index, field);
U val = luaU_to<U>(L, -1);
lua_pop(L, 1);
return val;
}
template <typename U>
inline U luaU_checkfield(lua_State* L, int index, const char* field)
{
#ifndef LUAW_NO_CXX11
static_assert(!LUAW_STD::is_same<U, const char*>::value,
"luaU_checkfield is not safe to use on const char*'s. (The string will be popped from the stack.)");
#endif
lua_getfield(L, index, field);
U val = luaU_check<U>(L, -1);
lua_pop(L, 1);
return val;
}
template <typename U>
inline U luaU_optfield(lua_State* L, int index, const char* field, const U& fallback = U())
{
#ifndef LUAW_NO_CXX11
static_assert(!LUAW_STD::is_same<U, const char*>::value,
"luaU_getfield is not safe to use on const char*'s. (The string will be popped from the stack.)");
#endif
lua_getfield(L, index, field);
U val = luaU_opt<U>(L, -1, fallback);
lua_pop(L, 1);
return val;
}
template <typename U>
inline void luaU_setfield(lua_State* L, int index, const char* field, U val)
{
luaU_push<U>(L, val);
lua_setfield(L, luaW_correctindex(L, index, 1), field);
}
///////////////////////////////////////////////////////////////////////////////
//
// A set of trivial getter and setter templates. These templates are designed
// to call trivial getters or setters.
//
// There are four forms:
// 1. Getting or setting a public member variable that is of a primitive type
// 2. Getting or setting a public member variable that is a pointer to an
// object
// 3. Getting or setting a private member variable that is of a primitive type
// through a getter or setter
// 3. Getting or setting a private member variable that is is a pointer to an
// object through a getter or setter
//
// The interface to all of them is the same however. In addition to plain
// getter and setter functions, there is a getset which does both - if an
// argument is supplied it attempts to set the value, and in either case it
// returns the value. In your lua table declaration in C++ rather than write
// individiual wrappers for each getter and setter you may do the following:
//
// static luaL_reg Foo_metatable[] =
// {
// { "GetBar", luaU_get<Foo, bool, &Widget::GetBar> },
// { "SetBar", luaU_set<Foo, bool, &Widget::SetBar> },
// { "Bar", luaU_getset<Foo, bool, &Widget::GetBar, &Widget::SetBar> },
// { NULL, NULL }
// }
//
// Getters and setters must have one of the following signatures:
// void T::Setter(U value);
// void T::Setter(U* value);
// void T::Setter(const U& value);
// U Getter() const;
// U* Getter() const;
//
// In this example, the variable Foo::bar is private and so it is accessed
// through getter and setter functions. If Foo::bar were public, it could be
// accessed directly, like so:
//
// static luaL_reg Foo_metatable[] =
// {
// { "GetBar", luaU_get<Foo, bool, &Widget::bar> },
// { "SetBar", luaU_set<Foo, bool, &Widget::bar> },
// { "Bar", luaU_getset<Foo, bool, &Widget::bar> },
// }
//
// In a Lua script, you can now use foo:GetBar(), foo:SetBar() and foo:Bar()
//
template <typename T, typename U, U T::*Member>
int luaU_get(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
luaU_push<U>(L, obj->*Member);
return 1;
}
template <typename T, typename U, U* T::*Member>
int luaU_get(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
luaW_push<U>(L, obj->*Member);
return 1;
}
template <typename T, typename U, U (T::*Getter)() const>
int luaU_get(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
luaU_push<U>(L, (obj->*Getter)());
return 1;
}
template <typename T, typename U, const U& (T::*Getter)() const>
int luaU_get(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
luaU_push<U>(L, (obj->*Getter)());
return 1;
}
template <typename T, typename U, U* (T::*Getter)() const>
int luaU_get(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
luaW_push<U>(L, (obj->*Getter)());
return 1;
}
template <typename T, typename U, U T::*Member>
int luaU_set(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj)
obj->*Member = luaU_check<U>(L, 2);
return 0;
}
template <typename T, typename U, U* T::*Member>
int luaU_set(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj)
{
U* member = luaW_opt<U>(L, 2);
obj->*Member = member;
}
return 0;
}
template <typename T, typename U, const U* T::*Member>
int luaU_set(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj)
{
U* member = luaW_opt<U>(L, 2);
obj->*Member = member;
}
return 0;
}
template <typename T, typename U, const U* T::*Member>
int luaU_setandrelease(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj)
{
U* member = luaW_opt<U>(L, 2);
obj->*Member = member;
if (member)
luaW_release<U>(L, member);
}
return 0;
}
template <typename T, typename U, void (T::*Setter)(U)>
int luaU_set(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj)
(obj->*Setter)(luaU_check<U>(L, 2));
return 0;
}
template <typename T, typename U, void (T::*Setter)(const U&)>
int luaU_set(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj)
(obj->*Setter)(luaU_check<U>(L, 2));
return 0;
}
template <typename T, typename U, void (T::*Setter)(U*)>
int luaU_set(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj)
{
U* member = luaW_opt<U>(L, 2);
(obj->*Setter)(member);
}
return 0;
}
template <typename T, typename U, void (T::*Setter)(U*)>
int luaU_setandrelease(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj)
{
U* member = luaW_opt<U>(L, 2);
(obj->*Setter)(member);
if (member)
luaW_release<U>(L, member);
}
return 0;
}
template <typename T, typename U, U T::*Member>
int luaU_getset(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj && lua_gettop(L) >= 2)
{
obj->*Member = luaU_check<U>(L, 2);
return 0;
}
else
{
luaU_push<U>(L, obj->*Member);
return 1;
}
}
template <typename T, typename U, U* T::*Member>
int luaU_getset(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj && lua_gettop(L) >= 2)
{
U* member = luaW_opt<U>(L, 2);
obj->*Member = member;
return 0;
}
else
{
luaW_push<U>(L, obj->*Member);
return 1;
}
}
template <typename T, typename U, U* T::*Member>
int luaU_getsetandrelease(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj && lua_gettop(L) >= 2)
{
U* member = luaW_opt<U>(L, 2);
obj->*Member = member;
if (member)
luaW_release<U>(L, member);
return 0;
}
else
{
luaW_push<U>(L, obj->*Member);
return 1;
}
}
template <typename T, typename U, U (T::*Getter)() const, void (T::*Setter)(U)>
int luaU_getset(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj && lua_gettop(L) >= 2)
{
(obj->*Setter)(luaU_check<U>(L, 2));
return 0;
}
else
{
luaU_push<U>(L, (obj->*Getter)());
return 1;
}
}
template <typename T, typename U, U (T::*Getter)() const, void (T::*Setter)(const U&)>
int luaU_getset(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj && lua_gettop(L) >= 2)
{
(obj->*Setter)(luaU_check<U>(L, 2));
return 0;
}
else
{
luaU_push<U>(L, (obj->*Getter)());
return 1;
}
}
template <typename T, typename U, const U& (T::*Getter)() const, void (T::*Setter)(const U&)>
int luaU_getset(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj && lua_gettop(L) >= 2)
{
(obj->*Setter)(luaU_check<U>(L, 2));
return 0;
}
else
{
luaU_push<U>(L, (obj->*Getter)());
return 1;
}
}
template <typename T, typename U, U* (T::*Getter)() const, void (T::*Setter)(U*)>
int luaU_getset(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj && lua_gettop(L) >= 2)
{
U* member = luaW_opt<U>(L, 2);
(obj->*Setter)(member);
return 0;
}
else
{
luaW_push<U>(L, (obj->*Getter)());
return 1;
}
}
template <typename T, typename U, U* (T::*Getter)() const, void (T::*Setter)(U*)>
int luaU_getsetandrelease(lua_State* L)
{
T* obj = luaW_check<T>(L, 1);
if (obj && lua_gettop(L) >= 2)
{
U* member = luaW_opt<U>(L, 2);
(obj->*Setter)(member);
if (member)
luaW_release<U>(L, member);
return 0;
}
else
{
luaW_push<U>(L, (obj->*Getter)());
return 1;
}
}
#if !defined(_WIN32) && !defined(LUAW_NO_CXX11)
///////////////////////////////////////////////////////////////////////////////
//
// luaU_func is a special macro that expands into a simple function wrapper.
// Unlike the getter setters above, you merely need to name the function you
// would like to wrap.
//
// For example,
//
// struct Foo
// {
// int DoSomething(int, const char*);
// };
//
// static luaL_reg Foo_metatable[] =
// {
// { "DoSomething", luaU_func(&Foo::DoSomething) },
// { NULL, NULL }
// }
//
// void RegisterFoo(lua_State* L)
// {
// luaW_register<Foo>(L, "Foo", NULL, Foo_metatable);
// }
//
// This macro will expand based on the function signature of Foo::DoSomething
// In this example, it would expand into the following wrapper:
//
// luaU_push(luaW_check<T>(L, 1)->DoSomething(luaU_check<int>(L, 2), luaU_check<const char*>(L, 3)));
// return 1;
//
// In this example there is only one member function called DoSomething. In some
// cases there may be multiple overrides for a function. For those cases, an
// additional macro has been provided, luaU_funcsig, which takes the entire
// function signature. The arguments to the macro reflect the order they would
// appear in the function signature: return type, type name, function name, and
// finally a list of all the argument types. For example:
//
// struct Foo
// {
// int DoSomething (const char*);
// int DoSomething (const char*, int);
// };
//
// const struct luaL_Reg Foo_metatable[] =
// {
// {"DoSomething1", luaU_funcsig(int, Foo, DoSomething, const char*)) },
// {"DoSomething1", luaU_funcsig(int, Foo, DoSomething, const char*, int)) },
// { NULL, NULL }
// };
//
// There`s also support for static and freestanding functions. Macros luaU_staticfunc
// and luaU_staticfuncsig work equally to luaU_func and luaU_funcsig, except for that
// you need to provide a separate metatable for static functions.
// For example,
//
// struct Foo
// {
// int DoSomething(int, const char*);
// static void DoSomethingElse(int a, int b, float c);
// };
//
// static luaL_reg Foo_metatable[] =
// {
// { "DoSomething", luaU_func(&Foo::DoSomething) },
// { NULL, NULL }
// };
//
// static luaL_reg Foo_metatable_static[] =
// {
// { "DoSomethingElse", luaU_staticfunc(&Foo::DoSomethingElse) },
// { NULL, NULL }
// };
//
// void RegisterFoo(lua_State* L)
// {
// luaW_register<Foo>(L, "Foo", Foo_metatable_static, Foo_metatable);
// }
//
// After that you will be able to use Foo class from Lua like this:
// local foo = Foo.new()
// foo:DoSomething(42, 'The Ultimate Question of Life, the Universe, and Everything.') -- member function call
// Foo:DoSomethingElse(30, 12, 3.1459) -- Static function call
//
// These macros and it's underlying templates are somewhat experimental and some
// refinements are probably needed. There are cases where it does not
// currently work and I expect some changes can be made to refine its behavior.
//
#define luaU_func(memberfunc) &luaU_MemberFuncWrapper<decltype(memberfunc),memberfunc>::call
#define luaU_funcsig(returntype, type, funcname, ...) luaU_func(static_cast<returntype (type::*)(__VA_ARGS__)>(&type::funcname))
#define luaU_staticfunc(func) &luaU_StaticFuncWrapper<decltype(func),func>::call
#define luaU_staticfuncsig(returntype, type, funcname, ...) luaU_staticfunc(static_cast<returntype (*)(__VA_ARGS__)>(&type::funcname))
template<int... ints> struct luaU_IntPack { };
template<int start, int count, int... tail> struct luaU_MakeIntRangeType { typedef typename luaU_MakeIntRangeType<start, count-1, start+count-1, tail...>::type type; };
template<int start, int... tail> struct luaU_MakeIntRangeType<start, 0, tail...> { typedef luaU_IntPack<tail...> type; };
template<int start, int count> inline typename luaU_MakeIntRangeType<start, count>::type luaU_makeIntRange() { return typename luaU_MakeIntRangeType<start, count>::type(); }
///////////////////////////////////////////////////////////////////////////////
//
// Member function wrapper
//
template<class MemFunPtrType, MemFunPtrType MemberFunc> struct luaU_MemberFuncWrapper;
template<class T, class ReturnType, class... Args, ReturnType(T::*MemberFunc)(Args...)>
struct luaU_MemberFuncWrapper<ReturnType (T::*)(Args...), MemberFunc>
{
public:
static int call(lua_State* L)
{
return callImpl(L, luaU_makeIntRange<2,sizeof...(Args)>());
}
private:
template<int... indices>
static int callImpl(lua_State* L, luaU_IntPack<indices...>)
{
luaU_push<ReturnType>(L, (luaW_check<T>(L, 1)->*MemberFunc)(luaU_check<typename luaW_remove_cr<Args>::type>(L, indices)...));
return 1;
}
};
template<class T, class... Args, void(T::*MemberFunc)(Args...)>
struct luaU_MemberFuncWrapper<void(T::*)(Args...), MemberFunc>
{
public:
static int call(lua_State* L)
{
return callImpl(L, luaU_makeIntRange<2, sizeof...(Args)>());
}
private:
template<int... indices>
static int callImpl(lua_State* L, luaU_IntPack<indices...>)
{
(luaW_check<T>(L, 1)->*MemberFunc)(luaU_check<typename luaW_remove_cr<Args>::type>(L, indices)...);
return 0;
}
};
///////////////////////////////////////////////////////////////////////////////
//
// static function wrapper
//
template<class FunPtrType, FunPtrType Func> struct luaU_StaticFuncWrapper;
template<class ReturnType, class... Args, ReturnType(*Func)(Args...)>
struct luaU_StaticFuncWrapper<ReturnType(*)(Args...), Func>
{
public:
static int call(lua_State* L)
{
return callImpl(L, luaU_makeIntRange<2,sizeof...(Args)>());
}
private:
template<int... indices>
static int callImpl(lua_State* L, luaU_IntPack<indices...>)
{
luaU_push<ReturnType>(L, (*Func)(luaU_check<typename luaW_remove_cr<Args>::type>(L, indices)...));
return 1;
}
};
template<class... Args, void(*Func)(Args...)>
struct luaU_StaticFuncWrapper<void(*)(Args...), Func>
{
public:
static int call(lua_State* L)
{
return callImpl(L, luaU_makeIntRange<2, sizeof...(Args)>());
}
private:
template<int... indices>
static int callImpl(lua_State* L, luaU_IntPack<indices...>)
{
(*Func)(luaU_check<typename luaW_remove_cr<Args>::type>(L, indices)...);
return 0;
}
};
#endif
///////////////////////////////////////////////////////////////////////////////
//
// Calls the copy constructor for an object of type T.
// Arguments may be passed in, in case they're needed for the postconstructor
//
// e.g.
//
// C++:
// luaL_reg Foo_Metatable[] =
// {
// { "clone", luaU_clone<Foo> },
// { NULL, NULL }
// };
//
// Lua:
// foo = Foo.new()
// foo2 = foo:clone()
//
template <typename T>
int luaU_clone(lua_State* L)
{
// obj ...
T* obj = new T(*luaW_check<T>(L, 1));
lua_remove(L, 1); // ...
int numargs = lua_gettop(L);
luaW_push<T>(L, obj); // ... clone
luaW_hold<T>(L, obj);
luaW_postconstructor<T>(L, numargs);
return 1;
}
///////////////////////////////////////////////////////////////////////////////
//
// luaU_build is intended to be used to initialize many values by passing in a
// table. They keys of the table are used as function names, and values are
// used as arguments to the function. This is intended to be used on functions
// that are simple setters.
//
// For example, if luaU_build is set as the post constructor, you can
// initialize an object as so:
//
// f = Foo.new
// {
// X = 10;
// Y = 20;
// }
//
// After the object is constructed, luaU_build will do the equivalent of
// calling f:X(10) and f:Y(20).
//
template <typename T>
int luaU_build(lua_State* L)
{
// obj {}
lua_insert(L, -2); // {} obj
if (lua_type(L, 1) == LUA_TTABLE)
{
for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1))
{
// {} obj k v
lua_pushvalue(L, -2); // {} obj k v k
lua_gettable(L, -4); // {} obj k v ud[k]
lua_pushvalue(L, -4); // {} obj k v ud[k] ud
lua_pushvalue(L, -3); // {} obj k v ud[k] ud v
lua_call(L, 2, 0); // {} obj k v
}
// {} ud
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////
//
// Takes the object of type T at the top of the stack and stores it in on a
// table with the name storagetable, on the table at the specified index.
//
// You may manually call luaW_hold and luaW_release to handle pointer
// ownership, but it is often easier to simply store a Lua userdata on a table
// that is owned by its parent. This ensures that your object will not be
// prematurely freed, and that it can only be destoryed after its parent.
//
// e.g.
//
// Foo* parent = luaW_check<Foo>(L, 1);
// Bar* child = luaW_check<Bar>(L, 2);
// if (parent && child)
// {
// luaU_store<Bar>(L, 1, "Children");
// parent->AddChild(child);
// }
//
template <typename T>
void luaU_store(lua_State* L, int index, const char* storagetable, const char* key = NULL)
{
// ... store ... obj
lua_getfield(L, index, storagetable); // ... store ... obj store.storagetable
if (key)
lua_pushstring(L, key); // ... store ... obj store.storagetable key
else
LuaWrapper<T>::identifier(L, luaW_to<T>(L, -2)); // ... store ... obj store.storagetable key
lua_pushvalue(L, -3); // ... store ... obj store.storagetable key obj
lua_settable(L, -3); // ... store ... obj store.storagetable
lua_pop(L, 1); // ... store ... obj
}
/*
* Copyright (c) 2010-2011 Alexander Ames
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#endif // LUAWRAPPERUTILS_HPP_