From ba1ca92cd578a38b3b8cf297489ed695dd8f066b Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 11 Sep 2024 12:31:55 +0200 Subject: [PATCH 1/2] add z_clone generic --- build.rs | 43 +++++++++++++++++++++++++++++++++++++++ include/zenoh_macros.h | 46 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/build.rs b/build.rs index d21c49cb3..ab3befd3e 100644 --- a/build.rs +++ b/build.rs @@ -964,6 +964,7 @@ pub fn create_generics_header(path_in: &str, path_out: &str) { let check_funcs = find_check_functions(path_in); let call_funcs = find_call_functions(path_in); let recv_funcs = find_recv_functions(path_in); + let clone_funcs = find_clone_functions(path_in); let drops = drop_funcs .iter() @@ -1072,6 +1073,10 @@ pub fn create_generics_header(path_in: &str, path_out: &str) { let out = generate_generic_recv_c(&recv_funcs); file_out.write_all(out.as_bytes()).unwrap(); + file_out.write_all("\n\n".as_bytes()).unwrap(); + + let out = generate_generic_clone_c(&clone_funcs); + file_out.write_all(out.as_bytes()).unwrap(); // // C++ part @@ -1129,6 +1134,10 @@ pub fn create_generics_header(path_in: &str, path_out: &str) { file_out.write_all(out.as_bytes()).unwrap(); file_out.write_all("\n\n".as_bytes()).unwrap(); + let out = generate_generic_clone_cpp(&clone_funcs); + file_out.write_all(out.as_bytes()).unwrap(); + file_out.write_all("\n\n".as_bytes()).unwrap(); + let out = generate_generic_loan_to_owned_type_cpp(&[loan_funcs, loan_mut_funcs].concat()); file_out.write_all(out.as_bytes()).unwrap(); @@ -1337,6 +1346,32 @@ pub fn find_recv_functions(path_in: &str) -> Vec { res } +pub fn find_clone_functions(path_in: &str) -> Vec { + let bindings = std::fs::read_to_string(path_in).unwrap(); + let re = Regex::new( + r"(\w+)\s+z_(\w+)_clone\(struct\s+(\w+)\s+\*(\w+),\s+const\s+struct\s+(\w+)\s+\*(\w+)\);", + ) + .unwrap(); + let mut res = Vec::::new(); + + for (_, [return_type, func_name, dst_type, dst_name, src_type, src_name]) in + re.captures_iter(&bindings).map(|c| c.extract()) + { + let (_, _, semantic, _) = split_type_name(dst_type); + let f = FunctionSignature::new( + semantic, + return_type, + "z_".to_string() + func_name + "_clone", + vec![ + FuncArg::new(&(dst_type.to_string() + "*"), dst_name), + FuncArg::new(&(src_type.to_string() + "*"), src_name), + ], + ); + res.push(f); + } + res +} + pub fn generate_generic_c( macro_func: &[FunctionSignature], generic_name: &str, @@ -1410,6 +1445,10 @@ pub fn generate_generic_drop_c(macro_func: &[FunctionSignature]) -> String { generate_generic_c(macro_func, "z_drop", false) } +pub fn generate_generic_clone_c(macro_func: &[FunctionSignature]) -> String { + generate_generic_c(macro_func, "z_clone", true) +} + pub fn generate_take_functions(macro_func: &[FunctionSignature]) -> String { let mut out = String::new(); for sig in macro_func { @@ -1600,6 +1639,10 @@ pub fn generate_generic_call_cpp(macro_func: &[FunctionSignature]) -> String { generate_generic_cpp(macro_func, "z_call", false) } +pub fn generate_generic_clone_cpp(macro_func: &[FunctionSignature]) -> String { + generate_generic_cpp(macro_func, "z_clone", true) +} + pub fn generate_generic_recv_cpp(macro_func: &[FunctionSignature]) -> String { let try_recv_funcs: Vec = macro_func .iter() diff --git a/include/zenoh_macros.h b/include/zenoh_macros.h index 2919ffcc5..00f037756 100644 --- a/include/zenoh_macros.h +++ b/include/zenoh_macros.h @@ -314,6 +314,20 @@ static inline void zc_closure_log_take(zc_owned_closure_log_t* closure_, zc_move const z_loaned_ring_handler_reply_t* : z_ring_handler_reply_recv, \ const z_loaned_ring_handler_sample_t* : z_ring_handler_sample_recv \ )(this_, query) + +#define z_clone(dst, this_) \ + _Generic((dst), \ + z_owned_bytes_t : z_bytes_clone, \ + z_owned_config_t : z_config_clone, \ + z_owned_encoding_t : z_encoding_clone, \ + z_owned_keyexpr_t : z_keyexpr_clone, \ + z_owned_query_t : z_query_clone, \ + z_owned_reply_t : z_reply_clone, \ + z_owned_reply_err_t : z_reply_err_clone, \ + z_owned_sample_t : z_sample_clone, \ + z_owned_slice_t : z_slice_clone, \ + z_owned_string_t : z_string_clone \ + )(&dst, this_) #else // #ifndef __cplusplus @@ -740,6 +754,38 @@ inline z_result_t z_recv(const z_loaned_ring_handler_sample_t* this_, z_owned_sa return z_ring_handler_sample_recv(this_, sample); }; + +inline void z_clone(z_owned_bytes_t& dst, z_loaned_bytes_t* this_) { + z_bytes_clone(&dst, this_); +}; +inline void z_clone(z_owned_config_t& dst, z_loaned_config_t* this_) { + z_config_clone(&dst, this_); +}; +inline void z_clone(z_owned_encoding_t& dst, z_loaned_encoding_t* this_) { + z_encoding_clone(&dst, this_); +}; +inline void z_clone(z_owned_keyexpr_t& dst, z_loaned_keyexpr_t* this_) { + z_keyexpr_clone(&dst, this_); +}; +inline void z_clone(z_owned_query_t& dst, z_loaned_query_t* this_) { + z_query_clone(&dst, this_); +}; +inline void z_clone(z_owned_reply_t& dst, z_loaned_reply_t* this_) { + z_reply_clone(&dst, this_); +}; +inline void z_clone(z_owned_reply_err_t& dst, z_loaned_reply_err_t* this_) { + z_reply_err_clone(&dst, this_); +}; +inline void z_clone(z_owned_sample_t& dst, z_loaned_sample_t* this_) { + z_sample_clone(&dst, this_); +}; +inline void z_clone(z_owned_slice_t& dst, z_loaned_slice_t* this_) { + z_slice_clone(&dst, this_); +}; +inline void z_clone(z_owned_string_t& dst, z_loaned_string_t* this_) { + z_string_clone(&dst, this_); +}; + template struct z_loaned_to_owned_type_t {}; template struct z_owned_to_loaned_type_t {}; template<> struct z_loaned_to_owned_type_t { typedef z_owned_bytes_t type; }; From 9c51a3fe48ea612b52aa0b41949cb9c880024426 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 11 Sep 2024 13:03:48 +0200 Subject: [PATCH 2/2] fix z_clone_signature to correspond to other constructors --- build.rs | 4 +-- include/zenoh_macros.h | 62 +++++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/build.rs b/build.rs index ab3befd3e..8783fdbec 100644 --- a/build.rs +++ b/build.rs @@ -1446,7 +1446,7 @@ pub fn generate_generic_drop_c(macro_func: &[FunctionSignature]) -> String { } pub fn generate_generic_clone_c(macro_func: &[FunctionSignature]) -> String { - generate_generic_c(macro_func, "z_clone", true) + generate_generic_c(macro_func, "z_clone", false) } pub fn generate_take_functions(macro_func: &[FunctionSignature]) -> String { @@ -1640,7 +1640,7 @@ pub fn generate_generic_call_cpp(macro_func: &[FunctionSignature]) -> String { } pub fn generate_generic_clone_cpp(macro_func: &[FunctionSignature]) -> String { - generate_generic_cpp(macro_func, "z_clone", true) + generate_generic_cpp(macro_func, "z_clone", false) } pub fn generate_generic_recv_cpp(macro_func: &[FunctionSignature]) -> String { diff --git a/include/zenoh_macros.h b/include/zenoh_macros.h index 00f037756..f5e49c1b5 100644 --- a/include/zenoh_macros.h +++ b/include/zenoh_macros.h @@ -317,17 +317,17 @@ static inline void zc_closure_log_take(zc_owned_closure_log_t* closure_, zc_move #define z_clone(dst, this_) \ _Generic((dst), \ - z_owned_bytes_t : z_bytes_clone, \ - z_owned_config_t : z_config_clone, \ - z_owned_encoding_t : z_encoding_clone, \ - z_owned_keyexpr_t : z_keyexpr_clone, \ - z_owned_query_t : z_query_clone, \ - z_owned_reply_t : z_reply_clone, \ - z_owned_reply_err_t : z_reply_err_clone, \ - z_owned_sample_t : z_sample_clone, \ - z_owned_slice_t : z_slice_clone, \ - z_owned_string_t : z_string_clone \ - )(&dst, this_) + z_owned_bytes_t* : z_bytes_clone, \ + z_owned_config_t* : z_config_clone, \ + z_owned_encoding_t* : z_encoding_clone, \ + z_owned_keyexpr_t* : z_keyexpr_clone, \ + z_owned_query_t* : z_query_clone, \ + z_owned_reply_t* : z_reply_clone, \ + z_owned_reply_err_t* : z_reply_err_clone, \ + z_owned_sample_t* : z_sample_clone, \ + z_owned_slice_t* : z_slice_clone, \ + z_owned_string_t* : z_string_clone \ + )(dst, this_) #else // #ifndef __cplusplus @@ -755,35 +755,35 @@ inline z_result_t z_recv(const z_loaned_ring_handler_sample_t* this_, z_owned_sa }; -inline void z_clone(z_owned_bytes_t& dst, z_loaned_bytes_t* this_) { - z_bytes_clone(&dst, this_); +inline void z_clone(z_owned_bytes_t* dst, z_loaned_bytes_t* this_) { + z_bytes_clone(dst, this_); }; -inline void z_clone(z_owned_config_t& dst, z_loaned_config_t* this_) { - z_config_clone(&dst, this_); +inline void z_clone(z_owned_config_t* dst, z_loaned_config_t* this_) { + z_config_clone(dst, this_); }; -inline void z_clone(z_owned_encoding_t& dst, z_loaned_encoding_t* this_) { - z_encoding_clone(&dst, this_); +inline void z_clone(z_owned_encoding_t* dst, z_loaned_encoding_t* this_) { + z_encoding_clone(dst, this_); }; -inline void z_clone(z_owned_keyexpr_t& dst, z_loaned_keyexpr_t* this_) { - z_keyexpr_clone(&dst, this_); +inline void z_clone(z_owned_keyexpr_t* dst, z_loaned_keyexpr_t* this_) { + z_keyexpr_clone(dst, this_); }; -inline void z_clone(z_owned_query_t& dst, z_loaned_query_t* this_) { - z_query_clone(&dst, this_); +inline void z_clone(z_owned_query_t* dst, z_loaned_query_t* this_) { + z_query_clone(dst, this_); }; -inline void z_clone(z_owned_reply_t& dst, z_loaned_reply_t* this_) { - z_reply_clone(&dst, this_); +inline void z_clone(z_owned_reply_t* dst, z_loaned_reply_t* this_) { + z_reply_clone(dst, this_); }; -inline void z_clone(z_owned_reply_err_t& dst, z_loaned_reply_err_t* this_) { - z_reply_err_clone(&dst, this_); +inline void z_clone(z_owned_reply_err_t* dst, z_loaned_reply_err_t* this_) { + z_reply_err_clone(dst, this_); }; -inline void z_clone(z_owned_sample_t& dst, z_loaned_sample_t* this_) { - z_sample_clone(&dst, this_); +inline void z_clone(z_owned_sample_t* dst, z_loaned_sample_t* this_) { + z_sample_clone(dst, this_); }; -inline void z_clone(z_owned_slice_t& dst, z_loaned_slice_t* this_) { - z_slice_clone(&dst, this_); +inline void z_clone(z_owned_slice_t* dst, z_loaned_slice_t* this_) { + z_slice_clone(dst, this_); }; -inline void z_clone(z_owned_string_t& dst, z_loaned_string_t* this_) { - z_string_clone(&dst, this_); +inline void z_clone(z_owned_string_t* dst, z_loaned_string_t* this_) { + z_string_clone(dst, this_); }; template struct z_loaned_to_owned_type_t {};