]> git.mxchange.org Git - flightgear.git/commitdiff
Spring-cleaning: some minor optimization
authorTorsten Dreyer <Torsten@t3r.de>
Tue, 6 Mar 2012 21:28:18 +0000 (22:28 +0100)
committerTorsten Dreyer <Torsten@t3r.de>
Tue, 6 Mar 2012 21:28:18 +0000 (22:28 +0100)
- 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

19 files changed:
src/Autopilot/flipflop.cxx
src/Autopilot/logic.cxx
src/Autopilot/pidcontroller.cxx
src/Autopilot/route_mgr.cxx
src/Environment/environment.cxx
src/Environment/fgmetar.cxx
src/Environment/metarproperties.cxx
src/Environment/precipitation_mgr.cxx
src/Environment/realwx_ctrl.cxx
src/Environment/ridge_lift.cxx
src/Input/FGButton.cxx
src/Input/FGButton.hxx
src/Input/FGEventInput.cxx
src/Input/FGEventInput.hxx
src/Input/FGJoystickInput.cxx
src/Input/FGKeyboardInput.cxx
src/Input/FGLinuxEventInput.cxx
src/Input/FGLinuxEventInput.hxx
src/Input/fgjs.cxx

index 8046ce5dcd8ddcc761ac44faf9a562bdc4fb4eba..b22adfa9ae6ab2f90ec9c21a038f0f07ed320a53 100644 (file)
@@ -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;
     }
index 83270427dc2ec233b6971339fcb536b95e894181..860af0e80043f8f5babab3584bfc7f12b7fdcb63 100644 (file)
@@ -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 );
 }
 
index c3631a22dfb28d899ed24e96d6316ef04c7d2d9a..d41608c09cab6ec59bafeef04cd51b096a2bfd43 100644 (file)
@@ -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)
index 1c4fa52f711f38837695e3a7bbdb20936f7e81e4..abb266a9707007d428fe18bfd4f19dc37954561d 100644 (file)
@@ -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<FGRouteMgr, const char*>(*this, 
     &FGRouteMgr::getDestinationName, NULL));
   
+  delete _arrivalWatcher;
   _arrivalWatcher = createWatcher(this, &FGRouteMgr::arrivalChanged);
   _arrivalWatcher->watch(destination->getChild("runway", 0, true));
   
index e927c3503494f60828515b9a4f3677f8b3c7b6b1..95b45852ebb2ac37400c40e7500aff67dc754776 100644 (file)
@@ -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
index 0e2c34a64e1a47401d6908d71f3e58818528f34f..26d33bc7aaeb6edab8363850d97622e7c5d66b20 100644 (file)
@@ -59,7 +59,7 @@ FGMetar::FGMetar(const string& icao) :
                        _min_visibility.set(12000.0);
 
                vector<SGMetarCloud> cv = _clouds;;
-               if (!cv.size()) {
+               if (cv.empty()) {
                        SGMetarCloud cl;
                        cl.set(5500 * SG_FEET_TO_METER, SGMetarCloud::COVERAGE_SCATTERED);
                        _clouds.push_back(cl);
index 4c93aaa97e2db4fdc5ea1bea0a3cdb315ebcb154..aa67b355a7b8fa9bfadcfb6a08c8f6ff0322e8ba 100644 (file)
@@ -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<string> weather = m->getWeather();
-    for( vector<string>::const_iterator it = weather.begin(); it != weather.end(); it++ ) {
+    for( vector<string>::const_iterator it = weather.begin(); it != weather.end(); ++it ) {
         if( false == _decoded.empty() ) _decoded.append(", ");
         _decoded.append(*it);
     }
index a3e91514dfaf41971a16a7ebf7851cf5794a8554..b3dd6e48012fe750b6e6a5c2853544fb75699883 100644 (file)
@@ -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;
index 08b3e348d1e52ee1ed54a814b45abbda2a3beb69..b93316490aad54313e2f2391d7ac317d6b66f294 100644 (file)
@@ -373,8 +373,8 @@ void NoaaMetarRealWxController::requestMetar( MetarDataHandler * metarDataHandle
               }
           }
 
-          bool fromMetarProxy() const
-          { return _fromProxy; }
+//          bool fromMetarProxy() const
+//          { return _fromProxy; }
     private:  
         string _metar;
         bool _fromProxy;
index cac1388aa90dfb6644c5f2aa4df6d959e4f5707f..2a26af5af960fef0a9f92bd3ab9980f343962df8 100644 (file)
@@ -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;
index 63a679d79e02f74cd3b7919af1617c69d6a1cb24..68fcf8e1b7c8cee9e89efe22a65e3e00ce77f90b 100644 (file)
@@ -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);
index 675f969d29ac5f254d5b60b93ecbf1526ffb9a1b..20cac858c26bac0b69c10809af8fadbbec9b2ebf 100644 (file)
@@ -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;
index a90d20c882e7c0aead8f5244ee3ec498bd6aa935..1717cfa1df24d74429eb843f7b64c18dcbb650a7 100644 (file)
@@ -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<int,FGInputDevice*>::iterator it = input_devices.begin(); it != input_devices.end(); it++ )
+  for( map<int,FGInputDevice*>::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<int,FGInputDevice*>::iterator it =  input_devices.begin(); it != input_devices.end(); it++ )
+  for( map<int,FGInputDevice*>::iterator it =  input_devices.begin(); it != input_devices.end(); ++it )
     (*it).second->update( dt );
 }
 
index 23b4dd4d1c3ef5f0cf46e62c7f56504ee1a6d6d6..c62a77aae0bad8deed54593c6b23107b6302508d 100644 (file)
@@ -187,7 +187,7 @@ typedef class SGSharedPtr<FGInputEvent> 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();
 
index 83e5f68a7294c91e37df5d14726a8dbf2e7b80b7..041fcc2e10bb9824f0d9457f0f83e304b7d9402e 100644 (file)
@@ -32,6 +32,7 @@
 #include "FGDeviceConfigurationMap.hxx"
 #include <Main/fg_props.hxx>
 #include <Scripting/NasalSys.hxx>
+#include <boost/foreach.hpp>
 
 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);
index 49426d27afa429250b917a4d1d05b138cf760fe3..f5eea9b3f7777d74494f43639f8afe0cd5a33607 100644 (file)
@@ -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;
index deff1b88fde701fb2241ee807d39f2447e7fff7a..43256aadd485a167c2042eb2c215d9e94f517bf6 100644 (file)
@@ -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; 
 }
index 5fcd81e24425ff5c17ffcd9d4a7c0dd5dfea5647..df151663cc40564aab021d90a5433d95a9d9f68a 100644 (file)
@@ -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; }
index 9c4e66487eaf3fb38977d9340456a2b2fa34fe91..59effd534ddf7cd080160ef3fe41eb7fd79f3b94 100644 (file)
@@ -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();