Skip to content

Commit

Permalink
Revises EventHandler members & updates hash function
Browse files Browse the repository at this point in the history
- converts `freq` and `eventHandler` (i.e. the callback) to `const`
- removes the non-const `when` from the hash computation
  • Loading branch information
cppcooper committed Mar 16, 2023
1 parent 22d2a98 commit e2bc986
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
5 changes: 2 additions & 3 deletions library/include/modules/EventManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ namespace DFHack {

struct EventHandler {
typedef void (*callback_t)(color_ostream&, void*); //called when the event happens
callback_t eventHandler;
int32_t freq; //how often event is allowed to fire (in ticks) use 0 to always fire when possible
const callback_t eventHandler;
const int32_t freq; //how often event is allowed to fire (in ticks) use 0 to always fire when possible
int32_t when = -1; //when to fire event (global tick count)

EventHandler(callback_t eventHandlerIn, int32_t freqIn) :
Expand Down Expand Up @@ -146,7 +146,6 @@ namespace std {
size_t r = 17;
const size_t m = 65537;
r = m*(r+(intptr_t)h.eventHandler);
r = m*(r+h.when);
r = m*(r+h.freq);
return r;
}
Expand Down
2 changes: 1 addition & 1 deletion library/modules/EventManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int32_t DFHack::EventManager::registerTick(EventHandler handler, int32_t when, P
}
DEBUG(log).print("registering handler %p from plugin %s for event TICK\n", handler.eventHandler, plugin->getName().c_str());
handler.when = when;
tickQueue.insert(pair<int32_t, EventHandler>(handler.when, handler));
tickQueue.emplace(handler.when, handler);
// we don't track this handler, this allows registerTick to retain the old behaviour of needing to re-register the tick event
//handlers[EventType::TICK].insert(pair<Plugin*,EventHandler>(plugin,handler));
// since the event isn't added to the handlers, we don't need to unregister these events
Expand Down
4 changes: 2 additions & 2 deletions plugins/devel/eventExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ command_result eventExample(color_ostream& out, vector<string>& parameters) {
EventManager::registerTick(timeHandler, 4, plugin_self);
EventManager::registerTick(timeHandler, 8, plugin_self);
int32_t t = EventManager::registerTick(timeHandler, 16, plugin_self);
timeHandler.freq = t;
timeHandler.when = t;
EventManager::unregister(EventManager::EventType::TICK, timeHandler, plugin_self);
t = EventManager::registerTick(timeHandler, 32, plugin_self);
t = EventManager::registerTick(timeHandler, 32, plugin_self);
t = EventManager::registerTick(timeHandler, 32, plugin_self);
timeHandler.freq = t;
timeHandler.when = t;
EventManager::unregister(EventManager::EventType::TICK, timeHandler, plugin_self);
EventManager::unregister(EventManager::EventType::TICK, timeHandler, plugin_self);

Expand Down

0 comments on commit e2bc986

Please sign in to comment.