Skip to content

Commit

Permalink
Move the global event context onto the general event queue.
Browse files Browse the repository at this point in the history
This moves the global event context onto the general event queue
and creates it through RAII instead.
  • Loading branch information
aginor committed Jul 17, 2016
1 parent 6964aa2 commit 516dacc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,14 @@ context::~context()
}
}

//this object stores all the event handlers. It is a stack of event 'contexts'.
//a new event context is created when e.g. a modal dialog is opened, and then
//closed when that dialog is closed. Each context contains a list of the handlers
//in that context. The current context is the one on the top of the stack
// This object stores all the event handlers. It is a stack of event 'contexts'.
// a new event context is created when e.g. a modal dialog is opened, and then
// closed when that dialog is closed. Each context contains a list of the handlers
// in that context. The current context is the one on the top of the stack.
// The global context must always be in the first position.
std::deque<context> event_contexts;

//add a global context for event handlers. Whichever object has joined this will always
//receive all events, regardless of the current context.
context global_context;


std::vector<pump_monitor*> pump_monitors;

Expand Down Expand Up @@ -196,6 +195,11 @@ sdl_handler::~sdl_handler()
}

void sdl_handler::join() {

// this assert will fire if someone will inadvertedly try to join
// an event context but might end up in the global context instead.
assert(&event_contexts.back() != &event_contexts.front());

join(event_contexts.back());
}

Expand All @@ -204,6 +208,7 @@ void sdl_handler::join(context &c)
if(has_joined_) {
leave(); // should not be in multiple event contexts
}

//join self
c.add_handler(this);
has_joined_ = true;
Expand Down Expand Up @@ -231,7 +236,7 @@ void sdl_handler::join_same(sdl_handler* parent)
}
}

join(global_context);
join(event_contexts.front());
}

void sdl_handler::leave()
Expand All @@ -258,7 +263,7 @@ void sdl_handler::join_global()
leave_global(); // should not be in multiple event contexts
}
//join self
global_context.add_handler(this);
event_contexts.front().add_handler(this);
has_joined_global_ = true;

//instruct members to join
Expand All @@ -279,7 +284,7 @@ void sdl_handler::leave_global()
}
}

global_context.remove_handler(this);
event_contexts.front().remove_handler(this);

has_joined_global_ = false;
}
Expand Down Expand Up @@ -455,7 +460,7 @@ void pump()
handler->handle_window_event(event);
}
}
const handler_list& event_handlers = global_context.handlers;
const handler_list& event_handlers = event_contexts.front().handlers;
for(auto handler : event_handlers) {
handler->handle_window_event(event);
}
Expand Down Expand Up @@ -538,7 +543,7 @@ void pump()
}
}

const handler_list& event_handlers = global_context.handlers;
const handler_list& event_handlers = event_contexts.front().handlers;
for(auto handler : event_handlers) {
handler->handle_event(event);
}
Expand Down
3 changes: 3 additions & 0 deletions src/wesnoth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,9 @@ int main(int argc, char** argv)
sigaction(SIGINT, &terminate_handler, nullptr);
#endif

//declare this here so that it will always be at the front of the event queue.
events::event_context global_context;

try {
std::cerr << "Battle for Wesnoth v" << game_config::revision << '\n';
const time_t t = time(nullptr);
Expand Down

0 comments on commit 516dacc

Please sign in to comment.