refactor(conv): tryTo -> try_to

This commit is contained in:
Marcus Holland-Moritz 2024-08-14 12:30:13 +02:00
parent e4dfd24e8d
commit 69453e1237
4 changed files with 9 additions and 9 deletions

View File

@ -38,7 +38,7 @@ std::optional<bool> str_to_bool(std::string_view s);
} // namespace detail } // namespace detail
template <typename T, typename U> template <typename T, typename U>
std::optional<T> tryTo(U&& s) std::optional<T> try_to(U&& s)
requires(!std::same_as<T, bool> && !std::convertible_to<U, T>) requires(!std::same_as<T, bool> && !std::convertible_to<U, T>)
{ {
#if defined(__GNUC__) && !defined(__clang__) #if defined(__GNUC__) && !defined(__clang__)
@ -55,21 +55,21 @@ std::optional<T> tryTo(U&& s)
} }
template <typename T, typename U> template <typename T, typename U>
std::optional<bool> tryTo(U&& s) std::optional<bool> try_to(U&& s)
requires(std::same_as<T, bool> && std::is_arithmetic_v<U>) requires(std::same_as<T, bool> && std::is_arithmetic_v<U>)
{ {
return s != U{}; return s != U{};
} }
template <typename T> template <typename T>
std::optional<bool> tryTo(std::string_view s) std::optional<bool> try_to(std::string_view s)
requires std::same_as<T, bool> requires std::same_as<T, bool>
{ {
return detail::str_to_bool(s); return detail::str_to_bool(s);
} }
template <typename T, typename U> template <typename T, typename U>
std::optional<T> tryTo(U&& s) std::optional<T> try_to(U&& s)
requires(std::convertible_to<U, T>) requires(std::convertible_to<U, T>)
{ {
return std::forward<U>(s); return std::forward<U>(s);
@ -80,7 +80,7 @@ T to(U&& s) {
if constexpr (std::same_as<T, std::decay_t<U>>) { if constexpr (std::same_as<T, std::decay_t<U>>) {
return std::forward<U>(s); return std::forward<U>(s);
} else { } else {
return tryTo<T>(std::forward<U>(s)).value(); return try_to<T>(std::forward<U>(s)).value();
} }
} }

View File

@ -267,7 +267,7 @@ std::filesystem::path canonical_path(std::filesystem::path p) {
bool getenv_is_enabled(char const* var) { bool getenv_is_enabled(char const* var) {
if (auto val = std::getenv(var)) { if (auto val = std::getenv(var)) {
if (auto maybeBool = tryTo<bool>(val); maybeBool && *maybeBool) { if (auto maybeBool = try_to<bool>(val); maybeBool && *maybeBool) {
return true; return true;
} }
} }

View File

@ -337,7 +337,7 @@ int dwarfsck_main(int argc, sys_char** argv, iolayer const& iol) {
? reader::block_access_level::no_verify ? reader::block_access_level::no_verify
: reader::block_access_level::unrestricted; : reader::block_access_level::unrestricted;
auto numeric_detail = tryTo<int>(detail); auto numeric_detail = try_to<int>(detail);
opts.features = opts.features =
numeric_detail.has_value() numeric_detail.has_value()
? reader::fsinfo_features::for_level(*numeric_detail) ? reader::fsinfo_features::for_level(*numeric_detail)

View File

@ -982,7 +982,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) {
if (vm.count("set-time")) { if (vm.count("set-time")) {
if (timestamp == "now") { if (timestamp == "now") {
options.timestamp = std::time(nullptr); options.timestamp = std::time(nullptr);
} else if (auto val = tryTo<uint64_t>(timestamp)) { } else if (auto val = try_to<uint64_t>(timestamp)) {
options.timestamp = *val; options.timestamp = *val;
} else { } else {
try { try {
@ -1000,7 +1000,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) {
if (auto it = time_resolutions.find(time_resolution); if (auto it = time_resolutions.find(time_resolution);
it != time_resolutions.end()) { it != time_resolutions.end()) {
options.time_resolution_sec = it->second; options.time_resolution_sec = it->second;
} else if (auto val = tryTo<uint32_t>(time_resolution)) { } else if (auto val = try_to<uint32_t>(time_resolution)) {
options.time_resolution_sec = *val; options.time_resolution_sec = *val;
if (options.time_resolution_sec == 0) { if (options.time_resolution_sec == 0) {
iol.err << "error: the argument to '--time-resolution' must be nonzero\n"; iol.err << "error: the argument to '--time-resolution' must be nonzero\n";