]> git.mxchange.org Git - flightgear.git/blobdiff - src/Systems/system_mgr.cxx
Merge branch 'timoore/ptrfix'
[flightgear.git] / src / Systems / system_mgr.cxx
index 8deb5a763c39f994cdab06846e5339b8191d212b..4936ea63818fec9a43fe5ceaac56d9fb75ff18ed 100644 (file)
@@ -3,6 +3,22 @@
 //
 // This file is in the Public Domain and comes with no warranty.
 
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <simgear/structure/exception.hxx>
+#include <simgear/misc/sg_path.hxx>
+#include <simgear/sg_inlines.h>
+#include <simgear/props/props_io.hxx>
+
+#include <Main/fg_props.hxx>
+#include <Main/globals.hxx>
+#include <Main/util.hxx>
+
+#include <iostream>
+#include <string>
+#include <sstream>
 
 #include "system_mgr.hxx"
 #include "electrical.hxx"
 
 FGSystemMgr::FGSystemMgr ()
 {
-    // NO-OP
-}
+    SGPropertyNode_ptr config_props = new SGPropertyNode;
 
-FGSystemMgr::~FGSystemMgr ()
-{
-    for (unsigned int i = 0; i < _systems.size(); i++) {
-        delete _systems[i];
-        _systems[i] = 0;
+    SGPropertyNode *path_n = fgGetNode("/sim/systems/path");
+
+    if (path_n) {
+        SGPath config( globals->get_fg_root() );
+        config.append( path_n->getStringValue() );
+
+        SG_LOG( SG_ALL, SG_INFO, "Reading systems from "
+                << config.str() );
+        try {
+            readProperties( config.str(), config_props );
+
+            if ( build(config_props) ) {
+                enabled = true;
+            } else {
+                SG_LOG( SG_ALL, SG_ALERT,
+                        "Detected an internal inconsistency in the systems");
+                SG_LOG( SG_ALL, SG_ALERT,
+                        " system specification file.  See earlier errors for" );
+                SG_LOG( SG_ALL, SG_ALERT,
+                        " details.");
+                exit(-1);
+            }        
+        } catch (const sg_exception&) {
+            SG_LOG( SG_ALL, SG_ALERT, "Failed to load systems system model: "
+                    << config.str() );
+        }
+
+    } else {
+        SG_LOG( SG_ALL, SG_WARN,
+                "No systems model specified for this model!");
     }
-}
 
-void
-FGSystemMgr::init ()
-{
-                                // TODO: replace with XML configuration
-    _systems.push_back(new FGElectricalSystem);
-    _systems.push_back(new PitotSystem);
-    _systems.push_back(new StaticSystem);
-    _systems.push_back(new VacuumSystem);
-
-                                // Initialize the individual systems
-    for (unsigned int i = 0; i < _systems.size(); i++)
-        _systems[i]->init();
 }
 
-void
-FGSystemMgr::bind ()
+FGSystemMgr::~FGSystemMgr ()
 {
-    // NO-OP
 }
 
-void
-FGSystemMgr::unbind ()
+bool FGSystemMgr::build (SGPropertyNode* config_props)
 {
-    // NO-OP
-}
+    SGPropertyNode *node;
+    int i;
 
-void
-FGSystemMgr::update (double dt)
-{
-    for (unsigned int i = 0; i < _systems.size(); i++)
-        _systems[i]->update(dt);
+    int count = config_props->nChildren();
+    for ( i = 0; i < count; ++i ) {
+        node = config_props->getChild(i);
+        string name = node->getName();
+        std::ostringstream temp;
+        temp << i;
+        if ( name == "electrical" ) {
+            set_subsystem( "electrical" + temp.str(),
+                           new FGElectricalSystem( node ) );
+        } else if ( name == "pitot" ) {
+            set_subsystem( "system" + temp.str(), 
+                           new PitotSystem( node ) );
+        } else if ( name == "static" ) {
+            set_subsystem( "system" + temp.str(), 
+                           new StaticSystem( node ) );
+        } else if ( name == "vacuum" ) {
+            set_subsystem( "system" + temp.str(), 
+                           new VacuumSystem( node ) );
+        } else {
+            SG_LOG( SG_ALL, SG_ALERT, "Unknown top level section: " 
+                    << name );
+            return false;
+        }
+    }
+    return true;
 }
 
 // end of system_manager.cxx