From 568cfa83fdee07517cdf3f7b7bd271c17f67304a Mon Sep 17 00:00:00 2001 From: Joseph Walton-Rivers <joseph@walton-rivers.uk> Date: Sun, 4 Sep 2022 18:16:10 +0100 Subject: [PATCH] stronger bounds checks --- include/fggl/entity/gridworld/zone.hpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/fggl/entity/gridworld/zone.hpp b/include/fggl/entity/gridworld/zone.hpp index 38a14de..9e184dd 100644 --- a/include/fggl/entity/gridworld/zone.hpp +++ b/include/fggl/entity/gridworld/zone.hpp @@ -34,17 +34,22 @@ namespace fggl::entity::grid { template<typename T, uint32_t width, uint32_t height> struct Grid { public: + constexpr static std::array<math::vec2i, 4> DIRECTIONS{{ {-1, 0}, {0, -1}, {1, 0}, {0, 1} }}; + Grid() = default; inline T& get(GridPos pos) { + assert(inBounds(pos)); return m_cells[getCellIndex(pos)]; } const T& get(GridPos pos) const { + assert(inBounds(pos)); return m_cells[getCellIndex(pos)]; } inline void set(GridPos pos, T value) { + assert(inBounds(pos)); m_cells[getCellIndex(pos)] = value; } @@ -57,7 +62,6 @@ namespace fggl::entity::grid { constexpr static math::vec2i size = math::vec2i(width, height); std::array<T, size.x * size.y> m_cells; - inline uint32_t getCellIndex(GridPos pos) const { assert( inBounds(pos)); return pos.y * size.x + pos.x; @@ -98,6 +102,7 @@ namespace fggl::entity::grid { clear(); } + [[nodiscard]] inline bool inBounds(GridPos pos) const { return 0 <= pos.x && pos.x <= width && 0 <= pos.y && pos.y <= height; -- GitLab