diff options
author | HampusM <hampus@hampusmat.com> | 2022-06-29 22:15:36 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-29 22:27:36 +0200 |
commit | 2bff8c999edde11270ecaf6fbd2d24f54d0e360b (patch) | |
tree | 7f879bdd592ba972b195e7fe55bb637f1b3c00b1 | |
parent | d02d46d27982a8e351736067ab9787f87052b989 (diff) |
refactor: cell helper take matrix as shared pointer
-rw-r--r-- | src/bootstrap.cpp | 4 | ||||
-rw-r--r-- | src/game/cell_helper.hpp | 4 | ||||
-rw-r--r-- | src/game/cell_helper_impl.hpp | 11 | ||||
-rw-r--r-- | src/interfaces/cell_helper.hpp | 4 | ||||
-rw-r--r-- | test/cell_helper_test.cpp | 74 |
5 files changed, 48 insertions, 49 deletions
diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index 368f2a8..b8649ec 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -73,7 +73,7 @@ auto bootstrap() noexcept -> yacppdic::DIContainer generation_tracker, status_manager, user_input_observer, - cell_helper_factory(*(scene->get_matrix())), + cell_helper_factory(scene->get_matrix()), rle_reader); }); @@ -105,7 +105,7 @@ auto bootstrap() noexcept -> yacppdic::DIContainer }); di_container.bind<ICellHelperFactory<char>>().to_factory( - [](const IMatrix<char> &matrix) + [](const std::shared_ptr<IMatrix<char>> &matrix) { return std::make_unique<CellHelper<char>>(matrix); }); diff --git a/src/game/cell_helper.hpp b/src/game/cell_helper.hpp index ee5ab27..cf84a75 100644 --- a/src/game/cell_helper.hpp +++ b/src/game/cell_helper.hpp @@ -13,7 +13,7 @@ template <typename MatrixElement> class CellHelper : public ICellHelper { public: - explicit CellHelper(const IMatrix<MatrixElement> &matrix) noexcept; + explicit CellHelper(std::shared_ptr<IMatrix<MatrixElement>> matrix) noexcept; [[nodiscard]] auto is_cell_dying(const Vector2 &cell_pos) const noexcept -> bool override; @@ -26,7 +26,7 @@ public: -> std::vector<Vector2> override; private: - const IMatrix<MatrixElement> &_matrix; + std::shared_ptr<IMatrix<MatrixElement>> _matrix; static auto _get_position_neighbours(const Vector2 &position) noexcept -> std::list<Vector2>; diff --git a/src/game/cell_helper_impl.hpp b/src/game/cell_helper_impl.hpp index b7da413..bd40794 100644 --- a/src/game/cell_helper_impl.hpp +++ b/src/game/cell_helper_impl.hpp @@ -6,18 +6,19 @@ template <typename MatrixElement> constexpr auto has_matrix_value( - const IMatrix<MatrixElement> &matrix, + const std::shared_ptr<IMatrix<MatrixElement>> &matrix, const MatrixElement &value) noexcept { return [&matrix, &value](const Vector2 &pos) { - return matrix.get(pos) == value; + return matrix->get(pos) == value; }; } template <typename MatrixElement> -CellHelper<MatrixElement>::CellHelper(const IMatrix<MatrixElement> &matrix) noexcept - : _matrix(matrix) +CellHelper<MatrixElement>::CellHelper( + std::shared_ptr<IMatrix<MatrixElement>> matrix) noexcept + : _matrix(std::move(matrix)) { } @@ -75,7 +76,7 @@ auto CellHelper<MatrixElement>::find_neighbours(const Vector2 &cell_pos) const n std::vector<Vector2> cell_positions = {}; const auto matrix_size = - Bounds({.width = _matrix.get_column_cnt(), .height = _matrix.get_row_cnt()}); + Bounds({.width = _matrix->get_column_cnt(), .height = _matrix->get_row_cnt()}); const auto neighbours = _get_position_neighbours(cell_pos); diff --git a/src/interfaces/cell_helper.hpp b/src/interfaces/cell_helper.hpp index d814aa7..69bf8b0 100644 --- a/src/interfaces/cell_helper.hpp +++ b/src/interfaces/cell_helper.hpp @@ -28,5 +28,5 @@ public: }; template <typename MatrixElement> -using ICellHelperFactory = - yacppdic::Factory<std::unique_ptr<ICellHelper>(const IMatrix<MatrixElement> &matrix)>; +using ICellHelperFactory = yacppdic::Factory<std::unique_ptr<ICellHelper>( + const std::shared_ptr<IMatrix<MatrixElement>> &matrix)>; diff --git a/test/cell_helper_test.cpp b/test/cell_helper_test.cpp index c9618fd..36f2a62 100644 --- a/test/cell_helper_test.cpp +++ b/test/cell_helper_test.cpp @@ -1,22 +1,20 @@ -#include "engine/data/vector2.hpp" -#include "game/cell_helper.hpp" - -#include "mocks/matrix.hpp" - #include <catch2/catch_test_macros.hpp> #include <catch2/matchers/catch_matchers_vector.hpp> -#include <trompeloeil.hpp> - -#include <list> #include <memory> +#include <trompeloeil.hpp> #include <vector> +#include "engine/data/vector2.hpp" +#include "game/cell_helper.hpp" + +#include "mocks/matrix.hpp" + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) TEST_CASE("CellHelper") { SECTION("can identify dying cell") { - auto mock_matrix = MockMatrix<char>(); + auto mock_matrix = std::make_shared<MockMatrix<char>>(); auto cell_helper = CellHelper<char>(mock_matrix); @@ -37,135 +35,135 @@ TEST_CASE("CellHelper") const auto cell_pos_down_right = cell_pos + Vector2::down() + Vector2::right(); // NOLINTNEXTLINE(readability-identifier-length) - REQUIRE_CALL(mock_matrix, get_row_cnt()).RETURN(MATRIX_ROWS).TIMES(AT_LEAST(1)); + REQUIRE_CALL(*mock_matrix, get_row_cnt()).RETURN(MATRIX_ROWS).TIMES(AT_LEAST(1)); - REQUIRE_CALL(mock_matrix, get_column_cnt()) + REQUIRE_CALL(*mock_matrix, get_column_cnt()) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(MATRIX_COLUMNS) .TIMES(AT_LEAST(1)); auto matrix_call_seq = trompeloeil::sequence(); - REQUIRE_CALL(mock_matrix, get(cell_pos_up)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_up)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN('x') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_down)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_down)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_left)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_left)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN('x') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_right)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_right)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN('x') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_up_left)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_up_left)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_up_right)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_up_right)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN('x') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_down_left)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_down_left)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_down_right)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_down_right)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); REQUIRE(cell_helper.is_cell_dying(cell_pos) == true); - REQUIRE_CALL(mock_matrix, get(cell_pos_up)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_up)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_down)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_down)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN('x') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_left)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_left)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_right)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_right)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_up_left)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_up_left)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_up_right)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_up_right)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_down_left)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_down_left)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN('x') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_down_right)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_down_right)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); REQUIRE(cell_helper.is_cell_dying(cell_pos) == false); - REQUIRE_CALL(mock_matrix, get(cell_pos_up)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_up)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_down)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_down)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_left)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_left)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN('x') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_right)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_right)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_up_left)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_up_left)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_up_right)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_up_right)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_down_left)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_down_left)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); - REQUIRE_CALL(mock_matrix, get(cell_pos_down_right)) + REQUIRE_CALL(*mock_matrix, get(cell_pos_down_right)) // NOLINTNEXTLINE(readability-identifier-length) .RETURN(' ') .IN_SEQUENCE(matrix_call_seq); @@ -175,7 +173,7 @@ TEST_CASE("CellHelper") SECTION("can find neighbours") { - auto mock_matrix = MockMatrix<char>(); + auto mock_matrix = std::make_shared<MockMatrix<char>>(); auto cell_helper = CellHelper<char>(mock_matrix); @@ -185,10 +183,10 @@ TEST_CASE("CellHelper") const auto cell_pos = Vector2({.x = 8, .y = 3}); // NOLINTNEXTLINE(readability-identifier-length) - REQUIRE_CALL(mock_matrix, get_row_cnt()).RETURN(MATRIX_ROWS); + REQUIRE_CALL(*mock_matrix, get_row_cnt()).RETURN(MATRIX_ROWS); // NOLINTNEXTLINE(readability-identifier-length) - REQUIRE_CALL(mock_matrix, get_column_cnt()).RETURN(MATRIX_COLUMNS); + REQUIRE_CALL(*mock_matrix, get_column_cnt()).RETURN(MATRIX_COLUMNS); const auto living_neighbour_cells = cell_helper.find_neighbours(cell_pos); |