mirror of
https://github.com/red0124/ssp.git
synced 2025-01-23 04:55:20 +01:00
fix try_next and try_object when returning false, update unit tests
This commit is contained in:
parent
7e1ea709ca
commit
91853a3287
@ -395,3 +395,4 @@ p.try_next<int>()
|
||||
.or_object<x, double>()
|
||||
.on_error([] { /* int and x (all) conversions failed */ });
|
||||
```
|
||||
*See unit tests for more examples.*
|
||||
|
@ -134,26 +134,27 @@ public:
|
||||
composite<Ts..., T> composite_with(T&& new_value) {
|
||||
auto merged_values =
|
||||
std::tuple_cat(std::move(values_),
|
||||
std::tuple{std::forward<T>(new_value)});
|
||||
std::tuple{parser_.valid()
|
||||
? std::forward<T>(new_value)
|
||||
: std::nullopt});
|
||||
return {std::move(merged_values), parser_};
|
||||
}
|
||||
|
||||
template <typename U, typename... Us, typename Fun = none>
|
||||
void try_convert_and_invoke(std::optional<U>& value, Fun&& fun) {
|
||||
if (!parser_.valid()) {
|
||||
std::optional<U> new_value;
|
||||
auto tuple_output = try_same<Us...>();
|
||||
if (!parser_.valid()) {
|
||||
return;
|
||||
}
|
||||
if constexpr (!std::is_same_v<U, decltype(tuple_output)>) {
|
||||
new_value = to_object<U>(std::move(tuple_output));
|
||||
value = to_object<U>(std::move(tuple_output));
|
||||
} else {
|
||||
new_value = std::move(tuple_output);
|
||||
value = std::move(tuple_output);
|
||||
}
|
||||
if (parser_.valid()) {
|
||||
value = std::move(new_value);
|
||||
parser_.try_invoke(*value, std::forward<Fun>(fun));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename U, typename... Us>
|
||||
no_void_validator_tup_t<U, Us...> try_same() {
|
||||
@ -175,26 +176,17 @@ public:
|
||||
template <typename... Ts, typename Fun = none>
|
||||
composite<std::optional<no_void_validator_tup_t<Ts...>>> try_next(
|
||||
Fun&& fun = none{}) {
|
||||
std::optional<no_void_validator_tup_t<Ts...>> value;
|
||||
auto new_value = get_next<Ts...>();
|
||||
if (valid()) {
|
||||
value = std::move(new_value);
|
||||
try_invoke(*value, std::forward<Fun>(fun));
|
||||
}
|
||||
return {std::move(value), *this};
|
||||
using Ret = no_void_validator_tup_t<Ts...>;
|
||||
return try_invoke_and_make_composite<
|
||||
std::optional<Ret>>(get_next<Ts...>(), std::forward<Fun>(fun));
|
||||
};
|
||||
|
||||
// identical to try_next but returns composite with object instead of a
|
||||
// tuple
|
||||
template <typename T, typename... Ts, typename Fun = none>
|
||||
composite<std::optional<T>> try_object(Fun&& fun = none{}) {
|
||||
std::optional<T> value;
|
||||
auto new_value = get_object<T, Ts...>();
|
||||
if (valid()) {
|
||||
value = std::move(new_value);
|
||||
try_invoke(*value, std::forward<Fun>(fun));
|
||||
}
|
||||
return {std::move(value), *this};
|
||||
return try_invoke_and_make_composite<
|
||||
std::optional<T>>(get_object<T, Ts...>(), std::forward<Fun>(fun));
|
||||
};
|
||||
|
||||
private:
|
||||
@ -243,6 +235,14 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename Fun = none>
|
||||
composite<T> try_invoke_and_make_composite(T&& value, Fun&& fun) {
|
||||
if (valid()) {
|
||||
try_invoke(*value, std::forward<Fun>(fun));
|
||||
}
|
||||
return {valid() ? std::move(value) : std::nullopt, *this};
|
||||
}
|
||||
|
||||
////////////////
|
||||
// line reading
|
||||
////////////////
|
||||
|
@ -181,7 +181,7 @@ TEST_CASE("testing composite conversion") {
|
||||
std::ofstream out{f.name};
|
||||
for (auto& i :
|
||||
{"10,a,11.1", "10,20,11.1", "junk", "10,11.1", "1,11.1,a", "junk",
|
||||
"10,junk", "11,junk", "10,11.1,c"}) {
|
||||
"10,junk", "11,junk", "10,11.1,c", "10,20", "10,22.2,f"}) {
|
||||
out << i << std::endl;
|
||||
}
|
||||
}
|
||||
@ -348,6 +348,42 @@ TEST_CASE("testing composite conversion") {
|
||||
CHECK(d1->tied() == std::tuple{10, 11.1, 'c'});
|
||||
}
|
||||
|
||||
{
|
||||
REQUIRE(!p.eof());
|
||||
|
||||
auto [d1, d2, d3, d4] =
|
||||
p.try_next<int, int>([] { return false; })
|
||||
.or_else<int, double>([](auto&) { return false; })
|
||||
.or_else<int, int>()
|
||||
.or_else<int, int>(fail)
|
||||
.values();
|
||||
|
||||
REQUIRE(p.valid());
|
||||
REQUIRE(!d1);
|
||||
REQUIRE(!d2);
|
||||
REQUIRE(d3);
|
||||
REQUIRE(!d4);
|
||||
CHECK(d3.value() == std::tuple{10, 20});
|
||||
}
|
||||
|
||||
{
|
||||
REQUIRE(!p.eof());
|
||||
|
||||
auto [d1, d2, d3, d4] =
|
||||
p.try_object<test_struct, int, double, char>([] { return false; })
|
||||
.or_else<int, double>([](auto&) { return false; })
|
||||
.or_object<test_struct, int, double, char>()
|
||||
.or_else<int, int>(fail)
|
||||
.values();
|
||||
|
||||
REQUIRE(p.valid());
|
||||
REQUIRE(!d1);
|
||||
REQUIRE(!d2);
|
||||
REQUIRE(d3);
|
||||
REQUIRE(!d4);
|
||||
CHECK(d3->tied() == std::tuple{10, 22.2, 'f'});
|
||||
}
|
||||
|
||||
CHECK(p.eof());
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user