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

Add z_clone macro #673

Merged
merged 2 commits into from
Sep 11, 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
43 changes: 43 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -1337,6 +1346,32 @@ pub fn find_recv_functions(path_in: &str) -> Vec<FunctionSignature> {
res
}

pub fn find_clone_functions(path_in: &str) -> Vec<FunctionSignature> {
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::<FunctionSignature>::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,
Expand Down Expand Up @@ -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", false)
}

pub fn generate_take_functions(macro_func: &[FunctionSignature]) -> String {
let mut out = String::new();
for sig in macro_func {
Expand Down Expand Up @@ -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", false)
}

pub fn generate_generic_recv_cpp(macro_func: &[FunctionSignature]) -> String {
let try_recv_funcs: Vec<FunctionSignature> = macro_func
.iter()
Expand Down
46 changes: 46 additions & 0 deletions include/zenoh_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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<class T> struct z_loaned_to_owned_type_t {};
template<class T> struct z_owned_to_loaned_type_t {};
template<> struct z_loaned_to_owned_type_t<z_loaned_bytes_t> { typedef z_owned_bytes_t type; };
Expand Down