Skip to content

Commit

Permalink
Added auto unregistering functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Sep 20, 2023
1 parent 950d3b9 commit 06350a6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
17 changes: 17 additions & 0 deletions nui/include/nui/backend/rpc_hub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <nui/data_structures/selectables_registry.hpp>
#include <nui/utility/meta/function_traits.hpp>
#include <nui/utility/meta/pick_first.hpp>
#include <nui/shared/on_destroy.hpp>
#include <nlohmann/json.hpp>
#include <fmt/format.h>

Expand Down Expand Up @@ -96,6 +97,22 @@ namespace Nui
}})();
)";

struct AutoUnregister : public OnDestroy
{
AutoUnregister(RpcHub const* hub, std::string name)
: OnDestroy{[hub, name = std::move(name)]() {
hub->unregisterFunction(name);
}}
{}
};

template <typename T>
AutoUnregister autoRegisterFunction(std::string const& name, T&& func) const
{
registerFunction(name, std::forward<T>(func));
return AutoUnregister{this, name};
}

template <typename T>
void registerFunction(std::string const& name, T&& func) const
{
Expand Down
22 changes: 22 additions & 0 deletions nui/include/nui/frontend/rpc_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <nui/frontend/utility/functions.hpp>
#include <nui/utility/meta/function_traits.hpp>
#include <nui/frontend/utility/val_conversion.hpp>
#include <nui/shared/on_destroy.hpp>

#include <string>
#include <cstdint>
Expand Down Expand Up @@ -231,6 +232,11 @@ namespace Nui
std::placeholders::_1));
}

/**
* @brief Unregister a function.
*
* @param name The name of the function.
*/
static void unregisterFunction(std::string const& name)
{
using namespace std::string_literals;
Expand All @@ -241,5 +247,21 @@ namespace Nui
}
Nui::val::global("nui_rpc")["frontend"].delete_(name.c_str());
}

struct AutoUnregister : public OnDestroy
{
AutoUnregister(std::string name)
: OnDestroy{[name = std::move(name)]() {
unregisterFunction(name);
}}
{}
};

template <typename FunctionT>
static AutoUnregister autoRegisterFunction(std::string const& name, FunctionT&& func)
{
registerFunction(name, std::forward<FunctionT>(func));
return AutoUnregister{name};
}
};
}
39 changes: 39 additions & 0 deletions nui/include/nui/shared/on_destroy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <functional>

namespace Nui
{
class OnDestroy
{
public:
OnDestroy(std::function<void()> onDestroy)
: wasMoved_{false}
, onDestroy_{std::move(onDestroy)}
{}
OnDestroy(OnDestroy const&) = delete;
OnDestroy(OnDestroy&& other)
: wasMoved_{other.wasMoved_}
, onDestroy_{std::move(other.onDestroy_)}
{
other.wasMoved_ = true;
}
OnDestroy& operator=(OnDestroy const&) = delete;
OnDestroy& operator=(OnDestroy&& other)
{
wasMoved_ = other.wasMoved_;
onDestroy_ = std::move(other.onDestroy_);
other.wasMoved_ = true;
return *this;
}
~OnDestroy()
{
if (!wasMoved_ && onDestroy_)
onDestroy_();
}

private:
bool wasMoved_;
std::function<void()> onDestroy_;
};
}

0 comments on commit 06350a6

Please sign in to comment.