Lua: lua_class<T>::bind_multi (initializer lists are useful)
This commit is contained in:
parent
e7472118a5
commit
1909357f99
7 changed files with 92 additions and 81 deletions
|
@ -634,6 +634,18 @@ template<class T> class lua_class;
|
|||
*/
|
||||
template<class T> lua_class<T>& objclass();
|
||||
|
||||
template<class T> struct lua_class_binding
|
||||
{
|
||||
/**
|
||||
* Name.
|
||||
*/
|
||||
const char* name;
|
||||
/**
|
||||
* Function.
|
||||
*/
|
||||
int (T::*fn)(lua_state& LS);
|
||||
};
|
||||
|
||||
/**
|
||||
* The type of Lua classes.
|
||||
*/
|
||||
|
@ -703,7 +715,6 @@ badtype:
|
|||
state.pushvalue(arg);
|
||||
return lua_obj_pin<T>(state, obj);
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
* Create a new Lua class.
|
||||
|
@ -748,7 +759,16 @@ public:
|
|||
state.rawset(-3);
|
||||
state.pop(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind multiple at once.
|
||||
*/
|
||||
void bind_multi(lua_state& state, std::initializer_list<lua_class_binding<T>> list, void* doonce_key = NULL)
|
||||
{
|
||||
static char once_key;
|
||||
if(state.do_once(doonce_key ? doonce_key : &once_key))
|
||||
for(auto i : list)
|
||||
bind(state, i.name, i.fn);
|
||||
}
|
||||
/**
|
||||
* Get a pointer to the object.
|
||||
*
|
||||
|
|
|
@ -32,11 +32,10 @@ namespace
|
|||
{
|
||||
lua_callbacks_list::lua_callbacks_list(lua_state& L)
|
||||
{
|
||||
static char doonce_key;
|
||||
if(L.do_once(&doonce_key)) {
|
||||
objclass<lua_callbacks_list>().bind(L, "__index", &lua_callbacks_list::index);
|
||||
objclass<lua_callbacks_list>().bind(L, "__newindex", &lua_callbacks_list::newindex);
|
||||
}
|
||||
objclass<lua_callbacks_list>().bind_multi(L, {
|
||||
{"__index", &lua_callbacks_list::index},
|
||||
{"__newindex", &lua_callbacks_list::newindex},
|
||||
});
|
||||
}
|
||||
|
||||
int lua_callbacks_list::index(lua_state& L)
|
||||
|
@ -53,12 +52,11 @@ namespace
|
|||
|
||||
lua_callback_obj::lua_callback_obj(lua_state& L, const std::string& name)
|
||||
{
|
||||
static char doonce_key;
|
||||
if(L.do_once(&doonce_key)) {
|
||||
objclass<lua_callback_obj>().bind(L, "register", &lua_callback_obj::_register);
|
||||
objclass<lua_callback_obj>().bind(L, "unregister", &lua_callback_obj::_unregister);
|
||||
objclass<lua_callback_obj>().bind(L, "__call", &lua_callback_obj::_call);
|
||||
}
|
||||
objclass<lua_callback_obj>().bind_multi(L, {
|
||||
{"register", &lua_callback_obj::_register},
|
||||
{"unregister", &lua_callback_obj::_unregister},
|
||||
{"__call", &lua_callback_obj::_call},
|
||||
});
|
||||
callback = NULL;
|
||||
special = 0;
|
||||
for(auto i : L.get_callbacks())
|
||||
|
|
|
@ -88,10 +88,9 @@ namespace
|
|||
lua_customfont::lua_customfont(lua_state& L, const std::string& filename)
|
||||
: font(filename)
|
||||
{
|
||||
static char done_key;
|
||||
if(L.do_once(&done_key)) {
|
||||
objclass<lua_customfont>().bind(L, "__call", &lua_customfont::draw);
|
||||
}
|
||||
objclass<lua_customfont>().bind_multi(L, {
|
||||
{"__call", &lua_customfont::draw},
|
||||
});
|
||||
}
|
||||
|
||||
lua_customfont::~lua_customfont() throw()
|
||||
|
|
|
@ -611,37 +611,35 @@ namespace
|
|||
lua_inputframe::lua_inputframe(lua_state& L, controller_frame _f)
|
||||
{
|
||||
f = _f;
|
||||
static char done_key;
|
||||
if(L.do_once(&done_key)) {
|
||||
objclass<lua_inputframe>().bind(L, "get_button", &lua_inputframe::get_button);
|
||||
objclass<lua_inputframe>().bind(L, "get_axis", &lua_inputframe::get_axis);
|
||||
objclass<lua_inputframe>().bind(L, "set_axis", &lua_inputframe::set_axis);
|
||||
objclass<lua_inputframe>().bind(L, "set_button", &lua_inputframe::set_axis);
|
||||
objclass<lua_inputframe>().bind(L, "serialize", &lua_inputframe::serialize);
|
||||
objclass<lua_inputframe>().bind(L, "unserialize", &lua_inputframe::unserialize);
|
||||
objclass<lua_inputframe>().bind(L, "get_stride", &lua_inputframe::get_stride);
|
||||
}
|
||||
objclass<lua_inputframe>().bind_multi(L, {
|
||||
{"get_button", &lua_inputframe::get_button},
|
||||
{"get_axis", &lua_inputframe::get_axis},
|
||||
{"set_axis", &lua_inputframe::set_axis},
|
||||
{"set_button", &lua_inputframe::set_axis},
|
||||
{"serialize", &lua_inputframe::serialize},
|
||||
{"unserialize", &lua_inputframe::unserialize},
|
||||
{"get_stride", &lua_inputframe::get_stride},
|
||||
});
|
||||
}
|
||||
|
||||
void lua_inputmovie::common_init(lua_state& L)
|
||||
{
|
||||
static char done_key;
|
||||
if(L.do_once(&done_key)) {
|
||||
objclass<lua_inputmovie>().bind(L, "copy_movie", &lua_inputmovie::copy_movie);
|
||||
objclass<lua_inputmovie>().bind(L, "get_frame", &lua_inputmovie::get_frame);
|
||||
objclass<lua_inputmovie>().bind(L, "set_frame", &lua_inputmovie::set_frame);
|
||||
objclass<lua_inputmovie>().bind(L, "get_size", &lua_inputmovie::get_size);
|
||||
objclass<lua_inputmovie>().bind(L, "count_frames", &lua_inputmovie::count_frames);
|
||||
objclass<lua_inputmovie>().bind(L, "find_frame", &lua_inputmovie::find_frame);
|
||||
objclass<lua_inputmovie>().bind(L, "blank_frame", &lua_inputmovie::blank_frame);
|
||||
objclass<lua_inputmovie>().bind(L, "append_frames", &lua_inputmovie::append_frames);
|
||||
objclass<lua_inputmovie>().bind(L, "append_frame", &lua_inputmovie::append_frame);
|
||||
objclass<lua_inputmovie>().bind(L, "truncate", &lua_inputmovie::truncate);
|
||||
objclass<lua_inputmovie>().bind(L, "edit", &lua_inputmovie::edit);
|
||||
objclass<lua_inputmovie>().bind(L, "debugdump", &lua_inputmovie::debugdump);
|
||||
objclass<lua_inputmovie>().bind(L, "copy_frames", &lua_inputmovie::copy_frames);
|
||||
objclass<lua_inputmovie>().bind(L, "serialize", &lua_inputmovie::serialize);
|
||||
}
|
||||
objclass<lua_inputmovie>().bind_multi(L, {
|
||||
{"copy_movie", &lua_inputmovie::copy_movie},
|
||||
{"get_frame", &lua_inputmovie::get_frame},
|
||||
{"set_frame", &lua_inputmovie::set_frame},
|
||||
{"get_size", &lua_inputmovie::get_size},
|
||||
{"count_frames", &lua_inputmovie::count_frames},
|
||||
{"find_frame", &lua_inputmovie::find_frame},
|
||||
{"blank_frame", &lua_inputmovie::blank_frame},
|
||||
{"append_frames", &lua_inputmovie::append_frames},
|
||||
{"append_frame", &lua_inputmovie::append_frame},
|
||||
{"truncate", &lua_inputmovie::truncate},
|
||||
{"edit", &lua_inputmovie::edit},
|
||||
{"debugdump", &lua_inputmovie::debugdump},
|
||||
{"copy_frames", &lua_inputmovie::copy_frames},
|
||||
{"serialize", &lua_inputmovie::serialize},
|
||||
});
|
||||
}
|
||||
|
||||
lua_inputmovie::lua_inputmovie(lua_state& L, const controller_frame_vector& _v)
|
||||
|
|
|
@ -320,10 +320,9 @@ namespace
|
|||
lua_file_reader::lua_file_reader(lua_state& L, std::istream* strm)
|
||||
: s(*strm)
|
||||
{
|
||||
static char doonce_key;
|
||||
if(L.do_once(&doonce_key)) {
|
||||
objclass<lua_file_reader>().bind(L, "__call", &lua_file_reader::read);
|
||||
objclass<lua_file_reader>().bind(L, "lines", &lua_file_reader::lines);
|
||||
}
|
||||
objclass<lua_file_reader>().bind_multi(L, {
|
||||
{"__call", &lua_file_reader::read},
|
||||
{"lines", &lua_file_reader::lines}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -474,10 +474,9 @@ DECLARE_LUACLASS(lua_mmap_struct, "MMAP_STRUCT");
|
|||
|
||||
lua_mmap_struct::lua_mmap_struct(lua_state& L)
|
||||
{
|
||||
static char done_key;
|
||||
if(L.do_once(&done_key)) {
|
||||
objclass<lua_mmap_struct>().bind(L, "__index", &lua_mmap_struct::index);
|
||||
objclass<lua_mmap_struct>().bind(L, "__newindex", &lua_mmap_struct::newindex);
|
||||
objclass<lua_mmap_struct>().bind(L, "__call", &lua_mmap_struct::map);
|
||||
}
|
||||
objclass<lua_mmap_struct>().bind_multi(L, {
|
||||
{"__index", &lua_mmap_struct::index},
|
||||
{"__newindex", &lua_mmap_struct::newindex},
|
||||
{"__call", &lua_mmap_struct::map},
|
||||
});
|
||||
}
|
||||
|
|
|
@ -77,26 +77,25 @@ namespace
|
|||
{
|
||||
lua_vma::lua_vma(lua_state& L, memory_region* r)
|
||||
{
|
||||
static char doonce_key;
|
||||
if(L.do_once(&doonce_key)) {
|
||||
objclass<lua_vma>().bind(L, "info", &lua_vma::info);
|
||||
objclass<lua_vma>().bind(L, "sbyte", &lua_vma::rw<int8_t, false>);
|
||||
objclass<lua_vma>().bind(L, "byte", &lua_vma::rw<uint8_t, false>);
|
||||
objclass<lua_vma>().bind(L, "sword", &lua_vma::rw<int16_t, false>);
|
||||
objclass<lua_vma>().bind(L, "word", &lua_vma::rw<uint16_t, false>);
|
||||
objclass<lua_vma>().bind(L, "sdword", &lua_vma::rw<int32_t, false>);
|
||||
objclass<lua_vma>().bind(L, "dword", &lua_vma::rw<uint32_t, false>);
|
||||
objclass<lua_vma>().bind(L, "sqword", &lua_vma::rw<int64_t, false>);
|
||||
objclass<lua_vma>().bind(L, "qword", &lua_vma::rw<uint64_t, false>);
|
||||
objclass<lua_vma>().bind(L, "isbyte", &lua_vma::rw<int8_t, true>);
|
||||
objclass<lua_vma>().bind(L, "ibyte", &lua_vma::rw<uint8_t, true>);
|
||||
objclass<lua_vma>().bind(L, "isword", &lua_vma::rw<int16_t, true>);
|
||||
objclass<lua_vma>().bind(L, "iword", &lua_vma::rw<uint16_t, true>);
|
||||
objclass<lua_vma>().bind(L, "isdword", &lua_vma::rw<int32_t, true>);
|
||||
objclass<lua_vma>().bind(L, "idword", &lua_vma::rw<uint32_t, true>);
|
||||
objclass<lua_vma>().bind(L, "isqword", &lua_vma::rw<int64_t, true>);
|
||||
objclass<lua_vma>().bind(L, "iqword", &lua_vma::rw<uint64_t, true>);
|
||||
}
|
||||
objclass<lua_vma>().bind_multi(L, {
|
||||
{"info", &lua_vma::info},
|
||||
{"sbyte", &lua_vma::rw<int8_t, false>},
|
||||
{"byte", &lua_vma::rw<uint8_t, false>},
|
||||
{"sword", &lua_vma::rw<int16_t, false>},
|
||||
{"word", &lua_vma::rw<uint16_t, false>},
|
||||
{"sdword", &lua_vma::rw<int32_t, false>},
|
||||
{"dword", &lua_vma::rw<uint32_t, false>},
|
||||
{"sqword", &lua_vma::rw<int64_t, false>},
|
||||
{"qword", &lua_vma::rw<uint64_t, false>},
|
||||
{"isbyte", &lua_vma::rw<int8_t, true>},
|
||||
{"ibyte", &lua_vma::rw<uint8_t, true>},
|
||||
{"isword", &lua_vma::rw<int16_t, true>},
|
||||
{"iword", &lua_vma::rw<uint16_t, true>},
|
||||
{"isdword", &lua_vma::rw<int32_t, true>},
|
||||
{"idword", &lua_vma::rw<uint32_t, true>},
|
||||
{"isqword", &lua_vma::rw<int64_t, true>},
|
||||
{"iqword", &lua_vma::rw<uint64_t, true>},
|
||||
});
|
||||
vmabase = r->base;
|
||||
vmasize = r->size;
|
||||
vma = r->name;
|
||||
|
@ -136,12 +135,11 @@ namespace
|
|||
|
||||
lua_vma_list::lua_vma_list(lua_state& L)
|
||||
{
|
||||
static char doonce_key;
|
||||
if(L.do_once(&doonce_key)) {
|
||||
objclass<lua_vma_list>().bind(L, "__index", &lua_vma_list::index);
|
||||
objclass<lua_vma_list>().bind(L, "__newindex", &lua_vma_list::newindex);
|
||||
objclass<lua_vma_list>().bind(L, "__call", &lua_vma_list::call);
|
||||
}
|
||||
objclass<lua_vma_list>().bind_multi(L, {
|
||||
{"__index", &lua_vma_list::index},
|
||||
{"__newindex", &lua_vma_list::newindex},
|
||||
{"__call", &lua_vma_list::call},
|
||||
});
|
||||
}
|
||||
|
||||
int lua_vma_list::call(lua_state& L)
|
||||
|
|
Loading…
Add table
Reference in a new issue