17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-31 06:30:53 +01:00

Fix/sonar maintainability rebased (#209)

* refactor(sonar): use override instead of redundant virtual (cpp:S3471)

* refactor(sonar): remove empty statements, one declaration per statement (cpp:S1116, cpp:S1659)

* refactor(sonar): drop redundant lambda return types (cpp:S3574)

* refactor(sonar): prefer using over typedef (cpp:S5416)

* refactor(sonar): use =default for trivial special members (cpp:S3490)

* refactor(sonar): use nullptr literal (cpp:S4962)

* refactor(sonar): remove redundant access specifiers (cpp:S3539)

* refactor(sonar): remove redundant casts (cpp:S1905)

* refactor(sonar): use std::scoped_lock instead of std::lock_guard (cpp:S5997)

* refactor(sonar): construct elements in place with emplace_back (cpp:S6003)

* refactor(sonar): make narrowing conversions explicit (cpp:S5276)

* refactor(sonar): mark unused parameters (cpp:S1172)

Unused names are commented out; parameters used only on some platforms
are marked [[maybe_unused]].

* refactor(sonar): declare non-mutating member functions const (cpp:S5817)

Virtual functions are left untouched so overrides keep matching.

* refactor(sonar): merge collapsible nested if statements (cpp:S1066)

Comments that preceded the inner if are kept above the merged statement.

* refactor(sonar): scope single-use variables in if init-statements (cpp:S6004)

* fix(sonar): give polymorphic base classes virtual destructors (cpp:S1235)

* refactor(sonar): use try_emplace for map insertion (cpp:S6030)

* refactor(sonar): use auto where the type is spelled out on the right (cpp:S5827)

* refactor(sonar): remove redundant parentheses (cpp:S1110)

* refactor(sonar): document intentionally empty functions (cpp:S1186)

* refactor(sonar): remove dead stores (cpp:S1854)

Side effects (e.g. LISTLINE++) are kept.

* refactor(sonar): remove unused variables, captures and private fields (cpp:S1481, cpp:S1068)

* refactor(sonar): use structured bindings for pair access (cpp:S6005)

* refactor(sonar): use range-based loops and ranges algorithms (cpp:S5566)

* refactor(sonar): split chained assignments (cpp:S1121)

* refactor(sonar): add default cases to switch statements (cpp:S3562)

* refactor(sonar): use std::size for array lengths (cpp:S7127)

* refactor(sonar): use std::ranges algorithms and std::erase(_if) (cpp:S6197)

* refactor(sonar): access static members through the class (cpp:S2209)

* refactor(sonar): add braces around nested if-else (cpp:S5261)

* refactor(sonar): avoid side effects inside typeid operands (cpp:S5279)

* refactor(sonar): drop top-level const from by-value return types (cpp:S5951)

* refactor(sonar): declare variables separately from their type definitions (cpp:S3646)

* refactor(sonar): declare loop counters in the for statement (cpp:S5955)

* refactor(sonar): rely on class template argument deduction (cpp:S6012)

* refactor(sonar): inherit base constructors instead of forwarding them (cpp:S5952)

* refactor(sonar): rename locals that shadow outer declarations (cpp:S1117)

Only local variables and definition parameters are renamed; shadowing
struct fields and enumerators in headers are left for a separate change.

* refactor(sonar): reduce enum qualification with using enum (cpp:S6177)

Applied only where no unqualified name in the block could be captured by
the introduced enumerators.

* refactor(sonar): mark converting constructors explicit (cpp:S1709)

sound_source and the gl::object conversion operators stay implicit; they
are relied upon by copy-list-initialisation and GL calls.

* refactor(sonar): initialise members in initialiser lists or declarations (cpp:S3230)

* refactor(sonar): make read-only local references and pointers const (cpp:S5350)

* refactor(sonar): take read-only pointer and reference parameters as const (cpp:S995)

TTrain command handlers keep their signature, it is fixed by the handler table.

* refactor(sonar): pass expensive-to-copy parameters by const reference (cpp:S1238)

Signatures declared in files touched by PR 206 (Driver.h, EU07.cpp) and
virtual overrides are left unchanged.

* refactor(sonar): use transparent comparators for string-keyed maps (cpp:S6045)

std::unordered_map instances and map types exposed in headers used by
other modules are left as they are.

* fix(sonar): do not use UI text as a printf format string (cpp:S5281)

Text lines containing '%' would otherwise be interpreted as format
specifiers by ImGui::TextColored.

* refactor(sonar): use std::bind_front for callbacks (cpp:S5995)

* refactor(sonar): drop const_cast on string literals passed to the Python C API (cpp:S859)

* refactor(sonar): take read-only string parameters as std::string_view (cpp:S6009)

The Windows messaging helpers keep std::string, they need c_str().

* refactor(sonar): make read-only global tables const (cpp:S5421)

Mutable global state (flags, timers, singletons) is left as is.

* refactor(sonar): replace single-case switch statements with if (cpp:S1301)

* refactor(sonar): format UUIDs and log timestamps with std::format (cpp:S6494, cpp:S5945)

* refactor(sonar): use std::array for local fixed-size arrays (cpp:S5945)

Arrays that are class members or part of headers are left unchanged.

* fix(sonar): test the bearing damage bit, not bit 0 (cpp:S872)

'DamageFlag & dtrain_bearing > 0' parsed as 'DamageFlag & (dtrain_bearing > 0)',
i.e. DamageFlag & 1, so bearing friction reacted to the wrong damage flag.

* refactor(sonar): assorted small fixes

- empty() instead of size() comparisons (cpp:S1155)
- member initialisers in declaration order (cpp:S3229)
- no redundant c_str() when a std::string is accepted (cpp:S7121)
- contains() on associative containers (cpp:S6171)
- const reference instead of a copying range-for (cpp:S6032)
- starts_with() for prefix checks (cpp:S6178)
- no std::move on temporaries (cpp:S5274)
- has_value() on std::optional (cpp:S7172)
- bool literals for bool members (cpp:S5820)
- inline only on the definition (cpp:S3548)
- explicit base access specifier (cpp:S5965)
- explicit element access instead of dereferencing an array (cpp:S7116)
- rely on the C++20 rewritten operator!= (cpp:S6186)

* refactor(sonar): more small fixes

- std::format instead of iostream manipulators in to_string/to_hex_str (cpp:S6495);
  output verified identical, negative hex values keep the unsigned form
- remove an empty compound statement (cpp:S108)
- drop an always-false unsigned < 0 branch (cpp:S1768)
- explicit lambda capture (cpp:S3608)
- std::make_unique (cpp:S5950)

* chore: strip stray carriage returns left in TTrain constructor comments

* fix: keep quantize() in its own value type

The S5276 change cast the result of the generic quantize<Type_>() to
unsigned int, which truncated float/double callers (shadow angle slider,
editor rotation snapping, AI speed control steps) and broke negative values.

* fix(sonar): use integer loop counters in segment sampling (cpp:S2193)

* refactor(sonar): clean up issues reported on PR 209

- remove locals left unused after the dead-store cleanup (cpp:S1481, cpp:S1854)
- unnamed unused parameter (cpp:S1172)
- single-use declarations moved into if init-statements (cpp:S6004)
- auto for casted initialisers (cpp:S5827)
- const references and pointers where nothing is modified (cpp:S5350)
- rename structured bindings shadowed by inner declarations (cpp:S1117)
This commit is contained in:
jerrrrycho
2026-10-11 13:22:33 +02:00
committed by GitHub
parent c26041f2e7
commit 6263fe539b
162 changed files with 4220 additions and 4598 deletions

View File

@@ -76,7 +76,7 @@ void TGauge::Init(TSubModel *Submodel, TSubModel *Submodelon, TGaugeAnimation Ty
auto const nulloffset { glm::vec3{} };
auto const offset { model_offset() };
{
std::vector<sound_source *> soundfxs = {
std::vector soundfxs = {
&m_soundfxincrease,
&m_soundfxdecrease,
&m_soundfxon,
@@ -88,17 +88,22 @@ void TGauge::Init(TSubModel *Submodel, TSubModel *Submodelon, TGaugeAnimation Ty
}
}
}
for( auto &soundfx : m_soundfxvalues ) {
if( soundfx.second.offset() == nulloffset ) {
soundfx.second.offset( offset );
for( auto &[soundfxkey, soundfxsource] : m_soundfxvalues ) {
if( soundfxsource.offset() == nulloffset ) {
soundfxsource.offset( offset );
}
}
};
void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mul ) {
std::string submodelname, gaugetypename;
float scale, endscale, endvalue, offset, friction;
std::string submodelname;
std::string gaugetypename;
float scale;
float endscale;
float endvalue;
float offset;
float friction;
endscale = -1;
endvalue = -1;
bool interpolatescale { false };
@@ -144,7 +149,7 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu
{
scratch_data scratchpad;
while( true == Load_mapping( Parser, scratchpad ) ) {
; // all work done by while()
// all work done by while()
}
// post-deserialization cleanup
// set provided custom soundproofing to assigned sounds (for sounds without their own custom soundproofing)
@@ -161,9 +166,9 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu
if( !m_soundfxoff.soundproofing() ) {
m_soundfxoff.soundproofing() = scratchpad.soundproofing;
}
for( auto &soundfxrecord : m_soundfxvalues ) {
if( !soundfxrecord.second.soundproofing() ) {
soundfxrecord.second.soundproofing() = scratchpad.soundproofing;
for( auto &[soundfxkey, soundfxsource] : m_soundfxvalues ) {
if( !soundfxsource.soundproofing() ) {
soundfxsource.soundproofing() = scratchpad.soundproofing;
}
}
}
@@ -175,13 +180,13 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu
m_soundfxdecrease.owner( Owner );
m_soundfxon.owner( Owner );
m_soundfxoff.owner( Owner );
for( auto &soundfxrecord : m_soundfxvalues ) {
soundfxrecord.second.owner( Owner );
for( auto &[soundfxkey, soundfxsource] : m_soundfxvalues ) {
soundfxsource.owner( Owner );
}
scale *= mul;
scale *= static_cast<float>(mul);
if( interpolatescale ) {
endscale *= mul;
endscale *= static_cast<float>(mul);
}
TSubModel *submodel { nullptr };
std::array<TModel3d *, 2> sources { Owner->mdKabina, Owner->mdLowPolyInt };
@@ -211,7 +216,7 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu
}
}
std::map<std::string, TGaugeAnimation> gaugetypes {
std::map<std::string, TGaugeAnimation, std::less<>> gaugetypes {
{ "rot", TGaugeAnimation::gt_Rotate },
{ "rotvar", TGaugeAnimation::gt_Rotate },
{ "mov", TGaugeAnimation::gt_Move },
@@ -235,13 +240,14 @@ TGauge::Load_mapping( cParser &Input, TGauge::scratch_data &Scratchpad ) {
if( true == key.empty() || key == "}" ) { return false; }
// if not block end then the key is followed by assigned value or sub-block
if( key == "type:" ) {
using enum TGaugeType;
auto const gaugetype { Input.getToken<std::string>( true, "\n\r\t ,;" ) };
m_type = gaugetype == "push" ? TGaugeType::push :
gaugetype == "impulse" ? TGaugeType::push :
gaugetype == "return" ? TGaugeType::push :
gaugetype == "delayed" ? TGaugeType::push_delayed :
gaugetype == "pushtoggle" ? TGaugeType::pushtoggle :
TGaugeType::toggle; // "toggle" and default
m_type = gaugetype == "push" ? push :
gaugetype == "impulse" ? push :
gaugetype == "return" ? push :
gaugetype == "delayed" ? push_delayed :
gaugetype == "pushtoggle" ? pushtoggle :
toggle; // "toggle" and default
}
else if( key == "soundinc:" ) {
m_soundfxincrease.deserialize( Input, sound_type::single );
@@ -273,7 +279,7 @@ TGauge::Load_mapping( cParser &Input, TGauge::scratch_data &Scratchpad ) {
auto const indexstart { key.find_first_of( "-1234567890" ) };
auto const indexend { key.find_first_not_of( "-1234567890", indexstart ) };
if( indexstart != std::string::npos ) {
m_soundfxvalues.emplace(
m_soundfxvalues.try_emplace(
std::stoi( key.substr( indexstart, indexend - indexstart ) ),
sound_source( m_soundtemplate ).deserialize( Input, sound_type::single ) );
}
@@ -285,11 +291,9 @@ TGauge::Load_mapping( cParser &Input, TGauge::scratch_data &Scratchpad ) {
bool
TGauge::UpdateValue( float fNewDesired, std::optional<sound_source> &Fallbacksound ) {
if( false == UpdateValue( fNewDesired ) ) {
if( Fallbacksound ) {
Fallbacksound->play( m_soundtype );
return true;
}
if (false == UpdateValue( fNewDesired ) && Fallbacksound) {
Fallbacksound->play( m_soundtype );
return true;
}
return false;
}
@@ -304,8 +308,7 @@ TGauge::UpdateValue( float fNewDesired ) {
m_targetvalue = fNewDesired;
// if there's any sound associated with new requested value, play it
// check value-specific table first...
auto const fullinteger { desiredtimes100 % 100 == 0 };
if( fullinteger ) {
if( auto const fullinteger { desiredtimes100 % 100 == 0 }; fullinteger ) {
// filter out values other than full integers
auto const lookup = m_soundfxvalues.find( desiredtimes100 / 100 );
if( lookup != m_soundfxvalues.end() ) {
@@ -318,9 +321,8 @@ TGauge::UpdateValue( float fNewDesired ) {
m_soundtype = sound_flags::exclusive;
}
// ...and if there isn't any, fall back on the basic set...
auto const currentvalue = GetValue();
// HACK: crude way to discern controls with continuous and quantized value range
if( currentvalue < fNewDesired ) {
if( auto const currentvalue = GetValue(); currentvalue < fNewDesired ) {
// shift up
if( false == m_soundfxincrease.empty() ) {
m_soundfxincrease.play( m_soundtype );
@@ -357,7 +359,7 @@ void TGauge::Update( bool const Power ) {
// update value
// TODO: remove passing manually power state when LD is in place
if( m_value != m_targetvalue ) {
float dt = Timer::GetDeltaTime();
auto dt = static_cast<float>(Timer::GetDeltaTime());
if( m_friction > 0 && dt < 0.5 * m_friction ) {
// McZapkie-281102: zabezpieczenie przed oscylacjami dla dlugich czasow
m_value += dt * ( m_targetvalue - m_value ) / m_friction;
@@ -424,11 +426,11 @@ void TGauge::UpdateValue()
break;
}
case 'd': {
UpdateValue( *dData );
UpdateValue( static_cast<float>(*dData) );
break;
}
case 'i': {
UpdateValue( *iData );
UpdateValue( static_cast<float>(*iData) );
break;
}
case 'b': {
@@ -452,13 +454,13 @@ float TGauge::GetScaledValue() const {
}
void
TGauge::UpdateAnimation( TSubModel *Submodel ) {
TGauge::UpdateAnimation( TSubModel *Submodel ) const {
if( Submodel == nullptr ) { return; }
switch (m_animation) {
case TGaugeAnimation::gt_Rotate: {
Submodel->SetRotate( float3( 0, 1, 0 ), GetScaledValue() * 360.0 );
Submodel->SetRotate( float3( 0, 1, 0 ), static_cast<float>(GetScaledValue() * 360.0) );
break;
}
case TGaugeAnimation::gt_Move: {
@@ -467,13 +469,12 @@ TGauge::UpdateAnimation( TSubModel *Submodel ) {
}
case TGaugeAnimation::gt_Wiper: {
auto const scaledvalue { GetScaledValue() };
Submodel->SetRotate( float3( 0, 1, 0 ), scaledvalue * 360.0 );
auto *sm = Submodel->ChildGet();
if( sm ) {
sm->SetRotate( float3( 0, 1, 0 ), scaledvalue * 360.0 );
Submodel->SetRotate( float3( 0, 1, 0 ), static_cast<float>(scaledvalue * 360.0) );
if( auto *sm = Submodel->ChildGet(); sm ) {
sm->SetRotate( float3( 0, 1, 0 ), static_cast<float>(scaledvalue * 360.0) );
sm = sm->ChildGet();
if( sm )
sm->SetRotate( float3( 0, 1, 0 ), scaledvalue * 360.0 );
sm->SetRotate( float3( 0, 1, 0 ), static_cast<float>(scaledvalue * 360.0) );
}
break;
}
@@ -488,7 +489,7 @@ TGauge::UpdateAnimation( TSubModel *Submodel ) {
&& std::isdigit(sm->pName[0]) ) {
sm->SetRotate(
float3( 0, 1, 0 ),
-36.0 * ( n[ '0' + 9 - sm->pName[ 0 ] ] - '0' ) );
static_cast<float>(-36.0 * ( n[ '0' + 9 - sm->pName[ 0 ] ] - '0' )) );
}
sm = sm->NextGet();
} while( sm );