Skip to content

Commit

Permalink
Fixed scope exit class.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Dec 1, 2024
1 parent 7ac7d97 commit ec72c70
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 32 deletions.
30 changes: 9 additions & 21 deletions nui/include/nui/utility/scope_exit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,26 @@
#include <nui/utility/unique_identifier.hpp>

#include <functional>
#include <type_traits>
#include <utility>

namespace Nui
{
template <typename EF>
class ScopeExit
{
public:
ScopeExit(EF&& func)
: onExit_(std::forward<EF>(func))
template <typename FunctionT>
requires std::is_nothrow_invocable_v<FunctionT>
explicit ScopeExit(FunctionT&& func)
: onExit_(std::forward<FunctionT>(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()
Expand All @@ -43,8 +33,6 @@ namespace Nui
private:
std::function<void()> onExit_;
};
template <typename T>
ScopeExit(T) -> ScopeExit<T>;

namespace Detail
{
Expand All @@ -53,10 +41,10 @@ namespace Nui
template <typename FunctionT>
auto operator->*(FunctionT&& fn) const
{
return ScopeExit<FunctionT>(std::forward<FunctionT>(fn));
return ScopeExit{std::forward<FunctionT>(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
}
2 changes: 1 addition & 1 deletion nui/src/nui/backend/environment_variables_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ namespace Nui::MacOs

id wkWebViewConfigurationFromOptions(HostNameMappingInfo const* mappingInfo, WindowOptions const& options)
{
std::vector<Nui::ScopeExit<std::function<void()>>> cleaners;

auto const* opts = &options;
std::optional<WindowOptions> optCopy;
if (options.folderMappingScheme)
Expand Down
6 changes: 3 additions & 3 deletions nui/src/nui/backend/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,8 @@ namespace Nui
}
if (msg.message == WM_APP)
{
auto f = reinterpret_cast<std::function<void()>*>(msg.lParam);
ScopeExit se{[f]() {
auto* f = reinterpret_cast<std::function<void()>*>(msg.lParam);
ScopeExit se{[f]() noexcept {
// yuck! but this is from webview internals
delete f;
}};
Expand Down Expand Up @@ -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();
}};

Expand Down
10 changes: 5 additions & 5 deletions nui/src/nui/backend/window_impl_linux.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}};

Expand All @@ -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);
}};

Expand Down

0 comments on commit ec72c70

Please sign in to comment.