18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 22:07:55 +01:00

refactor: std::array for the buffers of the physics log line and the seed of the lazy engine, ownP in the if

Sonar's remarks on the new code of this branch: the C arrays of append_fixed(), append_int() and
lazy_random_engine are std::arrays (S5945), the pressure of the ESt3 relays read once is declared in the
if that uses it (S6004), and the thread_local pointer to the lazy engine has the NOSONAR of the one above
it. The snprintf of the infinities and NaNs of the physics log stays, with a NOSONAR: std::format writes
them otherwise than printf's inf and nan(ind). No change of what the code does.
This commit is contained in:
jerrrrycho
2026-12-01 16:59:35 +01:00
parent 2883887f5c
commit ecf524063e
4 changed files with 15 additions and 17 deletions
+3 -7
View File
@@ -67,9 +67,7 @@ void TPrzekladnik::Update(double dt)
double BCP{ Next->P() };
double BVP{ BrakeRes->P() };
// the pressure of the relay itself, read once: nothing changes it before the flows are applied (Act())
double const ownP{ P() };
if ( BCP > ownP )
if ( double const ownP{ P() }; BCP > ownP )
dV = -PFVd(BCP, 0, d2A(10), ownP) * dt;
else {
if( BCP < ownP )
@@ -122,8 +120,7 @@ void TRapid::Update(double dt)
ActMult = 1.0;
}
double const ownP{ P() };
if (BCP * RapidMult > ownP * ActMult)
if (double const ownP{ P() }; BCP * RapidMult > ownP * ActMult)
dV = -PFVd(BCP, 0, DL, ownP * ActMult / RapidMult) * dt;
else if (BCP * RapidMult < ownP * ActMult)
dV = PFVa(BVP, BCP, DN, ownP * ActMult / RapidMult) * dt;
@@ -148,8 +145,7 @@ void TPrzekCiagly::Update(double dt)
double const BCP{ Next->P() };
double dV;
double const ownP{ P() };
if ( BCP > ownP * Mult)
if ( double const ownP{ P() }; BCP > ownP * Mult)
dV = -PFVd(BCP, 0, d2A(8.0), ownP * Mult) * dt;
else if (BCP < ownP * Mult)
dV = PFVa(BVP, BCP, d2A(8.0), ownP * Mult) * dt;
+2 -1
View File
@@ -13,13 +13,14 @@ http://mozilla.org/MPL/2.0/.
#pragma once
#include <array>
#include <cstdint>
#include <optional>
#include <random>
struct lazy_random_engine
{
std::uint32_t seed[2]{};
std::array<std::uint32_t, 2> seed{};
std::optional<std::mt19937> engine; // NOSONAR: randomness of the simulation, not of anything security-relevant
};
+1 -1
View File
@@ -182,7 +182,7 @@ bool ClearFlag(int &Flag, int const Value)
namespace
{
thread_local std::mt19937 *t_random_engine{nullptr}; // NOSONAR: randomness of the simulation, not of anything security-relevant
thread_local lazy_random_engine *t_lazy_random_engine{nullptr};
thread_local lazy_random_engine *t_lazy_random_engine{nullptr}; // NOSONAR: randomness of the simulation, not of anything security-relevant
std::mt19937 &random_engine() // NOSONAR: randomness of the simulation, not of anything security-relevant
{
+9 -8
View File
@@ -23,6 +23,7 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/physicslog.h"
#include "vehicle/Train.h"
#include "vehicle/brakereleaser.h"
#include <array>
#include <charconv>
#include <cmath>
#include <cstdio>
@@ -71,25 +72,25 @@ namespace {
// stream and the locale. infinities and NaNs (printf's inf, nan(ind)) and numbers too long for the buffer go through
// snprintf with the same format
void append_fixed( std::string &Line, double const Value ) {
char buffer[ 128 ];
std::array<char, 128> buffer;
if( std::isfinite( Value ) ) {
auto const result { std::to_chars( buffer, buffer + sizeof( buffer ), Value, std::chars_format::fixed, 4 ) };
auto const result { std::to_chars( buffer.data(), buffer.data() + buffer.size(), Value, std::chars_format::fixed, 4 ) };
if( result.ec == std::errc{} ) {
Line.append( buffer, result.ptr );
Line.append( buffer.data(), result.ptr );
return;
}
}
auto const length { std::snprintf( nullptr, 0, "%.4f", Value ) };
auto const length { std::snprintf( nullptr, 0, "%.4f", Value ) }; // NOSONAR: printf's own text of inf and NaN, which std::format writes otherwise
std::string text( static_cast<std::size_t>( length ) + 1, '\0' );
std::snprintf( text.data(), text.size(), "%.4f", Value );
std::snprintf( text.data(), text.size(), "%.4f", Value ); // NOSONAR: printf's own text of inf and NaN, which std::format writes otherwise
text.resize( static_cast<std::size_t>( length ) );
Line += text;
}
void append_int( std::string &Line, int const Value ) {
char buffer[ 16 ];
auto const result { std::to_chars( buffer, buffer + sizeof( buffer ), Value ) };
Line.append( buffer, result.ptr );
std::array<char, 16> buffer;
auto const result { std::to_chars( buffer.data(), buffer.data() + buffer.size(), Value ) };
Line.append( buffer.data(), result.ptr );
}
} // namespace