mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-08 03:49:44 -04:00
refactor(conv): tryTo -> try_to
This commit is contained in:
parent
e4dfd24e8d
commit
69453e1237
@ -38,7 +38,7 @@ std::optional<bool> str_to_bool(std::string_view s);
|
||||
} // namespace detail
|
||||
|
||||
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>)
|
||||
{
|
||||
#if defined(__GNUC__) && !defined(__clang__)
|
||||
@ -55,21 +55,21 @@ std::optional<T> tryTo(U&& s)
|
||||
}
|
||||
|
||||
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>)
|
||||
{
|
||||
return s != U{};
|
||||
}
|
||||
|
||||
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>
|
||||
{
|
||||
return detail::str_to_bool(s);
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
std::optional<T> tryTo(U&& s)
|
||||
std::optional<T> try_to(U&& s)
|
||||
requires(std::convertible_to<U, T>)
|
||||
{
|
||||
return std::forward<U>(s);
|
||||
@ -80,7 +80,7 @@ T to(U&& s) {
|
||||
if constexpr (std::same_as<T, std::decay_t<U>>) {
|
||||
return std::forward<U>(s);
|
||||
} else {
|
||||
return tryTo<T>(std::forward<U>(s)).value();
|
||||
return try_to<T>(std::forward<U>(s)).value();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -267,7 +267,7 @@ std::filesystem::path canonical_path(std::filesystem::path p) {
|
||||
|
||||
bool getenv_is_enabled(char const* 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;
|
||||
}
|
||||
}
|
||||
|
@ -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::unrestricted;
|
||||
|
||||
auto numeric_detail = tryTo<int>(detail);
|
||||
auto numeric_detail = try_to<int>(detail);
|
||||
opts.features =
|
||||
numeric_detail.has_value()
|
||||
? reader::fsinfo_features::for_level(*numeric_detail)
|
||||
|
@ -982,7 +982,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) {
|
||||
if (vm.count("set-time")) {
|
||||
if (timestamp == "now") {
|
||||
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;
|
||||
} else {
|
||||
try {
|
||||
@ -1000,7 +1000,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) {
|
||||
if (auto it = time_resolutions.find(time_resolution);
|
||||
it != time_resolutions.end()) {
|
||||
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;
|
||||
if (options.time_resolution_sec == 0) {
|
||||
iol.err << "error: the argument to '--time-resolution' must be nonzero\n";
|
||||
|
Loading…
x
Reference in New Issue
Block a user