From c67599a3e00ef2e5596a20a9a6d05f9bdfe400c6 Mon Sep 17 00:00:00 2001 From: SpectraL519 Date: Tue, 10 Dec 2024 11:17:53 +0100 Subject: [PATCH] cleaned up some stuff --- include/ap/argument_parser.hpp | 45 ++++++++----------- .../include/argument_parser_test_fixture.hpp | 14 +++--- .../test_argument_parser_parse_args.cpp | 34 +++++++------- tests/source/test_optional_argument.cpp | 4 +- 4 files changed, 45 insertions(+), 52 deletions(-) diff --git a/include/ap/argument_parser.hpp b/include/ap/argument_parser.hpp index 40c7d2d..f480954 100644 --- a/include/ap/argument_parser.hpp +++ b/include/ap/argument_parser.hpp @@ -81,21 +81,11 @@ template concept c_readable = requires(T value, std::istream& input_stream) { input_stream >> value; }; /** - * @brief The concept is satisfied when `T` is c_readable, copy constructible and assignable. + * @brief The concept is satisfied when `T` is c_readable and semiregular. * @tparam T Type to check. */ template -concept c_argument_value_type = - c_readable and std::copy_constructible and std::assignable_from; - -/** - * @brief The concept is satisfied when `T` is comparable using the equality operator `==`. - * @tparam T Type to check. - */ -template -concept c_equality_comparable = requires(T lhs, T rhs) { - { lhs == rhs } -> std::convertible_to; -}; +concept c_argument_value_type = c_readable and std::semiregular; /** * @brief Holds the boolean value indicating whether type `T` is the same as one of the `ValidTypes`. @@ -694,7 +684,7 @@ class positional_argument : public detail::argument_interface { * @note Requires T to be equality comparable. */ positional_argument& choices(const std::vector& choices) noexcept - requires(utility::c_equality_comparable) + requires(std::equality_comparable) { this->_choices = choices; return *this; @@ -976,7 +966,7 @@ class optional_argument : public detail::argument_interface { * @note Requires T to be equality comparable. */ optional_argument& choices(const std::vector& choices) noexcept - requires(utility::c_equality_comparable) + requires(std::equality_comparable) { this->_choices = choices; return *this; @@ -1040,7 +1030,7 @@ class optional_argument : public detail::argument_interface { /// @brief Mark the optional argument as used. void mark_used() noexcept override { - this->_nused++; + ++this->_nused; } /// @return True if the optional argument is used, false otherwise. @@ -1074,7 +1064,7 @@ class optional_argument : public detail::argument_interface { if (not (this->_nargs_range or this->_values.empty())) throw error::value_already_set_error(this->_name); - this->_values.push_back(value); + this->_values.emplace_back(std::move(value)); return *this; } @@ -1269,8 +1259,9 @@ class argument_parser { if (this->_is_arg_name_used(arg_name)) throw error::argument_name_used_error(arg_name); - this->_positional_args.push_back(std::make_unique>(arg_name - )); + this->_positional_args.emplace_back( + std::make_unique>(arg_name) + ); return static_cast&>(*this->_positional_args.back()); } @@ -1291,8 +1282,9 @@ class argument_parser { if (this->_is_arg_name_used(arg_name)) throw error::argument_name_used_error(arg_name); - this->_positional_args.push_back(std::make_unique>(arg_name - )); + this->_positional_args.emplace_back( + std::make_unique>(arg_name) + ); return static_cast&>(*this->_positional_args.back()); } @@ -1331,7 +1323,8 @@ class argument_parser { if (this->_is_arg_name_used(arg_name)) throw error::argument_name_used_error(arg_name); - this->_optional_args.push_back(std::make_unique>(arg_name)); + this->_optional_args.emplace_back(std::make_unique>(arg_name) + ); return static_cast&>(*this->_optional_args.back()); } @@ -1636,14 +1629,14 @@ class argument_parser { cmd_argument_list args; args.reserve(argc - 1); - for (int i = 1; i < argc; i++) { + for (int i = 1; i < argc; ++i) { std::string value = argv[i]; if (this->_is_flag(value)) { this->_strip_flag_prefix(value); - args.push_back(cmd_argument{cmd_argument::type_discriminator::flag, value}); + args.emplace_back(cmd_argument::type_discriminator::flag, std::move(value)); } else { - args.push_back(cmd_argument{cmd_argument::type_discriminator::value, value}); + args.emplace_back(cmd_argument::type_discriminator::value, std::move(value)); } } @@ -1702,7 +1695,7 @@ class argument_parser { return; pos_arg->set_value(cmd_it->value); - cmd_it++; + ++cmd_it; } } @@ -1735,7 +1728,7 @@ class argument_parser { curr_opt_arg->get()->set_value(cmd_it->value); } - cmd_it++; + ++cmd_it; } } diff --git a/tests/include/argument_parser_test_fixture.hpp b/tests/include/argument_parser_test_fixture.hpp index 012d26c..3162225 100644 --- a/tests/include/argument_parser_test_fixture.hpp +++ b/tests/include/argument_parser_test_fixture.hpp @@ -62,10 +62,10 @@ struct argument_parser_test_fixture { argv_vec.emplace_back("program"); - for (std::size_t i = 0; i < args_split; i++) // positional args + for (std::size_t i = 0; i < args_split; ++i) // positional args argv_vec.emplace_back(prepare_arg_value(i)); - for (std::size_t i = args_split; i < num_args; i++) { // optional args + for (std::size_t i = args_split; i < num_args; ++i) { // optional args argv_vec.emplace_back(prepare_arg_flag_primary(i)); argv_vec.emplace_back(prepare_arg_value(i)); } @@ -78,7 +78,7 @@ struct argument_parser_test_fixture { } void free_argv(std::size_t argc, char** argv) const { - for (std::size_t i = 0; i < argc; i++) + for (std::size_t i = 0; i < argc; ++i) delete[] argv[i]; delete[] argv; } @@ -89,12 +89,12 @@ struct argument_parser_test_fixture { void add_arguments(ap::argument_parser& parser, std::size_t num_args, std::size_t args_split) const { - for (std::size_t i = 0; i < args_split; i++) { // positional args + for (std::size_t i = 0; i < args_split; ++i) { // positional args const auto arg_name = prepare_arg_name(i); parser.add_positional_argument(arg_name.primary, arg_name.secondary.value()); } - for (std::size_t i = args_split; i < num_args; i++) { // optional args + for (std::size_t i = args_split; i < num_args; ++i) { // optional args const auto arg_name = prepare_arg_name(i); parser.add_optional_argument(arg_name.primary, arg_name.secondary.value()); } @@ -106,12 +106,12 @@ struct argument_parser_test_fixture { cmd_argument_list cmd_args; cmd_args.reserve(get_args_length(num_args, args_split)); - for (std::size_t i = 0; i < args_split; i++) { // positional args + for (std::size_t i = 0; i < args_split; ++i) { // positional args cmd_args.push_back( cmd_argument{cmd_argument::type_discriminator::value, prepare_arg_value(i)} ); } - for (std::size_t i = args_split; i < num_args; i++) { // optional args + for (std::size_t i = args_split; i < num_args; ++i) { // optional args cmd_args.push_back( cmd_argument{cmd_argument::type_discriminator::flag, prepare_arg_name(i).primary} ); diff --git a/tests/source/test_argument_parser_parse_args.cpp b/tests/source/test_argument_parser_parse_args.cpp index 33fb1c2..6612aae 100644 --- a/tests/source/test_argument_parser_parse_args.cpp +++ b/tests/source/test_argument_parser_parse_args.cpp @@ -54,7 +54,7 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(cmd_args.size(), get_args_length(non_default_num_args, non_default_args_split)); - for (std::size_t i = 0; i < non_default_args_split; i++) { // positional args + for (std::size_t i = 0; i < non_default_args_split; ++i) { // positional args REQUIRE_EQ(cmd_args.at(i).discriminator, cmd_argument::type_discriminator::value); CHECK_EQ(cmd_args.at(i).value, prepare_arg_value(i)); } @@ -67,7 +67,7 @@ TEST_CASE_FIXTURE( REQUIRE_EQ(cmd_args.at(i + 1).discriminator, cmd_argument::type_discriminator::value); CHECK_EQ(cmd_args.at(i + 1).value, prepare_arg_value(opt_arg_idx)); - opt_arg_idx++; + ++opt_arg_idx; } free_argv(argc, argv); @@ -115,7 +115,7 @@ TEST_CASE_FIXTURE( ) { add_arguments(sut, non_default_num_args, non_default_args_split); - for (std::size_t i = 0; i < non_default_num_args; i++) { + for (std::size_t i = 0; i < non_default_num_args; ++i) { const auto arg_name = prepare_arg_name(i); CHECK(sut_get_argument(arg_name.primary)); CHECK(sut_get_argument(arg_name.secondary.value())); @@ -318,7 +318,7 @@ TEST_CASE_FIXTURE( REQUIRE_NOTHROW(sut.parse_args(argc, argv)); - for (std::size_t i = 0; i < non_default_num_args; i++) { + for (std::size_t i = 0; i < non_default_num_args; ++i) { const auto arg_name = prepare_arg_name(i); CHECK(sut.has_value(arg_name.primary)); CHECK(sut.has_value(arg_name.secondary.value())); @@ -335,17 +335,17 @@ TEST_CASE_FIXTURE( REQUIRE_NOTHROW(sut.parse_args(argc, argv)); - for (std::size_t i = 0; i < non_default_args_split; i++) { + for (std::size_t i = 0; i < non_default_args_split; ++i) { const auto arg_name = prepare_arg_name(i); CHECK(sut.has_value(arg_name.primary)); CHECK(sut.has_value(arg_name.secondary.value())); } - for (std::size_t i = non_default_args_split; i < non_default_num_args; i++) { + for (std::size_t i = non_default_args_split; i < non_default_num_args; ++i) { const auto arg_name = prepare_arg_name(i); CHECK_FALSE(sut.has_value(arg_name.primary)); CHECK_FALSE(sut.has_value(arg_name.secondary.value())); } - for (std::size_t i = non_default_num_args; i < num_args; i++) { + for (std::size_t i = non_default_num_args; i < num_args; ++i) { const auto arg_name = prepare_arg_name(i); CHECK(sut.has_value(arg_name.primary)); CHECK(sut.has_value(arg_name.secondary.value())); @@ -380,7 +380,7 @@ TEST_CASE_FIXTURE( ) { add_arguments(sut, non_default_num_args, non_default_args_split); - for (std::size_t i = 0; i < non_default_num_args; i++) { + for (std::size_t i = 0; i < non_default_num_args; ++i) { const auto arg_name = prepare_arg_name(i); CHECK_THROWS_AS(sut.value(arg_name.primary), std::logic_error); CHECK_THROWS_AS(sut.value(arg_name.secondary.value()), std::logic_error); @@ -410,17 +410,17 @@ TEST_CASE_FIXTURE( REQUIRE_NOTHROW(sut.parse_args(argc, argv)); - for (std::size_t i = 0; i < non_default_args_split; i++) { + for (std::size_t i = 0; i < non_default_args_split; ++i) { const auto arg_name = prepare_arg_name(i); CHECK_NOTHROW(sut.value(arg_name.primary)); CHECK_NOTHROW(sut.value(arg_name.secondary.value())); } - for (std::size_t i = non_default_args_split; i < non_default_num_args; i++) { + for (std::size_t i = non_default_args_split; i < non_default_num_args; ++i) { const auto arg_name = prepare_arg_name(i); CHECK_THROWS_AS(sut.value(arg_name.primary), std::logic_error); CHECK_THROWS_AS(sut.value(arg_name.secondary.value()), std::logic_error); } - for (std::size_t i = non_default_num_args; i < num_args; i++) { + for (std::size_t i = non_default_num_args; i < num_args; ++i) { const auto arg_name = prepare_arg_name(i); CHECK_NOTHROW(sut.value(arg_name.primary)); CHECK_NOTHROW(sut.value(arg_name.secondary.value())); @@ -444,7 +444,7 @@ TEST_CASE_FIXTURE( using invalid_value_type = int; - for (std::size_t i = 0; i < non_default_num_args; i++) { + for (std::size_t i = 0; i < non_default_num_args; ++i) { const auto arg_name = prepare_arg_name(i); REQUIRE(sut.has_value(arg_name.primary)); @@ -475,7 +475,7 @@ TEST_CASE_FIXTURE( REQUIRE_NOTHROW(sut.parse_args(argc, argv)); - for (std::size_t i = 0; i < non_default_num_args; i++) { + for (std::size_t i = 0; i < non_default_num_args; ++i) { const auto arg_name = prepare_arg_name(i); const auto arg_value = prepare_arg_value(i); @@ -494,7 +494,7 @@ TEST_CASE_FIXTURE( ) { add_arguments(sut, non_default_num_args, non_default_args_split); - for (std::size_t i = 0; i < non_default_num_args; i++) { + for (std::size_t i = 0; i < non_default_num_args; ++i) { const auto arg_name = prepare_arg_name(i); CHECK_EQ(sut.count(arg_name.primary), 0u); CHECK_EQ(sut.count(arg_name.secondary.value()), 0u); @@ -611,7 +611,7 @@ TEST_CASE_FIXTURE( const std::string flag = "--" + optional_primary_name; argv[1] = new char[flag.length() + 1]; std::strcpy(argv[1], flag.c_str()); - for (int i = 2; i < argc; i++) { + for (int i = 2; i < argc; ++i) { const auto value = prepare_arg_value(i - 1); argv[i] = new char[value.length() + 1]; std::strcpy(argv[i], value.c_str()); @@ -703,7 +703,7 @@ TEST_CASE_FIXTURE( std::vector values; - for (int i = 2; i < argc; i++) { + for (int i = 2; i < argc; ++i) { const auto value = prepare_arg_value(i - 1); argv[i] = new char[value.length() + 1]; std::strcpy(argv[i], value.c_str()); @@ -716,7 +716,7 @@ TEST_CASE_FIXTURE( const auto& stored_values = sut.values(optional_primary_name); REQUIRE_EQ(stored_values.size(), values.size()); - for (std::size_t i = 0; i < stored_values.size(); i++) + for (std::size_t i = 0; i < stored_values.size(); ++i) CHECK_EQ(stored_values[i], values[i]); free_argv(argc, argv); diff --git a/tests/source/test_optional_argument.cpp b/tests/source/test_optional_argument.cpp index 6ca3e39..d759493 100644 --- a/tests/source/test_optional_argument.cpp +++ b/tests/source/test_optional_argument.cpp @@ -136,7 +136,7 @@ TEST_CASE_FIXTURE( auto sut = prepare_argument(primary_name); constexpr std::size_t nused = 5u; - for (std::size_t n = 0; n < nused; n++) + for (std::size_t n = 0; n < nused; ++n) sut_set_used(sut); CHECK_EQ(sut_get_nused(sut), nused); @@ -348,7 +348,7 @@ TEST_CASE_FIXTURE( const auto stored_values = sut_get_values(sut); REQUIRE_EQ(stored_values.size(), default_choices.size()); - for (std::size_t i = 0; i < stored_values.size(); i++) { + for (std::size_t i = 0; i < stored_values.size(); ++i) { REQUIRE_EQ(std::any_cast(stored_values[i]), default_choices[i]); } }