From 4ae8cd99a1e6cfa0c37da3ebe20247ffaf1f56ef Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Sun, 9 Feb 2014 17:21:11 +0100 Subject: [PATCH] XMLAutopilot: cleanup and remove code already available in SGSubsystemGroup. --- src/Autopilot/autopilotgroup.cxx | 242 ++++++++++++++++++------------- 1 file changed, 144 insertions(+), 98 deletions(-) diff --git a/src/Autopilot/autopilotgroup.cxx b/src/Autopilot/autopilotgroup.cxx index 836f7c4cb..d4e1e0c53 100644 --- a/src/Autopilot/autopilotgroup.cxx +++ b/src/Autopilot/autopilotgroup.cxx @@ -38,146 +38,192 @@ #include using std::vector; -using std::string; using simgear::PropertyList; +using FGXMLAutopilot::Autopilot; -class FGXMLAutopilotGroupImplementation : public FGXMLAutopilotGroup +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 ); + 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(); InitStatus incrementalInit(); void reinit(); - void update( double dt ); -private: + + 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 ) +//------------------------------------------------------------------------------ +void FGXMLAutopilotGroupImplementation::addAutopilot( const std::string& name, + SGPropertyNode_ptr apNode, + SGPropertyNode_ptr config ) { - FGXMLAutopilot::Autopilot * ap = (FGXMLAutopilot::Autopilot*)get_subsystem( name ); - if( ap == NULL ) return; // ? - remove_subsystem( name ); + if( has_subsystem(name) ) + { + SG_LOG( SG_ALL, + SG_ALERT, + "NOT adding duplicate " << _nodeName << " name '" << name << "'"); + return; + } + + Autopilot* ap = new Autopilot(apNode, config); + ap->set_name( name ); + + double updateInterval = config->getDoubleValue("update-interval-secs", 0.0); + set_subsystem( name, ap, updateInterval ); } - - -void FGXMLAutopilotGroupImplementation::update( double dt ) +//------------------------------------------------------------------------------ +void FGXMLAutopilotGroupImplementation::removeAutopilot(const std::string& name) { - // update all configured autopilots - SGSubsystemGroup::update( dt ); + Autopilot* ap = static_cast(get_subsystem(name)); + if( !ap ) + { + SG_LOG( SG_ALL, + SG_ALERT, + "CAN NOT remove unknown " << _nodeName << " '" << name << "'"); + return; + } + + remove_subsystem(name); } +//------------------------------------------------------------------------------ void FGXMLAutopilotGroupImplementation::reinit() { - SGSubsystemGroup::unbind(); + SGSubsystemGroup::unbind(); + clearSubsystems(); - for( vector::size_type i = 0; i < _autopilotNames.size(); i++ ) { - removeAutopilot( _autopilotNames[i] ); - } - _autopilotNames.clear(); - init(); + init(); } +//------------------------------------------------------------------------------ SGSubsystem::InitStatus FGXMLAutopilotGroupImplementation::incrementalInit() { init(); return INIT_DONE; } +//------------------------------------------------------------------------------ void FGXMLAutopilotGroupImplementation::init() { - initFrom( fgGetNode( "/sim/systems" ), _nodeName.c_str() ); + initFrom(fgGetNode("/sim/systems"), _nodeName.c_str()); - SGSubsystemGroup::bind(); - SGSubsystemGroup::init(); + SGSubsystemGroup::bind(); + SGSubsystemGroup::init(); } -void FGXMLAutopilotGroupImplementation::initFrom( SGPropertyNode_ptr rootNode, const char * childName ) +//------------------------------------------------------------------------------ +void FGXMLAutopilotGroupImplementation::initFrom( SGPropertyNode_ptr rootNode, + const char* childName ) { - if( rootNode == NULL ) - return; - - 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 = autopilotNode->getNode( "name" ); - if( nameNode != NULL ) { - apName = nameNode->getStringValue(); - } else { - std::ostringstream buf; - buf << "unnamed_autopilot_" << autopilotNode->getIndex(); - apName = buf.str(); - } - - { - // check for duplicate names - string name = apName; - for( unsigned i = 0; get_subsystem( apName.c_str() ) != NULL; i++ ) { - std::ostringstream buf; - buf << name << "_" << i; - apName = buf.str(); - } - if( apName != name ) - SG_LOG( SG_ALL, SG_WARN, "Duplicate property-rule configuration name " << name << ", renamed to " << apName ); - } - - addAutopilotFromFile( apName, autopilotNode, pathNode->getStringValue() ); + if( !rootNode ) + return; + + BOOST_FOREACH( SGPropertyNode_ptr autopilotNode, + rootNode->getChildren(childName) ) + { + SGPropertyNode_ptr pathNode = autopilotNode->getNode("path"); + if( !pathNode ) + { + SG_LOG + ( + SG_ALL, + SG_WARN, + "No configuration file specified for this " << childName << "!" + ); + continue; } -} -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; + std::string apName; + SGPropertyNode_ptr nameNode = autopilotNode->getNode( "name" ); + if( nameNode != NULL ) { + apName = nameNode->getStringValue(); + } else { + std::ostringstream buf; + buf << "unnamed_autopilot_" << autopilotNode->getIndex(); + apName = buf.str(); } - SG_LOG( SG_ALL, SG_INFO, "Reading property-rule configuration from " << config.str() ); - try { - SGPropertyNode_ptr configNode = new SGPropertyNode(); - readProperties( config.str(), configNode ); + { + // check for duplicate names + std::string name = apName; + for( unsigned i = 0; get_subsystem( apName.c_str() ) != NULL; i++ ) { + std::ostringstream buf; + buf << name << "_" << i; + apName = buf.str(); + } + if( apName != name ) + SG_LOG + ( + SG_ALL, + SG_WARN, + "Duplicate " << childName << " configuration name " << name + << ", renamed to " << apName + ); + } - SG_LOG( SG_AUTOPILOT, SG_INFO, "adding property-rule subsystem " << name ); - addAutopilot( name, apNode, configNode ); + addAutopilotFromFile(apName, autopilotNode, pathNode->getStringValue()); + } +} - } catch (const sg_exception& e) { - SG_LOG( SG_AUTOPILOT, SG_ALERT, "Failed to load property-rule configuration: " - << config.str() << ":" << e.getMessage() ); - return; - } +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 configNode = new SGPropertyNode(); + readProperties( config.str(), configNode ); + + 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() + ); + return; + } } -FGXMLAutopilotGroup * FGXMLAutopilotGroup::createInstance(const std::string& nodeName) +//------------------------------------------------------------------------------ +FGXMLAutopilotGroup* +FGXMLAutopilotGroup::createInstance(const std::string& nodeName) { - return new FGXMLAutopilotGroupImplementation(nodeName); + return new FGXMLAutopilotGroupImplementation(nodeName); } -- 2.39.5