Skip to content

Commit

Permalink
Merge pull request #638 from DenisBiryukov91/fix-z_closure-signature
Browse files Browse the repository at this point in the history
Fix C++ z_closure signature to accept extern "C" function pointers
  • Loading branch information
milyin authored Sep 3, 2024
2 parents b65f8d4 + d6eecef commit 304163d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
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

0 comments on commit 304163d

Please sign in to comment.