implement object registry

This commit is contained in:
Сергей Маринкевич
2025-12-04 20:03:00 +07:00
parent e7aa646a80
commit 47092fc6f1
10 changed files with 183 additions and 68 deletions
+33 -8
View File
@@ -1,16 +1,41 @@
#include "{{ cls.name }}.skeleton.h"
{{ cls.name }}Skeleton::{{ cls.name }}Skeleton({{ cls.name }}& obj, RpcInvoker& inv)
: invoker(inv) {
{{ cls.name }}Skeleton::{{ cls.name }}Skeleton({{ cls.name }}& obj)
: obj_(obj) {
}
RpcValue {{ cls.name }}Skeleton::invoke(const std::string& method,
const RpcArgs& args) {
const auto& table = handlers();
auto it = table.find(method);
if (it == table.end()) {
// Неизвестный метод — PoC: возвращаем 0.
return RpcValue::fromInt(0);
}
Handler fn = it->second;
return (this->*fn)(args);
}
const std::unordered_map<std::string, {{ cls.name }}Skeleton::Handler>&
{{ cls.name }}Skeleton::handlers() {
static const std::unordered_map<std::string, Handler> kHandlers = {
{% for m in cls.methods %}
invoker.registerMethod(&obj,
"{{ cls.name }}.{{ m.name }}",
&{{ cls.name }}::{{ m.name }});
{ "{{ cls.name }}.{{ m.name }}", &{{ cls.name }}Skeleton::call_{{ m.name }} }{% if not loop.last %},{% endif %}
{% endfor %}
};
return kHandlers;
}
RpcValue {{ cls.name }}Skeleton::dispatch(const std::string& method,
const RpcArgs& args) {
return invoker.dispatch(method, args);
{% for m in cls.methods %}
RpcValue {{ cls.name }}Skeleton::call_{{ m.name }}(const RpcArgs& args) {
// PoC: единственный тип аргументов и результата — int.
int idx = 0;
{% for a in m.args %}
int {{ a.name }} = args[idx++].asInt();
{% endfor %}
int result = obj_.{{ m.name }}({% for a in m.args %}{{ a.name }}{% if not loop.last %}, {% endif %}{% endfor %});
return RpcValue::fromInt(result);
}
{% endfor %}
+20 -6
View File
@@ -1,15 +1,29 @@
#pragma once
#include "{{ cls.name }}.h"
#include "rpc/RpcInvoker.h"
#include "rpc/RpcRegistry.h"
#include "rpc/RpcValue.h"
class {{ cls.name }}Skeleton {
#include <string>
#include <unordered_map>
// Skeleton-обёртка над {{ cls.name }}, реализующая IRpcObject::invoke().
class {{ cls.name }}Skeleton : public IRpcObject {
public:
explicit {{ cls.name }}Skeleton({{ cls.name }}& obj, RpcInvoker& invoker);
explicit {{ cls.name }}Skeleton({{ cls.name }}& obj);
// IPC-независимый диспетчер: принимает имя метода и RpcArgs.
RpcValue dispatch(const std::string& method, const RpcArgs& args);
RpcValue invoke(const std::string& method,
const RpcArgs& args) override;
private:
RpcInvoker& invoker;
using Handler = RpcValue ({{ cls.name }}Skeleton::*)(const RpcArgs&);
// Статическая таблица method-name -> member-function.
static const std::unordered_map<std::string, Handler>& handlers();
{% for m in cls.methods %}
RpcValue call_{{ m.name }}(const RpcArgs& args);
{% endfor %}
{{ cls.name }}& obj_;
};