From ec72c70684cf380070d4020a46cec7f9b3832af3 Mon Sep 17 00:00:00 2001 From: Tim Ebbeke Date: Sat, 30 Nov 2024 23:54:20 +0100 Subject: [PATCH] Fixed scope exit class. --- nui/include/nui/utility/scope_exit.hpp | 30 ++++++------------- .../nui/backend/environment_variables_win.cpp | 2 +- ...mac_webview_config_from_window_options.ipp | 2 -- nui/src/nui/backend/window.cpp | 6 ++-- nui/src/nui/backend/window_impl_linux.ipp | 10 +++---- 5 files changed, 18 insertions(+), 32 deletions(-) diff --git a/nui/include/nui/utility/scope_exit.hpp b/nui/include/nui/utility/scope_exit.hpp index 7324812b..79a962dd 100644 --- a/nui/include/nui/utility/scope_exit.hpp +++ b/nui/include/nui/utility/scope_exit.hpp @@ -3,36 +3,26 @@ #include #include +#include #include namespace Nui { - template class ScopeExit { public: - ScopeExit(EF&& func) - : onExit_(std::forward(func)) + template + requires std::is_nothrow_invocable_v + explicit ScopeExit(FunctionT&& func) + : onExit_(std::forward(func)) {} ~ScopeExit() { if (onExit_) onExit_(); } - ScopeExit(ScopeExit&& other) - : onExit_(std::move(other.onExit_)) - { - other.onExit_ = {}; - } - ScopeExit& operator=(ScopeExit&& other) - { - if (this != &other) - { - onExit_ = std::move(other.onExit_); - other.onExit_ = {}; - } - return *this; - } + ScopeExit(ScopeExit&& other) = delete; + ScopeExit& operator=(ScopeExit&& other) = delete; ScopeExit(const ScopeExit&) = delete; ScopeExit& operator=(const ScopeExit&) = delete; void disarm() @@ -43,8 +33,6 @@ namespace Nui private: std::function onExit_; }; - template - ScopeExit(T) -> ScopeExit; namespace Detail { @@ -53,10 +41,10 @@ namespace Nui template auto operator->*(FunctionT&& fn) const { - return ScopeExit(std::forward(fn)); + return ScopeExit{std::forward(fn)}; } }; } -#define NUI_ON_SCOPE_EXIT auto NUI_UNIQUE_IDENTIFIER = ::Nui::Detail::MakeScopeExitImpl{}->*[&] +#define NUI_ON_SCOPE_EXIT auto NUI_UNIQUE_IDENTIFIER = ::Nui::Detail::MakeScopeExitImpl{}->*[&]() noexcept } \ No newline at end of file diff --git a/nui/src/nui/backend/environment_variables_win.cpp b/nui/src/nui/backend/environment_variables_win.cpp index 6b19dd83..be6c634f 100644 --- a/nui/src/nui/backend/environment_variables_win.cpp +++ b/nui/src/nui/backend/environment_variables_win.cpp @@ -12,7 +12,7 @@ namespace Nui auto* envStrings = GetEnvironmentStrings(); if (envStrings == nullptr) return {}; - auto remover = ScopeExit{[&envStrings]() { + auto remover = ScopeExit{[&envStrings]() noexcept { FreeEnvironmentStrings(envStrings); }}; // var1=value1\0var2=value2\0\0 diff --git a/nui/src/nui/backend/mac_webview_config_from_window_options.ipp b/nui/src/nui/backend/mac_webview_config_from_window_options.ipp index c10de383..27147a0c 100644 --- a/nui/src/nui/backend/mac_webview_config_from_window_options.ipp +++ b/nui/src/nui/backend/mac_webview_config_from_window_options.ipp @@ -158,8 +158,6 @@ namespace Nui::MacOs id wkWebViewConfigurationFromOptions(HostNameMappingInfo const* mappingInfo, WindowOptions const& options) { - std::vector>> cleaners; - auto const* opts = &options; std::optional optCopy; if (options.folderMappingScheme) diff --git a/nui/src/nui/backend/window.cpp b/nui/src/nui/backend/window.cpp index 4daef5f4..280ccc3d 100644 --- a/nui/src/nui/backend/window.cpp +++ b/nui/src/nui/backend/window.cpp @@ -461,8 +461,8 @@ namespace Nui } if (msg.message == WM_APP) { - auto f = reinterpret_cast*>(msg.lParam); - ScopeExit se{[f]() { + auto* f = reinterpret_cast*>(msg.lParam); + ScopeExit se{[f]() noexcept { // yuck! but this is from webview internals delete f; }}; @@ -679,7 +679,7 @@ namespace Nui if (wv23 == nullptr) throw std::runtime_error("Could not get interface to set mapping."); - auto releaseInterface = Nui::ScopeExit{[wv23] { + auto releaseInterface = Nui::ScopeExit{[wv23]() noexcept { wv23->Release(); }}; diff --git a/nui/src/nui/backend/window_impl_linux.ipp b/nui/src/nui/backend/window_impl_linux.ipp index c0678324..17258237 100644 --- a/nui/src/nui/backend/window_impl_linux.ipp +++ b/nui/src/nui/backend/window_impl_linux.ipp @@ -57,10 +57,10 @@ extern "C" { // const auto scheme = std::string_view{webkit_uri_scheme_request_get_scheme(request)}; const auto uri = std::string_view{webkit_uri_scheme_request_get_uri(request)}; - auto exitError = Nui::ScopeExit{[&] { + auto exitError = Nui::ScopeExit{[&]() noexcept { auto* error = g_error_new(WEBKIT_DOWNLOAD_ERROR_DESTINATION, 1, "Invalid custom scheme / Host name mapping."); - auto freeError = Nui::ScopeExit{[error] { + auto freeError = Nui::ScopeExit{[error]() noexcept { g_error_free(error); }}; webkit_uri_scheme_request_finish_error(request, error); @@ -84,7 +84,7 @@ extern "C" { auto* stream = webkit_uri_scheme_request_get_http_body(request); if (stream == nullptr) return std::string{}; - Nui::ScopeExit deleteStream = Nui::ScopeExit{[stream] { + Nui::ScopeExit deleteStream = Nui::ScopeExit{[stream]() noexcept { g_input_stream_close(stream, nullptr, nullptr); }}; @@ -94,10 +94,10 @@ extern "C" { GError* error = NULL; gchar* data = g_data_input_stream_read_upto(dataInputStream, "", 0, &length, NULL, &error); - Nui::ScopeExit freeData = Nui::ScopeExit{[data] { + Nui::ScopeExit freeData = Nui::ScopeExit{[data]() noexcept { g_free(data); }}; - Nui::ScopeExit freeError = Nui::ScopeExit{[error] { + Nui::ScopeExit freeError = Nui::ScopeExit{[error]() noexcept { g_error_free(error); }};