1 #include "lua/internal.hpp"
2 #include "library/minmax.hpp"
7 class lua_callbacks_list
10 lua_callbacks_list(lua::state
& L
);
11 int index(lua::state
& L
, const std::string
& fname
);
12 int newindex(lua::state
& L
, const std::string
& fname
);
19 class lua_callback_obj
22 lua_callback_obj(lua::state
& L
, const std::string
& name
);
23 int _register(lua::state
& L
, const std::string
& fname
);
24 int _unregister(lua::state
& L
, const std::string
& fname
);
25 int _call(lua::state
& L
, const std::string
& fname
);
29 return callback
->get_name();
34 lua::state::callback_list
* callback
;
38 lua::_class
<lua_callbacks_list
> class_callbacks_list("CALLBACKS_LIST");
39 lua::_class
<lua_callback_obj
> class_callback_obj("CALLBACK_OBJ");
41 lua_callbacks_list::lua_callbacks_list(lua::state
& L
)
43 lua::objclass
<lua_callbacks_list
>().bind_multi(L
, {
44 {"__index", &lua_callbacks_list::index
},
45 {"__newindex", &lua_callbacks_list::newindex
},
49 int lua_callbacks_list::index(lua::state
& L
, const std::string
& fname
)
51 std::string name
= L
.get_string(2, fname
.c_str());
52 lua::_class
<lua_callback_obj
>::create(L
, name
);
56 int lua_callbacks_list::newindex(lua::state
& L
, const std::string
& fname
)
58 throw std::runtime_error("Writing is not allowed");
61 lua_callback_obj::lua_callback_obj(lua::state
& L
, const std::string
& name
)
63 lua::objclass
<lua_callback_obj
>().bind_multi(L
, {
64 {"register", &lua_callback_obj::_register
},
65 {"unregister", &lua_callback_obj::_unregister
},
66 {"__call", &lua_callback_obj::_call
},
70 for(auto i
: L
.get_callbacks())
71 if(i
->get_name() == name
)
73 if(name
== "register") {
77 if(name
== "unregister") {
81 if(!callback
&& !special
)
82 throw std::runtime_error("Unknown callback type '" + name
+ "' for callback.<foo>");
85 int lua_callback_obj::_register(lua::state
& L
, const std::string
& fname
)
88 throw std::runtime_error("callback.{,un}register.register not valid");
89 if(L
.type(2) != LUA_TFUNCTION
)
90 throw std::runtime_error("Expected function as 2nd argument to callback.<foo>:register");
92 callback
->_register(L
);
98 int lua_callback_obj::_unregister(lua::state
& L
, const std::string
& fname
)
101 throw std::runtime_error("callback.{,un}register.unregister not valid");
102 if(L
.type(2) != LUA_TFUNCTION
)
103 throw std::runtime_error("Expected function as 2nd argument to callback.<foo>:register");
105 callback
->_unregister(L
);
111 int lua_callback_obj::_call(lua::state
& L
, const std::string
& fname
)
114 throw std::runtime_error("Need to specify operation to do to callback");
115 std::string name
= L
.get_string(2, "callback.{,un}register");
116 if(L
.type(3) != LUA_TFUNCTION
)
117 throw std::runtime_error("Expected function as 2nd argument to callback.{,un}register");
119 for(auto i
: L
.get_callbacks()) {
120 if(i
->get_name() == name
) {
124 else if(special
== 2)
131 throw std::runtime_error("Unknown callback type '" + name
+ "' for callback.register");
136 lua::fnptr2
callback(lua_func_callback
, "callback", [](lua::state
& L
, lua::parameters
& P
) -> int {
137 lua::_class
<lua_callbacks_list
>::create(L
);