| 104 | } |
| 105 | |
| 106 | bool dispatch(const event_wrapper & event) |
| 107 | { |
| 108 | size_t handled = 0; |
| 109 | |
| 110 | const poly_typeid type = event.get_type(); |
| 111 | |
| 112 | // Dispatches to handlers only matching type (typical case) |
| 113 | ++dispatch_count; |
| 114 | auto range = map.equal_range(type); |
| 115 | for (auto it = range.first; it != range.second; ++it) { it->second.fn(event); handled++; }; |
| 116 | |
| 117 | // Dispatches to handlers listening to all events, regardless of type (infrequent) |
| 118 | range = map.equal_range(0); |
| 119 | for (auto it = range.first; it != range.second; ++it) { it->second.fn(event); handled++; }; |
| 120 | --dispatch_count; |
| 121 | |
| 122 | if (dispatch_count == 0) |
| 123 | { |
| 124 | for (auto & cmd : command_queue) |
| 125 | { |
| 126 | if (cmd.second.fn) map.emplace(cmd.first, std::move(cmd.second)); // queue a remove operation |
| 127 | else remove_impl(cmd.first, std::move(cmd.second)); // execute remove |
| 128 | } |
| 129 | command_queue.clear(); |
| 130 | } |
| 131 | |
| 132 | return (handled >= 1); |
| 133 | } |
| 134 | |
| 135 | size_t size() const { return map.size(); } |
| 136 | size_t handler_count(poly_typeid type) { return map.count(type); } |