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 Interpreter;
24 class Display;
25 class Mixer;
26 class InputEventGenerator;
27 class DiskFactory;
28 class DiskManipulator;
29 class DiskChanger;
30 class FilePool;
31 class UserSettings;
32 class RomDatabase;
33 class TclCallbackMessages;
34 class MSXMotherBoard;
35 class Setting;
36 class CommandLineParser;
37 class AfterCommand;
38 class QuitCommand;
39 class MessageCommand;
40 class MachineCommand;
41 class TestMachineCommand;
42 class CreateMachineCommand;
43 class DeleteMachineCommand;
44 class ListMachinesCommand;
45 class ActivateMachineCommand;
46 class StoreMachineCommand;
47 class RestoreMachineCommand;
48 class AviRecorder;
49 class ConfigInfo;
50 class RealTimeInfo;
51 class PollEventGenerator;
52 template <typename T> class EnumSetting;
53 
62 class Reactor : private Observer<Setting>, private EventListener,
63  private noncopyable
64 {
65 
66 public:
67  Reactor();
68  void init();
69  ~Reactor();
70 
74  void run(CommandLineParser& parser);
75 
76  void enterMainLoop();
77  void pollNow();
78 
84  Mixer& getMixer();
90 
91  void switchMachine(const std::string& machine);
93 
94  static std::vector<std::string> getHwConfigs(string_ref type);
95 
96  void block();
97  void unblock();
98 
99  // convenience methods
103  CliComm& getCliComm();
105  std::string getMachineID() const;
106 
107  typedef std::unique_ptr<MSXMotherBoard> Board;
109  void replaceBoard(MSXMotherBoard& oldBoard, Board newBoard); // for reverse
110 
111 private:
112  typedef std::vector<Board> Boards;
113 
114  void createMachineSetting();
115  void switchBoard(MSXMotherBoard* newBoard);
116  void deleteBoard(MSXMotherBoard* board);
117  MSXMotherBoard& getMachine(string_ref machineID) const;
118  std::vector<string_ref> getMachineIDs() const;
119 
120  // Observer<Setting>
121  virtual void update(const Setting& setting);
122 
123  // EventListener
124  virtual int signalEvent(const std::shared_ptr<const Event>& event);
125 
126  void unpause();
127  void pause();
128 
129  Semaphore mbSem; // this should come first, because it's still used by
130  // the destructors of the unique_ptr below
131 
132  // note: order of unique_ptr's is important
133  std::unique_ptr<EventDistributor> eventDistributor;
134  std::unique_ptr<GlobalCliComm> globalCliComm;
135  std::unique_ptr<GlobalCommandController> globalCommandController;
136  std::unique_ptr<GlobalSettings> globalSettings;
137  std::unique_ptr<InputEventGenerator> inputEventGenerator;
138 #if UNIQUE_PTR_BUG // see openmsx.hh
139  std::unique_ptr<Display> display2;
140  Display* display;
141 #else
142  std::unique_ptr<Display> display;
143 #endif
144  std::unique_ptr<Mixer> mixer;
145  std::unique_ptr<DiskFactory> diskFactory;
146  std::unique_ptr<DiskManipulator> diskManipulator;
147  std::unique_ptr<DiskChanger> virtualDrive;
148  std::unique_ptr<FilePool> filePool;
149 
150  std::unique_ptr<EnumSetting<int>> machineSetting;
151  std::unique_ptr<UserSettings> userSettings;
152  std::unique_ptr<RomDatabase> softwareDatabase;
153 
154  std::unique_ptr<AfterCommand> afterCommand;
155  std::unique_ptr<QuitCommand> quitCommand;
156  std::unique_ptr<MessageCommand> messageCommand;
157  std::unique_ptr<MachineCommand> machineCommand;
158  std::unique_ptr<TestMachineCommand> testMachineCommand;
159  std::unique_ptr<CreateMachineCommand> createMachineCommand;
160  std::unique_ptr<DeleteMachineCommand> deleteMachineCommand;
161  std::unique_ptr<ListMachinesCommand> listMachinesCommand;
162  std::unique_ptr<ActivateMachineCommand> activateMachineCommand;
163  std::unique_ptr<StoreMachineCommand> storeMachineCommand;
164  std::unique_ptr<RestoreMachineCommand> restoreMachineCommand;
165  std::unique_ptr<AviRecorder> aviRecordCommand;
166  std::unique_ptr<ConfigInfo> extensionInfo;
167  std::unique_ptr<ConfigInfo> machineInfo;
168  std::unique_ptr<RealTimeInfo> realTimeInfo;
169  std::unique_ptr<TclCallbackMessages> tclCallbackMessages;
170  std::unique_ptr<PollEventGenerator> pollEventGenerator;
171 
172  // Locking rules for activeBoard access:
173  // - main thread can always access activeBoard without taking a lock
174  // - changing activeBoard handle can only be done in the main thread
175  // and needs to take the mbSem lock
176  // - non-main thread can only access activeBoard via specific
177  // member functions (atm only via enterMainLoop()), it needs to take
178  // the mbSem lock
179  Boards boards;
180  Boards garbageBoards;
181  MSXMotherBoard* activeBoard; // either nullptr or a board inside 'boards'
182 
183  int blockedCounter;
184  bool paused;
185 
191  bool running;
192 
193  bool isInit; // has the init() method been run successfully
194 
195  friend class MachineCommand;
196  friend class TestMachineCommand;
197  friend class CreateMachineCommand;
198  friend class DeleteMachineCommand;
199  friend class ListMachinesCommand;
201  friend class StoreMachineCommand;
202  friend class RestoreMachineCommand;
203 };
204 
205 } // namespace openmsx
206 
207 #endif // REACTOR_HH
Contains the main loop of openMSX.
Definition: Reactor.hh:62
Mixer & getMixer()
Definition: Reactor.cc:319
Represents the output window/screen of openMSX.
Definition: Display.hh:33
static std::vector< std::string > getHwConfigs(string_ref type)
Definition: Reactor.cc:359
void replaceBoard(MSXMotherBoard &oldBoard, Board newBoard)
Definition: Reactor.cc:437
DiskManipulator & getDiskManipulator()
Definition: Reactor.cc:334
EnumSetting< int > & getMachineSetting()
Definition: Reactor.cc:339
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:349
GlobalCliComm & getGlobalCliComm()
Definition: Reactor.cc:288
GlobalCommandController & getGlobalCommandController()
Definition: Reactor.cc:283
GlobalSettings & getGlobalSettings()
Definition: Reactor.cc:344
RomDatabase & getSoftwareDatabase()
Definition: Reactor.cc:314
void run(CommandLineParser &parser)
Main loop.
Definition: Reactor.cc:571
InputEventGenerator & getInputEventGenerator()
Definition: Reactor.cc:303
DiskFactory & getDiskFactory()
Definition: Reactor.cc:324
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:402
FilePool & getFilePool()
Definition: Reactor.cc:329
std::unique_ptr< MSXMotherBoard > Board
Definition: Reactor.hh:107
void pollNow()
Definition: Reactor.cc:566
InfoCommand & getOpenMSXInfoCommand()
Definition: Reactor.cc:354
std::string getMachineID() const
Definition: Reactor.cc:408
void unblock()
Definition: Reactor.cc:654
void enterMainLoop()
Definition: Reactor.cc:550
CliComm & getCliComm()
Definition: Reactor.cc:293
Board createEmptyMotherBoard()
Definition: Reactor.cc:432
void switchMachine(const std::string &machine)
Definition: Reactor.cc:464
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:308
Interpreter & getInterpreter()
Definition: Reactor.cc:298