From 43fcda05a06bffced31bccdb1d98ec6231bf706c Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 10 Oct 2026 17:44:14 +0200 Subject: [PATCH] refactor(sonar): use std::array for local fixed-size arrays (cpp:S5945) Arrays that are class members or part of headers are left unchanged. Co-Authored-By: Claude Opus 5.5 --- McZapkie/Mover.cpp | 4 +-- environment/skydome.cpp | 24 ++++++------- gl/buffer.cpp | 2 +- gl/framebuffer.cpp | 6 ++-- gl/query.cpp | 2 +- gl/shader.cpp | 14 ++++---- rendering/frustum.cpp | 2 +- rendering/screenshot.cpp | 6 ++-- scene/sn_utils.cpp | 64 +++++++++++++++++------------------ simulation/simulationtime.cpp | 12 +++---- utilities/motiontelemetry.cpp | 4 +-- vehicle/Train.cpp | 8 ++--- 12 files changed, 74 insertions(+), 74 deletions(-) diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index a3d8de7b..059aea74 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -8887,7 +8887,7 @@ std::string TMoverParameters::EngineDescription(int what) const double TMoverParameters::GetTrainsetVoltage(int const Coupling) const { // ABu: funkcja zwracajaca napiecie dla calego skladu, przydatna dla EZT // TBD, TODO: call once per vehicle update, return cached results? - double voltages[] = {0.0, 0.0}; + std::array voltages{0.0, 0.0}; for (int end = end::front; end <= end::rear; ++end) { if (Couplers[end].Connected == nullptr) @@ -12011,7 +12011,7 @@ bool TMoverParameters::CheckLocomotiveParameters(bool ReadyFlag, int /*Dir*/) BrakeOpModeFlag = bom_PN; // yB: jesli pojazdy nie maja zadeklarowanych czasow, to wsadz z przepisow +-16,(6)% - int DefBrakeTable[8] = {15, 4, 25, 25, 13, 3, 12, 2}; + std::array const DefBrakeTable{15, 4, 25, 25, 13, 3, 12, 2}; for (int b = 1; b < 4; b++) { diff --git a/environment/skydome.cpp b/environment/skydome.cpp index 6eebfb7e..08240bf7 100644 --- a/environment/skydome.cpp +++ b/environment/skydome.cpp @@ -195,17 +195,17 @@ void CSkyDome::RebuildColors() { float zenithy = GetZenith( m_zenithymatrix, m_thetasun, m_turbidity ); // get perez function parametrs - float perezluminance[5]; - float perezx[5]; - float perezy[5]; - GetPerez( perezluminance, m_distributionluminance, m_turbidity ); - GetPerez( perezx, m_distributionxcomp, m_turbidity ); - GetPerez( perezy, m_distributionycomp, m_turbidity ); + std::array perezluminance{}; + std::array perezx{}; + std::array perezy{}; + GetPerez( perezluminance.data(), m_distributionluminance, m_turbidity ); + GetPerez( perezx.data(), m_distributionxcomp, m_turbidity ); + GetPerez( perezy.data(), m_distributionycomp, m_turbidity ); // make some precalculation - zenithx = PerezFunctionO1( perezx, m_thetasun, zenithx ); - zenithy = PerezFunctionO1( perezy, m_thetasun, zenithy ); - zenithluminance = PerezFunctionO1( perezluminance, m_thetasun, zenithluminance ); + zenithx = PerezFunctionO1( perezx.data(), m_thetasun, zenithx ); + zenithy = PerezFunctionO1( perezy.data(), m_thetasun, zenithy ); + zenithluminance = PerezFunctionO1( perezluminance.data(), m_thetasun, zenithluminance ); // start with fresh average for the new pass glm::vec3 averagecolor; @@ -241,11 +241,11 @@ void CSkyDome::RebuildColors() { float const cosgamma2 = static_cast(std::pow( std::cos( gamma ), 2 )); // Compute x,y values - float const x = PerezFunctionO2( perezx, icostheta, gamma, cosgamma2, zenithx ); - float const y = PerezFunctionO2( perezy, icostheta, gamma, cosgamma2, zenithy ); + float const x = PerezFunctionO2( perezx.data(), icostheta, gamma, cosgamma2, zenithx ); + float const y = PerezFunctionO2( perezy.data(), icostheta, gamma, cosgamma2, zenithy ); // luminance(Y) for clear & overcast sky - float const yclear = std::max( 0.01f, PerezFunctionO2( perezluminance, icostheta, gamma, cosgamma2, zenithluminance ) ); + float const yclear = std::max( 0.01f, PerezFunctionO2( perezluminance.data(), icostheta, gamma, cosgamma2, zenithluminance ) ); float const yover = std::max( 0.01f, zenithluminance * ( 1.0f + 2.0f * vertex.y ) / 3.0f ); float const Y = std::lerp( yclear, yover, m_overcast ); diff --git a/gl/buffer.cpp b/gl/buffer.cpp index 36e84452..2ac6dbfb 100644 --- a/gl/buffer.cpp +++ b/gl/buffer.cpp @@ -3,7 +3,7 @@ GLenum gl::buffer::glenum_target(gl::buffer::targets target) { - static GLenum mapping[13] = + static std::array const mapping = { GL_ARRAY_BUFFER, GL_ATOMIC_COUNTER_BUFFER, diff --git a/gl/framebuffer.cpp b/gl/framebuffer.cpp index 2c2b242a..207a0ea9 100644 --- a/gl/framebuffer.cpp +++ b/gl/framebuffer.cpp @@ -90,17 +90,17 @@ void gl::framebuffer::blit(framebuffer const *src, framebuffer const *dst, int s int attachment_n = attachment - GL_COLOR_ATTACHMENT0; { - GLenum outputs[8] = { GL_NONE }; + std::array outputs{ GL_NONE }; outputs[attachment_n] = src != nullptr ? attachment : GL_BACK_LEFT; glReadBuffer(attachment); } { - GLenum outputs[8] = { GL_NONE }; + std::array outputs{ GL_NONE }; outputs[attachment_n] = dst != nullptr ? attachment : GL_BACK_LEFT; - glDrawBuffers(attachment_n + 1, outputs); + glDrawBuffers(attachment_n + 1, outputs.data()); } } diff --git a/gl/query.cpp b/gl/query.cpp index 148d2ce2..8e445612 100644 --- a/gl/query.cpp +++ b/gl/query.cpp @@ -51,7 +51,7 @@ std::optional gl::query::result() GLenum gl::query::glenum_target(targets Target) const { - static GLenum mapping[6] = + static std::array const mapping = { GL_SAMPLES_PASSED, GL_ANY_SAMPLES_PASSED, diff --git a/gl/shader.cpp b/gl/shader.cpp index 7e28ce51..c77aa29a 100644 --- a/gl/shader.cpp +++ b/gl/shader.cpp @@ -318,11 +318,11 @@ gl::shader::shader(const std::string &filename) : name(filename) glGetShaderiv(*this, GL_COMPILE_STATUS, &status); if (!status) { - GLchar info[512]; - glGetShaderInfoLog(*this, 512, nullptr, info); - log_error(std::string(info)); + std::array info{}; + glGetShaderInfoLog(*this, static_cast(info.size()), nullptr, info.data()); + log_error(std::string(info.data())); - throw shader_exception("failed to compile " + filename + ": " + std::string(info)); + throw shader_exception("failed to compile " + filename + ": " + std::string(info.data())); } } @@ -392,9 +392,9 @@ void gl::program::link() glGetProgramiv(*this, GL_LINK_STATUS, &status); if (!status) { - GLchar info[512]; - glGetProgramInfoLog(*this, 512, nullptr, info); - throw shader_exception("failed to link program: " + std::string(info)); + std::array info{}; + glGetProgramInfoLog(*this, static_cast(info.size()), nullptr, info.data()); + throw shader_exception("failed to link program: " + std::string(info.data())); } init(); diff --git a/rendering/frustum.cpp b/rendering/frustum.cpp index 16ccf703..68349a5f 100644 --- a/rendering/frustum.cpp +++ b/rendering/frustum.cpp @@ -24,7 +24,7 @@ cFrustum::calculate( glm::mat4 const &Projection, glm::mat4 const &Modelview ) { // multiply the matrices to retrieve clipping planes (column-major, same layout as the old float[16] form) auto const clipmatrix { Projection * Modelview }; - float clip[ 16 ]; + std::array clip{}; for( int column = 0; column < 4; ++column ) { for( int row = 0; row < 4; ++row ) { clip[ column * 4 + row ] = clipmatrix[ column ][ row ]; diff --git a/rendering/screenshot.cpp b/rendering/screenshot.cpp index dc7005de..88091703 100644 --- a/rendering/screenshot.cpp +++ b/rendering/screenshot.cpp @@ -29,9 +29,9 @@ void screenshot_manager::screenshot_save_thread( char *img, int w, int h ) stride = -w * 3; } - char datetime[64]; + std::array datetime{}; std::tm const tm_info = local_tm(std::time(nullptr)); - strftime(datetime, 64, "%Y-%m-%d_%H-%M-%S", &tm_info); + strftime(datetime.data(), datetime.size(), "%Y-%m-%d_%H-%M-%S", &tm_info); uint64_t perf; #ifdef _WIN32 @@ -42,7 +42,7 @@ void screenshot_manager::screenshot_save_thread( char *img, int w, int h ) perf = ts.tv_nsec; #endif - std::string filename = Global.screenshot_dir + "/" + std::string(datetime) + + std::string filename = Global.screenshot_dir + "/" + std::string(datetime.data()) + "_" + std::to_string(perf) + ".png"; if (png_image_write_to_file(&png, filename.c_str(), 0, img, stride, nullptr) == 1) diff --git a/scene/sn_utils.cpp b/scene/sn_utils.cpp index 006fcccc..d74d97cb 100644 --- a/scene/sn_utils.cpp +++ b/scene/sn_utils.cpp @@ -16,8 +16,8 @@ static_assert(-1 == ~0, "Two's complement required"); // deserialize little endian uint16 uint16_t sn_utils::ld_uint16(std::istream &s) { - uint8_t buf[2]; - s.read((char*)buf, 2); + std::array buf{}; + s.read(reinterpret_cast(buf.data()), buf.size()); uint16_t v = static_cast(buf[1] << 8 | buf[0]); return v; } @@ -25,8 +25,8 @@ uint16_t sn_utils::ld_uint16(std::istream &s) // deserialize little endian uint32 uint32_t sn_utils::ld_uint32(std::istream &s) { - uint8_t buf[4]; - s.read((char*)buf, 4); + std::array buf{}; + s.read(reinterpret_cast(buf.data()), buf.size()); uint32_t v = buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0]; return v; } @@ -34,8 +34,8 @@ uint32_t sn_utils::ld_uint32(std::istream &s) // deserialize little endian int32 int32_t sn_utils::ld_int32(std::istream &s) { - uint8_t buf[4]; - s.read((char*)buf, 4); + std::array buf{}; + s.read(reinterpret_cast(buf.data()), buf.size()); uint32_t v = buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0]; return static_cast(v); } @@ -43,8 +43,8 @@ int32_t sn_utils::ld_int32(std::istream &s) // deserialize little endian uint64 uint64_t sn_utils::ld_uint64(std::istream &s) { - uint8_t buf[8]; - s.read((char*)buf, 8); + std::array buf{}; + s.read(reinterpret_cast(buf.data()), buf.size()); uint64_t v = (uint64_t)buf[7] << 56 | (uint64_t)buf[6] << 48 | (uint64_t)buf[5] << 40 | (uint64_t)buf[4] << 32 | (uint64_t)buf[3] << 24 | (uint64_t)buf[2] << 16 | @@ -55,8 +55,8 @@ uint64_t sn_utils::ld_uint64(std::istream &s) // deserialize little endian int64 int64_t sn_utils::ld_int64(std::istream &s) { - uint8_t buf[8]; - s.read((char*)buf, 8); + std::array buf{}; + s.read(reinterpret_cast(buf.data()), buf.size()); uint64_t v = (uint64_t)buf[7] << 56 | (uint64_t)buf[6] << 48 | (uint64_t)buf[5] << 40 | (uint64_t)buf[4] << 32 | (uint64_t)buf[3] << 24 | (uint64_t)buf[2] << 16 | @@ -67,8 +67,8 @@ int64_t sn_utils::ld_int64(std::istream &s) // deserialize little endian ieee754 float32 float sn_utils::ld_float32(std::istream &s) { - uint8_t buf[4]; - s.read((char*)buf, 4); + std::array buf{}; + s.read(reinterpret_cast(buf.data()), buf.size()); uint32_t v = buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0]; return std::bit_cast(v); } @@ -76,8 +76,8 @@ float sn_utils::ld_float32(std::istream &s) // deserialize little endian ieee754 float64 double sn_utils::ld_float64(std::istream &s) { - uint8_t buf[8]; - s.read((char*)buf, 8); + std::array buf{}; + s.read(reinterpret_cast(buf.data()), buf.size()); uint64_t v = (uint64_t)buf[7] << 56 | (uint64_t)buf[6] << 48 | (uint64_t)buf[5] << 40 | (uint64_t)buf[4] << 32 | (uint64_t)buf[3] << 24 | (uint64_t)buf[2] << 16 | @@ -90,13 +90,13 @@ std::string sn_utils::d_str(std::istream &s) { std::string r; r.reserve(32); - char buf[1]; + char c{}; while (true) { - s.read(buf, 1); - if (buf[0] == 0) + s.read(&c, 1); + if (c == 0) break; - r.push_back(buf[0]); + r.push_back(c); } return r; } @@ -141,35 +141,35 @@ uint8_t sn_utils::d_uint8( std::istream& s ) { void sn_utils::ls_uint16(std::ostream &s, uint16_t v) { - uint8_t buf[2]; + std::array buf{}; buf[0] = static_cast(v); buf[1] = v >> 8; - s.write((char*)buf, 2); + s.write(reinterpret_cast(buf.data()), buf.size()); } void sn_utils::ls_uint32(std::ostream &s, uint32_t v) { - uint8_t buf[4]; + std::array buf{}; buf[0] = static_cast(v); buf[1] = static_cast(v >> 8); buf[2] = static_cast(v >> 16); buf[3] = v >> 24; - s.write((char*)buf, 4); + s.write(reinterpret_cast(buf.data()), buf.size()); } void sn_utils::ls_int32(std::ostream &s, int32_t v) { - uint8_t buf[4]; + std::array buf{}; buf[0] = static_cast(v); buf[1] = static_cast(v >> 8); buf[2] = static_cast(v >> 16); buf[3] = v >> 24; - s.write((char*)buf, 4); + s.write(reinterpret_cast(buf.data()), buf.size()); } void sn_utils::ls_uint64(std::ostream &s, uint64_t v) { - uint8_t buf[8]; + std::array buf{}; buf[0] = static_cast(v); buf[1] = static_cast(v >> 8); buf[2] = static_cast(v >> 16); @@ -178,12 +178,12 @@ void sn_utils::ls_uint64(std::ostream &s, uint64_t v) buf[5] = static_cast(v >> 40); buf[6] = static_cast(v >> 48); buf[7] = v >> 56; - s.write((char*)buf, 8); + s.write(reinterpret_cast(buf.data()), buf.size()); } void sn_utils::ls_int64(std::ostream &s, int64_t v) { - uint8_t buf[8]; + std::array buf{}; buf[0] = static_cast(v); buf[1] = static_cast(v >> 8); buf[2] = static_cast(v >> 16); @@ -192,24 +192,24 @@ void sn_utils::ls_int64(std::ostream &s, int64_t v) buf[5] = static_cast(v >> 40); buf[6] = static_cast(v >> 48); buf[7] = v >> 56; - s.write((char*)buf, 8); + s.write(reinterpret_cast(buf.data()), buf.size()); } void sn_utils::ls_float32(std::ostream &s, float t) { auto v = std::bit_cast(t); - uint8_t buf[4]; + std::array buf{}; buf[0] = static_cast(v); buf[1] = static_cast(v >> 8); buf[2] = static_cast(v >> 16); buf[3] = v >> 24; - s.write((char*)buf, 4); + s.write(reinterpret_cast(buf.data()), buf.size()); } void sn_utils::ls_float64(std::ostream &s, double t) { auto v = std::bit_cast(t); - uint8_t buf[8]; + std::array buf{}; buf[0] = static_cast(v); buf[1] = static_cast(v >> 8); buf[2] = static_cast(v >> 16); @@ -218,7 +218,7 @@ void sn_utils::ls_float64(std::ostream &s, double t) buf[5] = static_cast(v >> 40); buf[6] = static_cast(v >> 48); buf[7] = v >> 56; - s.write((char*)buf, 8); + s.write(reinterpret_cast(buf.data()), buf.size()); } void sn_utils::s_uint8(std::ostream &s, uint8_t v) diff --git a/simulation/simulationtime.cpp b/simulation/simulationtime.cpp index ec4892a8..4c6246d5 100644 --- a/simulation/simulationtime.cpp +++ b/simulation/simulationtime.cpp @@ -134,10 +134,10 @@ scenario_time::update( double const Deltatime ) { int scenario_time::year_day( int Day, const int Month, const int Year ) const { - char const daytab[ 2 ][ 13 ] = { + std::array, 2> const daytab = {{ { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } - }; + }}; int const leap { is_leap( Year ) }; for( int i = 1; i < Month; ++i ) @@ -149,10 +149,10 @@ scenario_time::year_day( int Day, const int Month, const int Year ) const { void scenario_time::daymonth( WORD &Day, WORD &Month, WORD const Year, WORD const Yearday ) const { - WORD daytab[ 2 ][ 13 ] = { + std::array, 2> const daytab = {{ { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } - }; + }}; int const leap { is_leap( Year ) }; WORD idx = 1; @@ -219,10 +219,10 @@ scenario_time::day_of_month( int const Week, int const Weekday, int const Month, if( Week == 5 ) { // 5th week potentially indicates last week in the month, not necessarily actual 5th - char const daytab[ 2 ][ 13 ] = { + std::array, 2> const daytab = {{ { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } - }; + }}; int const leap { is_leap( Year ) }; while( day > daytab[ leap ][ Month ] ) { diff --git a/utilities/motiontelemetry.cpp b/utilities/motiontelemetry.cpp index bbd6ff06..b0dd02dc 100644 --- a/utilities/motiontelemetry.cpp +++ b/utilities/motiontelemetry.cpp @@ -145,7 +145,7 @@ void motiontelemetry::update() rot *= -1; } - float buffer[12] = { 0 }; + std::array buffer{}; buffer[0] = static_cast(Timer::GetTime()); buffer[1] = static_cast(velocity); buffer[2] = static_cast(local_acc.y); @@ -157,6 +157,6 @@ void motiontelemetry::update() buffer[8] = static_cast(yaw_vel); buffer[9] = 1.0f; - if (send(sock, (char*)buffer, sizeof(buffer), 0) == -1) + if (send(sock, reinterpret_cast(buffer.data()), sizeof(buffer), 0) == -1) WriteLog("motiontelemetry: socket send failed"); } diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index 7faf2526..bf02e762 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -806,10 +806,10 @@ std::shared_ptr TTrain::GetTrainState(dictionary_source const dict->insert("power_returned", mvOccupied->EnergyMeter.second); // induction motor state data - char const *TXTT[10] = {"fd", "fdt", "fdb", "pd", "pdt", "pdb", "itothv", "1", "2", "3"}; - char const *TXTC[10] = {"fr", "frt", "frb", "pr", "prt", "prb", "im", "vm", "ihv", "uhv"}; - char const *TXTD[10] = {"enrot", "nrot", "fill_des", "fill_real", "clutch_des", "clutch_real", "water_temp", "oil_press", "engine_temp", "retarder_fill"}; - char const *TXTP[7] = {"bc", "bp", "sp", "cp", "rp", "mass", "spring"}; + std::array const TXTT{"fd", "fdt", "fdb", "pd", "pdt", "pdb", "itothv", "1", "2", "3"}; + std::array const TXTC{"fr", "frt", "frb", "pr", "prt", "prb", "im", "vm", "ihv", "uhv"}; + std::array const TXTD{"enrot", "nrot", "fill_des", "fill_real", "clutch_des", "clutch_real", "water_temp", "oil_press", "engine_temp", "retarder_fill"}; + std::array const TXTP{"bc", "bp", "sp", "cp", "rp", "mass", "spring"}; char const *TXTB[2] = {"spring_active", "spring_shutoff"}; for (int j = 0; j < 10; ++j) dict->insert("eimp_t_" + std::string(TXTT[j]), fEIMParams[0][j]);