diff options
author | HampusM <hampus@hampusmat.com> | 2022-02-27 12:54:10 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:53 +0200 |
commit | 5864e5abc43b201c3801fa39a2fcaf9e3a9e8914 (patch) | |
tree | 98e5e324066ef4d1cbd3cc4c792a258fbd86c12d /src/game_of_life.cpp | |
parent | e233dc28491c33e8a7dc0a11576d3b8ce91cce2c (diff) |
refactor: use dependency injection
Diffstat (limited to 'src/game_of_life.cpp')
-rw-r--r-- | src/game_of_life.cpp | 97 |
1 files changed, 16 insertions, 81 deletions
diff --git a/src/game_of_life.cpp b/src/game_of_life.cpp index 3cd662b..9f7b019 100644 --- a/src/game_of_life.cpp +++ b/src/game_of_life.cpp @@ -1,94 +1,29 @@ -#include "conversion.hpp" -#include "randomization.hpp" +#include "bootstrap.hpp" +#include "interfaces/argument_parser.hpp" +#include "interfaces/randomization.hpp" #include <getopt.h> -#include <iostream> -#include <memory> -#include <string_view> #include <vector> -namespace -{ -void optarg_error(char arg, const std::string_view &error) -{ - std::cout << "Error: Invalid option argument for -" << arg << ". " << error - << std::endl; - exit(EXIT_FAILURE); -} - -/** - * Returns the current optarg as a string view. - */ -std::string_view get_str_optarg() -{ - return std::string_view(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 - */ -unsigned int get_uint_optarg(char arg, bool check_zero = false) -{ - 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 - -constexpr std::array<option, 8> options = { - option({"seed", required_argument, nullptr, 's'}), - option({"help", no_argument, nullptr, 0}), option({nullptr, 0, nullptr, 0})}; +const std::vector<option> options = {option({"seed", required_argument, nullptr, 's'}), + option({"help", no_argument, nullptr, 'h'}), + option({nullptr, 0, nullptr, 0})}; int main(int argc, char *argv[]) { - auto args = std::vector<std::string_view>(argv, argv + argc); + auto container = bootstrap(); - std::shared_ptr<RandomNumberGenerator> random_gen = nullptr; + auto argument_parser = container.get<IArgumentParser>(); - char arg = 0; - while ((arg = static_cast<char>( - getopt_long(argc, argv, "s:", options.data(), nullptr))) != -1) - { - switch (arg) - { - case 's': - { - auto seed = get_uint_optarg(arg, true); - - random_gen = std::make_shared<RandomNumberGenerator>(seed); - break; - } - case 0: - { - std::cout << "Usage: " << args[0] - << " [OPTION]...\n\n" - "Options:\n" - << " -s, --seed SEED The randomization seed used\n" - " --help Displays usage information" - << std::endl; - return EXIT_SUCCESS; - } - case '?': - { - std::cout << "\nTry '" << args[0] << " --help' for more information" - << std::endl; - return EXIT_FAILURE; - } - default: - abort(); - } - } + auto parsed_arguments = argument_parser->parse(options, "s:h", argc, argv); - if (random_gen == nullptr) + if (parsed_arguments.random_gen == nullptr) { - random_gen = std::make_shared<RandomNumberGenerator>(); + auto seed_generator_factory = container.get<ISeedGeneratorFactory>(); + auto random_number_generator_factory = + container.get<IRandomNumberGeneratorFactory>(); + + parsed_arguments.random_gen = + random_number_generator_factory(seed_generator_factory()->random_seed()); } } |