1 #ifndef _lua_int__hpp__included__
2 #define _lua_int__hpp__included__
12 std::string
get_string_argument(lua_State
* LS
, unsigned argindex
, const char* fname
);
13 bool get_boolean_argument(lua_State
* LS
, unsigned argindex
, const char* fname
);
14 void push_keygroup_parameters(lua_State
* LS
, const struct keygroup::parameters
& p
);
15 extern lua_render_context
* lua_render_ctx
;
16 extern controller_frame
* lua_input_controllerdata
;
17 extern bool lua_booted_flag
;
18 extern uint64_t lua_idle_hook_time
;
19 extern uint64_t lua_timer_hook_time
;
22 T
get_numeric_argument(lua_State
* LS
, unsigned argindex
, const char* fname
)
24 if(lua_isnone(LS
, argindex
) || !lua_isnumber(LS
, argindex
)) {
26 sprintf(buffer
, "argument #%i to %s must be numeric", argindex
, fname
);
27 lua_pushstring(LS
, buffer
);
30 return static_cast<T
>(lua_tonumber(LS
, argindex
));
34 void get_numeric_argument(lua_State
* LS
, unsigned argindex
, T
& value
, const char* fname
)
36 if(lua_isnoneornil(LS
, argindex
))
38 if(lua_isnone(LS
, argindex
) || !lua_isnumber(LS
, argindex
)) {
40 sprintf(buffer
, "argument #%i to %s must be numeric if present", argindex
, fname
);
41 lua_pushstring(LS
, buffer
);
44 value
= static_cast<T
>(lua_tonumber(LS
, argindex
));
48 template<typename T
> struct lua_obj_pin
50 lua_obj_pin(lua_State
* LS
, T
* _object
)
52 lua_pushlightuserdata(LS
, this);
53 lua_pushvalue(LS
, -2);
54 lua_rawset(LS
, LUA_REGISTRYINDEX
);
60 lua_pushlightuserdata(state
, this);
62 lua_rawset(state
, LUA_REGISTRYINDEX
);
64 T
* object() { return obj
; }
68 lua_obj_pin(const lua_obj_pin
&);
69 lua_obj_pin
& operator=(const lua_obj_pin
&);
72 template<class T
> class lua_class
;
74 template<class T
> lua_class
<T
>& objclass();
76 template<class T
> class lua_class
79 lua_class(const std::string
& _name
)
84 template<typename
... U
> T
* _create(lua_State
* LS
, U
... args
)
86 void* obj
= lua_newuserdata(LS
, sizeof(T
));
88 lua_setmetatable(LS
, -2);
89 T
* _obj
= reinterpret_cast<T
*>(obj
);
94 template<typename
... U
> static T
* create(lua_State
* LS
, U
... args
)
96 return objclass
<T
>()._create(LS
, args
...);
99 void bind(lua_State
* LS
)
102 lua_pushvalue(LS
, -3);
103 lua_pushvalue(LS
, -3);
107 void bind(lua_State
* LS
, const char* keyname
)
110 lua_pushstring(LS
, keyname
);
111 lua_pushvalue(LS
, -3);
115 T
* _get(lua_State
* LS
, int arg
, const char* fname
, bool optional
= false)
117 if(lua_type(LS
, arg
) == LUA_TNONE
|| lua_type(LS
, arg
) == LUA_TNIL
) {
124 if(!lua_getmetatable(LS
, arg
))
126 if(!lua_rawequal(LS
, -1, -2))
129 return reinterpret_cast<T
*>(lua_touserdata(LS
, arg
));
132 sprintf(buffer
, "argument #%i to %s must be %s", arg
, fname
, name
.c_str());
133 lua_pushstring(LS
, buffer
);
137 static T
* get(lua_State
* LS
, int arg
, const char* fname
, bool optional
= false)
139 return objclass
<T
>()._get(LS
, arg
, fname
, optional
);
142 bool _is(lua_State
* LS
, int arg
)
144 if(lua_type(LS
, arg
) != LUA_TUSERDATA
)
147 if(!lua_getmetatable(LS
, arg
)) {
151 bool ret
= lua_rawequal(LS
, -1, -2);
156 static bool is(lua_State
* LS
, int arg
)
158 return objclass
<T
>()._is(LS
, arg
);
161 lua_obj_pin
<T
>* _pin(lua_State
* LS
, int arg
, const char* fname
)
163 T
* obj
= get(LS
, arg
, fname
);
164 lua_pushvalue(LS
, arg
);
165 return new lua_obj_pin
<T
>(LS
, obj
);
168 static lua_obj_pin
<T
>* pin(lua_State
* LS
, int arg
, const char* fname
)
170 return objclass
<T
>()._pin(LS
, arg
, fname
);
173 static int dogc(lua_State
* LS
)
175 T
* obj
= reinterpret_cast<T
*>(lua_touserdata(LS
, 1));
179 void load_metatable(lua_State
* LS
)
182 lua_pushlightuserdata(LS
, this);
183 lua_rawget(LS
, LUA_REGISTRYINDEX
);
184 if(lua_type(LS
, -1) == LUA_TNIL
) {
186 lua_pushlightuserdata(LS
, this);
188 lua_pushstring(LS
, "__gc");
189 lua_pushcfunction(LS
, &lua_class
<T
>::dogc
);
191 lua_rawset(LS
, LUA_REGISTRYINDEX
);
196 lua_class(const lua_class
<T
>&);
197 lua_class
& operator=(const lua_class
<T
>&);
200 #define DECLARE_LUACLASS(x, X) template<> lua_class< x >& objclass() { static lua_class< x > clazz( X ); \