From ed7c23c8d2ed9d64b1732d84f57550f4131765be Mon Sep 17 00:00:00 2001 From: mjk Date: Sat, 20 Mar 2021 02:48:05 +0000 Subject: [PATCH] generalize glib::try_invoke to any return-by-value type and void --- .../api/include/ginvoke.hpp | 23 +++++++++++++++---- .../api/src/shortcutcreator.cpp | 3 ++- .../windows-notification/api/src/win32.cpp | 3 ++- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/plugins/windows-notification/api/include/ginvoke.hpp b/plugins/windows-notification/api/include/ginvoke.hpp index d269ea44..3cc20487 100644 --- a/plugins/windows-notification/api/include/ginvoke.hpp +++ b/plugins/windows-notification/api/include/ginvoke.hpp @@ -104,16 +104,31 @@ inline void log_invocation_failure_desc(const char* e, const char* e_desc, const } #undef FORMAT +struct regular_void {}; template -inline gboolean try_invoke(const char *func_name, Invokable &&i, const Arg &... a) noexcept try +inline auto invoke(Invokable &&i, const Arg &... a) { - return std::invoke(std::forward(i), a...); + if constexpr (std::is_void_v(i), a...))>) + { + std::invoke(std::forward(i), a...); + return regular_void{}; + } + else + return std::invoke(std::forward(i), a...); +} + +template +inline auto try_invoke(const char *func_name, Invokable &&i, const Arg &... a) noexcept + -> std::optional(i), a...))> +try +{ + return invoke(std::forward(i), a...); } catch (const std::exception &e) { log_invocation_failure(e.what(), func_name, a...); - return FALSE; + return {}; } catch (...) { @@ -126,7 +141,7 @@ catch (...) else log_invocation_failure("unknown error", func_name, a...); - return FALSE; + return {}; } } // namespace glib diff --git a/plugins/windows-notification/api/src/shortcutcreator.cpp b/plugins/windows-notification/api/src/shortcutcreator.cpp index d407046f..d51141f0 100644 --- a/plugins/windows-notification/api/src/shortcutcreator.cpp +++ b/plugins/windows-notification/api/src/shortcutcreator.cpp @@ -153,6 +153,7 @@ extern "C" gboolean EnsureAumiddedShortcutExists(const gchar *const aumid) noexcept { return g_try_invoke( - ImplEnsureAumiddedShortcutExists, R"(Programs\Dino)", aumid); + ImplEnsureAumiddedShortcutExists, R"(Programs\Dino)", aumid) + .value_or(false); } } diff --git a/plugins/windows-notification/api/src/win32.cpp b/plugins/windows-notification/api/src/win32.cpp index de9e82ee..7907ef24 100644 --- a/plugins/windows-notification/api/src/win32.cpp +++ b/plugins/windows-notification/api/src/win32.cpp @@ -72,6 +72,7 @@ std::wstring GetEnv(const wchar_t *const variable_name) return buf; } + static bool ImplSetProcessAumid(const std::string_view aumid) { const auto waumid = sview_to_wstr(aumid); @@ -100,6 +101,6 @@ extern "C" gboolean SetProcessAumid(const gchar *const aumid) noexcept { - return g_try_invoke(ImplSetProcessAumid, aumid); + return g_try_invoke(ImplSetProcessAumid, aumid).value_or(false); } }