openMSX
Reactor.hh
Go to the documentation of this file.
1 #ifndef REACTOR_HH
2 #define REACTOR_HH
3 
4 #include "Observer.hh"
5 #include "EventListener.hh"
6 #include "Semaphore.hh"
7 #include "noncopyable.hh"
8 #include "string_ref.hh"
9 #include "openmsx.hh"
10 #include <string>
11 #include <memory>
12 #include <vector>
13 
14 namespace openmsx {
15 
16 class EventDistributor;
17 class CommandController;
18 class InfoCommand;
19 class GlobalCliComm;
20 class GlobalCommandController;
21 class GlobalSettings;
22 class CliComm;
23 class Display;
24 class Mixer;
25 class InputEventGenerator;
26 class DiskFactory;
27 class DiskManipulator;
28 class DiskChanger;
29 class FilePool;
30 class UserSettings;
31 class RomDatabase;
32 class TclCallbackMessages;
33 class MSXMotherBoard;
34 class Setting;
35 class CommandLineParser;
36 class AfterCommand;
37 class QuitCommand;
38 class MessageCommand;
39 class MachineCommand;
40 class TestMachineCommand;
41 class CreateMachineCommand;
42 class DeleteMachineCommand;
43 class ListMachinesCommand;
44 class ActivateMachineCommand;
45 class StoreMachineCommand;
46 class RestoreMachineCommand;
47 class AviRecorder;
48 class ConfigInfo;
49 class RealTimeInfo;
50 class PollEventGenerator;
51 template <typename T> class EnumSetting;
52 
61 class Reactor : private Observer<Setting>, private EventListener,
62  private noncopyable
63 {
64 
65 public:
66  Reactor();
67  void init();
68  ~Reactor();
69 
73  void run(CommandLineParser& parser);
74 
75  void enterMainLoop();
76  void pollNow();
77 
83  Mixer& getMixer();
89 
90  void switchMachine(const std::string& machine);
92 
93  static std::vector<std::string> getHwConfigs(string_ref type);
94 
95  void block();
96  void unblock();
97 
98  // convenience methods
102  CliComm& getCliComm();
103  std::string getMachineID() const;
104 
105  typedef std::unique_ptr<MSXMotherBoard> Board;
107  void replaceBoard(MSXMotherBoard& oldBoard, Board newBoard); // for reverse
108 
109 private:
110  typedef std::vector<Board> Boards;
111 
112  void createMachineSetting();
113  void switchBoard(MSXMotherBoard* newBoard);
114  void deleteBoard(MSXMotherBoard* board);
115  MSXMotherBoard& getMachine(const std::string& machineID) const;
116  std::vector<string_ref> getMachineIDs() const;
117 
118  // Observer<Setting>
119  virtual void update(const Setting& setting);
120 
121  // EventListener
122  virtual int signalEvent(const std::shared_ptr<const Event>& event);
123 
124  void unpause();
125  void pause();
126 
127  Semaphore mbSem; // this should come first, because it's still used by
128  // the destructors of the unique_ptr below
129 
130  // note: order of unique_ptr's is important
131  std::unique_ptr<EventDistributor> eventDistributor;
132  std::unique_ptr<GlobalCliComm> globalCliComm;
133  std::unique_ptr<GlobalCommandController> globalCommandController;
134  std::unique_ptr<GlobalSettings> globalSettings;
135  std::unique_ptr<InputEventGenerator> inputEventGenerator;
136 #if UNIQUE_PTR_BUG // see openmsx.hh
137  std::unique_ptr<Display> display2;
138  Display* display;
139 #else
140  std::unique_ptr<Display> display;
141 #endif
142  std::unique_ptr<Mixer> mixer;
143  std::unique_ptr<DiskFactory> diskFactory;
144  std::unique_ptr<DiskManipulator> diskManipulator;
145  std::unique_ptr<DiskChanger> virtualDrive;
146  std::unique_ptr<FilePool> filePool;
147 
148  std::unique_ptr<EnumSetting<int>> machineSetting;
149  std::unique_ptr<UserSettings> userSettings;
150  std::unique_ptr<RomDatabase> softwareDatabase;
151 
152  std::unique_ptr<AfterCommand> afterCommand;
153  std::unique_ptr<QuitCommand> quitCommand;
154  std::unique_ptr<MessageCommand> messageCommand;
155  std::unique_ptr<MachineCommand> machineCommand;
156  std::unique_ptr<TestMachineCommand> testMachineCommand;
157  std::unique_ptr<CreateMachineCommand> createMachineCommand;
158  std::unique_ptr<DeleteMachineCommand> deleteMachineCommand;
159  std::unique_ptr<ListMachinesCommand> listMachinesCommand;
160  std::unique_ptr<ActivateMachineCommand> activateMachineCommand;
161  std::unique_ptr<StoreMachineCommand> storeMachineCommand;
162  std::unique_ptr<RestoreMachineCommand> restoreMachineCommand;
163  std::unique_ptr<AviRecorder> aviRecordCommand;
164  std::unique_ptr<ConfigInfo> extensionInfo;
165  std::unique_ptr<ConfigInfo> machineInfo;
166  std::unique_ptr<RealTimeInfo> realTimeInfo;
167  std::unique_ptr<TclCallbackMessages> tclCallbackMessages;
168  std::unique_ptr<PollEventGenerator> pollEventGenerator;
169 
170  // Locking rules for activeBoard access:
171  // - main thread can always access activeBoard without taking a lock
172  // - changing activeBoard handle can only be done in the main thread
173  // and needs to take the mbSem lock
174  // - non-main thread can only access activeBoard via specific
175  // member functions (atm only via enterMainLoop()), it needs to take
176  // the mbSem lock
177  Boards boards;
178  Boards garbageBoards;
179  MSXMotherBoard* activeBoard; // either nullptr or a board inside 'boards'
180 
181  int blockedCounter;
182  bool paused;
183 
189  bool running;
190 
191  bool isInit; // has the init() method been run successfully
192 
193  friend class MachineCommand;
194  friend class TestMachineCommand;
195  friend class CreateMachineCommand;
196  friend class DeleteMachineCommand;
197  friend class ListMachinesCommand;
199  friend class StoreMachineCommand;
200  friend class RestoreMachineCommand;
201 };
202 
203 } // namespace openmsx
204 
205 #endif // REACTOR_HH
Contains the main loop of openMSX.
Definition: Reactor.hh:61
Mixer & getMixer()
Definition: Reactor.cc:314
Represents the output window/screen of openMSX.
Definition: Display.hh:33
static std::vector< std::string > getHwConfigs(string_ref type)
Definition: Reactor.cc:354
void replaceBoard(MSXMotherBoard &oldBoard, Board newBoard)
Definition: Reactor.cc:432
DiskManipulator & getDiskManipulator()
Definition: Reactor.cc:329
EnumSetting< int > & getMachineSetting()
Definition: Reactor.cc:334
EventDistributor & getEventDistributor()
Definition: Reactor.cc:278
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
CommandController & getCommandController()
Definition: Reactor.cc:344
GlobalCliComm & getGlobalCliComm()
Definition: Reactor.cc:288
GlobalCommandController & getGlobalCommandController()
Definition: Reactor.cc:283
GlobalSettings & getGlobalSettings()
Definition: Reactor.cc:339
RomDatabase & getSoftwareDatabase()
Definition: Reactor.cc:309
void run(CommandLineParser &parser)
Main loop.
Definition: Reactor.cc:569
InputEventGenerator & getInputEventGenerator()
Definition: Reactor.cc:298
DiskFactory & getDiskFactory()
Definition: Reactor.cc:319
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:397
FilePool & getFilePool()
Definition: Reactor.cc:324
std::unique_ptr< MSXMotherBoard > Board
Definition: Reactor.hh:105
void pollNow()
Definition: Reactor.cc:564
InfoCommand & getOpenMSXInfoCommand()
Definition: Reactor.cc:349
std::string getMachineID() const
Definition: Reactor.cc:403
void unblock()
Definition: Reactor.cc:652
void enterMainLoop()
Definition: Reactor.cc:548
CliComm & getCliComm()
Definition: Reactor.cc:293
Board createEmptyMotherBoard()
Definition: Reactor.cc:427
void switchMachine(const std::string &machine)
Definition: Reactor.cc:459
Generic Gang-of-Four Observer class, templatized edition.
Definition: Observer.hh:9
Based on boost::noncopyable, see boost documentation: http://www.boost.org/libs/utility.
Definition: noncopyable.hh:12
This class contains settings that are used by several other class (including some singletons)...
Display & getDisplay()
Definition: Reactor.cc:303