From 644bb8c4f4dc65f38d32f70ef686a4fd79cfe08d Mon Sep 17 00:00:00 2001 From: Torsten Dreyer Date: Tue, 6 Mar 2012 22:28:18 +0100 Subject: [PATCH] Spring-cleaning: some minor optimization - initialize uninitialized properties - use prefix instead of postfix increments - reduce visibility of variables - use empty() instead of size() == 0 for vector and string - pass string by reference, not by value --- src/Autopilot/flipflop.cxx | 4 ++-- src/Autopilot/logic.cxx | 2 +- src/Autopilot/pidcontroller.cxx | 2 +- src/Autopilot/route_mgr.cxx | 8 +++++++- src/Environment/environment.cxx | 7 +++++++ src/Environment/fgmetar.cxx | 2 +- src/Environment/metarproperties.cxx | 4 ++-- src/Environment/precipitation_mgr.cxx | 3 +-- src/Environment/realwx_ctrl.cxx | 4 ++-- src/Environment/ridge_lift.cxx | 3 ++- src/Input/FGButton.cxx | 2 +- src/Input/FGButton.hxx | 2 +- src/Input/FGEventInput.cxx | 12 ++++++------ src/Input/FGEventInput.hxx | 2 +- src/Input/FGJoystickInput.cxx | 26 ++++++++++++-------------- src/Input/FGKeyboardInput.cxx | 11 ++++++++++- src/Input/FGLinuxEventInput.cxx | 4 ++-- src/Input/FGLinuxEventInput.hxx | 2 +- src/Input/fgjs.cxx | 16 ++++++++-------- 19 files changed, 68 insertions(+), 48 deletions(-) diff --git a/src/Autopilot/flipflop.cxx b/src/Autopilot/flipflop.cxx index 8046ce5dc..b22adfa9a 100644 --- a/src/Autopilot/flipflop.cxx +++ b/src/Autopilot/flipflop.cxx @@ -272,7 +272,7 @@ public: * @brief constructor for a MonoFlopImplementation * @param rIsDominant boolean flag to signal if RESET shall be dominant (true) or SET shall be dominant (false) */ - MonoFlopImplementation( bool rIsDominant = true ) : JKFlipFlopImplementation( rIsDominant ) {} + MonoFlopImplementation( bool rIsDominant = true ) : JKFlipFlopImplementation( rIsDominant ), _t(0.0) {} /** * @brief evaluates the output state from the input lines and returns to the stable state * after expiry of the internal timer @@ -464,7 +464,7 @@ void FlipFlop::update( bool firstTime, double dt ) if(_debug) { cout << "updating flip-flop \"" << get_name() << "\"" << endl; cout << "prev. Output:" << q0 << endl; - for( InputMap::const_iterator it = _input.begin(); it != _input.end(); it++ ) + for( InputMap::const_iterator it = _input.begin(); it != _input.end(); ++it ) cout << "Input \"" << (*it).first << "\":" << (*it).second->test() << endl; cout << "new Output:" << q << endl; } diff --git a/src/Autopilot/logic.cxx b/src/Autopilot/logic.cxx index 83270427d..860af0e80 100644 --- a/src/Autopilot/logic.cxx +++ b/src/Autopilot/logic.cxx @@ -45,7 +45,7 @@ void Logic::set_output( bool value ) if( _inverted ) value = !value; // set all outputs to the given value - for( OutputMap::iterator it = _output.begin(); it != _output.end(); it++ ) + for( OutputMap::iterator it = _output.begin(); it != _output.end(); ++it ) (*it).second->setValue( value ); } diff --git a/src/Autopilot/pidcontroller.cxx b/src/Autopilot/pidcontroller.cxx index c3631a22d..d41608c09 100644 --- a/src/Autopilot/pidcontroller.cxx +++ b/src/Autopilot/pidcontroller.cxx @@ -96,7 +96,6 @@ PIDController::PIDController(): void PIDController::update( bool firstTime, double dt ) { double edf_n = 0.0; - double delta_u_n = 0.0; // incremental output double u_n = 0.0; // absolute output double u_min = _minInput.get_value(); @@ -159,6 +158,7 @@ void PIDController::update( bool firstTime, double dt ) // Calculates the incremental output: double ti = Ti.get_value(); + double delta_u_n = 0.0; // incremental output if ( ti > 0.0 ) { delta_u_n = Kp.get_value() * ( (ep_n - ep_n_1) + ((Ts/ti) * e_n) diff --git a/src/Autopilot/route_mgr.cxx b/src/Autopilot/route_mgr.cxx index 1c4fa52f7..abb266a97 100644 --- a/src/Autopilot/route_mgr.cxx +++ b/src/Autopilot/route_mgr.cxx @@ -258,7 +258,9 @@ static bool commandDeleteWaypt(const SGPropertyNode* arg) FGRouteMgr::FGRouteMgr() : _currentIndex(0), input(fgGetNode( RM "input", true )), - mirror(fgGetNode( RM "route", true )) + mirror(fgGetNode( RM "route", true )), + _departureWatcher(NULL), + _arrivalWatcher(NULL) { listener = new InputListener(this); input->setStringValue(""); @@ -278,6 +280,8 @@ FGRouteMgr::~FGRouteMgr() { input->removeChangeListener(listener); delete listener; + delete _departureWatcher; + delete _arrivalWatcher; } @@ -296,6 +300,7 @@ void FGRouteMgr::init() { &FGRouteMgr::getDepartureName, NULL)); departure->setStringValue("runway", ""); + delete _departureWatcher; _departureWatcher = createWatcher(this, &FGRouteMgr::departureChanged); _departureWatcher->watch(departure->getChild("runway")); @@ -311,6 +316,7 @@ void FGRouteMgr::init() { destination->tie("name", SGRawValueMethods(*this, &FGRouteMgr::getDestinationName, NULL)); + delete _arrivalWatcher; _arrivalWatcher = createWatcher(this, &FGRouteMgr::arrivalChanged); _arrivalWatcher->watch(destination->getChild("runway", 0, true)); diff --git a/src/Environment/environment.cxx b/src/Environment/environment.cxx index e927c3503..95b45852e 100644 --- a/src/Environment/environment.cxx +++ b/src/Environment/environment.cxx @@ -180,6 +180,13 @@ FGEnvironment::copy (const FGEnvironment &env) wind_from_down_fps = env.wind_from_down_fps; turbulence_magnitude_norm = env.turbulence_magnitude_norm; turbulence_rate_hz = env.turbulence_rate_hz; + pressure_inhg = env.pressure_inhg; + density_slugft3 = env.density_slugft3; + density_tropo_avg_kgm3 = env.density_tropo_avg_kgm3; + relative_humidity = env.relative_humidity; + altitude_half_to_sun_m = env.altitude_half_to_sun_m; + altitude_tropo_top_m = env.altitude_tropo_top_m; + live_update = env.live_update; } static inline bool diff --git a/src/Environment/fgmetar.cxx b/src/Environment/fgmetar.cxx index 0e2c34a64..26d33bc7a 100644 --- a/src/Environment/fgmetar.cxx +++ b/src/Environment/fgmetar.cxx @@ -59,7 +59,7 @@ FGMetar::FGMetar(const string& icao) : _min_visibility.set(12000.0); vector cv = _clouds;; - if (!cv.size()) { + if (cv.empty()) { SGMetarCloud cl; cl.set(5500 * SG_FEET_TO_METER, SGMetarCloud::COVERAGE_SCATTERED); _clouds.push_back(cl); diff --git a/src/Environment/metarproperties.cxx b/src/Environment/metarproperties.cxx index 4c93aaa97..aa67b355a 100644 --- a/src/Environment/metarproperties.cxx +++ b/src/Environment/metarproperties.cxx @@ -137,7 +137,7 @@ MetarProperties::MetarProperties( SGPropertyNode_ptr rootNode ) : _magneticVariation(new MagneticVariation()) { // Hack to avoid static initialization order problems on OSX - if( coverage_string.size() == 0 ) { + if( coverage_string.empty() ) { coverage_string.push_back(SGCloudLayer::SG_CLOUD_CLEAR_STRING); coverage_string.push_back(SGCloudLayer::SG_CLOUD_FEW_STRING); coverage_string.push_back(SGCloudLayer::SG_CLOUD_SCATTERED_STRING); @@ -201,7 +201,7 @@ void MetarProperties::set_metar( const char * metar ) _decoded.clear(); const vector weather = m->getWeather(); - for( vector::const_iterator it = weather.begin(); it != weather.end(); it++ ) { + for( vector::const_iterator it = weather.begin(); it != weather.end(); ++it ) { if( false == _decoded.empty() ) _decoded.append(", "); _decoded.append(*it); } diff --git a/src/Environment/precipitation_mgr.cxx b/src/Environment/precipitation_mgr.cxx index a3e91514d..b3dd6e480 100644 --- a/src/Environment/precipitation_mgr.cxx +++ b/src/Environment/precipitation_mgr.cxx @@ -130,7 +130,6 @@ float FGPrecipitationMgr::getPrecipitationAtAltitudeMax(void) { int i; int max; - double elev; float result; SGPropertyNode *boundaryNode, *boundaryEntry; @@ -180,7 +179,7 @@ float FGPrecipitationMgr::getPrecipitationAtAltitudeMax(void) // For each boundary layers while ( ( boundaryEntry = boundaryNode->getNode( "entry", i ) ) != NULL ) { - elev = boundaryEntry->getDoubleValue( "elevation-ft" ); + double elev = boundaryEntry->getDoubleValue( "elevation-ft" ); if (elev > result) result = elev; diff --git a/src/Environment/realwx_ctrl.cxx b/src/Environment/realwx_ctrl.cxx index 08b3e348d..b93316490 100644 --- a/src/Environment/realwx_ctrl.cxx +++ b/src/Environment/realwx_ctrl.cxx @@ -373,8 +373,8 @@ void NoaaMetarRealWxController::requestMetar( MetarDataHandler * metarDataHandle } } - bool fromMetarProxy() const - { return _fromProxy; } +// bool fromMetarProxy() const +// { return _fromProxy; } private: string _metar; bool _fromProxy; diff --git a/src/Environment/ridge_lift.cxx b/src/Environment/ridge_lift.cxx index cac1388aa..2a26af5af 100644 --- a/src/Environment/ridge_lift.cxx +++ b/src/Environment/ridge_lift.cxx @@ -53,7 +53,8 @@ const double FGRidgeLift::dist_probe_m[] = { // in meters }; //constructor -FGRidgeLift::FGRidgeLift () +FGRidgeLift::FGRidgeLift () : + lift_factor(0.0) { strength = 0.0; timer = 0.0; diff --git a/src/Input/FGButton.cxx b/src/Input/FGButton.cxx index 63a679d79..68fcf8e1b 100644 --- a/src/Input/FGButton.cxx +++ b/src/Input/FGButton.cxx @@ -45,7 +45,7 @@ FGButton::~FGButton () } -void FGButton::init( const SGPropertyNode * node, const std::string name, std::string & module ) +void FGButton::init( const SGPropertyNode * node, const std::string & name, std::string & module ) { if (node == 0) { SG_LOG(SG_INPUT, SG_DEBUG, "No bindings for button " << name); diff --git a/src/Input/FGButton.hxx b/src/Input/FGButton.hxx index 675f969d2..20cac858c 100644 --- a/src/Input/FGButton.hxx +++ b/src/Input/FGButton.hxx @@ -32,7 +32,7 @@ class FGButton : public FGCommonInput { public: FGButton(); virtual ~FGButton(); - void init( const SGPropertyNode * node, const std::string name, std::string & module ); + void init( const SGPropertyNode * node, const std::string & name, std::string & module ); void update( int modifiers, bool pressed, int x = -1, int y = -1); bool is_repeatable; float interval_sec; diff --git a/src/Input/FGEventInput.cxx b/src/Input/FGEventInput.cxx index a90d20c88..1717cfa1d 100644 --- a/src/Input/FGEventInput.cxx +++ b/src/Input/FGEventInput.cxx @@ -100,7 +100,7 @@ FGInputEvent::FGInputEvent( FGInputDevice * aDevice, SGPropertyNode_ptr node ) : read_bindings( node, bindings, KEYMOD_NONE, device->GetNasalModule() ); PropertyList settingNodes = node->getChildren("setting"); - for( PropertyList::iterator it = settingNodes.begin(); it != settingNodes.end(); it++ ) + for( PropertyList::iterator it = settingNodes.begin(); it != settingNodes.end(); ++it ) settings.push_back( new FGEventSetting( *it ) ); } @@ -110,7 +110,7 @@ FGInputEvent::~FGInputEvent() void FGInputEvent::update( double dt ) { - for( setting_list_t::iterator it = settings.begin(); it != settings.end(); it++ ) { + for( setting_list_t::iterator it = settings.begin(); it != settings.end(); ++it ) { if( (*it)->Test() ) { double value = (*it)->GetValue(); if( value != lastSettingValue ) { @@ -126,7 +126,7 @@ void FGInputEvent::fire( FGEventData & eventData ) lastDt += eventData.dt; if( lastDt >= intervalSec ) { - for( binding_list_t::iterator it = bindings[eventData.modifiers].begin(); it != bindings[eventData.modifiers].end(); it++ ) + for( binding_list_t::iterator it = bindings[eventData.modifiers].begin(); it != bindings[eventData.modifiers].end(); ++it ) fire( *it, eventData ); lastDt -= intervalSec; @@ -242,7 +242,7 @@ void FGInputDevice::Configure( SGPropertyNode_ptr aDeviceNode ) nasalModule = string("__event:") + GetName(); PropertyList eventNodes = deviceNode->getChildren( "event" ); - for( PropertyList::iterator it = eventNodes.begin(); it != eventNodes.end(); it++ ) + for( PropertyList::iterator it = eventNodes.begin(); it != eventNodes.end(); ++it ) AddHandledEvent( FGInputEvent::NewObject( this, *it ) ); debugEvents = deviceNode->getBoolValue("debug-events", debugEvents ); @@ -298,7 +298,7 @@ FGEventInput::FGEventInput() : FGEventInput::~FGEventInput() { - for( map::iterator it = input_devices.begin(); it != input_devices.end(); it++ ) + for( map::iterator it = input_devices.begin(); it != input_devices.end(); ++it ) delete (*it).second; input_devices.clear(); } @@ -317,7 +317,7 @@ void FGEventInput::postinit () void FGEventInput::update( double dt ) { // call each associated device's update() method - for( map::iterator it = input_devices.begin(); it != input_devices.end(); it++ ) + for( map::iterator it = input_devices.begin(); it != input_devices.end(); ++it ) (*it).second->update( dt ); } diff --git a/src/Input/FGEventInput.hxx b/src/Input/FGEventInput.hxx index 23b4dd4d1..c62a77aae 100644 --- a/src/Input/FGEventInput.hxx +++ b/src/Input/FGEventInput.hxx @@ -187,7 +187,7 @@ typedef class SGSharedPtr FGInputEvent_ptr; class FGInputDevice : public SGReferenced { public: FGInputDevice() : debugEvents(false), grab(false) {} - FGInputDevice( std::string aName ) : name(aName) {} + FGInputDevice( std::string aName ) : name(aName), debugEvents(false), grab(false) {} virtual ~FGInputDevice(); diff --git a/src/Input/FGJoystickInput.cxx b/src/Input/FGJoystickInput.cxx index 83e5f68a7..041fcc2e1 100644 --- a/src/Input/FGJoystickInput.cxx +++ b/src/Input/FGJoystickInput.cxx @@ -32,6 +32,7 @@ #include "FGDeviceConfigurationMap.hxx" #include
#include +#include using simgear::PropertyList; @@ -265,30 +266,27 @@ void FGJoystickInput::postinit() // Initialize the buttons. // PropertyList buttons = js_node->getChildren("button"); - char buf[32]; - for (j = 0; j < buttons.size() && j < nbuttons; j++) { - const SGPropertyNode * button_node = buttons[j]; - const SGPropertyNode * num_node = button_node->getChild("number"); + BOOST_FOREACH( SGPropertyNode * button_node, buttons ) { size_t n_but = button_node->getIndex(); - if (num_node != 0) { + + const SGPropertyNode * num_node = button_node->getChild("number"); + if (NULL != num_node) n_but = num_node->getIntValue(TGT_PLATFORM,n_but); - } if (n_but >= nbuttons) { SG_LOG(SG_INPUT, SG_DEBUG, "Dropping bindings for button " << n_but); continue; } - sprintf(buf, "%u", (unsigned)n_but); - SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << n_but); - bindings[i].buttons[n_but].init(button_node, buf, module ); + std::ostringstream buf; + buf << (unsigned)n_but; - // get interval-sec property + SG_LOG(SG_INPUT, SG_DEBUG, "Initializing button " << buf.str()); FGButton &b = bindings[i].buttons[n_but]; - if (button_node != 0) { - b.interval_sec = button_node->getDoubleValue("interval-sec",0.0); - b.last_dt = 0.0; - } + b.init(button_node, buf.str(), module ); + // get interval-sec property + b.interval_sec = button_node->getDoubleValue("interval-sec",0.0); + b.last_dt = 0.0; } js->setMinRange(minRange); diff --git a/src/Input/FGKeyboardInput.cxx b/src/Input/FGKeyboardInput.cxx index 49426d27a..f5eea9b3f 100644 --- a/src/Input/FGKeyboardInput.cxx +++ b/src/Input/FGKeyboardInput.cxx @@ -71,7 +71,16 @@ static bool getModHyper () FGKeyboardInput * FGKeyboardInput::keyboardInput = NULL; FGKeyboardInput::FGKeyboardInput() : - _key_event(fgGetNode("/devices/status/keyboard/event", true)) + _key_event(fgGetNode("/devices/status/keyboard/event", true)), + _key_code(0), + _key_modifiers(0), + _key_pressed(0), + _key_shift(0), + _key_ctrl(0), + _key_alt(0), + _key_meta(0), + _key_super(0), + _key_hyper(0) { if( keyboardInput == NULL ) keyboardInput = this; diff --git a/src/Input/FGLinuxEventInput.cxx b/src/Input/FGLinuxEventInput.cxx index deff1b88f..43256aadd 100644 --- a/src/Input/FGLinuxEventInput.cxx +++ b/src/Input/FGLinuxEventInput.cxx @@ -42,7 +42,7 @@ struct TypeCode { return (unsigned long)type << 16 | (unsigned long)code; } - bool operator < ( const TypeCode other) const { + bool operator < ( const TypeCode & other) const { return hashCode() < other.hashCode(); } }; @@ -436,7 +436,7 @@ const char * FGLinuxInputDevice::TranslateEventName( FGEventData & eventData ) return EVENT_NAME_BY_TYPE[typeCode]; } -void FGLinuxInputDevice::SetDevname( std::string name ) +void FGLinuxInputDevice::SetDevname( const std::string & name ) { this->devname = name; } diff --git a/src/Input/FGLinuxEventInput.hxx b/src/Input/FGLinuxEventInput.hxx index 5fcd81e24..df151663c 100644 --- a/src/Input/FGLinuxEventInput.hxx +++ b/src/Input/FGLinuxEventInput.hxx @@ -50,7 +50,7 @@ public: virtual void Send( const char * eventName, double value ); virtual const char * TranslateEventName( FGEventData & eventData ); - void SetDevname( const std::string name ); + void SetDevname( const std::string & name ); std::string GetDevname() const { return devname; } int GetFd() { return fd; } diff --git a/src/Input/fgjs.cxx b/src/Input/fgjs.cxx index 9c4e66487..59effd534 100644 --- a/src/Input/fgjs.cxx +++ b/src/Input/fgjs.cxx @@ -137,12 +137,12 @@ int main( int argc, char *argv[] ) { SGPropertyNode *templatetree = new SGPropertyNode(); try { readProperties(templatefile.str().c_str(), templatetree); - } catch (sg_io_exception e) { + } catch (sg_io_exception & e) { cout << e.getFormattedMessage (); } PropertyList axes = templatetree->getChildren("axis"); - for(PropertyList::iterator iter = axes.begin(); iter != axes.end(); iter++) { + for(PropertyList::iterator iter = axes.begin(); iter != axes.end(); ++iter) { cout << "Move the control you wish to use for " << (*iter)->getStringValue("desc") << " " << (*iter)->getStringValue("direction") << endl; cout << "Pressing a button skips this axis" << endl; @@ -159,18 +159,18 @@ int main( int argc, char *argv[] ) { ->getDeadBand(jsi->getInputAxis())); axis->setDoubleValue("binding/factor", jsi->getInputAxisPositive() ? 1.0 : -1.0); } else { - iter--; + --iter; } } else { cout << "Skipping control" << endl; if ( ! confirmAnswer() ) - iter--; + --iter; } cout << endl; } PropertyList buttons = templatetree->getChildren("button"); - for(PropertyList::iterator iter = buttons.begin(); iter != buttons.end(); iter++) { + for(PropertyList::iterator iter = buttons.begin(); iter != buttons.end(); ++iter) { cout << "Press the button you wish to use for " << (*iter)->getStringValue("desc") << endl; cout << "Moving a joystick axis skips this button" << endl; fflush( stdout ); @@ -183,12 +183,12 @@ int main( int argc, char *argv[] ) { SGPropertyNode *button = jstree[ jsi->getInputJoystick() ]->getChild("button", jsi->getInputButton(), true); copyProperties(*iter, button); } else { - iter--; + --iter; } } else { cout << "Skipping control" << endl; if (! confirmAnswer()) - iter--; + --iter; } cout << endl; } @@ -204,7 +204,7 @@ int main( int argc, char *argv[] ) { jstree[i]->setStringValue("name", jss->getJoystick(i)->getName()); writeProperties(xfs[i], jstree[i], true); - } catch (sg_io_exception e) { + } catch (sg_io_exception & e) { cout << e.getFormattedMessage (); } xfs[i].close(); -- 2.39.5