| 1 |
// OSRail -- a network enabled railroad operations simulator and utilities |
|---|
| 2 |
// Copyright (C) 2007,2008 Samuel E. Henley sehenley@comcast.net |
|---|
| 3 |
// |
|---|
| 4 |
// This program is free software; you can redistribute it and/or modify |
|---|
| 5 |
// it under the terms of the GNU General Public License as published by |
|---|
| 6 |
// the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 |
// (at your option) any later version. |
|---|
| 8 |
// |
|---|
| 9 |
// This program is distributed in the hope that it will be useful, |
|---|
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 |
// GNU General Public License for more details. |
|---|
| 13 |
// |
|---|
| 14 |
// You should have received a copy of the GNU General Public License along |
|---|
| 15 |
// with this program; if not, write to the Free Software Foundation, Inc., |
|---|
| 16 |
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|---|
| 17 |
// |
|---|
| 18 |
// |
|---|
| 19 |
// Build System |
|---|
| 20 |
#ifndef SYSTEM_H |
|---|
| 21 |
#define SYSTEM_H |
|---|
| 22 |
/// \file |
|---|
| 23 |
/// Header file for System interface |
|---|
| 24 |
|
|---|
| 25 |
#ifndef WIN32 |
|---|
| 26 |
#include <signal.h> |
|---|
| 27 |
#endif //WIN32 |
|---|
| 28 |
|
|---|
| 29 |
/// \addtogroup simulator "Simulator" |
|---|
| 30 |
/// @{ |
|---|
| 31 |
/// \addtogroup system "System" |
|---|
| 32 |
/// @{ |
|---|
| 33 |
|
|---|
| 34 |
/// System is the simulation main loop. |
|---|
| 35 |
/// This is an implementation of the ISystem abstract class |
|---|
| 36 |
class System : public ISystem |
|---|
| 37 |
{ |
|---|
| 38 |
public: |
|---|
| 39 |
/// Simulation state mechine. |
|---|
| 40 |
/// States my be added - insure that |
|---|
| 41 |
/// there is a one to one mapping of the |
|---|
| 42 |
/// dispatch_table and the initialization |
|---|
| 43 |
/// code in System::startup. |
|---|
| 44 |
enum eSystemState |
|---|
| 45 |
{ |
|---|
| 46 |
NONE = 0, |
|---|
| 47 |
STARTUP, |
|---|
| 48 |
GUI, |
|---|
| 49 |
ROUTESTARTUP, |
|---|
| 50 |
ROUTESHUTDOWN, |
|---|
| 51 |
SIMULATION, |
|---|
| 52 |
SHUTDOWN |
|---|
| 53 |
}; |
|---|
| 54 |
|
|---|
| 55 |
// typedef void (System::*SystemDispatchFunction)(); |
|---|
| 56 |
virtual IAi* getAiInterface() { return static_cast<IAi*>( ai.get() );} |
|---|
| 57 |
virtual IConsoleClient* getConsoleClientInterface() { return static_cast<IConsoleClient*>( console.get());} |
|---|
| 58 |
virtual IFactory* getFactoryInterface() { return static_cast<IFactory*>( factory.get());} |
|---|
| 59 |
virtual IGraphics* getGraphicsInterface() { return static_cast<IGraphics*>( graphics.get());} |
|---|
| 60 |
virtual IGui* getGuiInterface() { return static_cast<IGui*>( gui.get());} |
|---|
| 61 |
virtual IInput* getInputInterface() { return static_cast<IInput*>( input.get());} |
|---|
| 62 |
virtual INetwork* getNetworkInterface() { return static_cast<INetwork*>( network.get());} |
|---|
| 63 |
virtual IPropertyManager* getPropertyManagerInterface() { return static_cast<IPropertyManager*>( propertymanager.get());} |
|---|
| 64 |
virtual ISimulation* getSimulationInterface() { return static_cast<ISimulation*>( simulation.get());} |
|---|
| 65 |
virtual ISound* getSoundInterface() { return static_cast<ISound*>( sound.get());} |
|---|
| 66 |
virtual ISystem* getSystemInterface() { return this;} |
|---|
| 67 |
virtual ITask* getTaskInterface() { return static_cast<ITask*>( task.get());} |
|---|
| 68 |
virtual ITest* getTestInterface() { return static_cast<ITest*>( test.get());} |
|---|
| 69 |
|
|---|
| 70 |
/// Get time from last render |
|---|
| 71 |
/// \return milliseconds from last graphics render |
|---|
| 72 |
virtual unsigned long getFrameMilliseconds() { return getGraphicsInterface()->getFrameMilliseconds(); } |
|---|
| 73 |
|
|---|
| 74 |
private: |
|---|
| 75 |
/// \addtogroup systemstate "System State Machine" |
|---|
| 76 |
/// @{ |
|---|
| 77 |
class SystemState |
|---|
| 78 |
{ |
|---|
| 79 |
public: |
|---|
| 80 |
/// Initialize the state. |
|---|
| 81 |
/// \param system Simulator interface. |
|---|
| 82 |
virtual void startup( System& system ) = 0; |
|---|
| 83 |
/// Uninitialize the state. |
|---|
| 84 |
/// \param system Simulator interface. |
|---|
| 85 |
virtual void shutdown( System& system ) = 0; |
|---|
| 86 |
/// Background frame processing for the state. |
|---|
| 87 |
/// \param system Simulator interface. |
|---|
| 88 |
virtual void background( System& system ) = 0; |
|---|
| 89 |
/// Foreground processing for the state, maybe foreground or background |
|---|
| 90 |
/// \param system Simulator interface. |
|---|
| 91 |
virtual void operator()( System& system ) = 0; |
|---|
| 92 |
}; |
|---|
| 93 |
|
|---|
| 94 |
template<int T> |
|---|
| 95 |
class State : public SystemState |
|---|
| 96 |
{ |
|---|
| 97 |
/// Initialize the state. |
|---|
| 98 |
/// \param system Simulator interface. |
|---|
| 99 |
virtual void startup( System& system ); |
|---|
| 100 |
/// Uninitialize the state. |
|---|
| 101 |
/// \param system Simulator interface. |
|---|
| 102 |
virtual void shutdown( System& system ); |
|---|
| 103 |
/// Background frame processing for the state. |
|---|
| 104 |
/// \param system Simulator interface. |
|---|
| 105 |
virtual void background( System& system ); |
|---|
| 106 |
/// Foreground processing for the state, maybe foreground or background |
|---|
| 107 |
/// \param system Simulator interface. |
|---|
| 108 |
virtual void operator()( System& system ); |
|---|
| 109 |
}; |
|---|
| 110 |
|
|---|
| 111 |
typedef class State<STARTUP> StartupState; |
|---|
| 112 |
typedef class State<GUI> GuiState; |
|---|
| 113 |
typedef class State<ROUTESTARTUP> RouteStartupState; |
|---|
| 114 |
typedef class State<ROUTESHUTDOWN> RouteShutdownState; |
|---|
| 115 |
typedef class State<SIMULATION> SimulationState; |
|---|
| 116 |
typedef class State<SHUTDOWN> ShutdownState; |
|---|
| 117 |
|
|---|
| 118 |
std::vector< boost::shared_ptr<SystemState> > states; |
|---|
| 119 |
/// @} group systemstate |
|---|
| 120 |
public: |
|---|
| 121 |
System(); |
|---|
| 122 |
virtual ~System(); |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
// ISimulatorComponent interfaces |
|---|
| 127 |
/// Initialize the simulator. |
|---|
| 128 |
virtual void startup(); // Initialize |
|---|
| 129 |
/// Per frame call |
|---|
| 130 |
virtual void operator()(); // ISystem Frame |
|---|
| 131 |
/// Shudown the simulator. |
|---|
| 132 |
virtual void shutdown(); // Shutdown |
|---|
| 133 |
/// Set the options from the cammandline. |
|---|
| 134 |
/// \param map - command line options from boost library. |
|---|
| 135 |
virtual void setOptions( const boost::program_options::variables_map& map ); |
|---|
| 136 |
|
|---|
| 137 |
/// Simulator is running (any state, and a window). |
|---|
| 138 |
/// \return true=running |
|---|
| 139 |
virtual bool running() const; |
|---|
| 140 |
|
|---|
| 141 |
/// Simulator is in any state. |
|---|
| 142 |
/// \return true=any state |
|---|
| 143 |
virtual bool anyState() const; |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
private: |
|---|
| 147 |
// std::vector<SystemDispatchFunction> dispatch_table; |
|---|
| 148 |
|
|---|
| 149 |
/// Use base class to reduce coupling |
|---|
| 150 |
boost::shared_ptr<ISimulatorComponent> ai; |
|---|
| 151 |
boost::shared_ptr<ISimulatorComponent> console; |
|---|
| 152 |
boost::shared_ptr<ISimulatorComponent> factory; |
|---|
| 153 |
boost::shared_ptr<ISimulatorComponent> graphics; |
|---|
| 154 |
boost::shared_ptr<ISimulatorComponent> gui; |
|---|
| 155 |
boost::shared_ptr<ISimulatorComponent> input; |
|---|
| 156 |
boost::shared_ptr<ISimulatorComponent> network; |
|---|
| 157 |
boost::shared_ptr<ISimulatorComponent> propertymanager; |
|---|
| 158 |
boost::shared_ptr<ISimulatorComponent> simulation; |
|---|
| 159 |
boost::shared_ptr<ISimulatorComponent> sound; |
|---|
| 160 |
boost::shared_ptr<ISimulatorComponent> task; |
|---|
| 161 |
boost::shared_ptr<ISimulatorComponent> test; |
|---|
| 162 |
boost::shared_ptr<ISimulatorComponent> tweaker; |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
friend class SystemState; |
|---|
| 166 |
friend class State<STARTUP>; |
|---|
| 167 |
friend class State<GUI>; |
|---|
| 168 |
friend class State<ROUTESTARTUP>; |
|---|
| 169 |
friend class State<ROUTESHUTDOWN>; |
|---|
| 170 |
friend class State<SIMULATION>; |
|---|
| 171 |
friend class State<SHUTDOWN>; |
|---|
| 172 |
|
|---|
| 173 |
}; |
|---|
| 174 |
|
|---|
| 175 |
/// @} group system |
|---|
| 176 |
/// @} group simulator |
|---|
| 177 |
#endif // SYSTEM_H |
|---|