diff options
author | HampusM <hampus@hampusmat.com> | 2022-03-14 18:02:18 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:56 +0200 |
commit | dc6222611ad14a33f642396558ba84ecba9d6605 (patch) | |
tree | d759020233b66be62c5539209a03842d283b67a9 /src/engine | |
parent | dbab54ebf134b6ab2cf719d7c26a191fbffeed34 (diff) |
perf: add noexcept almost everywhere
Diffstat (limited to 'src/engine')
-rw-r--r-- | src/engine/data/bounds.cpp | 18 | ||||
-rw-r--r-- | src/engine/data/bounds.hpp | 16 | ||||
-rw-r--r-- | src/engine/data/vector2.cpp | 22 | ||||
-rw-r--r-- | src/engine/data/vector2.hpp | 26 | ||||
-rw-r--r-- | src/engine/engine.cpp | 2 | ||||
-rw-r--r-- | src/engine/engine.hpp | 3 | ||||
-rw-r--r-- | src/engine/graphics/scene.cpp | 2 | ||||
-rw-r--r-- | src/engine/graphics/string_matrix.cpp | 12 | ||||
-rw-r--r-- | src/engine/graphics/string_matrix.hpp | 12 | ||||
-rw-r--r-- | src/engine/user/cursor.cpp | 16 | ||||
-rw-r--r-- | src/engine/user/cursor.hpp | 6 | ||||
-rw-r--r-- | src/engine/user/input.cpp | 10 | ||||
-rw-r--r-- | src/engine/user/input.hpp | 12 |
13 files changed, 77 insertions, 80 deletions
diff --git a/src/engine/data/bounds.cpp b/src/engine/data/bounds.cpp index 41b6dc0..fdf8c6b 100644 --- a/src/engine/data/bounds.cpp +++ b/src/engine/data/bounds.cpp @@ -1,48 +1,48 @@ #include "bounds.hpp" -Bounds::Bounds(const BoundsOptions &options) +Bounds::Bounds(const BoundsOptions &options) noexcept : _width(options.width), _height(options.height) { } -uint32_t Bounds::get_width() const noexcept +Bounds::Value Bounds::get_width() const noexcept { return _width; } -void Bounds::set_width(uint32_t width) noexcept +void Bounds::set_width(Bounds::Value width) noexcept { _width = width; } -uint32_t Bounds::get_height() const noexcept +Bounds::Value Bounds::get_height() const noexcept { return _height; } -void Bounds::set_height(uint32_t height) noexcept +void Bounds::set_height(Bounds::Value height) noexcept { _height = height; } CoordsValidation Bounds::validate_coords(const Vector2 &coords) const noexcept { - if (static_cast<uint32_t>(coords.get_x()) >= _width) + if (static_cast<Bounds::Value>(coords.get_x()) >= _width) { return CoordsValidation::X_HIGH; } - if (static_cast<uint32_t>(coords.get_x()) <= 0) + if (static_cast<Bounds::Value>(coords.get_x()) <= 0) { return CoordsValidation::X_LOW; } - if (static_cast<uint32_t>(coords.get_y()) >= _height) + if (static_cast<Bounds::Value>(coords.get_y()) >= _height) { return CoordsValidation::Y_HIGH; } - if (static_cast<uint32_t>(coords.get_y()) <= 0) + if (static_cast<Bounds::Value>(coords.get_y()) <= 0) { return CoordsValidation::Y_LOW; } diff --git a/src/engine/data/bounds.hpp b/src/engine/data/bounds.hpp index 9b72f59..4a29158 100644 --- a/src/engine/data/bounds.hpp +++ b/src/engine/data/bounds.hpp @@ -22,15 +22,17 @@ struct BoundsOptions class Bounds { public: - explicit Bounds(const BoundsOptions &options); + using Value = uint32_t; - [[nodiscard]] uint32_t get_width() const noexcept; + explicit Bounds(const BoundsOptions &options) noexcept; - void set_width(uint32_t width) noexcept; + [[nodiscard]] Value get_width() const noexcept; - [[nodiscard]] uint32_t get_height() const noexcept; + void set_width(Value width) noexcept; - void set_height(uint32_t height) noexcept; + [[nodiscard]] Value get_height() const noexcept; + + void set_height(Value height) noexcept; [[nodiscard]] CoordsValidation validate_coords(const Vector2 &coords) const noexcept; @@ -39,6 +41,6 @@ public: const Bounds &operator-=(const Bounds &bounds) noexcept; private: - uint32_t _width = 0U; - uint32_t _height = 0U; + Value _width = 0U; + Value _height = 0U; }; diff --git a/src/engine/data/vector2.cpp b/src/engine/data/vector2.cpp index 04cc42e..dba745f 100644 --- a/src/engine/data/vector2.cpp +++ b/src/engine/data/vector2.cpp @@ -2,8 +2,6 @@ #include "util/hash.hpp" -Vector2::Vector2(const Vector2Options &options) : _x(options.x), _y(options.y) {} - Vector2::Value Vector2::get_x() const noexcept { return _x; @@ -71,26 +69,6 @@ bool Vector2::operator==(const Vector2 &vector2) const noexcept return _x == vector2._x && _y == vector2._y; } -Vector2 Vector2::up() noexcept -{ - return Vector2({.x = 0, .y = -1}); -} - -Vector2 Vector2::down() noexcept -{ - return Vector2({.x = 0, .y = 1}); -} - -Vector2 Vector2::left() noexcept -{ - return Vector2({.x = -1, .y = 0}); -} - -Vector2 Vector2::right() noexcept -{ - return Vector2({.x = 1, .y = 0}); -} - std::size_t Vector2Hasher::operator()(const Vector2 &vector2) const noexcept { std::size_t result_hash = 0; diff --git a/src/engine/data/vector2.hpp b/src/engine/data/vector2.hpp index e835e65..868394a 100644 --- a/src/engine/data/vector2.hpp +++ b/src/engine/data/vector2.hpp @@ -17,7 +17,10 @@ class Vector2 public: using Value = int32_t; - explicit Vector2(const Vector2Options &options); + constexpr explicit Vector2(const Vector2Options &options) noexcept + : _x(options.x), _y(options.y) + { + } [[nodiscard]] Value get_x() const noexcept; @@ -41,22 +44,35 @@ public: /** * Returns Vector2({.x = 0, .y = -1}) */ - static Vector2 up() noexcept; + static constexpr Vector2 up() noexcept + { + return Vector2({.x = 0, .y = -1}); + } /** * Returns Vector2({.x = 0, .y = 1}) */ - static Vector2 down() noexcept; + static constexpr Vector2 down() noexcept + { + return Vector2({.x = 0, .y = 1}); + } /** * Returns Vector2({.x = -1, .y = 0}) */ - static Vector2 left() noexcept; + static constexpr Vector2 left() noexcept + { + + return Vector2({.x = -1, .y = 0}); + } /** * Returns Vector2({.x = 1, .y = 0}) */ - static Vector2 right() noexcept; + static constexpr Vector2 right() noexcept + { + return Vector2({.x = 1, .y = 0}); + } private: Value _x; diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index d793c65..2fc94fe 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -68,7 +68,7 @@ void CLIGameEngine::start() noexcept } void CLIGameEngine::_configure_input( - const std::unordered_map<char, Callback> &input_config) + const std::unordered_map<char, Callback> &input_config) noexcept { for (const auto &config_pair : input_config) { diff --git a/src/engine/engine.hpp b/src/engine/engine.hpp index 504fc28..42cc297 100644 --- a/src/engine/engine.hpp +++ b/src/engine/engine.hpp @@ -33,5 +33,6 @@ private: std::shared_ptr<ICursorController> _cursor_controller; std::shared_ptr<IWindow> _window; - void _configure_input(const std::unordered_map<char, Callback> &input_config); + void + _configure_input(const std::unordered_map<char, Callback> &input_config) noexcept; }; diff --git a/src/engine/graphics/scene.cpp b/src/engine/graphics/scene.cpp index 6b85915..660e3ca 100644 --- a/src/engine/graphics/scene.cpp +++ b/src/engine/graphics/scene.cpp @@ -53,7 +53,7 @@ void Scene::write_status(const std::string_view &str) noexcept _cursor_controller->move_to( Vector2({.x = 2, .y = static_cast<Vector2::Value>(window_size.get_height())})); - const auto background_color = get_background_esc_seq(STATUSBAR_COLOR); + auto background_color = get_background_esc_seq(STATUSBAR_COLOR); fmt::print("{}", background_color); fmt::print(ERASE_ENTIRE_LINE, fmt::arg("esc", ESC)); diff --git a/src/engine/graphics/string_matrix.cpp b/src/engine/graphics/string_matrix.cpp index 26d48df..ae06755 100644 --- a/src/engine/graphics/string_matrix.cpp +++ b/src/engine/graphics/string_matrix.cpp @@ -1,13 +1,13 @@ #include "string_matrix.hpp" -StringMatrix::StringMatrix(const Bounds &bounds) +StringMatrix::StringMatrix(const Bounds &bounds) noexcept : _rows(bounds.get_height()), _columns(bounds.get_width()) { _matrix.reserve(bounds.get_height()); _matrix.assign(_matrix.capacity(), std::vector<std::string_view>(bounds.get_width())); }; -void StringMatrix::fill(std::string_view element) +void StringMatrix::fill(std::string_view element) noexcept { for (uint32_t row = 0U; row < _matrix.capacity(); row++) { @@ -20,7 +20,7 @@ void StringMatrix::fill(std::string_view element) } } -std::string_view StringMatrix::get(const Vector2 &pos) const +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()); @@ -28,7 +28,7 @@ std::string_view StringMatrix::get(const Vector2 &pos) const return _matrix[y][x]; } -void StringMatrix::set(const Vector2 &pos, std::string_view element) +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()); @@ -36,12 +36,12 @@ void StringMatrix::set(const Vector2 &pos, std::string_view element) _matrix[y][x] = element; } -uint32_t StringMatrix::rows() const +uint32_t StringMatrix::rows() const noexcept { return _rows; } -uint32_t StringMatrix::columns() const +uint32_t StringMatrix::columns() const noexcept { return _columns; } diff --git a/src/engine/graphics/string_matrix.hpp b/src/engine/graphics/string_matrix.hpp index 540d63b..40fbb11 100644 --- a/src/engine/graphics/string_matrix.hpp +++ b/src/engine/graphics/string_matrix.hpp @@ -19,21 +19,21 @@ public: * * @param bounds The bounds of the matrix */ - explicit StringMatrix(const Bounds &bounds); + explicit StringMatrix(const Bounds &bounds) noexcept; /** * Fills the matrix with a element. * * @param element A element */ - void fill(std::string_view element) override; + void fill(std::string_view element) noexcept override; /** * Returns a element of the matrix. * * @param pos The position of a element */ - [[nodiscard]] std::string_view get(const Vector2 &pos) const override; + [[nodiscard]] std::string_view get(const Vector2 &pos) const noexcept override; /** * Sets a element of the matrix. @@ -41,17 +41,17 @@ public: * @param pos The position of a element * @param element A new element */ - void set(const Vector2 &pos, std::string_view element) override; + void set(const Vector2 &pos, std::string_view element) noexcept override; /** * Returns the number of rows the matrix has. */ - [[nodiscard]] uint32_t rows() const override; + [[nodiscard]] uint32_t rows() const noexcept override; /** * Returns the number of columns the matrix has. */ - [[nodiscard]] uint32_t columns() const override; + [[nodiscard]] uint32_t columns() const noexcept override; private: std::vector<std::vector<std::string_view>> _matrix; diff --git a/src/engine/user/cursor.cpp b/src/engine/user/cursor.cpp index 663d56a..b02a9bc 100644 --- a/src/engine/user/cursor.cpp +++ b/src/engine/user/cursor.cpp @@ -5,7 +5,7 @@ #include <cstdlib> #include <iostream> -CursorController::CursorController() : _position({.x = 0, .y = 0}) {} +CursorController::CursorController() noexcept : _position({.x = 0, .y = 0}) {} void CursorController::move(const Vector2 &direction, const uint32_t &amount) noexcept { @@ -32,12 +32,6 @@ Vector2 CursorController::where() const noexcept return _position; } -void CursorController::hide() -{ - fmt::print(CURSOR_INVISIBLE, fmt::arg("esc", ESC)); - std::cout.flush(); -} - void CursorController::ensure_position() noexcept { fmt::print(REQUEST_CURSOR_POSITION, fmt::arg("esc", ESC)); @@ -51,7 +45,13 @@ void CursorController::ensure_position() noexcept _position = Vector2(vector2_options); } -void CursorController::show() +void CursorController::hide() noexcept +{ + fmt::print(CURSOR_INVISIBLE, fmt::arg("esc", ESC)); + std::cout.flush(); +} + +void CursorController::show() noexcept { fmt::print(CURSOR_VISIBLE, fmt::arg("esc", ESC)); std::cout.flush(); diff --git a/src/engine/user/cursor.hpp b/src/engine/user/cursor.hpp index c591235..713ee31 100644 --- a/src/engine/user/cursor.hpp +++ b/src/engine/user/cursor.hpp @@ -33,7 +33,7 @@ class CursorController : public ICursorController, public AutoWirable<ICursorController, CursorController> { public: - CursorController(); + CursorController() noexcept; void move(const Vector2 &direction, const uint32_t &amount) noexcept override; @@ -43,9 +43,9 @@ public: void ensure_position() noexcept override; - static void hide(); + static void hide() noexcept; - static void show(); + static void show() noexcept; private: Vector2 _position; diff --git a/src/engine/user/input.cpp b/src/engine/user/input.cpp index 1dc7f63..62aef67 100644 --- a/src/engine/user/input.cpp +++ b/src/engine/user/input.cpp @@ -2,7 +2,7 @@ #include <unistd.h> -void InputHandler::listen() const +void InputHandler::listen() const noexcept { char character = 0; @@ -12,7 +12,7 @@ void InputHandler::listen() const } } -void InputHandler::attach(const char &event, Callback callback) +void InputHandler::attach(const char &event, Callback callback) noexcept { if (_key_observers.count(event) == 0) { @@ -22,7 +22,7 @@ void InputHandler::attach(const char &event, Callback callback) _key_observers[event].push_back(callback); } -void InputHandler::notify(const char &event) const +void InputHandler::notify(const char &event) const noexcept { if (_key_observers.count(event) == 0) { @@ -35,7 +35,7 @@ void InputHandler::notify(const char &event) const } } -void InputHandler::enter_raw_mode() +void InputHandler::enter_raw_mode() noexcept { if (_original_termios == nullptr) { @@ -51,7 +51,7 @@ void InputHandler::enter_raw_mode() tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_termios); } -void InputHandler::leave_raw_mode() +void InputHandler::leave_raw_mode() noexcept { if (_original_termios == nullptr) { diff --git a/src/engine/user/input.hpp b/src/engine/user/input.hpp index f48ab86..3dd7fa9 100644 --- a/src/engine/user/input.hpp +++ b/src/engine/user/input.hpp @@ -12,17 +12,17 @@ class InputHandler : public IInputHandler, public AutoWirable<IInputHandler, InputHandler> { public: - InputHandler() = default; + InputHandler() noexcept = default; - void listen() const override; + void listen() const noexcept override; - void attach(const char &event, Callback callback) override; + void attach(const char &event, Callback callback) noexcept override; - void notify(const char &event) const override; + void notify(const char &event) const noexcept override; - void enter_raw_mode() override; + void enter_raw_mode() noexcept override; - void leave_raw_mode() override; + void leave_raw_mode() noexcept override; private: std::unordered_map<char, std::vector<Callback>> _key_observers; |