-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patholua.hpp
executable file
·1142 lines (1009 loc) · 30.6 KB
/
olua.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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The MIT License (MIT)
*
* Copyright (c) 2019-2024 [email protected]
*
* https://github.com/zhongfq/olua
*
* 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.
*/
#ifndef __OLUA_HPP__
#define __OLUA_HPP__
#include "olua.h"
#include <string>
#include <locale>
#include <codecvt>
#include <functional>
#include <set>
#include <vector>
#include <limits>
#include <numeric>
#include <type_traits>
#include <map>
#include <memory>
#include <unordered_map>
/**
* New and close lua_State for several times, sometimes may got same
* memory address for lua_State, this because the malloc reuse memory.
* olua_context can return different id for each main lua_State.
* Define OLUA_HAVE_CONTEXT when you has implemention
*/
#ifndef OLUA_HAVE_CONTEXT
typedef int64_t olua_Context;
#endif
OLUA_BEGIN_DECLS
OLUA_API olua_Context olua_context(lua_State *L);
OLUA_API bool olua_contextequal(lua_State *L, olua_Context ctx);
OLUA_END_DECLS
/**
* Got lua main thread.
* Define OLUA_HAVE_MAINTHREAD when you has implemention
*/
OLUA_BEGIN_DECLS
OLUA_API lua_State *olua_mainthread(lua_State *L);
OLUA_END_DECLS
/**
* Help to check whether callback is run on the host thread of lua vm.
* It would be inserted into the start of callback block.
*/
#ifndef OLUA_HAVE_CHECKHOSTTHREAD
#define olua_checkhostthread() ((void)0)
#endif
/**
* Help you to delete ref when the function prototype don't provide enough
* information. You can record some information in olua_startcmpref and
* delete ref in olua_endcmpref by compare.
*
* prototype:
* void olua_startcmpref(lua_State *L, int idx, const char *refname);
* void olua_endcmpref(lua_State *L, int idx, const char *refname);
*
* example:
* static int func(lua_State *L)
* {
* ...
* olua_startcmpref(L);
* self->removeChildren();
* olua_endcmpref(L);
* ...
* }
*/
#ifndef OLUA_HAVE_CMPREF
#define olua_startcmpref(L, i, name) olua_noapi(olua_startcmpref)
#define olua_endcmpref(L, i, name) olua_noapi(olua_endcmpref)
#endif
/**
* Help you to get calling stack when thow lua error in c++ internal
* implementation. olua_startinvoke would be inserted into the start of
* function block and olua_endinvoke would be inserted before each
* return expression.
*
* example:
* static int func(lua_State *L)
* {
* olua_startinvoke(L);
* ...
* ...
* if (cond) {
* olua_endinvoke(L);
* return 2;
* }
* ...
* olua_endinvoke(L);
* return 1;
* }
*/
#ifndef OLUA_HAVE_TRACEINVOKING
#define olua_startinvoke(L) ((void)L)
#define olua_endinvoke(L) ((void)L)
#endif
template <class T> inline
T *olua_toobj(lua_State *L, int idx);
template <class T> inline
T *olua_checkobj(lua_State *L, int idx);
/**
* Handle the status of object after push, you can do some jobs
* according to the object status. For example, retain object in push and
* release object in __gc method.
*
* #define OLUA_HAVE_POSTPUSH
* template <class T> void olua_postpush(lua_State *L, T* obj, int status)
* {
* if (std::is_base_of<Object, T>::value && (status == OLUA_OBJ_NEW
* || status == OLUA_OBJ_UPDATE)) {
* ((Object *)obj)->retain();
* }
* }
*/
template <class T>
void olua_postpush(lua_State *L, T* obj, int status);
#ifndef OLUA_HAVE_POSTPUSH
template <class T>
void olua_postpush(lua_State *L, T* obj, int status) {}
#endif
/**
* Handle the status of object after new.
*
* example:
* static int Object_new(lua_State *L)
* {
* olua_startinvoke(L);
* Object *obj = new Object();
* olua_push_obj(L, obj, "Object");
* olua_postnew(L, obj);
* olua_endinvoke(L);
* return 1;
* }
*
* #define OLUA_HAVE_POSTNEW
* template <class T> void olua_postnew(lua_State *L, T *obj)
* {
* if (std::is_base_of<Object, T>::value) {
* ((Object *)obj)->autorelease();
* } else if (olua_getrawobj(L, obj)) {
* olua_setobjflag(L, -1, OLUA_FLAG_IN_HEAP);
* lua_pop(L, 1);
* }
* }
*
*/
template <class T>
void olua_postnew(lua_State *L, T *obj);
#ifndef OLUA_HAVE_POSTNEW
template <class T>
void olua_postnew(lua_State *L, T *obj)
{
if (olua_getrawobj(L, obj)) {
olua_setobjflag(L, -1, OLUA_FLAG_IN_HEAP);
lua_pop(L, 1);
}
}
#endif
/**
* delete object which belong to lua vm.
*/
template <class T>
void olua_postgc(lua_State *L, T *obj);
#ifndef OLUA_HAVE_POSTGC
template <class T>
void olua_postgc(lua_State *L, T *obj)
{
T *self = olua_checkobj<T>(L, -1);
if (self == obj) {
if (olua_hasobjflag(L, -1, OLUA_FLAG_IN_HEAP)) {
olua_setrawobj(L, -1, nullptr);
if (std::is_void<T>()) {
free((void *)obj);
} else {
delete obj;
}
} else if (olua_hasobjflag(L, -1, OLUA_FLAG_IN_USERDATA)) {
olua_setrawobj(L, -1, nullptr);
obj->~T();
}
} else {
luaL_error(L, "object error");
}
}
#endif
/**
* register or get lua type for c++ class.
* Define OLUA_HAVE_LUATYPE when you has implemention
*/
OLUA_BEGIN_DECLS
OLUA_API void olua_registerluatype(lua_State *L, const char *cpptype, const char *cls);
OLUA_API const char *olua_getluatype(lua_State *L, const char *cpptype);
OLUA_END_DECLS
template <class T> inline
void olua_registerluatype(lua_State *L, const char *cls)
{
const char *cpptype = typeid(T).name();
const char *last = olua_getluatype(L, cpptype);
if (last) {
if (last[0] != '*') {
lua_pushfstring(L, "*%s", last);
olua_registerluatype(L, cpptype, lua_tostring(L, -1));
lua_pop(L, 1);
}
} else {
olua_registerluatype(L, cpptype, cls);
}
}
template <class T>
const char *olua_getluatype(lua_State *L, const T *obj, const char *cls)
{
const char *preferred = NULL;
// try obj RTTI
if (olua_likely(obj)) {
preferred = olua_getluatype(L, typeid(*obj).name());
}
// try class RTTI
if (olua_unlikely(!preferred)) {
preferred = olua_getluatype(L, typeid(T).name());
}
if (olua_likely(preferred)) {
if (preferred[0] == '*') {
preferred = cls == NULL ? (preferred + 1) : cls;
}
return preferred;
} else {
return cls;
}
}
template <> inline
const char *olua_getluatype<void>(lua_State *L, const void *obj, const char *cls)
{
return cls == NULL ? OLUA_VOIDCLS : cls;
}
template <class T> inline
const char *olua_getluatype(lua_State *L)
{
return olua_getluatype(L, typeid(T).name());
}
template <class T> inline
bool olua_isa(lua_State *L, int idx)
{
return olua_isa(L, idx, olua_getluatype<T>(L));
}
template <class T> inline
void *olua_pushclassobj(lua_State *L)
{
return olua_pushclassobj(L, olua_getluatype<T>(L));
}
template <class T> inline
T *olua_toobj(lua_State *L, int idx)
{
return (T *)olua_toobj(L, idx, olua_getluatype<T>(L));
}
template <class T> inline
T *olua_checkobj(lua_State *L, int idx)
{
return (T *)olua_checkobj(L, idx, olua_getluatype<T>(L));
}
template <class T>
int olua_pushobj(lua_State *L, const T *value, const char *cls)
{
cls = olua_getluatype(L, value, cls);
if (!cls) {
luaL_error(L, "lua class not found: %s", typeid(T).name());
}
olua_postpush(L, (T *)value, olua_pushobj(L, (void *)value, cls));
return 1;
}
template <class T> inline
int olua_pushobj(lua_State *L, const T *value)
{
return olua_pushobj<T>(L, value, nullptr);
}
template <class T>
int olua_pushobj_as(lua_State *L, int idx, const T *value, const char *ref)
{
idx = lua_absindex(L, idx);
if (olua_loadref(L, idx, ref) != LUA_TUSERDATA) {
lua_pop(L, 1);
olua_pushobj(L, (void *)value, olua_getluatype<T>(L));
olua_setobjflag(L, -1, OLUA_FLAG_SKIP_GC);
olua_addref(L, idx, ref, -1, OLUA_REF_ALONE);
olua_addref(L, -1, "as.self", idx, OLUA_REF_ALONE);
}
return 1;
}
template <class T> inline
void *olua_newobjstub(lua_State *L)
{
return olua_newobjstub(L, olua_getluatype<T>(L));
}
template <class T> inline
int olua_pushobjstub(lua_State *L, T *value, void *stub)
{
const char *cls = olua_getluatype<T>(L, value, NULL);
return olua_pushobjstub(L, (void *)value, stub, cls);
}
// type traits
namespace std {
#if __cplusplus < 201402L
template< bool B, class T = void >
using enable_if_t = typename enable_if<B,T>::type;
template< class T >
using remove_cv_t = typename remove_cv<T>::type;
template< class T >
using remove_reference_t = typename remove_reference<T>::type;
template< class T >
using remove_pointer_t = typename remove_pointer<T>::type;
#endif
#if __cplusplus < 201703L
template <bool B>
using bool_constant = integral_constant<bool, B>;
#endif
}
namespace olua {
template <class T>
using remove_cvr_t = std::remove_cv_t<std::remove_reference_t<T>>;
template <class T>
using remove_cvrp_t = std::remove_pointer_t<olua::remove_cvr_t<T>>;
}
// class
template <class T, class S = void>
void oluacls_class(lua_State *L, const char *cls)
{
oluacls_class(L, cls, olua_getluatype<S>(L));
olua_registerluatype<T>(L, cls);
}
// const value
template <class T, std::enable_if_t<std::is_integral<T>::value, bool> = true> inline
void oluacls_const(lua_State *L, const char *name, const T value)
{
if (std::is_same<T, bool>::value) {
lua_pushboolean(L, (bool)value);
} else {
lua_pushinteger(L, (lua_Integer)value);
}
oluacls_const(L, name);
}
template <class T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true> inline
void oluacls_const(lua_State *L, const char *name, const T value)
{
lua_pushnumber(L, (lua_Number)value);
oluacls_const(L, name);
}
template <class T, std::enable_if_t<std::is_enum<T>::value, bool> = true> inline
void oluacls_const(lua_State *L, const char *name, const T value)
{
oluacls_enum(L, name, (lua_Integer)value);
}
static inline
void oluacls_const(lua_State *L, const char *name, const std::string &value)
{
olua_pushlstring(L, value.c_str(), value.size());
oluacls_const(L, name);
}
static inline
void oluacls_const(lua_State *L, const char *name, const char *value)
{
olua_pushstring(L, value);
oluacls_const(L, name);
}
template <class T> inline
void oluacls_const(lua_State *L, const char *name, const T *value)
{
olua_pushobj<T>(L, value);
oluacls_const(L, name);
}
// convertor between c++ and lua, use for code generation
// bool
static inline
bool olua_is_bool(lua_State *L, int idx)
{
return olua_isbool(L, idx);
}
template <class T, std::enable_if_t<std::is_integral<T>::value, bool> = true> inline
void olua_check_bool(lua_State *L, int idx, T *value)
{
*value = (T)olua_checkbool(L, idx);
}
template <class T, std::enable_if_t<std::is_integral<T>::value, bool> = true> inline
int olua_push_bool(lua_State *L, T value)
{
olua_pushbool(L, (bool)value);
return 1;
}
// integer
static inline
bool olua_is_integer(lua_State *L, int idx)
{
return olua_isinteger(L, idx);
}
template <class T, std::enable_if_t<std::is_integral<T>::value, bool> = true> inline
void olua_check_integer(lua_State *L, int idx, T *value)
{
*value = (T)olua_checkinteger(L, idx);
}
template <class T, std::enable_if_t<std::is_integral<T>::value, bool> = true> inline
int olua_push_integer(lua_State *L, T value)
{
olua_pushinteger(L, (lua_Integer)value);
return 1;
}
// number
static inline
bool olua_is_number(lua_State *L, int idx)
{
return olua_isnumber(L, idx);
}
template <class T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true> inline
void olua_check_number(lua_State *L, int idx, T *value)
{
*value = (T)olua_checknumber(L, idx);
}
template <class T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true> inline
int olua_push_number(lua_State *L, T value)
{
olua_pushnumber(L, (lua_Number)value);
return 1;
}
// string
static inline
bool olua_is_string(lua_State *L, int idx)
{
return olua_isstring(L, idx);
}
template <class T> inline
void olua_check_string(lua_State *L, int idx, T *value)
{
*value = (T)olua_checkstring(L, idx);
}
template <class T> inline
int olua_push_string(lua_State *L, const T &value)
{
olua_pushstring(L, (const char *)value);
return 1;
}
template <> inline
void olua_check_string<std::string>(lua_State *L, int idx, std::string *value)
{
size_t len;
const char *str = olua_checklstring(L, idx, &len);
value->assign(str, len);
}
template <> inline
int olua_push_string<std::string>(lua_State *L, const std::string &value)
{
lua_pushlstring(L, value.data(), value.size());
return 1;
}
#if __cplusplus >= 201703L
#include <string_view>
template <> inline
void olua_check_string<std::string_view>(lua_State *L, int idx, std::string_view *value)
{
size_t len;
const char *str = olua_checklstring(L, idx, &len);
*value = std::string_view(str, len);
}
template <> inline
int olua_push_string<std::string_view>(lua_State *L, const std::string_view &value)
{
lua_pushlstring(L, value.data(), value.size());
return 1;
}
#endif
// enum
static inline
bool olua_is_enum(lua_State *L, int idx)
{
return olua_isinteger(L, idx);
}
template <class T, std::enable_if_t<std::is_enum<T>::value, bool> = true> inline
void olua_check_enum(lua_State *L, int idx, T *value)
{
*value = (T)olua_checkinteger(L, idx);
}
template <class T, std::enable_if_t<std::is_enum<T>::value, bool> = true> inline
int olua_push_enum(lua_State *L, T value)
{
olua_pushinteger(L, (lua_Integer)value);
return 1;
}
// object
static inline
bool olua_is_object(lua_State *L, int idx, const char *cls)
{
return olua_isa(L, idx, cls);
}
template <class T> inline
void olua_check_object(lua_State *L, int idx, T **value, const char *cls)
{
*value = (T *)olua_checkobj(L, idx, cls);
}
template <class T> inline
void olua_to_object(lua_State *L, int idx, T **value, const char *cls)
{
*value = (T *)olua_toobj(L, idx, cls);
}
template <class T> inline
void olua_check_object(lua_State *L, int idx, T *value, const char *cls)
{
*value = *(T *)olua_checkobj(L, idx, cls);
}
template <class T> inline
int olua_push_object(lua_State *L, const T *value, const char *cls)
{
return olua_pushobj<T>(L, value, cls);
}
template <class T, std::enable_if_t<!std::is_pointer<T>::value, bool> = true> inline
int olua_push_object(lua_State *L, const T &value, const char *cls)
{
return olua_pushobj<T>(L, &value, cls);
}
template <class T, std::enable_if_t<
!std::is_pointer<T>::value &&
std::is_copy_constructible<T>::value, bool> = true> inline
int olua_copy_object(lua_State *L, T &value, const char *cls)
{
using Type = typename std::remove_const<T>::type;
void *ptr = olua_newrawobj(L, nullptr, sizeof(T));
Type *obj = new (ptr) Type(value);
olua_emplaceobj(L, -1, obj, olua_getluatype<T>(L, nullptr, cls));
olua_setobjflag(L, -1, OLUA_FLAG_IN_USERDATA);
return 1;
}
template <class T, std::enable_if_t<
!std::is_pointer<T>::value &&
!std::is_copy_constructible<T>::value &&
std::is_copy_assignable<T>::value, bool> = true> inline
int olua_copy_object(lua_State *L, T &value, const char *cls)
{
using Type = typename std::remove_const<T>::type;
void *ptr = olua_newrawobj(L, nullptr, sizeof(T));
Type *obj = new (ptr) Type();
*obj = value;
olua_emplaceobj(L, -1, obj, olua_getluatype<T>(L, nullptr, cls));
olua_setobjflag(L, -1, OLUA_FLAG_IN_USERDATA);
return 1;
}
// std::shared_ptr & std::weak_ptr
#define OLUA_SMART_PRT ".olua.ref.smartptr<>"
template <template<class> class SmartPtr, class T>
int olua_smartptr_gc(lua_State *L)
{
SmartPtr<T> *obj = olua_checkobj<SmartPtr<T>>(L, 1);
if (olua_hasobjflag(L, -1, OLUA_FLAG_IN_USERDATA)) {
obj->~SmartPtr<T>();
} else {
delete obj;
}
olua_setrawobj(L, 1, nullptr);
return 0;
}
template <template<class> class SmartPtr, class T>
int olua_smartptr_name(lua_State *L)
{
olua_pushobj<T>(L, olua_toobj<SmartPtr<T>>(L, 1)->get());
olua_getfield(L, -1, "name");
return 1;
}
template <template<class> class SmartPtr, class T>
int olua_smartptr_get(lua_State *L)
{
olua_pushobj<T>(L, olua_toobj<SmartPtr<T>>(L, 1)->get());
return 1;
}
template <template<class> class SmartPtr, class T>
void oluacls_class_smartptr(lua_State *L)
{
const char *smartptr_cls = olua_getluatype<SmartPtr<T>>(L);
if (!smartptr_cls) {
smartptr_cls = typeid(SmartPtr<T>).name();
oluacls_class<SmartPtr<T>>(L, smartptr_cls);
oluacls_func(L, "__gc", olua_smartptr_gc<SmartPtr, T>);
oluacls_func(L, "get", olua_smartptr_get<SmartPtr, T>);
oluacls_prop(L, "name", olua_smartptr_name<SmartPtr, T>, nullptr);
lua_pop(L, 1);
}
}
static inline
bool olua_is_smartptr(lua_State *L, int idx, const char *cls)
{
if (olua_isa(L, idx, cls)) {
int type = olua_loadref(L, idx, OLUA_SMART_PRT);
lua_pop(L, 1);
return type == LUA_TUSERDATA;
}
return false;
}
template <class T>
void olua_check_smartptr(lua_State *L, int idx, std::shared_ptr<T> *value, const char *)
{
idx = lua_absindex(L, idx);
olua_checkobj<T>(L, idx);
olua_loadref(L, idx, OLUA_SMART_PRT);
*value = *olua_checkobj<std::shared_ptr<T>>(L, -1);
lua_pop(L, 1);
}
template <class T>
int olua_push_smartptr(lua_State *L, const std::shared_ptr<T> *value, const char *cls)
{
oluacls_class_smartptr<std::shared_ptr, T>(L);
if (!value->get()) {
lua_pushnil(L);
return 1;
}
olua_pushobj<T>(L, value->get(), cls);
if (olua_loadref(L, -1, OLUA_SMART_PRT)) {
lua_pop(L, 1);
return 1;
}
olua_setobjflag(L, -2, OLUA_FLAG_SKIP_GC); // skip gc, managed by smart ptr
olua_setobjflag(L, -2, OLUA_FLAG_IN_SMARTPRT);
void *ptr = olua_newrawobj(L, nullptr, sizeof(*value));
std::shared_ptr<T> *obj = new (ptr) std::shared_ptr<T>(*value);
olua_emplaceobj(L, -1, obj, olua_getluatype<std::shared_ptr<T>>(L));
olua_setobjflag(L, -1, OLUA_FLAG_IN_USERDATA);
olua_addref(L, -3, OLUA_SMART_PRT, -1, OLUA_REF_ALONE);
lua_pop(L, 2); // pop nil and smartptr
return 1;
}
template <class T>
void olua_check_smartptr(lua_State *L, int idx, std::weak_ptr<T> *value, const char *)
{
idx = lua_absindex(L, idx);
olua_loadref(L, idx, OLUA_SMART_PRT);
*value = *olua_checkobj<std::shared_ptr<T>>(L, -1);
lua_pop(L, 1);
}
template <class T>
int olua_push_smartptr(lua_State *L, const std::weak_ptr<T> *value, const char *cls)
{
std::shared_ptr<T> sp = value->lock();
return olua_push_object(L, &sp, cls);
}
// map
static inline
bool olua_is_map(lua_State *L, int idx)
{
return olua_istable(L, idx);
}
template <class K, class V, template<class ...> class Map, class ...Ts>
void olua_insert_map(Map<K, V, Ts...> &map, const K &key, const V &value)
{
map.insert(std::make_pair(key, value));
}
template <class K, class V, template<class ...> class Map, class ...Ts>
void olua_foreach_map(const Map<K, V, Ts...> &map, const std::function<void(K &, V &)> &callback)
{
for (auto itor : map) {
callback(const_cast<K &>(itor.first), itor.second);
}
}
template <class K, class V, template<class ...> class Map, class ...Ts>
int olua_push_map(lua_State *L, const Map<K, V, Ts...> &map, const std::function<void(K &, V &)> &push)
{
lua_newtable(L);
olua_foreach_map<K, V>(map, [=](K &key, V &value) {
push(key, value);
olua_rawset(L, -3);
});
return 1;
}
template <class K, class V, template<class ...> class Map, class ...Ts>
void olua_check_map(lua_State *L, int idx, Map<K, V, Ts...> &map, const std::function<void(K *, V *)> &check)
{
idx = lua_absindex(L, idx);
luaL_checktype(L, idx, LUA_TTABLE);
lua_pushnil(L);
while (lua_next(L, idx)) {
K key;
V value;
check(&key, &value);
olua_insert_map<K, V>(map, key, value);
lua_pop(L, 1);
}
}
// array
template <class T, template<class ...> class Array, class ...Ts>
void olua_insert_array(Array<T, Ts...> &array, const T &value)
{
array.push_back(value);
}
template <class T>
void olua_insert_array(std::set<T> &array, const T &value)
{
array.insert(value);
}
template <class T, template<class ...> class Array, class ...Ts>
void olua_foreach_array(const Array<T, Ts...> &array, const std::function<void(T &)> &callback)
{
for (auto &itor : array) {
callback(const_cast<T &>(itor));
}
}
template <> inline
void olua_foreach_array<bool>(const std::vector<bool> &array, const std::function<void(bool &)> &callback)
{
for (auto itor : array) {
bool v = itor;
callback(v);
}
}
static inline
bool olua_is_array(lua_State *L, int idx)
{
return olua_istable(L, idx);
}
template <class T, template<class ...> class Array, class ...Ts>
int olua_push_array(lua_State *L, const Array<T, Ts...> &array, const std::function<void(T &)> &push)
{
int idx = 0;
lua_newtable(L);
olua_foreach_array<T>(array, [=](T &value) mutable {
push(value);
lua_rawseti(L, -2, ++idx);
});
return 1;
}
template <class T, template<class ...> class Array, class ...Ts>
void olua_check_array(lua_State *L, int idx, Array<T, Ts...> &array, const std::function<void(T *)> &check)
{
idx = lua_absindex(L, idx);
luaL_checktype(L, idx, LUA_TTABLE);
int total = (int)lua_rawlen(L, idx);
for (int i = 1; i <= total; i++) {
T obj;
lua_rawgeti(L, idx, i);
check(&obj);
olua_insert_array<T>(array, obj);
lua_pop(L, 1);
}
}
template <class T, template<class ...> class Array, class ...Ts>
void olua_pack_array(lua_State *L, int idx, Array<T, Ts...> &array, const std::function<void(T *)> &check)
{
idx = lua_absindex(L, idx);
int total = (int)(lua_gettop(L) - (idx - 1));
for (int i = 0; i < total; i++) {
T obj;
lua_pushvalue(L, idx + i);
check(&obj);
olua_insert_array<T>(array, obj);
lua_pop(L, 1);
}
}
// callback
OLUA_BEGIN_DECLS
OLUA_API bool olua_is_callback(lua_State *L, int idx, const char *cls);
OLUA_API int olua_push_callback(lua_State *L, const char *cls);
OLUA_END_DECLS
template <class T>
int olua_push_callback(lua_State *L, const T *value, const char *cls)
{
return olua_push_callback(L, olua_getluatype(L, value, cls));
}
template <class T> inline
void olua_check_callback(lua_State *L, int idx, T *value, const char *cls)
{
luaL_checktype(L, idx, LUA_TFUNCTION);
}
// pinter type
#if defined(_MSC_VER) && !defined(_SSIZE_T)
#include <BaseTsd.h>
#define _SSIZE_T
typedef SSIZE_T ssize_t;
#endif
namespace olua {
template<class T>
class pointer {
public:
pointer(const pointer &) = delete;
pointer &operator=(const pointer &) = delete;
~pointer()
{
if (_owner) {
delete[] _data;
}
}
pointer(size_t len = 1)
:_len(len)
,_owner(true)
,_data(new T[len]())
{}
pointer(T *v, size_t len = 0)
:_len(len)
,_owner(false)
,_data(v)
{}
T __index(unsigned idx)
{
olua_assert(idx >= 1 && idx <= _len, "index out of range");
return _data[idx - 1];
}
void __newindex(unsigned idx, const T &v)
{
olua_assert(idx >= 1 && idx <= _len, "newindex out of range");
_data[idx - 1] = v;
}
olua_Return __gc(lua_State *L)
{
pointer<T> *self = (pointer<T> *)olua_toobj(L, 1, OLUA_VOIDCLS);
if (self) {
if (olua_hasobjflag(L, -1, OLUA_FLAG_IN_HEAP)) {
olua_setrawobj(L, 1, nullptr);
delete self;
} else if (olua_hasobjflag(L, -1, OLUA_FLAG_IN_USERDATA)) {
olua_setrawobj(L, 1, nullptr);
self->~pointer<T>();
}
}
return 0;
}
olua_Return tostring(lua_State *L, size_t len)
{
olua_assert(_len > 0 && len <= _len, "invalid length");
lua_pushlstring(L, (const char*)_data, len * sizeof(T));
return 1;
}
pointer<T> *take()
{
olua_assert(_owner, "take only allow when owner is true ");
_owner = false;
return this;
}
OLUA_POSTNEW pointer<T> *sub(size_t from, size_t to = -1)
{
if (to == -1) {
to = _len;
}
olua_assert(from <= _len && from <= to, "invalid 'from' position");
olua_assert(to <= _len, "invalid 'to' position");
pointer<T> *ret = new pointer<T>(to - from + 1);
for (size_t i = 0; i < ret->_len; i++) {
ret->_data[i] = _data[from + i - 1];
}
return ret;
}
OLUA_POSTNEW pointer<T> *slice(size_t from, size_t to = -1)
{
if (to == -1) {
to = _len;
}
olua_assert(from <= _len && from <= to, "invalid 'from' position");
olua_assert(to <= _len, "invalid 'to' position");
pointer<T> *ret = new pointer<T>(&_data[from - 1]);
ret->_len = to - from + 1;
ret->_owner = false;
return ret;
}
OLUA_GETTER OLUA_TYPE(void *) T *buffer() {return _data;}
OLUA_GETTER const T &value() {return *_data;}
OLUA_SETTER void value(const T &v) {*_data = v;}
OLUA_GETTER size_t length() {return _len;}
private:
T *_data = nullptr;
size_t _len = 0;
bool _owner = true;
};
}
typedef char olua_char_t;
typedef short olua_short_t;
typedef int olua_int_t;
typedef long olua_long_t;
typedef long long olua_llong_t;
typedef unsigned char olua_uchar_t;
typedef unsigned short olua_ushort_t;