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 Event& 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  // 'listenersCopy' could be a local variable. But making it a member
37  // variable allows to reuse the allocated vector-capacity.
38  listenersCopy = listeners;
39  for (auto& l : listenersCopy) {
40  if (isRegistered(l)) {
41  // it's possible the listener unregistered itself
42  // (but is still present in the copy)
43  l->signalMSXEvent(event, time);
44  }
45  }
46 }
47 
48 } // namespace openmsx
void registerEventListener(MSXEventListener &listener)
Registers a given object to receive certain events.
void distributeEvent(const Event &event, EmuTime::param time)
Deliver the event to all registered listeners.
void unregisterEventListener(MSXEventListener &listener)
Unregisters a previously registered event listener.
This file implemented 3 utility functions:
Definition: Autofire.cc:9
void move_pop_back(VECTOR &v, typename VECTOR::iterator it)
Erase the pointed to element from the given vector.
Definition: stl.hh:134
auto rfind_unguarded(RANGE &range, const VAL &val, Proj proj={})
Similar to the find(_if)_unguarded functions above, but searches from the back to front.
Definition: stl.hh:109
constexpr bool contains(ITER first, ITER last, const VAL &val)
Check if a range contains a given value, using linear search.
Definition: stl.hh:32