diff options
author | HampusM <hampus@hampusmat.com> | 2022-02-16 19:31:24 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-02-16 19:31:24 +0100 |
commit | 91286f9dbb85fc2805345ba72468d5f145228be8 (patch) | |
tree | 9ad87c8d30e282e0c3478150efd901ef3dc17cca /src | |
parent | 5dae8f8d10d506abc3c75a1f66c1dfe620c84fc1 (diff) |
refactor: create app class & simplify app options
Diffstat (limited to 'src')
-rw-r--r-- | src/app/app.cpp | 12 | ||||
-rw-r--r-- | src/app/app.hpp | 11 | ||||
-rw-r--r-- | src/app/options.cpp | 43 | ||||
-rw-r--r-- | src/app/options.hpp | 32 | ||||
-rw-r--r-- | src/mazerator.cpp | 32 |
5 files changed, 39 insertions, 91 deletions
diff --git a/src/app/app.cpp b/src/app/app.cpp index 0942c3e..d860b16 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -8,19 +8,21 @@ #include <memory> #include <string_view> -void app_start(const AppOptions &app_options) +App::App(const AppOptions &options) : _options(options) {} + +void App::run() { - Matrix<std::string_view> matrix(*app_options.maze_bounds() * + Matrix<std::string_view> matrix(*_options.maze_bounds * Bounds({.width = 2U, .height = 2U}) + Bounds({.width = 1U, .height = 1U})); - matrix.fill(app_options.wall()); + matrix.fill(_options.wall); - auto start_pos = *app_options.start_coords() * Vector2({.x = 2U, .y = 2U}) + + auto start_pos = *_options.start_coords * Vector2({.x = 2U, .y = 2U}) + Vector2({.x = 1U, .y = 1U}); matrix_to_maze<std::string_view>(&matrix, std::make_shared<Vector2>(start_pos), " ", - app_options.random_gen()); + _options.random_gen); matrix.print(); } diff --git a/src/app/app.hpp b/src/app/app.hpp index d02c405..cafb864 100644 --- a/src/app/app.hpp +++ b/src/app/app.hpp @@ -2,4 +2,13 @@ #include "app/options.hpp" -void app_start(const AppOptions &app_options); +class App +{ +public: + explicit App(const AppOptions &options); + + void run(); + +private: + const AppOptions &_options; +}; diff --git a/src/app/options.cpp b/src/app/options.cpp deleted file mode 100644 index cb6e20e..0000000 --- a/src/app/options.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "options.hpp" - -#include <utility> - -std::shared_ptr<Bounds> AppOptions::maze_bounds() const -{ - return _maze_bounds; -} - -void AppOptions::maze_bounds(std::shared_ptr<Bounds> maze_bounds) -{ - _maze_bounds = std::move(maze_bounds); -} - -std::shared_ptr<Vector2> AppOptions::start_coords() const -{ - return _start_coords; -} - -void AppOptions::start_coords(std::shared_ptr<Vector2> start_coords) -{ - _start_coords = std::move(start_coords); -} - -std::string_view AppOptions::wall() const -{ - return _wall; -} - -void AppOptions::wall(std::string_view wall) -{ - _wall = wall; -} - -std::shared_ptr<RandomNumberGenerator> AppOptions::random_gen() const -{ - return _random_gen; -} - -void AppOptions::random_gen(std::shared_ptr<RandomNumberGenerator> random_gen) -{ - _random_gen = std::move(random_gen); -} diff --git a/src/app/options.hpp b/src/app/options.hpp index 0023283..7e691e3 100644 --- a/src/app/options.hpp +++ b/src/app/options.hpp @@ -7,32 +7,10 @@ #include <memory> #include <string_view> -/** - * Application options. - */ -class AppOptions +struct AppOptions { -public: - AppOptions() = default; - - [[nodiscard]] std::shared_ptr<Bounds> maze_bounds() const; - void maze_bounds(std::shared_ptr<Bounds> maze_bounds); - - [[nodiscard]] std::shared_ptr<Vector2> start_coords() const; - void start_coords(std::shared_ptr<Vector2> start_coords); - - [[nodiscard]] std::string_view wall() const; - void wall(std::string_view wall); - - [[nodiscard]] std::shared_ptr<RandomNumberGenerator> random_gen() const; - void random_gen(std::shared_ptr<RandomNumberGenerator> random_gen); - -private: - std::shared_ptr<Bounds> _maze_bounds = nullptr; - - std::shared_ptr<Vector2> _start_coords = nullptr; - - std::string_view _wall; - - std::shared_ptr<RandomNumberGenerator> _random_gen = nullptr; + std::shared_ptr<Bounds> maze_bounds; + std::shared_ptr<Vector2> start_coords; + std::string_view wall; + std::shared_ptr<RandomNumberGenerator> random_gen; }; diff --git a/src/mazerator.cpp b/src/mazerator.cpp index 81960af..a884bbd 100644 --- a/src/mazerator.cpp +++ b/src/mazerator.cpp @@ -65,9 +65,9 @@ int main(int argc, char *argv[]) BoundsOpts default_maze_bounds = {DEFAULT_MAZE_WIDTH, DEFAULT_MAZE_HEIGHT}; - app_options.maze_bounds(std::make_shared<Bounds>(default_maze_bounds)); + app_options.maze_bounds = std::make_shared<Bounds>(default_maze_bounds); - app_options.wall(DEFAULT_MAZE_WALL); + app_options.wall = DEFAULT_MAZE_WALL; std::unique_ptr<unsigned int> start_x = nullptr; std::unique_ptr<unsigned int> start_y = nullptr; @@ -79,13 +79,13 @@ int main(int argc, char *argv[]) { case 'w': { - app_options.maze_bounds()->width( + app_options.maze_bounds->width( static_cast<unsigned int>(std::stoul(optarg, nullptr, NUMBER_BASE))); break; } case 'h': { - app_options.maze_bounds()->height( + app_options.maze_bounds->height( static_cast<unsigned int>(std::stoul(optarg, nullptr, NUMBER_BASE))); break; } @@ -105,7 +105,7 @@ int main(int argc, char *argv[]) } case 'W': { - app_options.wall(optarg); + app_options.wall = optarg; break; } case 's': @@ -113,7 +113,7 @@ int main(int argc, char *argv[]) unsigned int seed = 0; parse_uint_arg(&seed, arg, true); - app_options.random_gen(std::make_shared<RandomNumberGenerator>(seed)); + app_options.random_gen = std::make_shared<RandomNumberGenerator>(seed); break; } case 0: @@ -152,30 +152,30 @@ int main(int argc, char *argv[]) } } - if (app_options.random_gen() == nullptr) + if (app_options.random_gen == nullptr) { - app_options.random_gen(std::make_shared<RandomNumberGenerator>()); + app_options.random_gen = std::make_shared<RandomNumberGenerator>(); } if (start_x == nullptr) { - start_x = std::make_unique<unsigned int>(app_options.random_gen()->in_range( - 0, app_options.maze_bounds()->width() - 1U)); + start_x = std::make_unique<unsigned int>( + app_options.random_gen->in_range(0, app_options.maze_bounds->width() - 1U)); } if (start_y == nullptr) { - start_y = std::make_unique<unsigned int>(app_options.random_gen()->in_range( - 0, app_options.maze_bounds()->height() - 1U)); + start_y = std::make_unique<unsigned int>( + app_options.random_gen->in_range(0, app_options.maze_bounds->height() - 1U)); } Vector2Opts start_coords = {.x = *start_x, .y = *start_y}; - app_options.start_coords(std::make_shared<Vector2>(start_coords)); + app_options.start_coords = std::make_shared<Vector2>(start_coords); try { - app_options.maze_bounds()->verify_coords(*app_options.start_coords()); + app_options.maze_bounds->verify_coords(*app_options.start_coords); } catch (const char *error) { @@ -183,5 +183,7 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - app_start(app_options); + auto app = App(app_options); + + app.run(); } |