generalize glib::try_invoke to any return-by-value type and void

This commit is contained in:
mjk 2021-03-20 02:48:05 +00:00 committed by LAGonauta
parent b1cb64d716
commit ed7c23c8d2
3 changed files with 23 additions and 6 deletions

View file

@ -104,16 +104,31 @@ inline void log_invocation_failure_desc(const char* e, const char* e_desc, const
} }
#undef FORMAT #undef FORMAT
struct regular_void {};
template<typename Invokable, typename... Arg> template<typename Invokable, typename... Arg>
inline gboolean try_invoke(const char *func_name, Invokable &&i, const Arg &... a) noexcept try inline auto invoke(Invokable &&i, const Arg &... a)
{ {
if constexpr (std::is_void_v<decltype(std::invoke(std::forward<Invokable>(i), a...))>)
{
std::invoke(std::forward<Invokable>(i), a...);
return regular_void{};
}
else
return std::invoke(std::forward<Invokable>(i), a...); return std::invoke(std::forward<Invokable>(i), a...);
} }
template<typename Invokable, typename... Arg>
inline auto try_invoke(const char *func_name, Invokable &&i, const Arg &... a) noexcept
-> std::optional<decltype(invoke(std::forward<Invokable>(i), a...))>
try
{
return invoke(std::forward<Invokable>(i), a...);
}
catch (const std::exception &e) catch (const std::exception &e)
{ {
log_invocation_failure(e.what(), func_name, a...); log_invocation_failure(e.what(), func_name, a...);
return FALSE; return {};
} }
catch (...) catch (...)
{ {
@ -126,7 +141,7 @@ catch (...)
else else
log_invocation_failure("unknown error", func_name, a...); log_invocation_failure("unknown error", func_name, a...);
return FALSE; return {};
} }
} // namespace glib } // namespace glib

View file

@ -153,6 +153,7 @@ extern "C"
gboolean EnsureAumiddedShortcutExists(const gchar *const aumid) noexcept gboolean EnsureAumiddedShortcutExists(const gchar *const aumid) noexcept
{ {
return g_try_invoke( return g_try_invoke(
ImplEnsureAumiddedShortcutExists, R"(Programs\Dino)", aumid); ImplEnsureAumiddedShortcutExists, R"(Programs\Dino)", aumid)
.value_or(false);
} }
} }

View file

@ -72,6 +72,7 @@ std::wstring GetEnv(const wchar_t *const variable_name)
return buf; return buf;
} }
static bool ImplSetProcessAumid(const std::string_view aumid) static bool ImplSetProcessAumid(const std::string_view aumid)
{ {
const auto waumid = sview_to_wstr(aumid); const auto waumid = sview_to_wstr(aumid);
@ -100,6 +101,6 @@ extern "C"
gboolean SetProcessAumid(const gchar *const aumid) noexcept gboolean SetProcessAumid(const gchar *const aumid) noexcept
{ {
return g_try_invoke(ImplSetProcessAumid, aumid); return g_try_invoke(ImplSetProcessAumid, aumid).value_or(false);
} }
} }