aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/conversion.cpp32
-rw-r--r--src/conversion.hpp23
-rw-r--r--src/game/options.hpp10
-rw-r--r--src/game_of_life.cpp30
4 files changed, 41 insertions, 54 deletions
diff --git a/src/conversion.cpp b/src/conversion.cpp
index 79b3587..5f9494f 100644
--- a/src/conversion.cpp
+++ b/src/conversion.cpp
@@ -3,39 +3,31 @@
#include <climits>
#include <stdexcept>
-unsigned int str_to_uint(const std::string_view &str)
+ConversionResult<unsigned int> str_to_uint(const std::string_view &str)
{
- if (str.at(0) == '-')
+ if (!ctre::match<IS_VALID_UINT>(str))
{
- throw "Less than 0";
+ return ConversionResult(false, 0U, "Not a number");
}
- std::size_t waste_pos = 0;
-
- uint64_t num = 0;
-
- try
- {
- num = std::stoul(str.data(), &waste_pos, NUMBER_BASE);
- }
- catch (const std::invalid_argument &exc)
+ if (!ctre::match<IS_UINT_IN_RANGE>(str))
{
- throw "Not a number";
- }
- catch (const std::out_of_range &exc)
- {
- throw "Out of range";
+ return ConversionResult(false, 0U, "Out of range");
}
+ std::size_t waste_pos = 0;
+
+ auto num = std::stoul(str.data(), &waste_pos, NUMBER_BASE);
+
if (waste_pos != str.length())
{
- throw "Not a number";
+ return ConversionResult(false, 0U, "Not a number");
}
if (num > UINT_MAX)
{
- throw "Out of range";
+ return ConversionResult(false, 0U, "Out of range");
}
- return static_cast<unsigned int>(num);
+ return ConversionResult(true, static_cast<unsigned int>(num));
}
diff --git a/src/conversion.hpp b/src/conversion.hpp
index 4ae5b7c..0bdf82d 100644
--- a/src/conversion.hpp
+++ b/src/conversion.hpp
@@ -1,13 +1,32 @@
#pragma once
+#include <ctre.hpp>
#include <string_view>
constexpr unsigned int NUMBER_BASE = 10U;
+template <typename ResultType>
+class ConversionResult
+{
+public:
+ explicit ConversionResult(const bool &success_, const ResultType &result_,
+ const std::string_view &fail_reason_ = "")
+ : success(success_), result(result_), fail_reason(fail_reason_)
+ {
+ }
+
+ const bool &success;
+ const ResultType &result;
+ const std::string_view &fail_reason;
+};
+
+static constexpr auto IS_VALID_UINT = ctll::fixed_string("^[0-9]+$");
+static constexpr auto IS_UINT_IN_RANGE = ctll::fixed_string("^[0-9]{1,19}$");
+
/**
* Converts a string to a unsigned integer.
*
* @param str A string that possibly is a unsigned integer
- * @returns A unsigned integer
+ * @returns A conversion result
*/
-unsigned int str_to_uint(const std::string_view &str);
+ConversionResult<unsigned int> str_to_uint(const std::string_view &str);
diff --git a/src/game/options.hpp b/src/game/options.hpp
deleted file mode 100644
index 98360b8..0000000
--- a/src/game/options.hpp
+++ /dev/null
@@ -1,10 +0,0 @@
-#pragma once
-
-#include "randomization.hpp"
-
-#include <memory>
-
-struct GameOptions
-{
- std::shared_ptr<RandomNumberGenerator> random_gen;
-};
diff --git a/src/game_of_life.cpp b/src/game_of_life.cpp
index 32d393e..3cd662b 100644
--- a/src/game_of_life.cpp
+++ b/src/game_of_life.cpp
@@ -1,5 +1,4 @@
#include "conversion.hpp"
-#include "game/options.hpp"
#include "randomization.hpp"
#include <getopt.h>
@@ -33,23 +32,14 @@ std::string_view get_str_optarg()
*/
unsigned int get_uint_optarg(char arg, bool check_zero = false)
{
- unsigned int num = 0;
+ auto conversion_result = str_to_uint(get_str_optarg());
- try
+ if (!conversion_result.success || (check_zero && conversion_result.result == 0))
{
- num = str_to_uint(get_str_optarg());
-
- if (check_zero && num == 0)
- {
- throw "It should not be 0";
- }
- }
- catch (const char *error)
- {
- optarg_error(arg, std::string_view(error));
+ optarg_error(arg, conversion_result.fail_reason);
}
- return num;
+ return conversion_result.result;
}
} // namespace
@@ -61,7 +51,7 @@ int main(int argc, char *argv[])
{
auto args = std::vector<std::string_view>(argv, argv + argc);
- GameOptions game_options;
+ std::shared_ptr<RandomNumberGenerator> random_gen = nullptr;
char arg = 0;
while ((arg = static_cast<char>(
@@ -73,7 +63,7 @@ int main(int argc, char *argv[])
{
auto seed = get_uint_optarg(arg, true);
- game_options.random_gen = std::make_shared<RandomNumberGenerator>(seed);
+ random_gen = std::make_shared<RandomNumberGenerator>(seed);
break;
}
case 0:
@@ -97,12 +87,8 @@ int main(int argc, char *argv[])
}
}
- if (game_options.random_gen == nullptr)
+ if (random_gen == nullptr)
{
- game_options.random_gen = std::make_shared<RandomNumberGenerator>();
+ random_gen = std::make_shared<RandomNumberGenerator>();
}
-
- // auto game = Game(game_options);
-
- // game.run();
}