diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/argument_parser.cpp | 104 | ||||
-rw-r--r-- | src/argument_parser.hpp | 31 | ||||
-rw-r--r-- | src/bootstrap.cpp | 13 | ||||
-rw-r--r-- | src/game_of_life.cpp | 28 | ||||
-rw-r--r-- | src/interfaces/argument_parser.hpp | 28 | ||||
-rw-r--r-- | src/interfaces/randomization.hpp | 33 | ||||
-rw-r--r-- | src/randomization/generator.cpp | 14 | ||||
-rw-r--r-- | src/randomization/generator.hpp | 18 | ||||
-rw-r--r-- | src/randomization/seed_generator.cpp | 6 | ||||
-rw-r--r-- | src/randomization/seed_generator.hpp | 19 |
11 files changed, 3 insertions, 296 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0c576e3..76a6d5a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,7 +12,6 @@ file(GLOB SOURCES game_of_life.cpp bootstrap.cpp conversion.cpp - argument_parser.cpp util/color.cpp game/game.cpp game/status_manager.cpp @@ -24,9 +23,7 @@ file(GLOB SOURCES engine/graphics/string_matrix.cpp engine/graphics/statusline.cpp engine/user/input.cpp - engine/user/cursor.cpp - randomization/generator.cpp - randomization/seed_generator.cpp) + engine/user/cursor.cpp) add_executable(${PROJECT_NAME} ${SOURCES}) diff --git a/src/argument_parser.cpp b/src/argument_parser.cpp deleted file mode 100644 index 117d979..0000000 --- a/src/argument_parser.cpp +++ /dev/null @@ -1,104 +0,0 @@ -#include "argument_parser.hpp" - -#include "conversion.hpp" -#include "interfaces/randomization.hpp" - -#include <cstdlib> -#include <iostream> -#include <string_view> - -namespace -{ -void optarg_error(char arg, const std::string_view &error) noexcept -{ - std::cout << "Error: Invalid option argument for -" << arg << ". " << error - << std::endl; - exit(EXIT_FAILURE); -} - -/** - * Returns the current optarg as a string view. - */ -auto get_str_optarg() noexcept -> std::string_view -{ - return {optarg}; -} - -/** - * Returns the current optarg as a unsigned integer. - * - * @param arg The current command-line argument character - * @param check_zero Whether or not to make sure that the result is not zero - */ -auto get_uint_optarg(char arg, bool check_zero = false) noexcept -> uint32_t -{ - auto conversion_result = str_to_uint(get_str_optarg()); - - if (!conversion_result.success || (check_zero && conversion_result.result == 0)) - { - optarg_error(arg, conversion_result.fail_reason); - } - - return conversion_result.result; -} -} // namespace - -ArgumentParser::ArgumentParser( - IRandomNumberGeneratorFactory random_number_generator_factory) noexcept - : _random_number_generator_factory(random_number_generator_factory) -{ -} - -auto ArgumentParser::parse( - const std::vector<option> &options, - const std::string_view &short_options, - const int &argc, - char *const *argv) noexcept - -> ParsedArguments // NOLINT(cppcoreguidelines-avoid-c-arrays, - // modernize-avoid-c-arrays) -{ - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - const auto args = std::vector<std::string_view>(argv, argv + argc); - - const auto program_name = args[0]; - - ParsedArguments parsed_arguments; - - char arg = 0; - while ( - (arg = static_cast<char>( - getopt_long(argc, argv, short_options.data(), options.data(), nullptr))) != - -1) - { - switch (arg) - { - case 's': - { - auto seed = get_uint_optarg(arg, true); - - parsed_arguments.random_gen = _random_number_generator_factory(seed); - break; - } - case 'h': - { - std::cout << "Usage: " << program_name - << " [OPTION]...\n\n" - "Options:\n" - << " -s, --seed SEED The randomization seed used\n" - " -h --help Displays usage information" - << std::endl; - exit(EXIT_SUCCESS); - } - case '?': - { - std::cout << "\nTry '" << program_name << " --help' for more information" - << std::endl; - exit(EXIT_FAILURE); - } - default: - abort(); - } - } - - return parsed_arguments; -} diff --git a/src/argument_parser.hpp b/src/argument_parser.hpp deleted file mode 100644 index 3a32e0b..0000000 --- a/src/argument_parser.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include "interfaces/argument_parser.hpp" -#include "interfaces/randomization.hpp" - -#include <yacppdic/auto_wirable.hpp> - -#include <getopt.h> -#include <string_view> -#include <vector> - -class ArgumentParser - : public IArgumentParser, - public yacppdic:: - AutoWirable<IArgumentParser, ArgumentParser, IRandomNumberGeneratorFactory> -{ -public: - explicit ArgumentParser( - IRandomNumberGeneratorFactory random_number_generator_factory) noexcept; - - auto parse( - const std::vector<option> &options, - const std::string_view &short_options, - const int &argc, - char *const *argv) noexcept - -> ParsedArguments override; // NOLINT(cppcoreguidelines-avoid-c-arrays, - // modernize-avoid-c-arrays) - -private: - IRandomNumberGeneratorFactory _random_number_generator_factory; -}; diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index adb53a2..1effa96 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -1,7 +1,6 @@ #include "bootstrap.hpp" // Interfaces -#include "interfaces/argument_parser.hpp" #include "interfaces/cell_helper.hpp" #include "interfaces/cursor.hpp" #include "interfaces/engine.hpp" @@ -9,13 +8,11 @@ #include "interfaces/generation_tracker.hpp" #include "interfaces/input.hpp" #include "interfaces/matrix.hpp" -#include "interfaces/randomization.hpp" #include "interfaces/scene.hpp" #include "interfaces/status_manager.hpp" #include "interfaces/statusline.hpp" // Implementations -#include "argument_parser.hpp" #include "engine/data/bounds.hpp" #include "engine/data/vector2.hpp" #include "engine/engine.hpp" @@ -28,8 +25,6 @@ #include "game/game.hpp" #include "game/generation_tracker.hpp" #include "game/status_manager.hpp" -#include "randomization/generator.hpp" -#include "randomization/seed_generator.hpp" #include <fmt/core.h> @@ -43,11 +38,9 @@ auto bootstrap() noexcept -> yacppdic::Container { auto container = yacppdic::Container(); - container.bind<IArgumentParser>().to<ArgumentParser>(); container.bind<IUserInputObserver>().to<UserInputObserver>(); container.bind<ICursorController>().to<CursorController>(); container.bind<ICLIGameEngine>().to<CLIGameEngine>(); - container.bind<ISeedGenerator>().to<SeedGenerator>(); container.bind<IGameFactory>().to_factory( [&container]( @@ -75,12 +68,6 @@ auto bootstrap() noexcept -> yacppdic::Container cell_helper_factory(*(scene->get_matrix()))); }); - container.bind<IRandomNumberGeneratorFactory>().to_factory( - [](const uint32_t &seed) - { - return std::make_unique<RandomNumberGenerator>(seed); - }); - container.bind<IMatrixFactory<char>>().to_factory( [](const Bounds &bounds) { diff --git a/src/game_of_life.cpp b/src/game_of_life.cpp index 2d6d5ee..c0f8630 100644 --- a/src/game_of_life.cpp +++ b/src/game_of_life.cpp @@ -1,35 +1,11 @@ -#include "bootstrap.hpp" -#include "interfaces/argument_parser.hpp" #include "interfaces/engine.hpp" -#include "interfaces/randomization.hpp" - -#include <getopt.h> -#include <vector> -const std::vector<option> options = { - option({"seed", required_argument, nullptr, 's'}), - option({"help", no_argument, nullptr, 'h'}), - option({nullptr, 0, nullptr, 0})}; +#include "bootstrap.hpp" -auto main(int argc, char *argv[]) noexcept -> int +auto main() noexcept -> int { auto container = bootstrap(); - auto argument_parser = container.get<IArgumentParser>(); - - auto parsed_arguments = argument_parser->parse(options, "s:h", argc, argv); - - if (parsed_arguments.random_gen == nullptr) - { - auto seed_generator = container.get<ISeedGenerator>(); - - auto random_number_generator_factory = - container.get<IRandomNumberGeneratorFactory>(); - - parsed_arguments.random_gen = - random_number_generator_factory(seed_generator->random_seed()); - } - auto engine = container.get<ICLIGameEngine>(); engine->start(); diff --git a/src/interfaces/argument_parser.hpp b/src/interfaces/argument_parser.hpp deleted file mode 100644 index 6415422..0000000 --- a/src/interfaces/argument_parser.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include "interfaces/randomization.hpp" - -#include <getopt.h> -#include <memory> -#include <string_view> -#include <vector> - -struct ParsedArguments -{ - std::shared_ptr<IRandomNumberGenerator> random_gen = nullptr; -}; - -// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) -class IArgumentParser -{ -public: - virtual ~IArgumentParser() noexcept = default; - - virtual auto parse( - const std::vector<option> &options, - const std::string_view &short_options, - const int &argc, - char *const *argv) noexcept - -> ParsedArguments = 0; // NOLINT(cppcoreguidelines-avoid-c-arrays, - // modernize-avoid-c-arrays) -}; diff --git a/src/interfaces/randomization.hpp b/src/interfaces/randomization.hpp deleted file mode 100644 index 06f7d49..0000000 --- a/src/interfaces/randomization.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include <yacppdic/factory.hpp> - -#include <memory> - -// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) -class ISeedGenerator -{ -public: - virtual ~ISeedGenerator() noexcept = default; - - [[nodiscard]] virtual auto random_seed() noexcept -> uint32_t = 0; -}; - -// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) -class IRandomNumberGenerator -{ -public: - virtual ~IRandomNumberGenerator() noexcept = default; - - /** - * Returns a number in the range of a to b. - * - * @param a A number lower than b - * @param b A number greater than a - */ - [[nodiscard]] virtual auto - in_range(const uint32_t &a, const uint32_t &b) const noexcept -> uint32_t = 0; -}; - -using IRandomNumberGeneratorFactory = - yacppdic::Factory<std::unique_ptr<IRandomNumberGenerator>(const uint32_t &seed)>; diff --git a/src/randomization/generator.cpp b/src/randomization/generator.cpp deleted file mode 100644 index 49adb97..0000000 --- a/src/randomization/generator.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "generator.hpp" - -RandomNumberGenerator::RandomNumberGenerator(const uint32_t &seed) noexcept -{ - this->_generator = std::make_unique<std::mt19937>(seed); -} - -auto RandomNumberGenerator::in_range(const uint32_t &a, const uint32_t &b) const noexcept - -> uint32_t -{ - auto random_distribution = std::uniform_int_distribution<uint32_t>(a, b); - - return random_distribution(*this->_generator); -} diff --git a/src/randomization/generator.hpp b/src/randomization/generator.hpp deleted file mode 100644 index 24b9bd6..0000000 --- a/src/randomization/generator.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include "interfaces/randomization.hpp" - -#include <memory> -#include <random> - -class RandomNumberGenerator : public IRandomNumberGenerator -{ -public: - explicit RandomNumberGenerator(const uint32_t &seed) noexcept; - - [[nodiscard]] auto in_range(const uint32_t &a, const uint32_t &b) const noexcept - -> uint32_t override; - -private: - std::unique_ptr<std::mt19937> _generator; -}; diff --git a/src/randomization/seed_generator.cpp b/src/randomization/seed_generator.cpp deleted file mode 100644 index a0f9303..0000000 --- a/src/randomization/seed_generator.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "seed_generator.hpp" - -auto SeedGenerator::random_seed() noexcept -> uint32_t -{ - return _random_device(); -} diff --git a/src/randomization/seed_generator.hpp b/src/randomization/seed_generator.hpp deleted file mode 100644 index 3584966..0000000 --- a/src/randomization/seed_generator.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "interfaces/randomization.hpp" - -#include <yacppdic/auto_wirable.hpp> - -#include <random> - -class SeedGenerator : public ISeedGenerator, - public yacppdic::AutoWirable<ISeedGenerator, SeedGenerator> -{ -public: - SeedGenerator() noexcept = default; - - [[nodiscard]] auto random_seed() noexcept -> uint32_t override; - -private: - std::random_device _random_device; -}; |