Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix C++ z_closure signature to accept extern "C" function pointers #638

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,7 @@ pub fn generate_generic_recv_cpp(macro_func: &[FunctionSignature]) -> String {
pub fn generate_generic_closure_cpp(macro_func: &[FunctionSignature]) -> String {
let mut out = "".to_owned();

out += "extern \"C\" using z_closure_drop_callback_t = void(void*);\n";
for func in macro_func {
let return_type = &func.return_type.typename;
let closure_name = &func.args[0].name;
Expand All @@ -1629,12 +1630,20 @@ pub fn generate_generic_closure_cpp(macro_func: &[FunctionSignature]) -> String
.typename
.replace("loaned", "owned");
let arg_type = &func.args[1].typename.typename;
let callback_type = func.args[0]
.typename
.clone()
.decay()
.typename
.replace("_t", "_callback_t")
.replace("_loaned", "");
out += "\n";
out += &format!(
"inline void z_closure(
"extern \"C\" using {callback_type} = {return_type}({arg_type}, void*);
inline void z_closure(
{closure_type} {closure_name},
{return_type} (*call)({arg_type}, void*),
void (*drop)(void*),
{callback_type}* call,
z_closure_drop_callback_t* drop,
void *context) {{
{closure_name}->context = context;
{closure_name}->drop = drop;
Expand Down
21 changes: 13 additions & 8 deletions include/zenoh_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,38 +657,43 @@ inline void z_call(const z_loaned_closure_sample_t* closure, const z_loaned_samp
z_closure_sample_call(closure, sample);
};

extern "C" typedef void (*z_closure_drop_callback_t)(void*);

extern "C" typedef void (*z_closure_hello_callback_t)(const z_loaned_hello_t*, void*);
inline void z_closure(
z_owned_closure_hello_t* closure,
void (*call)(const z_loaned_hello_t*, void*),
void (*drop)(void*),
z_closure_hello_callback_t call,
z_closure_drop_callback_t drop,
void *context) {
closure->context = context;
closure->drop = drop;
closure->call = call;
};
extern "C" typedef void (*z_closure_query_callback_t)(const z_loaned_query_t*, void*);
inline void z_closure(
z_owned_closure_query_t* closure,
void (*call)(const z_loaned_query_t*, void*),
void (*drop)(void*),
z_closure_query_callback_t call,
z_closure_drop_callback_t drop,
void *context) {
closure->context = context;
closure->drop = drop;
closure->call = call;
};
extern "C" typedef void (*z_closure_reply_callback_t)(const z_loaned_reply_t*, void*);
inline void z_closure(
z_owned_closure_reply_t* closure,
void (*call)(const z_loaned_reply_t*, void*),
void (*drop)(void*),
z_closure_reply_callback_t call,
z_closure_drop_callback_t drop,
void *context) {
closure->context = context;
closure->drop = drop;
closure->call = call;
};
extern "C" typedef void (*z_closure_sample_callback_t)(const z_loaned_sample_t*, void*);
inline void z_closure(
z_owned_closure_sample_t* closure,
void (*call)(const z_loaned_sample_t*, void*),
void (*drop)(void*),
z_closure_sample_callback_t call,
z_closure_drop_callback_t drop,
void *context) {
closure->context = context;
closure->drop = drop;
Expand Down
Loading