implement client C binding

This commit is contained in:
2026-01-25 13:16:29 +07:00
parent 168b5fdfea
commit f261621c68
9 changed files with 403 additions and 21 deletions
+8
View File
@@ -262,6 +262,8 @@ def main():
proxy_cpp = env.get_template("proxy.cpp.j2")
skeleton_h = env.get_template("skeleton.h.j2")
skeleton_cpp = env.get_template("skeleton.cpp.j2")
client_c_h = env.get_template("client_c.h.j2")
client_c_cpp = env.get_template("client_c.cpp.j2")
base = args.out_base
@@ -277,6 +279,12 @@ def main():
with open(f"{out_dir}/{base}.skeleton.cpp", "w") as f:
f.write(skeleton_cpp.render(cls=cls))
with open(f"{out_dir}/{base}Client_c.h", "w") as f:
f.write(client_c_h.render(cls=cls))
with open(f"{out_dir}/{base}Client_c.cpp", "w") as f:
f.write(client_c_cpp.render(cls=cls))
print("Generated files for class", cls.name, "into base", base)
return 0
+45
View File
@@ -0,0 +1,45 @@
#include "{{ cls.name }}Client_c.h"
#include "rpc/rpc_client_c_impl.h"
#include "ipc/IpcMarshaller.h"
#include <stdexcept>
extern "C" {
struct {{ cls.name }}Client {
IpcMarshaller* marshaller;
{{ cls.name }}Client(IpcMarshaller* m) : marshaller(m) {}
};
{{ cls.name }}Client* {{ cls.name|lower }}_client_create(IpcMarshallerHandle* marshaller) {
if (!marshaller || !marshaller->marshaller) {
return nullptr;
}
try {
return new {{ cls.name }}Client(marshaller->marshaller);
} catch (...) {
return nullptr;
}
}
void {{ cls.name|lower }}_client_destroy({{ cls.name }}Client* client) {
if (client) {
delete client;
}
}
{% for m in cls.methods %}
{{ m.return_type }} {{ cls.name|lower }}_{{ m.name }}({{ cls.name }}Client* client{% for a in m.args %}, {{ a.type }} {{ a.name }}{% endfor %}) {
if (!client || !client->marshaller) {
return -1;
}
try {
return client->marshaller->template callTyped<{{ m.return_type }}>("{{ cls.name }}.{{ m.name }}"{% for a in m.args %}, {{ a.name }}{% endfor %});
} catch (...) {
return -1;
}
}
{% endfor %}
} // extern "C"
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include "rpc/rpc_client_c.h"
#ifdef __cplusplus
extern "C" {
#endif
// Непрозрачный тип для клиента {{ cls.name }}
typedef struct {{ cls.name }}Client {{ cls.name }}Client;
// Создание клиента {{ cls.name }}
// marshaller - маршаллер, созданный через rpc_client_create_marshaller
// Возвращает указатель на клиент или NULL при ошибке
{{ cls.name }}Client* {{ cls.name|lower }}_client_create(IpcMarshallerHandle* marshaller);
// Уничтожение клиента {{ cls.name }}
void {{ cls.name|lower }}_client_destroy({{ cls.name }}Client* client);
{% for m in cls.methods %}
// Вызов метода {{ m.name }}
// client - клиент {{ cls.name }}
{% for a in m.args %}
// {{ a.name }} - {{ a.type }} аргумент
{% endfor %}
// Возвращает результат или -1 при ошибке
{{ m.return_type }} {{ cls.name|lower }}_{{ m.name }}({{ cls.name }}Client* client{% for a in m.args %}, {{ a.type }} {{ a.name }}{% endfor %});
{% endfor %}
#ifdef __cplusplus
}
#endif