Graphite Version 3
An experimental 3D geometry processing program
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>
49
50
51#ifdef GEO_COMPILER_MSVC
52#pragma warning( push )
53#pragma warning( disable: 4702 )
54#endif
55
61namespace GEO {
62
67 typedef int (*lua_test_func)(lua_State* L, int idx);
68
69
81 inline int my_lua_isboolean(lua_State* L, int idx) {
82 return lua_isboolean(L,idx);
83 }
84
96 inline int my_lua_islightuserdata(lua_State* L, int idx) {
97 return lua_islightuserdata(L,idx);
98 }
99
109 inline int my_lua_ispositiveinteger(lua_State* L, int idx) {
110 if(!lua_isinteger(L,idx)) {
111 return 0;
112 }
113 lua_Integer x = lua_tointeger(L,idx);
114 return (x > 0) ? 1 : 0;
115 }
116
125 inline void lua_set_error(lua_State* L, const char* error) {
126 lua_pushstring(L, error);
127 lua_setfield(L,LUA_REGISTRYINDEX,"last_geogram_error");
128 }
129
138 inline void lua_set_error(lua_State* L, const std::string& error) {
139 lua_set_error(L,error.c_str());
140 }
141
146 inline void lua_clear_error(lua_State* L) {
147 lua_pushnil(L);
148 lua_setfield(L,LUA_REGISTRYINDEX,"last_geogram_error");
149 }
150
156 inline bool lua_has_error(lua_State* L) {
157 lua_getfield(L,LUA_REGISTRYINDEX,"last_geogram_error");
158 bool result = !lua_isnil(L,-1);
159 lua_pop(L,1);
160 return result;
161 }
162
175 inline bool lua_check_type(lua_State* L, int idx, lua_test_func test) {
176 if(!test(L,idx)) {
177 std::string error = std::string("Argument ") +
178 String::to_string(idx) + ": wrong argument type (" +
179 "got " + lua_typename(L,lua_type(L,idx)) + ")";
180 lua_set_error(L, error.c_str());
181 return false;
182 }
183 return true;
184 }
185
198 inline bool lua_check_nb_args(lua_State* L, int expected_nb_args) {
199 if(lua_gettop(L) != expected_nb_args) {
200 std::string error =
201 "Expected " + String::to_string(expected_nb_args)
202 + " arg(s), got " + String::to_string(lua_gettop(L));
203 lua_set_error(L,error.c_str());
204 return false;
205 }
206 return true;
207 }
208
222 inline int lua_notify_last_error(lua_State* L) {
223 std::string error;
224 lua_getfield(L,LUA_REGISTRYINDEX,"last_geogram_error");
225 if(lua_isstring(L,-1)) {
226 error += lua_tostring(L,-1);
227 }
228 lua_pop(L,1);
230 return luaL_error(L,error.c_str());
231 }
232
233 /**********************************************************************/
234
240 template <class T> class lua_to {
241 public:
254 lua_to(lua_State* L, int idx) {
255 geo_argused(L);
256 geo_argused(idx);
258 }
259
272 static bool can_convert(lua_State* L, int idx) {
273 geo_argused(L);
274 geo_argused(idx);
276 }
277
282 operator T() const {
284 }
285 protected:
286 T x_;
287 };
288
292 template<> class lua_to<int> {
293 public:
294 lua_to(lua_State* L, int idx) {
295 x_ = int(lua_tointeger(L,idx));
296 }
297 static bool can_convert(lua_State* L, int idx) {
298 return lua_check_type(L, idx, lua_isinteger);
299 }
300 operator int() const {
301 return x_;
302 }
303 private:
304 int x_;
305 };
306
310 template<> class lua_to<Numeric::uint32> {
311 public:
312 lua_to(lua_State* L, int idx) {
313 x_ = Numeric::uint32(lua_tointeger(L,idx));
314 }
315 static bool can_convert(lua_State* L, int idx) {
317 }
318 operator Numeric::uint32() const {
319 return x_;
320 }
321 private:
323 };
324
328 template<> class lua_to<Numeric::uint64> {
329 public:
330 lua_to(lua_State* L, int idx) {
331 x_ = Numeric::uint64(lua_tointeger(L,idx));
332 }
333 static bool can_convert(lua_State* L, int idx) {
335 }
336 operator Numeric::uint64() const {
337 return x_;
338 }
339 private:
341 };
342
343
347 template<> class lua_to<Numeric::int64> {
348 public:
349 lua_to(lua_State* L, int idx) {
350 x_ = Numeric::int64(lua_tointeger(L,idx));
351 }
352 static bool can_convert(lua_State* L, int idx) {
353 return lua_check_type(L, idx, lua_isinteger);
354 }
355 operator Numeric::int64() const {
356 return x_;
357 }
358 private:
360 };
361
365 template<> class lua_to<float> {
366 public:
367 lua_to(lua_State* L, int idx) {
368 x_ = float(lua_tonumber(L,idx));
369 }
370 static bool can_convert(lua_State* L, int idx) {
371 return lua_check_type(L, idx, lua_isnumber);
372 }
373 operator float() const {
374 return x_;
375 }
376 private:
377 float x_;
378 };
379
383 template<> class lua_to<double> {
384 public:
385 lua_to(lua_State* L, int idx) {
386 x_ = double(lua_tonumber(L,idx));
387 }
388 static bool can_convert(lua_State* L, int idx) {
389 return lua_check_type(L, idx, lua_isnumber);
390 }
391 operator double() const {
392 return x_;
393 }
394 private:
395 double x_;
396 };
397
401 template<> class lua_to<bool> {
402 public:
403 lua_to(lua_State* L, int idx) {
404 x_ = (lua_toboolean(L,idx) != 0);
405 }
406 static bool can_convert(lua_State* L, int idx) {
407 return lua_check_type(L, idx, my_lua_isboolean);
408 }
409 operator bool() const {
410 return x_;
411 }
412 private:
413 bool x_;
414 };
415
419 template<> class lua_to<const char*> {
420 public:
421 lua_to(lua_State* L, int idx) {
422 x_ = lua_tostring(L,idx);
423 }
424 static bool can_convert(lua_State* L, int idx) {
425 return lua_check_type(L, idx, lua_isstring);
426 }
427 operator const char*() const {
428 return x_;
429 }
430 private:
431 const char* x_;
432 };
433
437 template<> class lua_to<const std::string&> {
438 public:
439 lua_to(lua_State* L, int idx) {
440 x_ = lua_tostring(L,idx);
441 }
442 static bool can_convert(lua_State* L, int idx) {
443 return lua_check_type(L, idx, lua_isstring);
444 }
445 operator const std::string&() const {
446 return x_;
447 }
448 private:
449 std::string x_;
450 };
451
455 template<> class lua_to<std::string> {
456 public:
457 lua_to(lua_State* L, int idx) {
458 x_ = lua_tostring(L,idx);
459 }
460 static bool can_convert(lua_State* L, int idx) {
461 return lua_check_type(L, idx, lua_isstring);
462 }
463 operator std::string() const {
464 return x_;
465 }
466 private:
467 std::string x_;
468 };
469
470
474 template<class T, unsigned int N> class lua_to< const vecng<N,T>& > {
475 public:
476 lua_to(lua_State* L, int idx) {
477 lua_tovec(L, idx, x_);
478 }
479 static bool can_convert(lua_State* L, int idx) {
480 vecng<N,T> x;
481 return lua_tovec(L, idx, x);
482 }
483 operator vecng<N,T>() const {
484 return x_;
485 }
486 private:
487 vecng<N,T> x_;
488 };
489
493 template<class T, unsigned int N> class lua_to< const Matrix<N,T>& > {
494 public:
495 lua_to(lua_State* L, int idx) {
496 lua_tomat(L, idx, x_);
497 }
498 static bool can_convert(lua_State* L, int idx) {
499 Matrix<N,T> x;
500 return lua_tomat(L, idx, x);
501 }
502 operator Matrix<N,T>() const {
503 return x_;
504 }
505 private:
506 Matrix<N,T> x_;
507 };
508
509 /**********************************************************************/
510
521 template<class T> inline void lua_push(lua_State* L, T x) {
522 geo_argused(L);
523 geo_argused(x);
525 }
526
530 template<> inline void lua_push(lua_State* L, int x) {
531 lua_pushinteger(L,lua_Integer(x));
532 }
533
537 template<> inline void lua_push(lua_State* L, Numeric::uint32 x) {
538 lua_pushinteger(L,lua_Integer(x));
539 }
540
544 template<> inline void lua_push(lua_State* L, Numeric::uint64 x) {
545 lua_pushinteger(L,lua_Integer(x));
546 }
547
551 template<> inline void lua_push(lua_State* L, Numeric::int64 x) {
552 lua_pushinteger(L,lua_Integer(x));
553 }
554
558 template<> inline void lua_push(lua_State* L, float x) {
559 lua_pushnumber(L,lua_Number(x));
560 }
561
565 template<> inline void lua_push(lua_State* L, double x) {
566 lua_pushnumber(L,lua_Number(x));
567 }
568
572 template<> inline void lua_push(lua_State* L, bool x) {
573 lua_pushboolean(L,x?1:0);
574 }
575
579 template<> inline void lua_push(lua_State* L, const char* x) {
580 lua_pushstring(L,x);
581 }
582
586 template<> inline void lua_push(lua_State* L, const std::string& x) {
587 lua_pushstring(L,x.c_str());
588 }
589
593 template<> inline void lua_push(lua_State* L, std::string x) {
594 lua_pushstring(L,x.c_str());
595 }
596
602 template<class T> inline void lua_push(
603 lua_State* L, const std::vector<T>& x
604 ) {
605 lua_newtable(L);
606 for(size_t i=0; i<x.size(); ++i) {
607 lua_push(L,x[i]);
608 lua_seti(L,-2,lua_Integer(i+1));
609 }
610 }
611
622#define LUA_DECLAREENUMTYPE(T) \
623 template<> inline void lua_push(lua_State* L, T x) { \
624 lua_push(L, int(x)); \
625 } \
626 \
627 template<> class lua_to<T> : public GEO::lua_to<int> { \
628 public: \
629 lua_to(lua_State* L, int idx) : lua_to<int>(L,idx) { \
630 } \
631 operator T() const { \
632 return T(lua_to<int>::operator int()); \
633 } \
634 }
635
636 /**********************************************************************/
637
645 template <class R> inline int lua_wrap(lua_State* L, R (*fptr)(void)) {
646 if(!lua_check_nb_args(L,0)) {
647 return lua_notify_last_error(L);
648 }
649 R retval = fptr();
650 lua_push(L,retval);
651 return 1;
652 }
653
661 template <class R, class T1> inline int lua_wrap(
662 lua_State* L, R (*fptr)(T1)
663 ) {
664 if(
665 !lua_check_nb_args(L,1) ||
667 ) {
668 return lua_notify_last_error(L);
669 }
670 R retval = fptr(
671 lua_to<T1>(L,1)
672 );
673 lua_push(L,retval);
674 return 1;
675 }
676
684 template <class R, class T1, class T2> inline int lua_wrap(
685 lua_State* L, R (*fptr)(T1,T2)
686 ) {
687 if(
688 !lua_check_nb_args(L,2) ||
691 ) {
692 return lua_notify_last_error(L);
693 }
694 R retval = fptr(
695 lua_to<T1>(L,1),
696 lua_to<T2>(L,2)
697 );
698 lua_push(L,retval);
699 return 1;
700 }
701
709 template <class R, class T1, class T2, class T3> inline int lua_wrap(
710 lua_State* L, R (*fptr)(T1,T2,T3)
711 ) {
712 if(
713 !lua_check_nb_args(L,3) ||
717 ) {
718 return lua_notify_last_error(L);
719 }
720 R retval = fptr(
721 lua_to<T1>(L,1),
722 lua_to<T2>(L,2),
723 lua_to<T3>(L,3)
724 );
725 lua_push(L,retval);
726 return 1;
727 }
728
736 template <class R, class T1, class T2, class T3, class T4>
737 inline int lua_wrap(lua_State* L, R (*fptr)(T1,T2,T3,T4)) {
738 if(
739 !lua_check_nb_args(L,4) ||
744 ) {
745 return lua_notify_last_error(L);
746 }
747 R retval = fptr(
748 lua_to<T1>(L,1),
749 lua_to<T2>(L,2),
750 lua_to<T3>(L,3),
751 lua_to<T4>(L,4)
752 );
753 lua_push(L,retval);
754 return 1;
755 }
756
757 /*************************************************************************/
758
765 template <> inline int lua_wrap(lua_State* L, void (*fptr)(void)) {
766 if(!lua_check_nb_args(L,0)) {
767 return lua_notify_last_error(L);
768 }
769 fptr();
770 return 0;
771 }
772
779 template <class T1> inline int lua_wrap(lua_State* L, void (*fptr)(T1)) {
780 if(
781 !lua_check_nb_args(L,1) ||
783 ) {
784 return lua_notify_last_error(L);
785 }
786 fptr(
787 lua_to<T1>(L,1)
788 );
789 return 0;
790 }
791
798 template <class T1, class T2> inline int lua_wrap(
799 lua_State* L, void (*fptr)(T1,T2)
800 ) {
801 if(
802 !lua_check_nb_args(L,2) ||
805 ) {
806 return lua_notify_last_error(L);
807 }
808 fptr(
809 lua_to<T1>(L,1),
810 lua_to<T2>(L,2)
811 );
812 return 0;
813 }
814
821 template <class T1, class T2, class T3>
822 inline int lua_wrap(lua_State* L, void (*fptr)(T1,T2,T3)) {
823 if(
824 !lua_check_nb_args(L,3) ||
828 ) {
829 return lua_notify_last_error(L);
830 }
831 fptr(
832 lua_to<T1>(L,1),
833 lua_to<T2>(L,2),
834 lua_to<T3>(L,3)
835 );
836 return 0;
837 }
838
845 template <class T1, class T2, class T3, class T4>
846 inline int lua_wrap(lua_State* L, void (*fptr)(T1,T2,T3,T4)) {
847 if(
848 !lua_check_nb_args(L,4) ||
853 ) {
854 return lua_notify_last_error(L);
855 }
856 fptr(
857 lua_to<T1>(L,1),
858 lua_to<T2>(L,2),
859 lua_to<T3>(L,3),
860 lua_to<T4>(L,4)
861 );
862 return 0;
863 }
864
871 template <
872 class T1, class T2, class T3, class T4, class T5
873 >
874 inline int lua_wrap(
875 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5)
876 ) {
877 if(
878 !lua_check_nb_args(L,5) ||
884 ) {
885 return lua_notify_last_error(L);
886 }
887 fptr(
888 lua_to<T1>(L,1),
889 lua_to<T2>(L,2),
890 lua_to<T3>(L,3),
891 lua_to<T4>(L,4),
892 lua_to<T5>(L,5)
893 );
894 return 0;
895 }
896
903 template <
904 class T1, class T2, class T3, class T4, class T5, class T6
905 >
906 inline int lua_wrap(
907 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6)
908 ) {
909 if(
910 !lua_check_nb_args(L,6) ||
917 ) {
918 return lua_notify_last_error(L);
919 }
920 fptr(
921 lua_to<T1>(L,1),
922 lua_to<T2>(L,2),
923 lua_to<T3>(L,3),
924 lua_to<T4>(L,4),
925 lua_to<T5>(L,5),
926 lua_to<T6>(L,6)
927 );
928 return 0;
929 }
930
937 template <
938 class T1, class T2, class T3, class T4, class T5,
939 class T6, class T7
940 >
941 inline int lua_wrap(
942 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7)
943 ) {
944 if(
945 !lua_check_nb_args(L,7) ||
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 );
965 return 0;
966 }
967
968
975 template <
976 class T1, class T2, class T3, class T4, class T5,
977 class T6, class T7, class T8
978 >
979 inline int lua_wrap(
980 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8)
981 ) {
982 if(
983 !lua_check_nb_args(L,8) ||
992 ) {
993 return lua_notify_last_error(L);
994 }
995 fptr(
996 lua_to<T1>(L,1),
997 lua_to<T2>(L,2),
998 lua_to<T3>(L,3),
999 lua_to<T4>(L,4),
1000 lua_to<T5>(L,5),
1001 lua_to<T6>(L,6),
1002 lua_to<T7>(L,7),
1003 lua_to<T8>(L,8)
1004 );
1005 return 0;
1006 }
1007
1008
1015 template <
1016 class T1, class T2, class T3, class T4, class T5,
1017 class T6, class T7, class T8, class T9
1018 >
1019 inline int lua_wrap(
1020 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9)
1021 ) {
1022 if(
1023 !lua_check_nb_args(L,9) ||
1033 ) {
1034 return lua_notify_last_error(L);
1035 }
1036 fptr(
1037 lua_to<T1>(L,1),
1038 lua_to<T2>(L,2),
1039 lua_to<T3>(L,3),
1040 lua_to<T4>(L,4),
1041 lua_to<T5>(L,5),
1042 lua_to<T6>(L,6),
1043 lua_to<T7>(L,7),
1044 lua_to<T8>(L,8),
1045 lua_to<T9>(L,9)
1046 );
1047 return 0;
1048 }
1049
1050
1057 template <
1058 class T1, class T2, class T3, class T4, class T5,
1059 class T6, class T7, class T8, class T9, class T10
1060 >
1061 inline int lua_wrap(
1062 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10)
1063 ) {
1064 if(
1065 !lua_check_nb_args(L,10) ||
1076 ) {
1077 return lua_notify_last_error(L);
1078 }
1079 fptr(
1080 lua_to<T1>(L,1),
1081 lua_to<T2>(L,2),
1082 lua_to<T3>(L,3),
1083 lua_to<T4>(L,4),
1084 lua_to<T5>(L,5),
1085 lua_to<T6>(L,6),
1086 lua_to<T7>(L,7),
1087 lua_to<T8>(L,8),
1088 lua_to<T9>(L,9),
1089 lua_to<T10>(L,10)
1090 );
1091 return 0;
1092 }
1093
1094
1101 template <
1102 class T1, class T2, class T3, class T4, class T5,
1103 class T6, class T7, class T8, class T9, class T10,
1104 class T11
1105 >
1106 inline int lua_wrap(
1107 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11)
1108 ) {
1109 if(
1110 !lua_check_nb_args(L,11) ||
1120 !lua_to<T10>::can_convert(L,10) ||
1122 ) {
1123 return lua_notify_last_error(L);
1124 }
1125 fptr(
1126 lua_to<T1>(L,1),
1127 lua_to<T2>(L,2),
1128 lua_to<T3>(L,3),
1129 lua_to<T4>(L,4),
1130 lua_to<T5>(L,5),
1131 lua_to<T6>(L,6),
1132 lua_to<T7>(L,7),
1133 lua_to<T8>(L,8),
1134 lua_to<T9>(L,9),
1135 lua_to<T10>(L,10),
1136 lua_to<T11>(L,11)
1137 );
1138 return 0;
1139 }
1140
1147 template <
1148 class T1, class T2, class T3, class T4, class T5,
1149 class T6, class T7, class T8, class T9, class T10,
1150 class T11, class T12
1151 >
1152 inline int lua_wrap(
1153 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12)
1154 ) {
1155 if(
1156 !lua_check_nb_args(L,12) ||
1166 !lua_to<T10>::can_convert(L,10) ||
1167 !lua_to<T11>::can_convert(L,11) ||
1169 ) {
1170 return lua_notify_last_error(L);
1171 }
1172 fptr(
1173 lua_to<T1>(L,1),
1174 lua_to<T2>(L,2),
1175 lua_to<T3>(L,3),
1176 lua_to<T4>(L,4),
1177 lua_to<T5>(L,5),
1178 lua_to<T6>(L,6),
1179 lua_to<T7>(L,7),
1180 lua_to<T8>(L,8),
1181 lua_to<T9>(L,9),
1182 lua_to<T10>(L,10),
1183 lua_to<T11>(L,11),
1184 lua_to<T12>(L,12)
1185 );
1186 return 0;
1187 }
1188
1189
1196 template <
1197 class T1, class T2, class T3, class T4, class T5,
1198 class T6, class T7, class T8, class T9, class T10,
1199 class T11, class T12, class T13
1200 >
1201 inline int lua_wrap(
1202 lua_State* L, void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13)
1203 ) {
1204 if(
1205 !lua_check_nb_args(L,13) ||
1215 !lua_to<T10>::can_convert(L,10) ||
1216 !lua_to<T11>::can_convert(L,11) ||
1217 !lua_to<T12>::can_convert(L,12) ||
1219 ) {
1220 return lua_notify_last_error(L);
1221 }
1222 fptr(
1223 lua_to<T1>(L,1),
1224 lua_to<T2>(L,2),
1225 lua_to<T3>(L,3),
1226 lua_to<T4>(L,4),
1227 lua_to<T5>(L,5),
1228 lua_to<T6>(L,6),
1229 lua_to<T7>(L,7),
1230 lua_to<T8>(L,8),
1231 lua_to<T9>(L,9),
1232 lua_to<T10>(L,10),
1233 lua_to<T11>(L,11),
1234 lua_to<T12>(L,12),
1235 lua_to<T13>(L,13)
1236 );
1237 return 0;
1238 }
1239
1246 template<> inline int lua_wrap(lua_State* L, lua_CFunction fptr) {
1247 return fptr(L);
1248 }
1249
1250
1251 /*************************************************************************/
1252
1260 template <class FPTR> class lua_wrapper {
1261 public:
1262
1271 static int call(lua_State* L) {
1272 FPTR f = Memory::generic_pointer_to_function_pointer<FPTR>(
1273 lua_touserdata(L, lua_upvalueindex(1))
1274 );
1275 return lua_wrap(L, f);
1276 }
1277
1284 static void push(lua_State* L, FPTR f) {
1285 lua_pushlightuserdata(
1286 L, Memory::function_pointer_to_generic_pointer<FPTR>(f)
1287 );
1288 lua_pushcclosure(L, lua_wrapper<FPTR>::call, 1);
1289 }
1290 };
1291
1292 /**************************************************************************/
1293
1300 template <class FPTR> inline void lua_pushwrapper(lua_State* L, FPTR f) {
1302 }
1303
1308 template<> inline void lua_pushwrapper(lua_State* L, lua_CFunction f) {
1309 lua_pushcfunction(L,f);
1310 }
1311
1312 /**************************************************************************/
1313
1322 template <class FPTR> inline void lua_bindwrapperwithname(
1323 lua_State* L, FPTR f, const std::string& name
1324 ) {
1325 geo_assert(lua_gettop(L) > 0);
1326 geo_assert(lua_istable(L,-1));
1327 lua_pushstring(L,name.c_str());
1328 lua_pushwrapper(L,f);
1329 lua_settable(L,-3);
1330 }
1331
1332
1339 template <class FPTR> inline void lua_bindwrapperwithnameglobal(
1340 lua_State* L, FPTR f, const std::string& name
1341 ) {
1342 lua_pushwrapper(L,f);
1343 lua_setglobal(L,name.c_str());
1344 }
1345
1346 /**************************************************************************/
1347
1355 inline std::string lua_wrappername(lua_State* L, const char* functionname) {
1356 geo_argused(L);
1357 std::string result(functionname);
1358 size_t pos = result.find_last_of(":");
1359 if(pos != std::string::npos) {
1360 result = result.substr(pos+1, result.length()-pos);
1361 }
1362 return result;
1363 }
1364
1365 /**************************************************************************/
1366
1382#define lua_bindwrapper(L, f) lua_bindwrapperwithname( \
1383 (L),(f),GEO::lua_wrappername(L,#f) \
1384 )
1385
1400#define lua_bindwrapperglobal(L, f) lua_bindwrapperwithnameglobal( \
1401 (L),(f),GEO::lua_wrappername(L,#f) \
1402 )
1403
1404
1405 /*************************************************************************/
1406
1407}
1408
1409#ifdef GEO_COMPILER_MSVC
1410#pragma warning( pop )
1411#endif
1412
1413#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
A matrix type.
Definition matrix.h:66
Converts LUA variables to C++ variables.
Definition lua_wrap.h:240
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:272
lua_to(lua_State *L, int idx)
lua_to constructor.
Definition lua_wrap.h:254
Manages wrappers around C++ functions to be called from LUA.
Definition lua_wrap.h:1260
static int call(lua_State *L)
Implementation of the wrapper.
Definition lua_wrap.h:1271
static void push(lua_State *L, FPTR f)
Pushes a wrapper for a given C++ function onto the LUA stack.
Definition lua_wrap.h:1284
Generic maths vector.
Definition vecg.h:74
Common include file, providing basic definitions. Should be included before anything else by all head...
Functions to exchange vec2,vec3,vec4 and mat4 objects between Lua and Geogram.
Types and functions for memory manipulation.
uint64_t uint64
Definition numeric.h:145
uint32_t uint32
Definition numeric.h:142
int64_t int64
Definition numeric.h:133
Global Vorpaline namespace.
std::string lua_wrappername(lua_State *L, const char *functionname)
Converts a C++ function name into a LUA function name.
Definition lua_wrap.h:1355
bool lua_tomat(lua_State *L, int index, ::GEO::Matrix< N, T > &result)
Converts a Lua object into a Geogram mat2, mat3 or mat4.
int my_lua_isboolean(lua_State *L, int idx)
Tests whether a LUA variable is a boolean.
Definition lua_wrap.h:81
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:1339
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:175
void lua_set_error(lua_State *L, const char *error)
Memorizes an error message in LUA registry.
Definition lua_wrap.h:125
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:1322
void lua_clear_error(lua_State *L)
Clears the last error message in LUA registry.
Definition lua_wrap.h:146
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
Definition argused.h:60
void lua_push(lua_State *L, const ::GEO::vecng< N, T > &V)
Pushes a vector onto the Lua stack.
int my_lua_islightuserdata(lua_State *L, int idx)
Tests whether a LUA variable is a light user data.
Definition lua_wrap.h:96
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:198
int my_lua_ispositiveinteger(lua_State *L, int idx)
Tests whether a LUA variable is a positive integer.
Definition lua_wrap.h:109
bool lua_has_error(lua_State *L)
Tests whether an error message was memorized in the registry.
Definition lua_wrap.h:156
bool lua_tovec(lua_State *L, int index, ::GEO::vecng< N, T > &result)
Converts a lua object into a Geogram vec2,vec3 or vec4.
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:222
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:67
void lua_pushwrapper(lua_State *L, FPTR f)
Pushes a wrapper for a given C++ function onto the LUA stack.
Definition lua_wrap.h:1300
int lua_wrap(lua_State *L, R(*fptr)(void))
Calls a C++ function from LUA.
Definition lua_wrap.h:645
Types and functions for numbers manipulation.
Functions for string manipulation.