Make fire_event_callback take a std::function instead of function pointer

This commit is contained in:
ridiculousfish
2017-01-21 17:15:45 -08:00
parent a91dad35db
commit 439f233ccc
4 changed files with 13 additions and 29 deletions

View File

@@ -37,8 +37,7 @@ static int wait_on_escape_ms = WAIT_ON_ESCAPE_DEFAULT;
static std::deque<wchar_t> lookahead_list;
// Queue of pairs of (function pointer, argument) to be invoked. Expected to be mostly empty.
typedef std::pair<void (*)(void *), void *> callback_info_t;
typedef std::queue<callback_info_t, std::list<callback_info_t> > callback_queue_t;
typedef std::list<std::function<void(void)>> callback_queue_t;
static callback_queue_t callback_queue;
static void input_flush_callbacks(void);
@@ -237,22 +236,18 @@ void input_common_queue_ch(wint_t ch) { lookahead_push_back(ch); }
void input_common_next_ch(wint_t ch) { lookahead_push_front(ch); }
void input_common_add_callback(void (*callback)(void *), void *arg) {
void input_common_add_callback(std::function<void(void)> callback) {
ASSERT_IS_MAIN_THREAD();
callback_queue.push(callback_info_t(callback, arg));
callback_queue.push_back(std::move(callback));
}
static void input_flush_callbacks(void) {
// Nothing to do if nothing to do.
if (callback_queue.empty()) return;
// We move the queue into a local variable, so that events queued up during a callback don't get
// fired until next round.
ASSERT_IS_MAIN_THREAD();
callback_queue_t local_queue;
std::swap(local_queue, callback_queue);
while (!local_queue.empty()) {
const callback_info_t &callback = local_queue.front();
callback.first(callback.second); // cute
local_queue.pop();
for (auto &f : local_queue) {
f();
}
}