X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FAutopilot%2Fautopilotgroup.cxx;h=c9df860d220a58da2a65b0442eae59204c5e94ad;hb=ff91fec1bb4b59dc2a7084de2a5ab0469abb1f3e;hp=022b83d0c99e282b7d0a0c6bee05d17c8d516307;hpb=52ac173257bf9cc780f0faee5049314442083140;p=flightgear.git diff --git a/src/Autopilot/autopilotgroup.cxx b/src/Autopilot/autopilotgroup.cxx index 022b83d0c..c9df860d2 100644 --- a/src/Autopilot/autopilotgroup.cxx +++ b/src/Autopilot/autopilotgroup.cxx @@ -31,25 +31,59 @@ #include #include +#include #include #include #include
+#include - +using std::vector; +using std::string; using simgear::PropertyList; class FGXMLAutopilotGroupImplementation : public FGXMLAutopilotGroup { public: + FGXMLAutopilotGroupImplementation(const std::string& nodeName) : + FGXMLAutopilotGroup(), _nodeName(nodeName) {} + virtual void addAutopilot( const std::string & name, SGPropertyNode_ptr apNode, SGPropertyNode_ptr config ); + virtual void removeAutopilot( const std::string & name ); void init(); void reinit(); void update( double dt ); private: void initFrom( SGPropertyNode_ptr rootNode, const char * childName ); vector _autopilotNames; + std::string _nodeName; }; +void FGXMLAutopilotGroupImplementation::addAutopilot( const std::string & name, SGPropertyNode_ptr apNode, SGPropertyNode_ptr config ) +{ + BOOST_FOREACH( std::string & n, _autopilotNames ) { + if( n == name ) { + SG_LOG(SG_ALL, SG_ALERT, "NOT adding duplicate property rule name " << name ); + return; + } + } + FGXMLAutopilot::Autopilot * ap = new FGXMLAutopilot::Autopilot( apNode, config ); + ap->set_name( name ); + + double updateInterval = config->getDoubleValue( "update-interval-secs", 0.0 ); + set_subsystem( name, ap, updateInterval ); + _autopilotNames.push_back( name ); +} + +void FGXMLAutopilotGroupImplementation::removeAutopilot( const std::string & name ) +{ + FGXMLAutopilot::Autopilot * ap = (FGXMLAutopilot::Autopilot*)get_subsystem( name ); + if( ap == NULL ) return; // ? + remove_subsystem( name ); + delete ap; +} + + + void FGXMLAutopilotGroupImplementation::update( double dt ) { // update all configured autopilots @@ -61,10 +95,7 @@ void FGXMLAutopilotGroupImplementation::reinit() SGSubsystemGroup::unbind(); for( vector::size_type i = 0; i < _autopilotNames.size(); i++ ) { - FGXMLAutopilot::Autopilot * ap = (FGXMLAutopilot::Autopilot*)get_subsystem( _autopilotNames[i] ); - if( ap == NULL ) continue; // ? - remove_subsystem( _autopilotNames[i] ); - delete ap; + removeAutopilot( _autopilotNames[i] ); } _autopilotNames.clear(); init(); @@ -72,12 +103,10 @@ void FGXMLAutopilotGroupImplementation::reinit() void FGXMLAutopilotGroupImplementation::init() { - static const char * nodeNames[] = { - "autopilot", - "property-rule" - }; - for( unsigned i = 0; i < sizeof(nodeNames)/sizeof(nodeNames[0]); i++ ) - initFrom( fgGetNode( "/sim/systems" ), nodeNames[i] ); + initFrom( fgGetNode( "/sim/systems" ), _nodeName.c_str() ); + + SGSubsystemGroup::bind(); + SGSubsystemGroup::init(); } void FGXMLAutopilotGroupImplementation::initFrom( SGPropertyNode_ptr rootNode, const char * childName ) @@ -85,21 +114,20 @@ void FGXMLAutopilotGroupImplementation::initFrom( SGPropertyNode_ptr rootNode, c if( rootNode == NULL ) return; - PropertyList autopilotNodes = rootNode->getChildren(childName); - for( PropertyList::size_type i = 0; i < autopilotNodes.size(); i++ ) { - SGPropertyNode_ptr pathNode = autopilotNodes[i]->getNode( "path" ); + BOOST_FOREACH( SGPropertyNode_ptr autopilotNode, rootNode->getChildren(childName) ) { + SGPropertyNode_ptr pathNode = autopilotNode->getNode( "path" ); if( pathNode == NULL ) { SG_LOG( SG_ALL, SG_WARN, "No configuration file specified for this property-rule!"); continue; } string apName; - SGPropertyNode_ptr nameNode = autopilotNodes[i]->getNode( "name" ); + SGPropertyNode_ptr nameNode = autopilotNode->getNode( "name" ); if( nameNode != NULL ) { apName = nameNode->getStringValue(); } else { std::ostringstream buf; - buf << "unnamed_autopilot_" << i; + buf << "unnamed_autopilot_" << autopilotNode->getIndex(); apName = buf.str(); } @@ -107,40 +135,43 @@ void FGXMLAutopilotGroupImplementation::initFrom( SGPropertyNode_ptr rootNode, c // check for duplicate names string name = apName; for( unsigned i = 0; get_subsystem( apName.c_str() ) != NULL; i++ ) { - ostringstream buf; - buf << apName << "_" << i; + std::ostringstream buf; + buf << name << "_" << i; apName = buf.str(); } if( apName != name ) - SG_LOG( SG_ALL, SG_ALERT, "Duplicate property-rule configuration name " << name << ", renamed to " << apName ); + SG_LOG( SG_ALL, SG_WARN, "Duplicate property-rule configuration name " << name << ", renamed to " << apName ); } - SGPath config = globals->resolve_aircraft_path(pathNode->getStringValue()); + addAutopilotFromFile( apName, autopilotNode, pathNode->getStringValue() ); + } +} - SG_LOG( SG_ALL, SG_INFO, "Reading property-rule configuration from " << config.str() ); +void FGXMLAutopilotGroup::addAutopilotFromFile( const std::string & name, SGPropertyNode_ptr apNode, const char * path ) +{ + SGPath config = globals->resolve_maybe_aircraft_path(path); + if (config.isNull()) + { + SG_LOG( SG_ALL, SG_ALERT, "Cannot find property-rule configuration file '" << path << "'." ); + return; + } + SG_LOG( SG_ALL, SG_INFO, "Reading property-rule configuration from " << config.str() ); - try { - SGPropertyNode_ptr root = new SGPropertyNode(); - readProperties( config.str(), root ); + try { + SGPropertyNode_ptr configNode = new SGPropertyNode(); + readProperties( config.str(), configNode ); - SG_LOG( SG_AUTOPILOT, SG_INFO, "adding property-rule subsystem " << apName ); - FGXMLAutopilot::Autopilot * ap = new FGXMLAutopilot::Autopilot( autopilotNodes[i], root ); - ap->set_name( apName ); - set_subsystem( apName, ap ); - _autopilotNames.push_back( apName ); + SG_LOG( SG_AUTOPILOT, SG_INFO, "adding property-rule subsystem " << name ); + addAutopilot( name, apNode, configNode ); - } catch (const sg_exception& e) { - SG_LOG( SG_AUTOPILOT, SG_ALERT, "Failed to load property-rule configuration: " - << config.str() << ":" << e.getMessage() ); - continue; - } + } catch (const sg_exception& e) { + SG_LOG( SG_AUTOPILOT, SG_ALERT, "Failed to load property-rule configuration: " + << config.str() << ":" << e.getMessage() ); + return; } - - SGSubsystemGroup::bind(); - SGSubsystemGroup::init(); } -FGXMLAutopilotGroup * FGXMLAutopilotGroup::createInstance() +FGXMLAutopilotGroup * FGXMLAutopilotGroup::createInstance(const std::string& nodeName) { - return new FGXMLAutopilotGroupImplementation(); + return new FGXMLAutopilotGroupImplementation(nodeName); }