22 EventHandler *EventHandler::_global_event_handler =
nullptr;
29 EventHandler(
EventQueue *ev_queue) : _queue(*ev_queue) {
39 fi = _futures.find(event_name);
43 if (fi != _futures.end() && !fi->second->cancelled()) {
47 _futures[event_name] = fut;
59 while (!_queue.is_queue_empty()) {
69 nassertv(event !=
nullptr);
73 Hooks::const_iterator hi;
74 hi = _hooks.find(event->get_name());
76 if (hi != _hooks.end()) {
81 Functions::const_iterator fi;
82 for (fi = copy_functions.begin(); fi != copy_functions.end(); ++fi) {
83 if (event_cat.is_spam()) {
85 <<
"calling callback 0x" << (
void*)(*fi)
86 <<
" for event '" <<
event->get_name() <<
"'"
94 CallbackHooks::const_iterator chi;
95 chi = _cbhooks.find(event->get_name());
97 if (chi != _cbhooks.end()) {
101 CallbackFunctions::const_iterator cfi;
102 for (cfi = copy_functions.begin(); cfi != copy_functions.end(); ++cfi) {
103 ((*cfi).first)(event, (*cfi).second);
108 Futures::iterator fi;
109 fi = _futures.find(event->get_name());
111 if (fi != _futures.end()) {
125 write(std::ostream &out)
const {
126 Hooks::const_iterator hi;
129 CallbackHooks::const_iterator chi;
130 chi = _cbhooks.begin();
132 while (hi != _hooks.end() && chi != _cbhooks.end()) {
133 if ((*hi).first < (*chi).first) {
134 write_hook(out, *hi);
136 }
else if ((*chi).first < (*hi).first) {
137 write_cbhook(out, *chi);
140 write_hook(out, *hi);
141 write_cbhook(out, *chi);
147 while (hi != _hooks.end()) {
148 write_hook(out, *hi);
152 while (chi != _cbhooks.end()) {
153 write_cbhook(out, *chi);
166 add_hook(
const string &event_name, EventFunction *
function) {
167 if (event_cat.is_debug()) {
169 <<
"adding hook for event '" << event_name
170 <<
"' with function 0x" << (
void*)
function << std::endl;
172 assert(!event_name.empty());
174 return _hooks[event_name].insert(
function).second;
185 add_hook(
const string &event_name, EventCallbackFunction *
function,
187 assert(!event_name.empty());
189 return _cbhooks[event_name].insert(CallbackFunction(
function, data)).second;
197 has_hook(
const string &event_name)
const {
198 assert(!event_name.empty());
199 Hooks::const_iterator hi;
200 hi = _hooks.find(event_name);
201 if (hi != _hooks.end()) {
202 if (!(*hi).second.empty()) {
207 CallbackHooks::const_iterator chi;
208 chi = _cbhooks.find(event_name);
209 if (chi != _cbhooks.end()) {
210 if (!(*chi).second.empty()) {
224 has_hook(
const string &event_name, EventFunction *
function)
const {
225 assert(!event_name.empty());
226 Hooks::const_iterator hi;
227 hi = _hooks.find(event_name);
228 if (hi != _hooks.end()) {
229 const Functions& functions = (*hi).second;
230 if (functions.find(
function) != functions.end()) {
244 has_hook(
const string &event_name, EventCallbackFunction *
function,
void *data)
const {
245 assert(!event_name.empty());
246 CallbackHooks::const_iterator chi;
247 chi = _cbhooks.find(event_name);
248 if (chi != _cbhooks.end()) {
250 if (cbfunctions.find(CallbackFunction(
function, data)) != cbfunctions.end()) {
264 remove_hook(
const string &event_name, EventFunction *
function) {
265 assert(!event_name.empty());
267 return _hooks[event_name].erase(
function) != 0;
277 remove_hook(
const string &event_name, EventCallbackFunction *
function,
279 assert(!event_name.empty());
281 return _cbhooks[event_name].erase(CallbackFunction(
function, data)) != 0;
290 assert(!event_name.empty());
291 bool any_removed =
false;
293 Hooks::iterator hi = _hooks.find(event_name);
294 if (hi != _hooks.end()) {
295 if (!(*hi).second.empty()) {
301 CallbackHooks::iterator chi = _cbhooks.find(event_name);
302 if (chi != _cbhooks.end()) {
303 if (!(*chi).second.empty()) {
318 bool any_removed =
false;
320 CallbackHooks::iterator chi;
321 for (chi = _cbhooks.begin(); chi != _cbhooks.end(); ++chi) {
323 CallbackFunctions::iterator cfi;
326 for (cfi = funcs.begin(); cfi != funcs.end(); ++cfi) {
327 if ((*cfi).second == data) {
330 new_funcs.insert(*cfi);
333 funcs.swap(new_funcs);
353 make_global_event_handler() {
363 write_hook(std::ostream &out,
const EventHandler::Hooks::value_type &hook)
const {
364 if (!hook.second.empty()) {
365 out << hook.first <<
" has " << hook.second.size() <<
" functions.\n";
373 write_cbhook(std::ostream &out,
const EventHandler::CallbackHooks::value_type &hook)
const {
374 if (!hook.second.empty()) {
375 out << hook.first <<
" has " << hook.second.size() <<
" callback functions.\n";
This class represents a thread-safe handle to a promised future result of an asynchronous operation,...
void set_result(std::nullptr_t)
Sets this future's result.
bool done() const
Returns true if the future is done or has been cancelled.
A class to monitor events from the C++ side of things.
void process_events()
The main processing loop of the EventHandler.
bool remove_hooks_with(void *data)
Removes all CallbackFunction hooks that have the indicated pointer as the associated data pointer.
virtual void dispatch_event(const Event *event)
Calls the hooks assigned to the indicated single event.
bool remove_hooks(const std::string &event_name)
Removes all functions from the named event hook.
void remove_all_hooks()
Removes all hooks assigned to all events.
AsyncFuture * get_future(const std::string &event_name)
Returns a pending future that will be marked as done when the event is next fired.
bool remove_hook(const std::string &event_name, EventFunction *function)
Removes the indicated function from the named event hook.
bool has_hook(const std::string &event_name) const
Returns true if there is any hook added on the indicated event name, false otherwise.
bool add_hook(const std::string &event_name, EventFunction *function)
Adds the indicated function to the list of those that will be called when the named event is thrown.
A queue of pending events.
static EventQueue * get_global_event_queue()
Returns a pointer to the one global EventQueue object.
A named event, possibly with parameters.
TypeHandle is the identifier used to differentiate C++ class types.
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
This is our own Panda specialization on the default STL set.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.