1 #include "lua/internal.hpp"
2 #include "library/minmax.hpp"
7 class lua_callbacks_list
10 lua_callbacks_list(lua::state
& L
);
11 static size_t overcommit() { return 0; }
12 static int create(lua::state
& L
, lua::parameters
& P
);
13 int index(lua::state
& L
, lua::parameters
& P
);
14 int newindex(lua::state
& L
, lua::parameters
& P
);
17 class lua_callback_obj
20 lua_callback_obj(lua::state
& L
, const std::string
& name
);
21 static size_t overcommit(const std::string
& name
) { return 0; }
22 int _register(lua::state
& L
, lua::parameters
& P
);
23 int _unregister(lua::state
& L
, lua::parameters
& P
);
24 int _call(lua::state
& L
, lua::parameters
& P
);
28 return "global register";
30 return "global unregister";
32 return callback
->get_name();
37 lua::state::callback_list
* callback
;
41 lua::_class
<lua_callbacks_list
> LUA_class_callbacks_list(lua_class_callback
, "CALLBACKS_LIST", {
42 {"new", lua_callbacks_list::create
},
44 {"__index", &lua_callbacks_list::index
},
45 {"__newindex", &lua_callbacks_list::newindex
},
47 lua::_class
<lua_callback_obj
> LUA_class_callback_obj(lua_class_callback
, "CALLBACK_OBJ", {}, {
48 {"register", &lua_callback_obj::_register
},
49 {"unregister", &lua_callback_obj::_unregister
},
50 {"__call", &lua_callback_obj::_call
},
51 }, &lua_callback_obj::print
);
53 lua_callbacks_list::lua_callbacks_list(lua::state
& L
)
57 int lua_callbacks_list::create(lua::state
& L
, lua::parameters
& P
)
59 lua::_class
<lua_callbacks_list
>::create(L
);
63 int lua_callbacks_list::index(lua::state
& L
, lua::parameters
& P
)
69 lua::_class
<lua_callback_obj
>::create(L
, name
);
73 int lua_callbacks_list::newindex(lua::state
& L
, lua::parameters
& P
)
75 throw std::runtime_error("Writing is not allowed");
78 lua_callback_obj::lua_callback_obj(lua::state
& L
, const std::string
& name
)
82 for(auto i
: L
.get_callbacks())
83 if(i
->get_name() == name
)
85 if(name
== "register") {
89 if(name
== "unregister") {
93 if(!callback
&& !special
)
94 throw std::runtime_error("Unknown callback type '" + name
+ "' for callback.<foo>");
97 int lua_callback_obj::_register(lua::state
& L
, lua::parameters
& P
)
101 if(!callback
) throw std::runtime_error(P
.get_fname() + ": not valid");
103 P(P
.skipped(), P
.function(lfn
));
106 callback
->_register(L
);
112 int lua_callback_obj::_unregister(lua::state
& L
, lua::parameters
& P
)
116 if(!callback
) throw std::runtime_error(P
.get_fname() + ": not valid");
118 P(P
.skipped(), P
.function(lfn
));
121 callback
->_unregister(L
);
127 int lua_callback_obj::_call(lua::state
& L
, lua::parameters
& P
)
132 if(!special
) throw std::runtime_error("Need to specify operation to do to callback");
134 P(P
.skipped(), name
, P
.function(lfn
));
137 for(auto i
: L
.get_callbacks()) {
138 if(i
->get_name() == name
) {
142 else if(special
== 2)
149 throw std::runtime_error("Unknown callback type '" + name
+ "' for callback.register");