1
0
Fork 0

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
This commit is contained in:
Torsten Dreyer 2012-03-06 22:28:18 +01:00
parent daf3fbcd67
commit 644bb8c4f4
19 changed files with 68 additions and 48 deletions

View 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;
}

View 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 );
}

View 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)

View 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));

View 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

View 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);

View 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);
}

View 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;

View 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;

View 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;

View 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);

View 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;

View 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 );
}

View 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();

View 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,31 +266,28 @@ 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.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);
js->setMaxRange(maxRange);

View 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;

View 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;
}

View 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; }

View 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();