40#ifndef GEOGRAM_LUA_LUA_WRAP
43#include <geogram/lua/lua.h>
50#ifdef GEO_COMPILER_MSVC
51#pragma warning( push )
52#pragma warning( disable: 4702 )
81 return lua_isboolean(L,idx);
96 return lua_islightuserdata(L,idx);
109 if(!lua_isinteger(L,idx)) {
112 lua_Integer x = lua_tointeger(L,idx);
113 return (x > 0) ? 1 : 0;
125 lua_pushstring(L, error);
126 lua_setfield(L,LUA_REGISTRYINDEX,
"last_geogram_error");
147 lua_setfield(L,LUA_REGISTRYINDEX,
"last_geogram_error");
156 lua_getfield(L,LUA_REGISTRYINDEX,
"last_geogram_error");
157 bool result = !lua_isnil(L,-1);
176 std::string error = std::string(
"Argument ") +
177 String::to_string(idx) +
": wrong argument type (" +
178 "got " + lua_typename(L,lua_type(L,idx)) +
")";
198 if(lua_gettop(L) != expected_nb_args) {
200 "Expected " + String::to_string(expected_nb_args)
201 +
" arg(s), got " + String::to_string(lua_gettop(L));
223 lua_getfield(L,LUA_REGISTRYINDEX,
"last_geogram_error");
224 if(lua_isstring(L,-1)) {
225 error += lua_tostring(L,-1);
229 return luaL_error(L,error.c_str());
293 lua_to(lua_State* L,
int idx) {
294 x_ = int(lua_tointeger(L,idx));
299 operator int()
const {
309 template<>
class lua_to<Numeric::uint32> {
311 lua_to(lua_State* L,
int idx) {
327 template<>
class lua_to<Numeric::uint64> {
329 lua_to(lua_State* L,
int idx) {
346 template<>
class lua_to<Numeric::int64> {
348 lua_to(lua_State* L,
int idx) {
366 lua_to(lua_State* L,
int idx) {
367 x_ = float(lua_tonumber(L,idx));
372 operator float()
const {
384 lua_to(lua_State* L,
int idx) {
385 x_ = double(lua_tonumber(L,idx));
390 operator double()
const {
402 lua_to(lua_State* L,
int idx) {
403 x_ = (lua_toboolean(L,idx) != 0);
408 operator bool()
const {
420 lua_to(lua_State* L,
int idx) {
421 x_ = lua_tostring(L,idx);
423 static bool can_convert(lua_State* L,
int idx) {
426 operator const char*()
const {
436 template<>
class lua_to<const std::string&> {
438 lua_to(lua_State* L,
int idx) {
439 x_ = lua_tostring(L,idx);
444 operator const std::string&()
const {
456 lua_to(lua_State* L,
int idx) {
457 x_ = lua_tostring(L,idx);
462 operator std::string()
const {
481 template<
class T>
inline void lua_push(lua_State* L, T x) {
490 template<>
inline void lua_push(lua_State* L,
int x) {
491 lua_pushinteger(L,lua_Integer(x));
499 lua_pushinteger(L,lua_Integer(x));
506 lua_pushinteger(L,lua_Integer(x));
513 lua_pushinteger(L,lua_Integer(x));
519 template<>
inline void lua_push(lua_State* L,
float x) {
520 lua_pushnumber(L,lua_Number(x));
526 template<>
inline void lua_push(lua_State* L,
double x) {
527 lua_pushnumber(L,lua_Number(x));
533 template<>
inline void lua_push(lua_State* L,
bool x) {
534 lua_pushboolean(L,x?1:0);
540 template<>
inline void lua_push(lua_State* L,
const char* x) {
547 template<>
inline void lua_push(lua_State* L,
const std::string& x) {
548 lua_pushstring(L,x.c_str());
554 template<>
inline void lua_push(lua_State* L, std::string x) {
555 lua_pushstring(L,x.c_str());
564 lua_State* L,
const std::vector<T>& x
567 for(
size_t i=0; i<x.size(); ++i) {
569 lua_seti(L,-2,lua_Integer(i+1));
583#define LUA_DECLAREENUMTYPE(T) \
584 template<> inline void lua_push(lua_State* L, T x) { \
585 lua_push(L, int(x)); \
588 template<> class lua_to<T> : public GEO::lua_to<int> { \
590 lua_to(lua_State* L, int idx) : lua_to<int>(L,idx) { \
592 operator T() const { \
593 return T(lua_to<int>::operator int()); \
606 template <
class R>
inline int lua_wrap(lua_State* L, R (*fptr)(
void)) {
623 lua_State* L, R (*fptr)(T1)
645 template <
class R,
class T1,
class T2>
inline int lua_wrap(
646 lua_State* L, R (*fptr)(T1,T2)
670 template <
class R,
class T1,
class T2,
class T3>
inline int lua_wrap(
671 lua_State* L, R (*fptr)(T1,T2,T3)
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)) {
726 template <>
inline int lua_wrap(lua_State* L,
void (*fptr)(
void)) {
740 template <
class T1>
inline int lua_wrap(lua_State* L,
void (*fptr)(T1)) {
760 lua_State* L,
void (*fptr)(T1,T2)
782 template <
class T1,
class T2,
class T3>
783 inline int lua_wrap(lua_State* L,
void (*fptr)(T1,T2,T3)) {
806 template <
class T1,
class T2,
class T3,
class T4>
807 inline int lua_wrap(lua_State* L,
void (*fptr)(T1,T2,T3,T4)) {
833 class T1,
class T2,
class T3,
class T4,
class T5
836 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5)
865 class T1,
class T2,
class T3,
class T4,
class T5,
class T6
868 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6)
899 class T1,
class T2,
class T3,
class T4,
class T5,
903 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7)
937 class T1,
class T2,
class T3,
class T4,
class T5,
938 class T6,
class T7,
class T8
941 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8)
977 class T1,
class T2,
class T3,
class T4,
class T5,
978 class T6,
class T7,
class T8,
class T9
981 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9)
1019 class T1,
class T2,
class T3,
class T4,
class T5,
1020 class T6,
class T7,
class T8,
class T9,
class T10
1023 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10)
1063 class T1,
class T2,
class T3,
class T4,
class T5,
1064 class T6,
class T7,
class T8,
class T9,
class T10,
1068 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11)
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
1114 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12)
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
1163 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13)
1207 template<>
inline int lua_wrap(lua_State* L, lua_CFunction fptr) {
1235 lua_touserdata(L, lua_upvalueindex(1))
1247 static void push(lua_State* L, FPTR f) {
1248 lua_pushlightuserdata(
1275 lua_pushcfunction(L,f);
1289 lua_State* L, FPTR f,
const std::string& name
1293 lua_pushstring(L,name.c_str());
1306 lua_State* L, FPTR f,
const std::string& name
1309 lua_setglobal(L,name.c_str());
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);
1348#define lua_bindwrapper(L, f) lua_bindwrapperwithname( \
1349 (L),(f),GEO::lua_wrappername(L,#f) \
1366#define lua_bindwrapperglobal(L, f) lua_bindwrapperwithnameglobal( \
1367 (L),(f),GEO::lua_wrappername(L,#f) \
1375#ifdef GEO_COMPILER_MSVC
1376#pragma warning( pop )
Assertion checking mechanism.
#define geo_assert_not_reached
Sets a non reachable point in the program.
#define geo_assert(x)
Verifies that a condition is met.
Converts LUA variables to C++ variables.
static bool can_convert(lua_State *L, int idx)
Tests whether a LUA variable can be converted to a C++ variable.
lua_to(lua_State *L, int idx)
lua_to constructor.
Manages wrappers around C++ functions to be called from LUA.
static int call(lua_State *L)
Implementation of the wrapper.
static void push(lua_State *L, FPTR f)
Pushes a wrapper for a given C++ function onto the LUA stack.
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.
pointer function_pointer_to_generic_pointer(function_pointer fptr)
Converts a function pointer to a generic pointer.
function_pointer generic_pointer_to_function_pointer(pointer ptr)
Converts a generic pointer to a function pointer.
Global Vorpaline namespace.
std::string lua_wrappername(lua_State *L, const char *functionname)
Converts a C++ function name into a LUA function name.
int my_lua_isboolean(lua_State *L, int idx)
Tests whether a LUA variable is a boolean.
void lua_bindwrapperwithnameglobal(lua_State *L, FPTR f, const std::string &name)
Binds a wrapper to a name in the global scole.
bool lua_check_type(lua_State *L, int idx, lua_test_func test)
Tests whether a LUA variable has the correct type.
void lua_set_error(lua_State *L, const char *error)
Memorizes an error message in LUA registry.
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.
void lua_clear_error(lua_State *L)
Clears the last error message in LUA registry.
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
int my_lua_islightuserdata(lua_State *L, int idx)
Tests whether a LUA variable is a light user data.
void lua_push(lua_State *L, T x)
Converts and pushes a C++ variable onto the LUA stack.
bool lua_check_nb_args(lua_State *L, int expected_nb_args)
Tests whether the expected number of arguments was pushed onto the stack.
int my_lua_ispositiveinteger(lua_State *L, int idx)
Tests whether a LUA variable is a positive integer.
bool lua_has_error(lua_State *L)
Tests whether an error message was memorized in the registry.
int lua_notify_last_error(lua_State *L)
Takes the last error message memorized in the registry and sends it back to LUA.
int(* lua_test_func)(lua_State *L, int idx)
A pointer to a LUA function to test an argument in the LUA stack.
void lua_pushwrapper(lua_State *L, FPTR f)
Pushes a wrapper for a given C++ function onto the LUA stack.
int lua_wrap(lua_State *L, R(*fptr)(void))
Calls a C++ function from LUA.
Types and functions for numbers manipulation.
Functions for string manipulation.