From: James Turner Date: Thu, 10 Dec 2015 21:05:54 +0000 (-0600) Subject: Templated subsystem handling X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d7a680e8488e2bd5bad1cc63f42eaebca9c14986;p=flightgear.git Templated subsystem handling - remove explicit FGControls var from globals, as part of work towards unit-testing infrastructure. --- diff --git a/src/Aircraft/controls.cxx b/src/Aircraft/controls.cxx index 879662bc2..e49b6e55a 100644 --- a/src/Aircraft/controls.cxx +++ b/src/Aircraft/controls.cxx @@ -78,7 +78,6 @@ FGControls::FGControls() : } reset_all(); - globals->set_controls( this ); } @@ -162,9 +161,8 @@ void FGControls::reset_all() // Destructor -FGControls::~FGControls() { - if (globals) - globals->set_controls( NULL ); +FGControls::~FGControls() +{ } diff --git a/src/Aircraft/controls.hxx b/src/Aircraft/controls.hxx index 980233e5a..aefe94b8d 100644 --- a/src/Aircraft/controls.hxx +++ b/src/Aircraft/controls.hxx @@ -639,6 +639,7 @@ public: // controls/autoflight/autopilot[n]/ void set_autopilot_engage( int ap, bool val ); + static const char* subsystemName() { return "controls"; } private: inline void do_autocoordination() { // check for autocoordination diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index 56111fbc9..0b58c32cb 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -849,7 +849,7 @@ void fgCreateSubsystems(bool duringReset) { // Initialize the controls subsystem. //////////////////////////////////////////////////////////////////// - globals->add_subsystem("controls", new FGControls, SGSubsystemMgr::GENERAL); + globals->add_new_subsystem(SGSubsystemMgr::GENERAL); //////////////////////////////////////////////////////////////////// // Initialize the input subsystem. diff --git a/src/Main/globals.cxx b/src/Main/globals.cxx index d1ec0b47c..22d5eff46 100644 --- a/src/Main/globals.cxx +++ b/src/Main/globals.cxx @@ -160,7 +160,6 @@ FGGlobals::FGGlobals() : time_params( NULL ), ephem( NULL ), route_mgr( NULL ), - controls( NULL ), viewmgr( NULL ), commands( SGCommandMgr::instance() ), channel_options_list( NULL ), @@ -509,7 +508,7 @@ FGGlobals::get_subsystem_mgr () const } SGSubsystem * -FGGlobals::get_subsystem (const char * name) +FGGlobals::get_subsystem (const char * name) const { if (!subsystem_mgr) { return NULL; @@ -751,6 +750,11 @@ void FGGlobals::set_matlib( SGMaterialLib *m ) matlib = m; } +FGControls *FGGlobals::get_controls() const +{ + return get_subsystem(); +} + FGSampleQueue* FGGlobals::get_chatter_queue() const { return _chatter_queue; diff --git a/src/Main/globals.hxx b/src/Main/globals.hxx index f5a3f20d5..889fa40aa 100644 --- a/src/Main/globals.hxx +++ b/src/Main/globals.hxx @@ -123,9 +123,6 @@ private: // Global autopilot "route" FGRouteMgr *route_mgr; - // control input state - FGControls *controls; - // viewer manager FGViewMgr *viewmgr; @@ -180,19 +177,36 @@ public: virtual FGRenderer *get_renderer () const; void set_renderer(FGRenderer* render); - virtual SGSubsystemMgr *get_subsystem_mgr () const; + SGSubsystemMgr *get_subsystem_mgr () const; + + SGSubsystem *get_subsystem (const char * name) const; - virtual SGSubsystem *get_subsystem (const char * name); + template + T* get_subsystem() const + { + return dynamic_cast(get_subsystem(T::subsystemName())); + } - virtual void add_subsystem (const char * name, + + void add_subsystem (const char * name, SGSubsystem * subsystem, SGSubsystemMgr::GroupType type = SGSubsystemMgr::GENERAL, double min_time_sec = 0); - virtual SGEventMgr *get_event_mgr () const; + template + T* add_new_subsystem (SGSubsystemMgr::GroupType + type = SGSubsystemMgr::GENERAL, + double min_time_sec = 0) + { + T* sub = new T; + add_subsystem(T::subsystemName(), sub, type, min_time_sec); + return sub; + } + + SGEventMgr *get_event_mgr () const; - virtual SGSoundMgr *get_soundmgr () const; + SGSoundMgr *get_soundmgr () const; inline double get_sim_time_sec () const { return sim_time_sec; } inline void inc_sim_time_sec (double dt) { sim_time_sec += dt; } @@ -293,9 +307,6 @@ public: inline SGMaterialLib *get_matlib() const { return matlib; } void set_matlib( SGMaterialLib *m ); - inline FGControls *get_controls() const { return controls; } - inline void set_controls( FGControls *c ) { controls = c; } - inline FGViewMgr *get_viewmgr() const { return viewmgr; } inline void set_viewmgr( FGViewMgr *vm ) { viewmgr = vm; } FGViewer *get_current_view() const; @@ -337,6 +348,8 @@ public: initial_waypoints = list; } + FGControls *get_controls() const; + FGScenery * get_scenery () const; void set_scenery ( FGScenery *s );