openMSX
MSXEventDistributor.cc
Go to the documentation of this file.
1 #include "MSXEventDistributor.hh"
2 #include "MSXEventListener.hh"
3 #include "stl.hh"
4 #include <cassert>
5 
6 namespace openmsx {
7 
9 {
10  assert(listeners.empty());
11 }
12 
13 bool MSXEventDistributor::isRegistered(MSXEventListener* listener) const
14 {
15  return contains(listeners, listener);
16 }
17 
19 {
20  assert(!isRegistered(&listener));
21  listeners.push_back(&listener);
22 }
23 
25 {
26  move_pop_back(listeners, rfind_unguarded(listeners, &listener));
27 }
28 
29 void MSXEventDistributor::distributeEvent(const EventPtr& event, EmuTime::param time)
30 {
31  // Iterate over a copy because signalMSXEvent() may indirect call back into
32  // registerEventListener().
33  // e.g. signalMSXEvent() -> .. -> PlugCmd::execute() -> .. ->
34  // Connector::plug() -> .. -> Joystick::plugHelper() ->
35  // registerEventListener()
36  auto copy = listeners;
37  for (auto& l : copy) {
38  if (isRegistered(l)) {
39  // it's possible the listener unregistered itself
40  // (but is still present in the copy)
41  l->signalMSXEvent(event, time);
42  }
43  }
44 }
45 
46 } // namespace openmsx
auto copy(InputRange &&range, OutputIter out)
Definition: ranges.hh:149
std::shared_ptr< const Event > EventPtr
bool contains(ITER first, ITER last, const VAL &val)
Check if a range contains a given value, using linear search.
Definition: stl.hh:92
void registerEventListener(MSXEventListener &listener)
Registers a given object to receive certain events.
void move_pop_back(VECTOR &v, typename VECTOR::iterator it)
Erase the pointed to element from the given vector.
Definition: stl.hh:177
void distributeEvent(const EventPtr &event, EmuTime::param time)
Deliver the event to all registered listeners.
void unregisterEventListener(MSXEventListener &listener)
Unregisters a previously registered event listener.
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
auto rfind_unguarded(RANGE &range, const VAL &val)
Similar to the find(_if)_unguarded functions above, but searches from the back to front...
Definition: stl.hh:152