useStlAlgorithm: ranges algorithms + justifica returnDanglingLifetime
This commit is contained in:
1 parent
d7db02bafd
commit
822bed0748
5 files changed
+15
-25
No files matched your search
@@ -4,6 +4,7 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <numeric>
|
||||||
|
|
||||||
#include "utils/log.hpp"
|
#include "utils/log.hpp"
|
||||||
|
|
||||||
@@ -19,12 +20,7 @@ namespace Resource {
|
|||||||
auto Pack::calculateChecksum(const std::vector<std::uint8_t>& data) -> std::uint32_t {
|
auto Pack::calculateChecksum(const std::vector<std::uint8_t>& data) -> std::uint32_t {
|
||||||
// djb2: seed 0x12345678. Compatible amb aee/projecte_2026 per si mai
|
// djb2: seed 0x12345678. Compatible amb aee/projecte_2026 per si mai
|
||||||
// reutilitzem eines de diagnòstic entre projectes.
|
// reutilitzem eines de diagnòstic entre projectes.
|
||||||
std::uint32_t checksum = 0x12345678;
|
return std::accumulate(data.begin(), data.end(), std::uint32_t{0x12345678}, [](std::uint32_t acc, unsigned char b) { return ((acc << 5) + acc) + b; });
|
||||||
for (unsigned char b : data) {
|
|
||||||
// cppcheck-suppress useStlAlgorithm
|
|
||||||
checksum = ((checksum << 5) + checksum) + b;
|
|
||||||
}
|
|
||||||
return checksum;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Pack::xorInPlace(std::vector<std::uint8_t>& data, const std::string& key) {
|
void Pack::xorInPlace(std::vector<std::uint8_t>& data, const std::string& key) {
|
||||||
@@ -151,7 +147,7 @@ namespace Resource {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// cppcheck-suppress useStlAlgorithm
|
// cppcheck-suppress useStlAlgorithm -- bucle amb early-return en error (addFile fallit) + filtre amb continue: no es pot expressar net amb std::ranges::all_of perquè la lògica combina filtratge i acumulació amb sortida prematura.
|
||||||
for (const auto& entry : std::filesystem::recursive_directory_iterator(directory)) {
|
for (const auto& entry : std::filesystem::recursive_directory_iterator(directory)) {
|
||||||
if (!entry.is_regular_file()) { continue; }
|
if (!entry.is_regular_file()) { continue; }
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#include "game/entities/animation_component.hpp"
|
#include "game/entities/animation_component.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <iterator>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
@@ -33,8 +35,7 @@ namespace {
|
|||||||
if (speeds.empty()) {
|
if (speeds.empty()) {
|
||||||
return std::vector<float>(frame_count, K_DEFAULT_FRAME_SECONDS);
|
return std::vector<float>(frame_count, K_DEFAULT_FRAME_SECONDS);
|
||||||
}
|
}
|
||||||
// cppcheck-suppress useStlAlgorithm
|
std::ranges::transform(speeds, speeds.begin(), sanitizeSpeed);
|
||||||
for (auto& s : speeds) { s = sanitizeSpeed(s); }
|
|
||||||
if (speeds.size() == frame_count) { return speeds; }
|
if (speeds.size() == frame_count) { return speeds; }
|
||||||
if (speeds.size() > frame_count) {
|
if (speeds.size() > frame_count) {
|
||||||
speeds.resize(frame_count);
|
speeds.resize(frame_count);
|
||||||
@@ -50,10 +51,7 @@ namespace {
|
|||||||
// component vàlid.
|
// component vàlid.
|
||||||
auto parseFrames(const fkyaml::node& anim) -> std::vector<int> {
|
auto parseFrames(const fkyaml::node& anim) -> std::vector<int> {
|
||||||
std::vector<int> frames;
|
std::vector<int> frames;
|
||||||
for (const auto& f : anim["frames"]) {
|
std::ranges::transform(anim["frames"], std::back_inserter(frames), [](const fkyaml::node& f) { return f.template get_value<int>(); });
|
||||||
// cppcheck-suppress useStlAlgorithm
|
|
||||||
frames.push_back(f.get_value<int>());
|
|
||||||
}
|
|
||||||
if (frames.empty()) { frames.push_back(0); }
|
if (frames.empty()) { frames.push_back(0); }
|
||||||
return frames;
|
return frames;
|
||||||
}
|
}
|
||||||
@@ -66,10 +64,7 @@ namespace {
|
|||||||
std::vector<float> speeds_secs;
|
std::vector<float> speeds_secs;
|
||||||
const auto& fm = anim["frame_ms"];
|
const auto& fm = anim["frame_ms"];
|
||||||
if (fm.is_sequence()) {
|
if (fm.is_sequence()) {
|
||||||
for (const auto& v : fm) {
|
std::ranges::transform(fm, std::back_inserter(speeds_secs), [](const fkyaml::node& v) { return static_cast<float>(v.template get_value<int>()) / 1000.0F; });
|
||||||
// cppcheck-suppress useStlAlgorithm
|
|
||||||
speeds_secs.push_back(static_cast<float>(v.get_value<int>()) / 1000.0F);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
const float SECS = static_cast<float>(fm.get_value<int>()) / 1000.0F;
|
const float SECS = static_cast<float>(fm.get_value<int>()) / 1000.0F;
|
||||||
speeds_secs.assign(frame_count, SECS);
|
speeds_secs.assign(frame_count, SECS);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <iterator>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -41,10 +42,7 @@ namespace {
|
|||||||
if (!yaml.contains("animations") || !yaml["animations"].contains("swing")) { return; }
|
if (!yaml.contains("animations") || !yaml["animations"].contains("swing")) { return; }
|
||||||
const auto& sw = yaml["animations"]["swing"];
|
const auto& sw = yaml["animations"]["swing"];
|
||||||
if (!sw.contains("active_frames")) { return; }
|
if (!sw.contains("active_frames")) { return; }
|
||||||
for (const auto& f : sw["active_frames"]) {
|
std::ranges::transform(sw["active_frames"], std::back_inserter(active_out), [](const fkyaml::node& f) { return f.template get_value<int>(); });
|
||||||
// cppcheck-suppress useStlAlgorithm
|
|
||||||
active_out.push_back(f.get_value<int>());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "game/entities/entity_loader.hpp"
|
#include "game/entities/entity_loader.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iterator>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
@@ -51,8 +53,7 @@ namespace EntityLoader {
|
|||||||
const auto& list = root["entities"];
|
const auto& list = root["entities"];
|
||||||
if (!list.is_sequence()) { return spawns; }
|
if (!list.is_sequence()) { return spawns; }
|
||||||
spawns.reserve(list.size());
|
spawns.reserve(list.size());
|
||||||
// cppcheck-suppress useStlAlgorithm
|
std::ranges::copy(list, std::back_inserter(spawns));
|
||||||
for (const auto& item : list) { spawns.push_back(item); }
|
|
||||||
return spawns;
|
return spawns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ auto EntityManager::add(std::unique_ptr<Entity> entity) -> Entity* {
|
|||||||
entity->setEntityManager(this);
|
entity->setEntityManager(this);
|
||||||
Entity* raw = entity.get();
|
Entity* raw = entity.get();
|
||||||
entities_.push_back(std::move(entity));
|
entities_.push_back(std::move(entity));
|
||||||
// cppcheck-suppress returnDanglingLifetime
|
// cppcheck-suppress returnDanglingLifetime -- fals positiu: std::move transfereix ownership del unique_ptr al vector, però l'objecte apuntat per `raw` no es mou ni es destrueix. El punter continua vàlid mentre el vector el posseesca.
|
||||||
return raw;
|
return raw;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,7 +124,7 @@ auto EntityManager::addSarcophagus(std::unique_ptr<Sarcophagus> sarc) -> Sarcoph
|
|||||||
Sarcophagus* raw = sarc.get();
|
Sarcophagus* raw = sarc.get();
|
||||||
entities_.push_back(std::move(sarc));
|
entities_.push_back(std::move(sarc));
|
||||||
++total_sarcs_;
|
++total_sarcs_;
|
||||||
// cppcheck-suppress returnDanglingLifetime
|
// cppcheck-suppress returnDanglingLifetime -- fals positiu: std::move transfereix ownership al vector però l'objecte no es mou. `raw` continua vàlid (vegeu `add` més amunt).
|
||||||
return raw;
|
return raw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user