]> git.mxchange.org Git - flightgear.git/blobdiff - src/Autopilot/autopilot.cxx
Merge branch 'merge-requests/1555' into next
[flightgear.git] / src / Autopilot / autopilot.cxx
index d37c38d7db00b1f9b060a566429c37f26c4478b9..be3a4f0d90d02704422a990727591a34e7442383 100644 (file)
 
 #include "Main/fg_props.hxx"
 
+using std::map;
+using std::string;
+
 using namespace FGXMLAutopilot;
 
+class ComponentForge : public map<string,FunctorBase<Component> *> {
+public:
+    virtual ~ ComponentForge();
+};
+
+ComponentForge::~ComponentForge()
+{
+    for( iterator it = begin(); it != end(); ++it )
+        delete it->second;
+}
+
+static ComponentForge componentForge;
+
 Autopilot::Autopilot( SGPropertyNode_ptr rootNode, SGPropertyNode_ptr configNode ) :
   _name("unnamed autopilot"),
   _serviceable(true),
   _rootNode(rootNode)
 {
-  map<string,FunctorBase<Component> *> componentForge;
+
   componentForge["pid-controller"]       = new CreateAndConfigureFunctor<PIDController,Component>();
   componentForge["pi-simple-controller"] = new CreateAndConfigureFunctor<PISimpleController,Component>();
   componentForge["predict-simple"]       = new CreateAndConfigureFunctor<Predictor,Component>();
@@ -58,13 +74,13 @@ Autopilot::Autopilot( SGPropertyNode_ptr rootNode, SGPropertyNode_ptr configNode
     SGPropertyNode_ptr node = configNode->getChild(i);
     string childName = node->getName();
     if( componentForge.count(childName) == 0 ) {
-      SG_LOG( SG_AUTOPILOT, SG_BULK, "unhandled element <" << childName << ">" << endl );
+      SG_LOG( SG_AUTOPILOT, SG_BULK, "unhandled element <" << childName << ">" << std::endl );
       continue;
     }
 
     Component * component = (*componentForge[childName])(node);
     if( component->get_name().length() == 0 ) {
-      ostringstream buf;
+      std::ostringstream buf;
       buf <<  "unnamed_component_" << i;
       component->set_name( buf.str() );
     }
@@ -93,11 +109,16 @@ void Autopilot::add_component( Component * component )
 {
   if( component == NULL ) return;
 
+  // check for duplicate name
   std::string name = component->get_name();
-  if( get_subsystem( name.c_str() ) != NULL ) {
-    SG_LOG( SG_ALL, SG_ALERT, "Duplicate autopilot component " << name << " ignored" );
-    return;
+  for( unsigned i = 0; get_subsystem( name.c_str() ) != NULL; i++ ) {
+      std::ostringstream buf;
+      buf <<  component->get_name() << "_" << i;
+      name = buf.str();
   }
+  if( name != component->get_name() )
+    SG_LOG( SG_ALL, SG_WARN, "Duplicate autopilot component " << component->get_name() << ", renamed to " << name );
+
   set_subsystem( name.c_str(), component );
 }