diff options
author | HampusM <hampus@hampusmat.com> | 2022-03-27 19:21:22 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:57 +0200 |
commit | 5c1c672e018e41e5c87a0263a851684a52d9c1fb (patch) | |
tree | 815a067fb558fe3c55b19842402d68e0dc0953f2 /src | |
parent | 15c7a8fda742cc393310d75e0e1be89c3042eac0 (diff) |
refactor: replace string matrix with template matrix
Diffstat (limited to 'src')
-rw-r--r-- | src/bootstrap.cpp | 4 | ||||
-rw-r--r-- | src/engine/graphics/matrix.hpp | 53 | ||||
-rw-r--r-- | src/engine/graphics/matrix.tpp | 156 | ||||
-rw-r--r-- | src/engine/graphics/string_matrix.cpp | 140 | ||||
-rw-r--r-- | src/engine/graphics/string_matrix.hpp | 52 | ||||
-rw-r--r-- | src/interfaces/matrix.hpp | 4 |
6 files changed, 213 insertions, 196 deletions
diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index 8dec609..e8315bd 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -19,8 +19,8 @@ #include "engine/data/bounds.hpp" #include "engine/data/vector2.hpp" #include "engine/engine.hpp" +#include "engine/graphics/matrix.hpp" #include "engine/graphics/scene.hpp" -#include "engine/graphics/string_matrix.hpp" #include "engine/graphics/window.hpp" #include "engine/user/cursor.hpp" #include "engine/user/input.hpp" @@ -83,7 +83,7 @@ Container bootstrap() noexcept [](const Bounds &bounds) { return std::dynamic_pointer_cast<IMatrix<std::string_view>>( - std::make_shared<StringMatrix>(bounds)); + std::make_shared<Matrix<std::string_view>>(bounds)); }); container.bind<ISceneFactory>().to_factory(normalize_lambda( diff --git a/src/engine/graphics/matrix.hpp b/src/engine/graphics/matrix.hpp new file mode 100644 index 0000000..2c9ba81 --- /dev/null +++ b/src/engine/graphics/matrix.hpp @@ -0,0 +1,53 @@ +#pragma once + +#include "interfaces/matrix.hpp" + +#include "engine/data/bounds.hpp" +#include "engine/data/vector2.hpp" +#include "engine/matrix_iterator.hpp" + +#include <gsl/pointers> +#include <memory> + +template <typename Element> +class Matrix : public IMatrix<Element> +{ +public: + explicit Matrix(const Bounds &bounds) noexcept; + + Matrix(const Matrix &matrix) noexcept; + + Matrix(Matrix &&matrix) noexcept; + + ~Matrix() noexcept override; + + void fill(Element element) noexcept override; + + [[nodiscard]] Element get(const Vector2 &pos) const noexcept override; + + void set(const Vector2 &pos, Element element) noexcept override; + + [[nodiscard]] uint32_t get_row_cnt() const noexcept override; + + [[nodiscard]] uint32_t get_column_cnt() const noexcept override; + + [[nodiscard]] MatrixIterator<Element> begin() const noexcept override; + + [[nodiscard]] MatrixIterator<Element> end() const noexcept override; + + Matrix &operator=(const Matrix &rhs) noexcept; + + Matrix &operator=(Matrix &&rhs) noexcept; + +private: + gsl::owner<Element **> _matrix; + + uint32_t _row_cnt; + uint32_t _column_cnt; + + void _delete_matrix() noexcept; + + void _copy_matrix_from(const Matrix &source) noexcept; +}; + +#include "matrix.tpp" diff --git a/src/engine/graphics/matrix.tpp b/src/engine/graphics/matrix.tpp new file mode 100644 index 0000000..e0e65bb --- /dev/null +++ b/src/engine/graphics/matrix.tpp @@ -0,0 +1,156 @@ +#pragma once + +#include "matrix.hpp" + +template <typename Element> +Matrix<Element>::Matrix(const Bounds &bounds) noexcept + : _matrix(new Element *[bounds.get_height()]), + _row_cnt(bounds.get_height()), + _column_cnt(bounds.get_width()) +{ + for (gsl::owner<Element **> row = _matrix; row != _matrix + _row_cnt; row++) + { + *row = static_cast<gsl::owner<Element *>>(new Element[bounds.get_width()]); + } +}; + +template <typename Element> +Matrix<Element>::Matrix(const Matrix &matrix) noexcept + : _matrix(new Element *[matrix._row_cnt]), + _row_cnt(matrix._row_cnt), + _column_cnt(matrix._column_cnt) +{ + _copy_matrix_from(matrix); +} + +template <typename Element> +Matrix<Element>::Matrix(Matrix &&matrix) noexcept + : _matrix(matrix._matrix), _row_cnt(matrix._row_cnt), _column_cnt(matrix._column_cnt) +{ + matrix._matrix = nullptr; +} + +template <typename Element> +Matrix<Element>::~Matrix() noexcept +{ + _delete_matrix(); +} + +template <typename Element> +void Matrix<Element>::fill(Element element) noexcept +{ + for (auto row : *this) + { + for (auto &col : row) + { + col = element; + } + } +} + +template <typename Element> +Element Matrix<Element>::get(const Vector2 &pos) const noexcept +{ + + auto x = static_cast<std::size_t>(pos.get_x()); + auto y = static_cast<std::size_t>(pos.get_y()); + + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + return _matrix[y][x]; +} + +template <typename Element> +void Matrix<Element>::set(const Vector2 &pos, Element element) noexcept +{ + auto x = static_cast<std::size_t>(pos.get_x()); + auto y = static_cast<std::size_t>(pos.get_y()); + + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + _matrix[y][x] = element; +} + +template <typename Element> +uint32_t Matrix<Element>::get_row_cnt() const noexcept +{ + return _row_cnt; +} + +template <typename Element> +uint32_t Matrix<Element>::get_column_cnt() const noexcept +{ + return _column_cnt; +} + +template <typename Element> +MatrixIterator<Element> Matrix<Element>::begin() const noexcept +{ + return MatrixIterator(_matrix, _column_cnt); +} + +template <typename Element> +MatrixIterator<Element> Matrix<Element>::end() const noexcept +{ + return MatrixIterator(_matrix + _row_cnt, _column_cnt); +} + +template <typename Element> +Matrix<Element> &Matrix<Element>::operator=(const Matrix &rhs) noexcept +{ + if (&rhs != this) + { + _delete_matrix(); + + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) + _matrix = nullptr; + + _matrix = new Element *[rhs._row_cnt]; + _copy_matrix_from(rhs); + } + + return *this; +} + +template <typename Element> +Matrix<Element> &Matrix<Element>::operator=(Matrix &&rhs) noexcept +{ + if (&rhs != this) + { + _delete_matrix(); + + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) + _matrix = rhs._matrix; + + rhs._matrix = nullptr; + } + + return *this; +} + +template <typename Element> +void Matrix<Element>::_delete_matrix() noexcept +{ + for (gsl::owner<Element **> row = _matrix; row != _matrix + _row_cnt; row++) + { + delete[](*row); + } + + delete[] _matrix; +} + +template <typename Element> +void Matrix<Element>::_copy_matrix_from(const Matrix &source) noexcept +{ + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) + gsl::owner<Element **> source_row = source._matrix; + + for (gsl::owner<Element **> row = _matrix; row != _matrix + _row_cnt; + row++, source_row++) + { + *row = static_cast<gsl::owner<Element *>>(new Element[_column_cnt]); + + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + auto *end = *source_row + _column_cnt; + + std::copy(*source_row, end, *row); + } +} diff --git a/src/engine/graphics/string_matrix.cpp b/src/engine/graphics/string_matrix.cpp deleted file mode 100644 index 3920b8e..0000000 --- a/src/engine/graphics/string_matrix.cpp +++ /dev/null @@ -1,140 +0,0 @@ -#include "string_matrix.hpp" - -#include <iostream> -#include <ranges> - -StringMatrix::StringMatrix(const Bounds &bounds) noexcept - : _matrix(new std::string_view *[bounds.get_height()]), - _row_cnt(bounds.get_height()), - _column_cnt(bounds.get_width()) -{ - for (gsl::owner<std::string_view **> row = _matrix; row != _matrix + _row_cnt; row++) - { - *row = static_cast<gsl::owner<std::string_view *>>( - new std::string_view[bounds.get_width()]); - } -}; - -StringMatrix::StringMatrix(const StringMatrix &string_matrix) noexcept - : _matrix(new std::string_view *[string_matrix._row_cnt]), - _row_cnt(string_matrix._row_cnt), - _column_cnt(string_matrix._column_cnt) -{ - _copy_matrix_from(string_matrix); -} - -StringMatrix::StringMatrix(StringMatrix &&string_matrix) noexcept - : _matrix(string_matrix._matrix), - _row_cnt(string_matrix._row_cnt), - _column_cnt(string_matrix._column_cnt) -{ - string_matrix._matrix = nullptr; -} - -StringMatrix::~StringMatrix() noexcept -{ - _delete_matrix(); -} - -void StringMatrix::fill(std::string_view element) noexcept -{ - for (auto row : *this) - { - for (auto &col : row) - { - col = element; - } - } -} - -std::string_view StringMatrix::get(const Vector2 &pos) const noexcept -{ - - auto x = static_cast<std::size_t>(pos.get_x()); - auto y = static_cast<std::size_t>(pos.get_y()); - - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - return _matrix[y][x]; -} - -void StringMatrix::set(const Vector2 &pos, std::string_view element) noexcept -{ - auto x = static_cast<std::size_t>(pos.get_x()); - auto y = static_cast<std::size_t>(pos.get_y()); - - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - _matrix[y][x] = element; -} - -uint32_t StringMatrix::get_row_cnt() const noexcept -{ - return _row_cnt; -} - -uint32_t StringMatrix::get_column_cnt() const noexcept -{ - return _column_cnt; -} - -MatrixIterator<std::string_view> StringMatrix::begin() const noexcept -{ - return MatrixIterator(_matrix, _column_cnt); -} - -MatrixIterator<std::string_view> StringMatrix::end() const noexcept -{ - return MatrixIterator(_matrix + _row_cnt, _column_cnt); -} - -StringMatrix &StringMatrix::operator=(const StringMatrix &rhs) noexcept -{ - if (&rhs != this) - { - _delete_matrix(); - _matrix = nullptr; - - _matrix = new std::string_view *[rhs._row_cnt]; - _copy_matrix_from(rhs); - } - - return *this; -} - -StringMatrix &StringMatrix::operator=(StringMatrix &&rhs) noexcept -{ - if (&rhs != this) - { - _delete_matrix(); - _matrix = rhs._matrix; - rhs._matrix = nullptr; - } - - return *this; -} - -void StringMatrix::_delete_matrix() noexcept -{ - for (gsl::owner<std::string_view **> row = _matrix; row != _matrix + _row_cnt; row++) - { - delete[](*row); - } - - delete[] _matrix; -} - -void StringMatrix::_copy_matrix_from(const StringMatrix &source) noexcept -{ - gsl::owner<std::string_view **> source_row = source._matrix; - - for (gsl::owner<std::string_view **> row = _matrix; row != _matrix + _row_cnt; - row++, source_row++) - { - *row = static_cast<gsl::owner<std::string_view *>>( - new std::string_view[_column_cnt]); - - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - auto *end = *source_row + _column_cnt; - - std::copy(*source_row, end, *row); - } -} diff --git a/src/engine/graphics/string_matrix.hpp b/src/engine/graphics/string_matrix.hpp deleted file mode 100644 index ff0939c..0000000 --- a/src/engine/graphics/string_matrix.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once - -#include "interfaces/matrix.hpp" - -#include "engine/data/bounds.hpp" -#include "engine/data/vector2.hpp" -#include "engine/matrix_iterator.hpp" - -#include <gsl/pointers> -#include <memory> -#include <string_view> -#include <vector> - -class StringMatrix : public IMatrix<std::string_view> -{ -public: - explicit StringMatrix(const Bounds &bounds) noexcept; - - StringMatrix(const StringMatrix &string_matrix) noexcept; - - StringMatrix(StringMatrix &&string_matrix) noexcept; - - ~StringMatrix() noexcept override; - - void fill(std::string_view element) noexcept override; - - [[nodiscard]] std::string_view get(const Vector2 &pos) const noexcept override; - - void set(const Vector2 &pos, std::string_view element) noexcept override; - - [[nodiscard]] uint32_t get_row_cnt() const noexcept override; - - [[nodiscard]] uint32_t get_column_cnt() const noexcept override; - - [[nodiscard]] MatrixIterator<std::string_view> begin() const noexcept override; - - [[nodiscard]] MatrixIterator<std::string_view> end() const noexcept override; - - StringMatrix &operator=(const StringMatrix &rhs) noexcept; - - StringMatrix &operator=(StringMatrix &&rhs) noexcept; - -private: - gsl::owner<std::string_view **> _matrix; - - uint32_t _row_cnt; - uint32_t _column_cnt; - - void _delete_matrix() noexcept; - - void _copy_matrix_from(const StringMatrix &source) noexcept; -}; diff --git a/src/interfaces/matrix.hpp b/src/interfaces/matrix.hpp index c22c0a7..2b33342 100644 --- a/src/interfaces/matrix.hpp +++ b/src/interfaces/matrix.hpp @@ -29,9 +29,9 @@ public: [[nodiscard]] virtual uint32_t get_column_cnt() const noexcept = 0; - [[nodiscard]] virtual MatrixIterator<std::string_view> begin() const noexcept = 0; + [[nodiscard]] virtual MatrixIterator<Element> begin() const noexcept = 0; - [[nodiscard]] virtual MatrixIterator<std::string_view> end() const noexcept = 0; + [[nodiscard]] virtual MatrixIterator<Element> end() const noexcept = 0; IMatrix &operator=(const IMatrix &matrix) noexcept = default; |