Geogram Version 1.9.7
A programming library of geometric algorithms
Loading...
Searching...
No Matches
lua_wrap.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40#ifndef GEOGRAM_LUA_LUA_WRAP
41
43#include <geogram/lua/lua.h>
48
49
50#ifdef GEO_COMPILER_MSVC
51#pragma warning( push )
52#pragma warning( disable: 4702 )
53#endif
54
60namespace GEO {
61
66 typedef int (*lua_test_func)(lua_State* L, int idx);
67
68
80 inline int my_lua_isboolean(lua_State* L, int idx) {
81 return lua_isboolean(L,idx);
82 }
83
95 inline int my_lua_islightuserdata(lua_State* L, int idx) {
96 return lua_islightuserdata(L,idx);
97 }
98
108 inline int my_lua_ispositiveinteger(lua_State* L, int idx) {
109 if(!lua_isinteger(L,idx)) {
110 return 0;
111 }
112 lua_Integer x = lua_tointeger(L,idx);
113 return (x > 0) ? 1 : 0;
114 }
115
124 inline void lua_set_error(lua_State* L, const char* error) {
125 lua_pushstring(L, error);
126 lua_setfield(L,LUA_REGISTRYINDEX,"last_geogram_error");
127 }
128
137 inline void lua_set_error(lua_State* L, const std::string& error) {
138 lua_set_error(L,error.c_str());
139 }
140
145 inline void lua_clear_error(lua_State* L) {
146 lua_pushnil(L);
147 lua_setfield(L,LUA_REGISTRYINDEX,"last_geogram_error");
148 }
149
155 inline bool lua_has_error(lua_State* L) {
156 lua_getfield(L,LUA_REGISTRYINDEX,"last_geogram_error");
157 bool result = !lua_isnil(L,-1);
158 lua_pop(L,1);
159 return result;
160 }
161
174 inline bool lua_check_type(lua_State* L, int idx, lua_test_func test) {
175 if(!test(L,idx)) {
176 std::string error = std::string("Argument ") +
177 String::to_string(idx) + ": wrong argument type (" +
178 "got " + lua_typename(L,lua_type(L,idx)) + ")";
179 lua_set_error(L, error.c_str());
180 return false;
181 }
182 return true;
183 }
184
197 inline bool lua_check_nb_args(lua_State* L, int expected_nb_args) {
198 if(lua_gettop(L) != expected_nb_args) {
199 std::string error =
200 "Expected " + String::to_string(expected_nb_args)
201 + " arg(s), got " + String::to_string(lua_gettop(L));
202 lua_set_error(L,error.c_str());
203 return false;
204 }
205 return true;
206 }
207
221 inline int lua_notify_last_error(lua_State* L) {
222 std::string error;
223 lua_getfield(L,LUA_REGISTRYINDEX,"last_geogram_error");
224 if(lua_isstring(L,-1)) {
225 error += lua_tostring(L,-1);
226 }
227 lua_pop(L,1);
229 return luaL_error(L,error.c_str());
230 }
231
232 /**********************************************************************/
233
239 template <class T> class lua_to {
240 public:
253 lua_to(lua_State* L, int idx) {
254 geo_argused(L);
255 geo_argused(idx);
257 }
258
271 static bool can_convert(lua_State* L, int idx) {
272 geo_argused(L);
273 geo_argused(idx);
275 }
276
281 operator T() const {
283 }
284 protected:
285 T x_;
286 };
287
291 template<> class lua_to<int> {
292 public:
293 lua_to(lua_State* L, int idx) {
294 x_ = int(lua_tointeger(L,idx));
295 }
296 static bool can_convert(lua_State* L, int idx) {
297 return lua_check_type(L, idx, lua_isinteger);
298 }
299 operator int() const {
300 return x_;
301 }
302 private:
303 int x_;
304 };
305
309 template<> class lua_to<Numeric::uint32> {
310 public:
311 lua_to(lua_State* L, int idx) {
312 x_ = Numeric::uint32(lua_tointeger(L,idx));
313 }
314 static bool can_convert(lua_State* L, int idx) {
316 }
317 operator Numeric::uint32() const {
318 return x_;
319 }
320 private:
322 };
323
327 template<> class lua_to<Numeric::uint64> {
328 public:
329 lua_to(lua_State* L, int idx) {
330 x_ = Numeric::uint64(lua_tointeger(L,idx));
331 }
332 static bool can_convert(lua_State* L, int idx) {
334 }
335 operator Numeric::uint64() const {
336 return x_;
337 }
338 private:
340 };
341
342
346 template<> class lua_to<Numeric::int64> {
347 public:
348 lua_to(lua_State* L, int idx) {
349 x_ = Numeric::int64(lua_tointeger(L,idx));
350 }
351 static bool can_convert(lua_State* L, int idx) {
352 return lua_check_type(L, idx, lua_isinteger);
353 }
354 operator Numeric::int64() const {
355 return x_;
356 }
357 private:
359 };
360
364 template<> class lua_to<float> {
365 public:
366 lua_to(lua_State* L, int idx) {
367 x_ = float(lua_tonumber(L,idx));
368 }
369 static bool can_convert(lua_State* L, int idx) {
370 return lua_check_type(L, idx, lua_isnumber);
371 }
372 operator float() const {
373 return x_;
374 }
375 private:
376 float x_;
377 };
378
382 template<> class lua_to<double> {
383 public:
384 lua_to(lua_State* L, int idx) {
385 x_ = double(lua_tonumber(L,idx));
386 }
387 static bool can_convert(lua_State* L, int idx) {
388 return lua_check_type(L, idx, lua_isnumber);
389 }
390 operator double() const {
391 return x_;
392 }
393 private:
394 double x_;
395 };
396
400 template<> class lua_to<bool> {
401 public:
402 lua_to(lua_State* L, int idx) {
403 x_ = (lua_toboolean(L,idx) != 0);
404 }
405 static bool can_convert(lua_State* L, int idx) {
406 return lua_check_type(L, idx, my_lua_isboolean);
407 }
408 operator bool() const {
409 return x_;
410 }
411 private:
412 bool x_;
413 };
414
418 template<> class lua_to<const char*> {
419 public:
420 lua_to(lua_State* L, int idx) {
421 x_ = lua_tostring(L,idx);
422 }
423 static bool can_convert(lua_State* L, int idx) {
424 return lua_check_type(L, idx, lua_isstring);
425 }
426 operator const char*() const {
427 return x_;
428 }
429 private:
430 const char* x_;
431 };
432
436 template<> class lua_to<const std::string&> {
437 public:
438 lua_to(lua_State* L, int idx) {
439 x_ = lua_tostring(L,idx);
440 }
441 static bool can_convert(lua_State* L, int idx) {
442 return lua_check_type(L, idx, lua_isstring);
443 }
444 operator const std::string&() const {
445 return x_;
446 }
447 private:
448 std::string x_;
449 };
450
454 template<> class lua_to<std::string> {
455 public:
456 lua_to(lua_State* L, int idx) {
457 x_ = lua_tostring(L,idx);
458 }
459 static bool can_convert(lua_State* L, int idx) {
460 return lua_check_type(L, idx, lua_isstring);
461 }
462 operator std::string() const {
463 return x_;
464 }
465 private:
466 std::string x_;
467 };
468
469 /**********************************************************************/
470
481 template<class T> inline void lua_push(lua_State* L, T x) {
482 geo_argused(L);
483 geo_argused(x);
485 }
486
490 template<> inline void lua_push(lua_State* L, int x) {
491 lua_pushinteger(L,lua_Integer(x));
492 }
493
494
498 template<> inline void lua_push(lua_State* L, Numeric::uint32 x) {
499 lua_pushinteger(L,lua_Integer(x));
500 }
501
505 template<> inline void lua_push(lua_State* L, Numeric::uint64 x) {
506 lua_pushinteger(L,lua_Integer(x));
507 }
508
512 template<> inline void lua_push(lua_State* L, Numeric::int64 x) {
513 lua_pushinteger(L,lua_Integer(x));
514 }
515
519 template<> inline void lua_push(lua_State* L, float x) {
520 lua_pushnumber(L,lua_Number(x));
521 }
522
526 template<> inline void lua_push(lua_State* L, double x) {
527 lua_pushnumber(L,lua_Number(x));
528 }
529
533 template<> inline void lua_push(lua_State* L, bool x) {
534 lua_pushboolean(L,x?1:0);
535 }
536
540 template<> inline void lua_push(lua_State* L, const char* x) {
541 lua_pushstring(L,x);
542 }
543
547 template<> inline void lua_push(lua_State* L, const std::string& x) {
548 lua_pushstring(L,x.c_str());
549 }
550
554 template<> inline void lua_push(lua_State* L, std::string x) {
555 lua_pushstring(L,x.c_str());
556 }
557
563 template<class T> inline void lua_push(
564 lua_State* L, const std::vector<T>& x
565 ) {
566 lua_newtable(L);
567 for(size_t i=0; i<x.size(); ++i) {
568 lua_push(L,x[i]);
569 lua_seti(L,-2,lua_Integer(i+1));
570 }
571 }
572
583#define LUA_DECLAREENUMTYPE(T) \
584 template<> inline void lua_push(lua_State* L, T x) { \
585 lua_push(L, int(x)); \
586 } \
587 \
588 template<> class lua_to<T> : public GEO::lua_to<int> { \
589 public: \
590 lua_to(lua_State* L, int idx) : lua_to<int>(L,idx) { \
591 } \
592 operator T() const { \
593 return T(lua_to<int>::operator int()); \
594 } \
595 }
596
597 /**********************************************************************/
598
606 template <class R> inline int lua_wrap(lua_State* L, R (*fptr)(void)) {
607 if(!lua_check_nb_args(L,0)) {
608 return lua_notify_last_error(L);
609 }
610 R retval = fptr();
611 lua_push(L,retval);
612 return 1;
613 }
614
622 template <class R, class T1> inline int lua_wrap(
623 lua_State* L, R (*fptr)(T1)
624 ) {
625 if(
626 !lua_check_nb_args(L,1) ||
628 ) {
629 return lua_notify_last_error(L);
630 }
631 R retval = fptr(
632 lua_to<T1>(L,1)
633 );
634 lua_push(L,retval);
635 return 1;
636 }
637
645 template <class R, class T1, class T2> inline int lua_wrap(
646 lua_State* L, R (*fptr)(T1,T2)
647 ) {
648 if(
649 !lua_check_nb_args(L,2) ||
652 ) {
653 return lua_notify_last_error(L);
654 }
655 R retval = fptr(
656 lua_to<T1>(L,1),
657 lua_to<T2>(L,2)
658 );
659 lua_push(L,retval);
660 return 1;
661 }
662
670 template <class R, class T1, class T2, class T3> inline int lua_wrap(
671 lua_State* L, R (*fptr)(T1,T2,T3)
672 ) {
673 if(
674 !lua_check_nb_args(L,3) ||
678 ) {
679 return lua_notify_last_error(L);
680 }
681 R retval = fptr(
682 lua_to<T1>(L,1),
683 lua_to<T2>(L,2),
684 lua_to<T3>(L,3)
685 );
686 lua_push(L,retval);
687 return 1;
688 }
689
697 template <class R, class T1, class T2, class T3, class T4>
698 inline int lua_wrap(lua_State* L, R (*fptr)(T1,T2,T3,T4)) {
699 if(
700 !lua_check_nb_args(L,4) ||
705 ) {
706 return lua_notify_last_error(L);
707 }
708 R retval = fptr(
709 lua_to<T1>(L,1),
710 lua_to<T2>(L,2),
711 lua_to<T3>(L,3),
712 lua_to<T4>(L,4)
713 );
714 lua_push(L,retval);
715 return 1;
716 }
717
718 /*************************************************************************/
719
726 template <> inline int lua_wrap(lua_State* L, void (*fptr)(void)) {
727 if(!lua_check_nb_args(L,0)) {
728 return lua_notify_last_error(L);
729 }
730 fptr();
731 return 0;
732 }
733
740 template <class T1> inline int lua_wrap(lua_State* L, void (*fptr)(T1)) {
741 if(
742 !lua_check_nb_args(L,1) ||
744 ) {
745 return lua_notify_last_error(L);
746 }
747 fptr(
748 lua_to<T1>(L,1)
749 );
750 return 0;
751 }
752
759 template <class T1, class T2> inline int lua_wrap(
760 lua_State* L, void (*fptr)(T1,T2)
761 ) {
762 if(
763 !lua_check_nb_args(L,2) ||
766 ) {
767 return lua_notify_last_error(L);
768 }
769 fptr(
770 lua_to<T1>(L,1),
771 lua_to<T2>(L,2)
772 );
773 return 0;
774 }
775
782 template <class T1, class T2, class T3>
783 inline int lua_wrap(lua_State* L, void (*fptr)(T1,T2,T3)) {
784 if(
785 !lua_check_nb_args(L,3) ||
789 ) {
790 return lua_notify_last_error(L);
791 }
792 fptr(
793 lua_to<T1>(L,1),
794 lua_to<T2>(L,2),
795 lua_to<T3>(L,3)
796 );
797 return 0;
798 }
799
806 template <class T1, class T2, class T3, class T4>
807 inline int lua_wrap(lua_State* L, void (*fptr)(T1,T2,T3,T4)) {
808 if(
809 !lua_check_nb_args(L,4) ||
814 ) {
815 return lua_notify_last_error(L);
816 }
817 fptr(
818 lua_to<T1>(L,1),
819 lua_to<T2>(L,2),
820 lua_to<T3>(L,3),
821 lua_to<T4>(L,4)
822 );
823 return 0;
824 }
825
832 template <
833 class T1, class T2, class T3, class T4, class T5
834 >
835 inline int lua_wrap(
836 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5)
837 ) {
838 if(
839 !lua_check_nb_args(L,5) ||
845 ) {
846 return lua_notify_last_error(L);
847 }
848 fptr(
849 lua_to<T1>(L,1),
850 lua_to<T2>(L,2),
851 lua_to<T3>(L,3),
852 lua_to<T4>(L,4),
853 lua_to<T5>(L,5)
854 );
855 return 0;
856 }
857
864 template <
865 class T1, class T2, class T3, class T4, class T5, class T6
866 >
867 inline int lua_wrap(
868 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6)
869 ) {
870 if(
871 !lua_check_nb_args(L,6) ||
878 ) {
879 return lua_notify_last_error(L);
880 }
881 fptr(
882 lua_to<T1>(L,1),
883 lua_to<T2>(L,2),
884 lua_to<T3>(L,3),
885 lua_to<T4>(L,4),
886 lua_to<T5>(L,5),
887 lua_to<T6>(L,6)
888 );
889 return 0;
890 }
891
898 template <
899 class T1, class T2, class T3, class T4, class T5,
900 class T6, class T7
901 >
902 inline int lua_wrap(
903 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7)
904 ) {
905 if(
906 !lua_check_nb_args(L,7) ||
914 ) {
915 return lua_notify_last_error(L);
916 }
917 fptr(
918 lua_to<T1>(L,1),
919 lua_to<T2>(L,2),
920 lua_to<T3>(L,3),
921 lua_to<T4>(L,4),
922 lua_to<T5>(L,5),
923 lua_to<T6>(L,6),
924 lua_to<T7>(L,7)
925 );
926 return 0;
927 }
928
929
936 template <
937 class T1, class T2, class T3, class T4, class T5,
938 class T6, class T7, class T8
939 >
940 inline int lua_wrap(
941 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8)
942 ) {
943 if(
944 !lua_check_nb_args(L,8) ||
953 ) {
954 return lua_notify_last_error(L);
955 }
956 fptr(
957 lua_to<T1>(L,1),
958 lua_to<T2>(L,2),
959 lua_to<T3>(L,3),
960 lua_to<T4>(L,4),
961 lua_to<T5>(L,5),
962 lua_to<T6>(L,6),
963 lua_to<T7>(L,7),
964 lua_to<T8>(L,8)
965 );
966 return 0;
967 }
968
969
976 template <
977 class T1, class T2, class T3, class T4, class T5,
978 class T6, class T7, class T8, class T9
979 >
980 inline int lua_wrap(
981 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9)
982 ) {
983 if(
984 !lua_check_nb_args(L,9) ||
994 ) {
995 return lua_notify_last_error(L);
996 }
997 fptr(
998 lua_to<T1>(L,1),
999 lua_to<T2>(L,2),
1000 lua_to<T3>(L,3),
1001 lua_to<T4>(L,4),
1002 lua_to<T5>(L,5),
1003 lua_to<T6>(L,6),
1004 lua_to<T7>(L,7),
1005 lua_to<T8>(L,8),
1006 lua_to<T9>(L,9)
1007 );
1008 return 0;
1009 }
1010
1011
1018 template <
1019 class T1, class T2, class T3, class T4, class T5,
1020 class T6, class T7, class T8, class T9, class T10
1021 >
1022 inline int lua_wrap(
1023 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10)
1024 ) {
1025 if(
1026 !lua_check_nb_args(L,10) ||
1037 ) {
1038 return lua_notify_last_error(L);
1039 }
1040 fptr(
1041 lua_to<T1>(L,1),
1042 lua_to<T2>(L,2),
1043 lua_to<T3>(L,3),
1044 lua_to<T4>(L,4),
1045 lua_to<T5>(L,5),
1046 lua_to<T6>(L,6),
1047 lua_to<T7>(L,7),
1048 lua_to<T8>(L,8),
1049 lua_to<T9>(L,9),
1050 lua_to<T10>(L,10)
1051 );
1052 return 0;
1053 }
1054
1055
1062 template <
1063 class T1, class T2, class T3, class T4, class T5,
1064 class T6, class T7, class T8, class T9, class T10,
1065 class T11
1066 >
1067 inline int lua_wrap(
1068 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11)
1069 ) {
1070 if(
1071 !lua_check_nb_args(L,11) ||
1081 !lua_to<T10>::can_convert(L,10) ||
1083 ) {
1084 return lua_notify_last_error(L);
1085 }
1086 fptr(
1087 lua_to<T1>(L,1),
1088 lua_to<T2>(L,2),
1089 lua_to<T3>(L,3),
1090 lua_to<T4>(L,4),
1091 lua_to<T5>(L,5),
1092 lua_to<T6>(L,6),
1093 lua_to<T7>(L,7),
1094 lua_to<T8>(L,8),
1095 lua_to<T9>(L,9),
1096 lua_to<T10>(L,10),
1097 lua_to<T11>(L,11)
1098 );
1099 return 0;
1100 }
1101
1108 template <
1109 class T1, class T2, class T3, class T4, class T5,
1110 class T6, class T7, class T8, class T9, class T10,
1111 class T11, class T12
1112 >
1113 inline int lua_wrap(
1114 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12)
1115 ) {
1116 if(
1117 !lua_check_nb_args(L,12) ||
1127 !lua_to<T10>::can_convert(L,10) ||
1128 !lua_to<T11>::can_convert(L,11) ||
1130 ) {
1131 return lua_notify_last_error(L);
1132 }
1133 fptr(
1134 lua_to<T1>(L,1),
1135 lua_to<T2>(L,2),
1136 lua_to<T3>(L,3),
1137 lua_to<T4>(L,4),
1138 lua_to<T5>(L,5),
1139 lua_to<T6>(L,6),
1140 lua_to<T7>(L,7),
1141 lua_to<T8>(L,8),
1142 lua_to<T9>(L,9),
1143 lua_to<T10>(L,10),
1144 lua_to<T11>(L,11),
1145 lua_to<T12>(L,12)
1146 );
1147 return 0;
1148 }
1149
1150
1157 template <
1158 class T1, class T2, class T3, class T4, class T5,
1159 class T6, class T7, class T8, class T9, class T10,
1160 class T11, class T12, class T13
1161 >
1162 inline int lua_wrap(
1163 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13)
1164 ) {
1165 if(
1166 !lua_check_nb_args(L,13) ||
1176 !lua_to<T10>::can_convert(L,10) ||
1177 !lua_to<T11>::can_convert(L,11) ||
1178 !lua_to<T12>::can_convert(L,12) ||
1180 ) {
1181 return lua_notify_last_error(L);
1182 }
1183 fptr(
1184 lua_to<T1>(L,1),
1185 lua_to<T2>(L,2),
1186 lua_to<T3>(L,3),
1187 lua_to<T4>(L,4),
1188 lua_to<T5>(L,5),
1189 lua_to<T6>(L,6),
1190 lua_to<T7>(L,7),
1191 lua_to<T8>(L,8),
1192 lua_to<T9>(L,9),
1193 lua_to<T10>(L,10),
1194 lua_to<T11>(L,11),
1195 lua_to<T12>(L,12),
1196 lua_to<T13>(L,13)
1197 );
1198 return 0;
1199 }
1200
1207 template<> inline int lua_wrap(lua_State* L, lua_CFunction fptr) {
1208 return fptr(L);
1209 }
1210
1211
1212 /*************************************************************************/
1213
1221 template <class FPTR> class lua_wrapper {
1222 public:
1223
1232 static int call(lua_State* L) {
1233 FPTR f = FPTR(
1235 lua_touserdata(L, lua_upvalueindex(1))
1236 )
1237 );
1238 return lua_wrap(L, f);
1239 }
1240
1247 static void push(lua_State* L, FPTR f) {
1248 lua_pushlightuserdata(
1249 L,
1252 )
1253 );
1254 lua_pushcclosure(L, lua_wrapper<FPTR>::call, 1);
1255 }
1256 };
1257
1258 /**************************************************************************/
1259
1266 template <class FPTR> inline void lua_pushwrapper(lua_State* L, FPTR f) {
1268 }
1269
1274 template<> inline void lua_pushwrapper(lua_State* L, lua_CFunction f) {
1275 lua_pushcfunction(L,f);
1276 }
1277
1278 /**************************************************************************/
1279
1288 template <class FPTR> inline void lua_bindwrapperwithname(
1289 lua_State* L, FPTR f, const std::string& name
1290 ) {
1291 geo_assert(lua_gettop(L) > 0);
1292 geo_assert(lua_istable(L,-1));
1293 lua_pushstring(L,name.c_str());
1294 lua_pushwrapper(L,f);
1295 lua_settable(L,-3);
1296 }
1297
1298
1305 template <class FPTR> inline void lua_bindwrapperwithnameglobal(
1306 lua_State* L, FPTR f, const std::string& name
1307 ) {
1308 lua_pushwrapper(L,f);
1309 lua_setglobal(L,name.c_str());
1310 }
1311
1312 /**************************************************************************/
1313
1321 inline std::string lua_wrappername(lua_State* L, const char* functionname) {
1322 geo_argused(L);
1323 std::string result(functionname);
1324 size_t pos = result.find_last_of(":");
1325 if(pos != std::string::npos) {
1326 result = result.substr(pos+1, result.length()-pos);
1327 }
1328 return result;
1329 }
1330
1331 /**************************************************************************/
1332
1348#define lua_bindwrapper(L, f) lua_bindwrapperwithname( \
1349 (L),(f),GEO::lua_wrappername(L,#f) \
1350 )
1351
1366#define lua_bindwrapperglobal(L, f) lua_bindwrapperwithnameglobal( \
1367 (L),(f),GEO::lua_wrappername(L,#f) \
1368 )
1369
1370
1371 /*************************************************************************/
1372
1373}
1374
1375#ifdef GEO_COMPILER_MSVC
1376#pragma warning( pop )
1377#endif
1378
1379#endif
Assertion checking mechanism.
#define geo_assert_not_reached
Sets a non reachable point in the program.
Definition assert.h:177
#define geo_assert(x)
Verifies that a condition is met.
Definition assert.h:149
Converts LUA variables to C++ variables.
Definition lua_wrap.h:239
static bool can_convert(lua_State *L, int idx)
Tests whether a LUA variable can be converted to a C++ variable.
Definition lua_wrap.h:271
lua_to(lua_State *L, int idx)
lua_to constructor.
Definition lua_wrap.h:253
Manages wrappers around C++ functions to be called from LUA.
Definition lua_wrap.h:1221
static int call(lua_State *L)
Implementation of the wrapper.
Definition lua_wrap.h:1232
static void push(lua_State *L, FPTR f)
Pushes a wrapper for a given C++ function onto the LUA stack.
Definition lua_wrap.h:1247
Common include file, providing basic definitions. Should be included before anything else by all head...
Types and functions for memory manipulation.
void(* function_pointer)()
Generic function pointer.
Definition memory.h:107
pointer function_pointer_to_generic_pointer(function_pointer fptr)
Converts a function pointer to a generic pointer.
Definition memory.h:142
function_pointer generic_pointer_to_function_pointer(pointer ptr)
Converts a generic pointer to a function pointer.
Definition memory.h:161
uint64_t uint64
Definition numeric.h:144
uint32_t uint32
Definition numeric.h:141
int64_t int64
Definition numeric.h:132
Global Vorpaline namespace.
Definition basic.h:55
std::string lua_wrappername(lua_State *L, const char *functionname)
Converts a C++ function name into a LUA function name.
Definition lua_wrap.h:1321
int my_lua_isboolean(lua_State *L, int idx)
Tests whether a LUA variable is a boolean.
Definition lua_wrap.h:80
void lua_bindwrapperwithnameglobal(lua_State *L, FPTR f, const std::string &name)
Binds a wrapper to a name in the global scole.
Definition lua_wrap.h:1305
bool lua_check_type(lua_State *L, int idx, lua_test_func test)
Tests whether a LUA variable has the correct type.
Definition lua_wrap.h:174
void lua_set_error(lua_State *L, const char *error)
Memorizes an error message in LUA registry.
Definition lua_wrap.h:124
void lua_bindwrapperwithname(lua_State *L, FPTR f, const std::string &name)
Binds a wrapper to a name in the table at the top of the LUA stack.
Definition lua_wrap.h:1288
void lua_clear_error(lua_State *L)
Clears the last error message in LUA registry.
Definition lua_wrap.h:145
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
Definition argused.h:60
int my_lua_islightuserdata(lua_State *L, int idx)
Tests whether a LUA variable is a light user data.
Definition lua_wrap.h:95
void lua_push(lua_State *L, T x)
Converts and pushes a C++ variable onto the LUA stack.
Definition lua_wrap.h:481
bool lua_check_nb_args(lua_State *L, int expected_nb_args)
Tests whether the expected number of arguments was pushed onto the stack.
Definition lua_wrap.h:197
int my_lua_ispositiveinteger(lua_State *L, int idx)
Tests whether a LUA variable is a positive integer.
Definition lua_wrap.h:108
bool lua_has_error(lua_State *L)
Tests whether an error message was memorized in the registry.
Definition lua_wrap.h:155
int lua_notify_last_error(lua_State *L)
Takes the last error message memorized in the registry and sends it back to LUA.
Definition lua_wrap.h:221
int(* lua_test_func)(lua_State *L, int idx)
A pointer to a LUA function to test an argument in the LUA stack.
Definition lua_wrap.h:66
void lua_pushwrapper(lua_State *L, FPTR f)
Pushes a wrapper for a given C++ function onto the LUA stack.
Definition lua_wrap.h:1266
int lua_wrap(lua_State *L, R(*fptr)(void))
Calls a C++ function from LUA.
Definition lua_wrap.h:606
Types and functions for numbers manipulation.
Functions for string manipulation.