From 8342bf39690ff4693fda66907d50cad40d9b3e36 Mon Sep 17 00:00:00 2001 From: zulianc Date: Thu, 19 Jun 2025 22:31:41 +0200 Subject: [PATCH] petits [[nodiscard]] pour le fun --- README.md | 5 +++-- src/Benchmark/main.cpp | 25 ++++++++++++++++++++----- src/Core/Menu.h | 2 +- src/Core/PiecesList.cpp | 9 ++++----- src/Core/PiecesList.h | 6 +++--- src/Pieces/PiecesFiles.h | 14 +++++++------- 6 files changed, 38 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 2361fdb..1db1e94 100644 --- a/README.md +++ b/README.md @@ -79,9 +79,10 @@ To switch between debug and release mode: ``xmake f -m debug`` ``xmake f -m release`` -If for some reasons you wanna run the command line version (not updated): +If for some reasons you wanna run the command line version: ``xmake build text`` -``xmake run text`` +``xmake run text`` +The command line version is **not** updated. ### Package the project diff --git a/src/Benchmark/main.cpp b/src/Benchmark/main.cpp index 8af817d..562b24c 100644 --- a/src/Benchmark/main.cpp +++ b/src/Benchmark/main.cpp @@ -35,25 +35,35 @@ void benchmarking(int min_size, int max_size) { Generator gen; PiecesFiles pf; + auto t1 = high_resolution_clock::now(); + auto t2 = high_resolution_clock::now(); + duration ms_double; + bool ok; + for (int i = min_size; i <= max_size; i++) { std::cout << "| " << i; - auto t1 = high_resolution_clock::now(); + t1 = high_resolution_clock::now(); std::vector polyominoes = gen.generatePolyominoes(i); - auto t2 = high_resolution_clock::now(); - duration ms_double = t2 - t1; + t2 = high_resolution_clock::now(); + ms_double = t2 - t1; std::cout << " | " << polyominoes.size(); std::flush(std::cout); std::cout << " | " << (int) ms_double.count() / 1000 << "s " << std::fmod(ms_double.count(), 1000) << "ms "; std::flush(std::cout); t1 = high_resolution_clock::now(); - pf.savePieces(i, polyominoes); + ok = pf.savePieces(i, polyominoes); t2 = high_resolution_clock::now(); ms_double = t2 - t1; std::cout << " | " << (int) ms_double.count() / 1000 << "s " << std::fmod(ms_double.count(), 1000) << "ms "; std::flush(std::cout); + if (!ok) { + std::cerr << "\nERROR: Could not save pieces to file." << std::endl; + return; + } + polyominoes.clear(); polyominoes.shrink_to_fit(); @@ -63,12 +73,17 @@ void benchmarking(int min_size, int max_size) { std::vector otherPieces; t1 = high_resolution_clock::now(); - pf.loadPieces(i, pieces, convexPieces, holelessPieces, otherPieces); + ok = pf.loadPieces(i, pieces, convexPieces, holelessPieces, otherPieces); t2 = high_resolution_clock::now(); ms_double = t2 - t1; std::cout << " | " << (int) ms_double.count() / 1000 << "s " << std::fmod(ms_double.count(), 1000) << "ms "; std::flush(std::cout); + if (!ok) { + std::cerr << "\nERROR: Could not load pieces from file." << std::endl; + return; + } + pieces.clear(); pieces.shrink_to_fit(); convexPieces.clear(); diff --git a/src/Core/Menu.h b/src/Core/Menu.h index 74f180e..6a99d3a 100644 --- a/src/Core/Menu.h +++ b/src/Core/Menu.h @@ -4,7 +4,7 @@ #include "Player.h" #include "Game.h" -static const int FRAMES_PER_SECOND = 60; // the number of frames per second, all the values in the app were choosen with this number in mind +static const int FRAMES_PER_SECOND = 60; // the number of frames per second, all the values in the app were choosen with this number in mind /** diff --git a/src/Core/PiecesList.cpp b/src/Core/PiecesList.cpp index 45ba910..a6bd0d3 100644 --- a/src/Core/PiecesList.cpp +++ b/src/Core/PiecesList.cpp @@ -27,13 +27,12 @@ PiecesList::PiecesList() { this->pushBackEmptyVectors(); } -bool PiecesList::loadPieces(int size) { - if (size < 1) return false; - if (size <= this->highestLoadedSize) return true; +bool PiecesList::loadPieces(int max_size) { + if (max_size < 1) return false; + if (max_size <= this->highestLoadedSize) return true; PiecesFiles piecesFiles; - for (int i = this->highestLoadedSize + 1; i <= size; i++) { - + for (int i = this->highestLoadedSize + 1; i <= max_size; i++) { if (!piecesFiles.loadPieces(i, this->loadedPieces.at(i), this->convexPieces.at(i), this->holelessPieces.at(i), this->otherPieces.at(i))) { return false; } diff --git a/src/Core/PiecesList.h b/src/Core/PiecesList.h index c35227c..8c4991f 100644 --- a/src/Core/PiecesList.h +++ b/src/Core/PiecesList.h @@ -30,10 +30,10 @@ class PiecesList { PiecesList(); /** - * Makes the list load all pieces of the specified size - * @return If it sucessfully loaded the pieces + * Makes the list load all pieces up to the specified size + * @return If all pieces up to the specified size are correctly loaded */ - bool loadPieces(int size); + [[nodiscard]] bool loadPieces(int max_size); /** * Selects the specified piece diff --git a/src/Pieces/PiecesFiles.h b/src/Pieces/PiecesFiles.h index 1c7cf13..72f79c2 100644 --- a/src/Pieces/PiecesFiles.h +++ b/src/Pieces/PiecesFiles.h @@ -18,21 +18,21 @@ class PiecesFiles { /** * Generate a file containing all the pieces of the specified size - * @return If the file could be created + * @return If the pieces are saved correctly */ - bool savePieces(int polyominoSize) const; + [[nodiscard]] bool savePieces(int polyominoSize) const; /** * Generate a file containing all the pieces of the specified size, assuming they have been correctly generated - * @return If the file could be created + * @return If the piece are saved correctly */ - bool savePieces(int polyominoSize, std::vector& polyominoes) const; + [[nodiscard]] bool savePieces(int polyominoSize, std::vector& polyominoes) const; /** - * Replace the content of the vectors by the pieces of the specified size, if the file wasn't found the vectors stays untouched - * @return If the file was found + * Replace the content of the vectors by the pieces of the specified size, if the pieces can't be loaded correctly the vectors stays untouched + * @return If the pieces could be loaded correctly */ - bool loadPieces(int polyominoSize, std::vector& pieces, std::vector& convexPieces, std::vector& holelessPieces, std::vector& otherPieces) const; + [[nodiscard]] bool loadPieces(int polyominoSize, std::vector& pieces, std::vector& convexPieces, std::vector& holelessPieces, std::vector& otherPieces) const; /** * Puts the path to the piece file of the specified size in order, if the data folder wasn't found the string stays untouched