Avoid usage of std::move_only_function

This commit is contained in:
DH
2025-08-27 20:57:44 +03:00
parent 90be5c47d5
commit 3656f692f3
4 changed files with 28 additions and 25 deletions

View File

@@ -39,10 +39,10 @@ public:
virtual ~Protocol() = default;
virtual void call(std::string_view method, json params,
std::move_only_function<void(json)> responseHandler) = 0;
std::function<void(json)> responseHandler) = 0;
virtual void notify(std::string_view method, json params) = 0;
virtual void onEvent(std::string_view method,
std::move_only_function<void(json)> eventHandler) = 0;
std::function<void(json)> eventHandler) = 0;
virtual int processMessages() = 0;
virtual void sendLogMessage(LogLevel level, std::string_view message) = 0;
@@ -52,11 +52,11 @@ public:
virtual void addMethodHandler(
std::string_view method,
std::move_only_function<void(std::size_t id, json body)> handler) = 0;
std::function<void(std::size_t id, json body)> handler) = 0;
virtual void
addNotificationHandler(std::string_view notification,
std::move_only_function<void(json body)> handler) = 0;
std::function<void(json body)> handler) = 0;
virtual void addObject(std::string_view interfaceName,
void (*builder)(InterfaceBuilder &builder),

View File

@@ -46,21 +46,24 @@ public:
requires requires { ObjectType(std::forward<Args>(args)...); }
{
using InterfaceType = typename ObjectType::InterfaceType;
auto object = std::unique_ptr<InterfaceType, void (*)(void *)>(
static_cast<InterfaceType *>(
new ObjectType(std::forward<Args>(args)...)),
[](void *object) {
delete static_cast<ObjectType *>(
static_cast<InterfaceType *>(object));
});
this->objectCreate(
{
.name = std::string(name),
.interface = std::string(ObjectType::kInterfaceId),
},
[this, object = std::move(object)](
const ObjectCreateResponse &response) mutable {
[this, args = std::forward_as_tuple<Args...>(std::forward<Args>(
args)...)](const ObjectCreateResponse &response) {
auto object = std::unique_ptr<InterfaceType, void (*)(void *)>(
static_cast<InterfaceType *>(std::apply(
[](auto &&...args) {
return new ObjectType(std::forward<Args>(args)...);
},
args)),
[](void *object) {
delete static_cast<ObjectType *>(
static_cast<InterfaceType *>(object));
});
m_protocol->addObject(
ObjectType::kInterfaceId,
&ObjectType::Builder::template build<InterfaceBuilder>,
@@ -72,7 +75,7 @@ public:
};
using ExtensionBuilder =
std::move_only_function<std::unique_ptr<ExtensionBase>(Protocol *)>;
std::function<std::unique_ptr<ExtensionBase>(Protocol *)>;
template <typename T> ExtensionBuilder createExtension() {
auto builder = [](Protocol *protocol) {

View File

@@ -186,7 +186,7 @@ struct JsonRpcProtocol : Protocol {
}
void call(std::string_view method, json params,
std::move_only_function<void(json)> responseHandler) override {
std::function<void(json)> responseHandler) override {
std::size_t id = mNextId++;
send({
{"jsonrpc", "2.0"},
@@ -230,18 +230,18 @@ struct JsonRpcProtocol : Protocol {
void
addNotificationHandler(std::string_view notification,
std::move_only_function<void(json)> handler) override {
std::function<void(json)> handler) override {
mNotifyHandlers[std::string(notification)] = std::move(handler);
}
void addMethodHandler(
std::string_view method,
std::move_only_function<void(std::size_t, json)> handler) override {
std::function<void(std::size_t, json)> handler) override {
mMethodHandlers[std::string(method)] = std::move(handler);
}
void onEvent(std::string_view method,
std::move_only_function<void(json)> eventHandler) override {
std::function<void(json)> eventHandler) override {
mEventHandlers[std::string(method)].push_back(std::move(eventHandler));
}
@@ -395,12 +395,12 @@ private:
getTransport()->flush();
}
std::map<std::string, std::move_only_function<void(std::size_t, json)>>
std::map<std::string, std::function<void(std::size_t, json)>>
mMethodHandlers;
std::map<std::string, std::move_only_function<void(json)>> mNotifyHandlers;
std::map<std::string, std::vector<std::move_only_function<void(json)>>>
std::map<std::string, std::function<void(json)>> mNotifyHandlers;
std::map<std::string, std::vector<std::function<void(json)>>>
mEventHandlers;
std::map<std::size_t, std::move_only_function<void(json)>> mExpectedResponses;
std::map<std::size_t, std::function<void(json)>> mExpectedResponses;
std::size_t mNextId = 1;
};

View File

@@ -489,7 +489,7 @@ struct ${label} {
const params = "params" in method ? `const ${uLabel}Request &params, ` : '';
this.content += `
auto ${label}(${params}std::move_only_function<void(${returnType})> result) {
auto ${label}(${params}std::function<void(${returnType})> result) {
return protocol().call("${component}/${name}", ${params ? "params" : "{}"}, std::move(result));
}`
}
@@ -518,7 +518,7 @@ struct ${label} {
}
this.content += `
auto on${uLabel}(std::move_only_function<void(${typeName})> callback) {
auto on${uLabel}(std::function<void(${typeName})> callback) {
return protocol().onEvent("${component}/${name}", std::move(callback));
}`
}