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

allow to use z_move, z_loan etc with pointers #599

Draft
wants to merge 113 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
9841d45
macro generation
milyin Jun 30, 2024
2b2b0d1
moved types added to decl
milyin Jun 30, 2024
c0fe84e
moved type drop
milyin Jun 30, 2024
2197cad
switched rust to z_moved
milyin Jun 30, 2024
4ce12e5
moved closures
milyin Jun 30, 2024
b19de8d
build macros fixed
milyin Jun 30, 2024
25d2c96
z_move name restored
milyin Jun 30, 2024
a27ea46
into_rust_type for moved, payloads
milyin Jun 30, 2024
fc54e00
tests updated
milyin Jun 30, 2024
26b6555
cargo fmt
milyin Jul 1, 2024
6a1b03c
moved structs in some drops/undeclares
milyin Jul 1, 2024
aa087ab
moved as separate parameter
milyin Jul 2, 2024
7ffc28f
removed asref from moved
milyin Jul 2, 2024
b8e5c95
moved unfinished
milyin Jul 2, 2024
1ac56fa
moved type with into_rust_type trait, comiles without shm
milyin Jul 3, 2024
2755e80
build with shm passed
milyin Jul 3, 2024
9be5468
option added to some decl_c_type
milyin Jul 3, 2024
b26d929
clippy fix
milyin Jul 3, 2024
91a7cce
Merge branch 'dev/1.0.0' into move_protection2
milyin Jul 3, 2024
036518c
build fix
milyin Jul 3, 2024
eef474a
moved types added
milyin Jul 3, 2024
da0e172
task moved used
milyin Jul 3, 2024
51b51cb
some examples fixes
milyin Jul 4, 2024
52f07dd
Merge branch 'dev/1.0.0' into move_protection2
milyin Jul 4, 2024
c83f304
macros corrected to use auto derive loaned type from owned feature
milyin Jul 4, 2024
d3c36b7
optional comma allowed in macros where forgotten
milyin Jul 4, 2024
ad606ee
property moved get
milyin Jul 4, 2024
f9cf7db
put options move
milyin Jul 4, 2024
910534a
publisher delete options made simpler
milyin Jul 4, 2024
1901504
put options with moved
milyin Jul 4, 2024
949c033
delete options timestamp simplified
milyin Jul 4, 2024
a1a506d
more moved in options, timestamp simplified
milyin Jul 4, 2024
384606c
examples,tests updated
milyin Jul 4, 2024
697e769
tests compile fixes
milyin Jul 5, 2024
3656761
fix for test failure due to calling z_moved_xxx_t destructor on uniti…
milyin Jul 5, 2024
6c2c5b1
cargo fmt imports
milyin Jul 5, 2024
b302209
Merge branch 'dev/1.0.0' into move_protection2
milyin Jul 5, 2024
d32dc16
build fixes
milyin Jul 5, 2024
9555c54
Merge branch 'dev/1.0.0' into move_protection2
milyin Jul 28, 2024
f466f2a
some xompile errors fixed
milyin Jul 28, 2024
ce2de57
some build errors fixed
milyin Jul 28, 2024
bb71c4a
some build errors fixed
milyin Jul 28, 2024
4fe741e
build fixes
milyin Jul 28, 2024
a4de03c
cargo fmt
milyin Jul 28, 2024
d533dc8
into_rust_type usage fixes
milyin Jul 28, 2024
eb5839d
encoding drop fixed
milyin Jul 28, 2024
c5baf3d
restored headers
milyin Jul 28, 2024
51aa6a8
zcu renamed back to zc
milyin Jul 28, 2024
c99f1ed
zcu renamed back to zc
milyin Jul 28, 2024
485c885
z_xxx_move is static inline, cpp fixes
milyin Jul 29, 2024
f69ec33
clang format from start
milyin Jul 30, 2024
a5a8802
cargo fmt
milyin Jul 30, 2024
9c09c0b
Merge branch 'dev/1.0.0' into move_protection2
milyin Jul 30, 2024
6176248
macros contains funcions now, it needs types defined
milyin Jul 30, 2024
f8d2a1f
removed zenoh_macros include
milyin Jul 30, 2024
5c55758
zenoh_macros include returned back to place
milyin Jul 30, 2024
a812196
C++ build test added, fails for now
milyin Jul 30, 2024
90a45e9
C++ enabling correction
milyin Jul 30, 2024
ed5d9d7
C++ compilation for tests added
milyin Jul 31, 2024
60c01da
C++ build test
milyin Aug 1, 2024
90bb511
cargo lock update
milyin Aug 1, 2024
2920868
retrun value if not void from template functions
milyin Aug 1, 2024
1cf9569
Merge branch 'dev/1.0.0' into move_protection2
milyin Aug 1, 2024
15cf196
cargo fmt
milyin Aug 1, 2024
82f9233
build fixes
milyin Aug 1, 2024
bae3273
build fix after cargo.lock update
milyin Aug 1, 2024
4ab1965
Merge branch 'global_client_sroage_build_fix' into move_protection2
milyin Aug 1, 2024
ec153b6
moved types for buffer creation functions
milyin Aug 2, 2024
9014d82
clippy fix
milyin Aug 2, 2024
933daa1
clippy fix: c_char can be i8 or u8 depending on platform
milyin Aug 2, 2024
2428236
headers restored
milyin Aug 2, 2024
5bc1ca6
cargo fmt
milyin Aug 2, 2024
7a1d734
-c c++ flag for clang only
milyin Aug 2, 2024
ac5b6d6
c++ build fix - brackets removed
milyin Aug 2, 2024
5562413
type specific take functions added, _ptr in moved
milyin Aug 3, 2024
fa799c5
generic_take_cpp
milyin Aug 3, 2024
7f15915
z_take impls at the end
milyin Aug 3, 2024
165e81b
take funcs before generics
milyin Aug 3, 2024
05de856
take moved after null
milyin Aug 3, 2024
2e66048
names fix
milyin Aug 3, 2024
5172aae
missing null functioj added
milyin Aug 3, 2024
6ad18e7
tests fixed for c++
milyin Aug 3, 2024
d5d5775
explicit null calls
milyin Aug 4, 2024
8957e5f
fix generic parameter names c compilation
milyin Aug 4, 2024
47e7c15
null call fix
milyin Aug 4, 2024
5189daa
misprint fixed
milyin Aug 4, 2024
016ae13
return removed
milyin Aug 4, 2024
a4e5e37
Rename `close` to `undeclare` for Publication Cache and Querying Subs…
fuzzypixelz Aug 1, 2024
5cb9e26
Temporarily use original pull request branch
fuzzypixelz Aug 1, 2024
cda429f
Update to eclipse-zenoh/zenoh@ce4e9bf
fuzzypixelz Aug 2, 2024
700b522
Fix `z_ref_shm_client_storage_global`
fuzzypixelz Aug 2, 2024
c8ee1ea
Update Cargo.toml
Mallets Aug 2, 2024
d46e38d
Merge branch 'dev/1.0.0' into move_protection2
milyin Aug 4, 2024
31be258
Merge branch 'main' into merge_main
milyin Aug 12, 2024
62686ff
decl_c_type corrected
milyin Aug 12, 2024
7e4c979
Merge branch 'dev/1.0.0' into merge_main
milyin Aug 12, 2024
8dc6a0e
cargo check run
milyin Aug 12, 2024
e1b06cc
borrow error fix
milyin Aug 12, 2024
1742d26
Merge branch 'dev/1.0.0' into merge_main
milyin Aug 16, 2024
a5d0707
Merge branch 'merge_main' into moved_as_ptr_merge_main
milyin Aug 16, 2024
9c384fa
c generics accepts both pointer and value
milyin Aug 17, 2024
357c144
template functions in c++ accepts both ref and pointer, like in C
milyin Aug 17, 2024
fc202ab
clang-format
milyin Aug 17, 2024
bf3a0f9
compilation fix
milyin Aug 17, 2024
3db136e
parse arg fix
milyin Aug 17, 2024
d2f9eea
example compilation fix
milyin Aug 17, 2024
c7fa4a0
examples compile fix
milyin Aug 17, 2024
ebe6b89
examples build fixes
milyin Aug 17, 2024
f8babaa
removed duplicated z_config_default (it's called in parsing args later)
milyin Aug 17, 2024
35a2cca
clang format
milyin Aug 17, 2024
e16ac3b
clang format
milyin Aug 17, 2024
7140a38
cargo.toml restore
milyin Aug 17, 2024
a56d016
Merge branch 'moved_as_ptr_merge_main2' into moved_as_ptr_merge_main
milyin Aug 17, 2024
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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ include/zenoh_opaque.h

# Build resources
.build_resources*
src/opaque_types/mod.rs
src/opaque_types/mod.rs

# CMake
CMakeFiles/
debug/
release/
53 changes: 38 additions & 15 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ pub fn generate_generic_c(
let va_args = macro_func
.iter()
.any(|f| f.args.len() != macro_func[0].args.len());
let mut args = macro_func
let args = macro_func
.first()
.unwrap_or_else(|| panic!("no sigatures found for building generic {generic_name}"))
.args
Expand All @@ -1367,26 +1367,35 @@ pub fn generate_generic_c(
args[0],
)
};
if decay {
args[0] = format!("&{}", args[0]);
}

for func in macro_func {
let owned_type = if decay {
func.args[0].typename.clone().decay().typename
} else {
func.args[0].typename.typename.clone()
};
let func_name = &func.func_name;
let select_type = func.args[0].typename.typename.clone();
out += ", \\\n";
out += &format!(" {owned_type} : {func_name}");
out += &format!(" {select_type} : {func_name}");
if decay {
let select_type = func.args[0].typename.clone().decay().typename;
out += ", \\\n";
out += &format!(" {select_type} : {func_name}");
}
}
out += " \\\n";
if va_args {
out += &format!(" )({}, __VA_ARGS__)", args.join(", "));
out += " \\\n )(";
let sargs = args.join(", ") + if va_args { ", __VA_ARGS__" } else { "" };
if decay {
out += format!("_Generic(({})", args[0]).as_str();
for func in macro_func {
let select_type = func.args[0].typename.typename.clone();
out += ", \\\n";
out += &format!(" {select_type} : ({sargs})");

let select_type = func.args[0].typename.clone().decay().typename;
out += ", \\\n";
out += &format!(" {select_type} : (&{sargs})");
}
out += " \\\n )";
} else {
out += &format!(" )({})", args.join(", "));
out += &sargs;
}
out += ")\n";
out
}

Expand Down Expand Up @@ -1497,6 +1506,20 @@ pub fn generate_generic_cpp(
macro_func: &[FunctionSignature],
generic_name: &str,
decay: bool,
) -> String {
if decay {
let mut out = generate_generic_cpp_impl(macro_func, generic_name, false);
out += &generate_generic_cpp_impl(macro_func, generic_name, true);
out
} else {
generate_generic_cpp_impl(macro_func, generic_name, false)
}
}

pub fn generate_generic_cpp_impl(
macro_func: &[FunctionSignature],
generic_name: &str,
decay: bool,
) -> String {
let mut out = "".to_owned();

Expand Down
216 changes: 216 additions & 0 deletions examples/parse_args.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
//
// Copyright (c) 2024 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "zenoh.h"

#define COMMON_HELP \
"\
-c <CONFIG> (optional, string): The path to a configuration file for the session. If this option isn't passed, the default configuration will be used.\n\
-m <MODE> (optional, string, default='peer'): The zenoh session mode. [possible values: peer, client, router]\n\
-e <CONNECT> (optional, string): endpoint to connect to. Repeat option to pass multiple endpoints. If none are given, endpoints will be discovered through multicast-scouting if it is enabled.\n\
e.g.: '-e tcp/192.168.1.1:7447'\n\
-l <LISTEN> (optional, string): locator to listen on. Repeat option to pass multiple locators. If none are given, the default configuration will be used.\n\
e.g.: '-l tcp/192.168.1.1:7447'\n\
--no-multicast-scouting (optional): By default zenohd replies to multicast scouting messages for being discovered by peers and clients. This option disables this feature.\n\
"

/**
* Parse an option of format `-f`, `--flag`, `-f <value>` or `--flag <value>` from `argv`. If found, the option and its
* eventual value are each replaced by NULL in `argv`
* @param argc: argc passed from `main` function
* @param argv: argv passed from `main` function
* @param opt: option to parse (without `-` or `--` prefix)
* @param opt_has_value: if true, the option is of format `-f <value>` or `--flag <value>` and `value` will be returned
* if found, else an error message is printed and program will exit. If false, option has no value and a non-null
* pointer will be returned if option is found.
* @returns NULL if option was not found, else a non-null value depending on if `opt_has_value`.
*/
const char* parse_opt(int argc, char** argv, const char* opt, bool opt_has_value) {
size_t optlen = strlen(opt);
for (int i = 1; i < argc; i++) {
if (argv[i] == NULL) {
continue;
}
size_t len = strlen(argv[i]);
if (len < 2) {
continue;
}
if (optlen == 1) {
if (argv[i][0] == '-' && argv[i][1] == opt[0]) {
argv[i] = NULL;
if (!opt_has_value) {
return (char*)opt;
} else if (i + 1 < argc && argv[i + 1]) {
char* value = argv[i + 1];
argv[i + 1] = NULL;
return value;
} else {
printf("Option -%s given without a value\n", opt);
exit(-1);
}
}
} else if (optlen > 1 && len > 3 && argv[i][0] == '-' && argv[i][1] == '-') {
// Note: support for '--arg=<value>' syntax can be added here
if (strcmp(argv[i] + 2, opt) == 0) {
argv[i] = NULL;
if (!opt_has_value) {
return (char*)opt;
} else if (i + 1 < argc && argv[i + 1]) {
char* value = argv[i + 1];
argv[i + 1] = NULL;
return value;
} else {
printf("Option --%s given without a value\n", opt);
exit(-1);
}
}
}
}
return NULL;
}

/**
* Check if any options remains in `argv`. Must be called after all expected options are parsed
* @param argc
* @param argv
* @returns NULL if no option was found, else the first option string that was found
*/
const char* check_unknown_opts(int argc, char** const argv) {
for (int i = 1; i < argc; i++) {
if (argv[i] && argv[i][0] == '-') {
return argv[i];
}
}
return NULL;
}

/**
* Parse positional arguments from `argv`. Must be called after all expected options are parsed, and after checking that
* no unknown options remain in `argv`
* @param argc
* @param argv
* @param nb_args: number of expected positional arguments
* @returns NULL if found more positional arguments than `nb_args`. Else an array of found arguments in order, followed
* by NULL values if found less positional arguments than `nb_args`
* @note Returned pointer is dynamically allocated and must be freed
*/
char** parse_pos_args(const int argc, char** argv, const size_t nb_args) {
char** pos_argv = (char**)calloc(nb_args, sizeof(char*));
size_t pos_argc = 0;
for (int i = 1; i < argc; i++) {
if (argv[i]) {
pos_argc++;
if (pos_argc > nb_args) {
free(pos_argv);
return NULL;
}
pos_argv[pos_argc - 1] = argv[i];
}
}
return pos_argv;
}

/**
* Parse zenoh options that require a JSON-serialized list (-e, -l from common args) and add them to
* `config`. Prints error message and exits if fails to insert parsed values
* @param argc
* @param argv
* @param opt: option to parse (without `-` or `--` prefix)
* @param config: address of an owned zenoh configuration
* @param config_key: zenoh configuration key under which the parsed values will be inserted
*/
void parse_zenoh_json_list_config(int argc, char** argv, const char* opt, const char* config_key,
z_owned_config_t* config) {
char* buf = (char*)calloc(1, sizeof(char));
const char* value = parse_opt(argc, argv, opt, true);
while (value) {
size_t len_newbuf = strlen(buf) + strlen(value) + 4; // value + quotes + comma + nullbyte
char* newbuf = (char*)malloc(len_newbuf);
snprintf(newbuf, len_newbuf, "%s'%s',", buf, value);
free(buf);
buf = newbuf;
value = parse_opt(argc, argv, opt, true);
}
size_t buflen = strlen(buf);
if (buflen > 0) {
// remove trailing comma
buf[buflen - 1] = '\0';
buflen--;
// add list delimiters
size_t json_list_len = buflen + 3; // buf + brackets + nullbyte
char* json_list = (char*)malloc(json_list_len);
snprintf(json_list, json_list_len, "[%s]", buf);
// insert in config
if (zc_config_insert_json(z_loan_mut(*config), config_key, json_list) < 0) {
printf(
"Couldn't insert value `%s` in configuration at `%s`\n`%s` is either not a JSON-serialized list of "
"strings, or values within the list do not respect expected format for `%s`\n",
json_list, config_key, json_list, config_key);
free(json_list);
exit(-1);
}
free(json_list);
}
free(buf);
}

/**
* Parse zenoh options that are common to all examples (-c, -m, -e, -l, --no-multicast-scouting) and add them to
* `config`
* @param argc
* @param argv
* @param config: address of an owned zenoh configuration
*/
void parse_zenoh_common_args(const int argc, char** argv, z_owned_config_t* config) {
// -c: A configuration file.
const char* config_file = parse_opt(argc, argv, "c", true);
if (config_file) {
zc_config_from_file(config, config_file);
} else {
z_config_default(config);
}
// -m: The Zenoh session mode [default: peer].
const char* mode = parse_opt(argc, argv, "m", true);
if (mode) {
size_t buflen = strlen(mode) + 3; // mode + quotes + nullbyte
char* buf = (char*)malloc(buflen);
snprintf(buf, buflen, "'%s'", mode);
if (zc_config_insert_json(z_loan_mut(*config), Z_CONFIG_MODE_KEY, buf) < 0) {
printf(
"Couldn't insert value `%s` in configuration at `%s`. Value must be one of: 'client', 'peer' or "
"'router'\n",
mode, Z_CONFIG_MODE_KEY);
free(buf);
exit(-1);
}
free(buf);
}
// -e: Endpoint to connect to. Can be repeated
parse_zenoh_json_list_config(argc, argv, "e", Z_CONFIG_CONNECT_KEY, config);
// -l: Endpoint to listen on. Can be repeated
parse_zenoh_json_list_config(argc, argv, "l", Z_CONFIG_LISTEN_KEY, config);
// --no-multicast-scrouting: Disable the multicast-based scouting mechanism.
const char* no_multicast_scouting = parse_opt(argc, argv, "no-multicast-scouting", false);
if (no_multicast_scouting &&
zc_config_insert_json(z_loan_mut(*config), Z_CONFIG_MULTICAST_SCOUTING_KEY, "false") < 0) {
printf("Couldn't disable multicast-scouting.\n");
exit(-1);
}
}
63 changes: 48 additions & 15 deletions examples/z_delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,19 @@
#include <stdio.h>
#include <string.h>

#include "parse_args.h"
#include "zenoh.h"

int main(int argc, char **argv) {
char *keyexpr = "demo/example/zenoh-c-put";
#define DEFAULT_KEYEXPR "demo/example/zenoh-c-put"

if (argc > 1) keyexpr = argv[1];
struct args_t {
char* keyexpr; // -k
};
struct args_t parse_args(int argc, char** argv, z_owned_config_t* config);

int main(int argc, char** argv) {
z_owned_config_t config;
z_config_default(&config);
if (argc > 3) {
if (zc_config_insert_json(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, argv[3]) < 0) {
printf(
"Couldn't insert value `%s` in configuration at `%s`. This is likely because `%s` expects a "
"JSON-serialized list of strings\n",
argv[3], Z_CONFIG_CONNECT_KEY, Z_CONFIG_CONNECT_KEY);
exit(-1);
}
}
struct args_t args = parse_args(argc, argv, &config);

printf("Opening session...\n");
z_owned_session_t s;
Expand All @@ -40,9 +35,9 @@ int main(int argc, char **argv) {
exit(-1);
}

printf("Deleting resources matching '%s'...\n", keyexpr);
printf("Deleting resources matching '%s'...\n", args.keyexpr);
z_view_keyexpr_t ke;
z_view_keyexpr_from_str(&ke, keyexpr);
z_view_keyexpr_from_str(&ke, args.keyexpr);
int res = z_delete(z_loan(s), z_loan(ke), NULL);
if (res < 0) {
printf("Delete failed...\n");
Expand All @@ -51,3 +46,41 @@ int main(int argc, char **argv) {
z_close(z_move(s));
return 0;
}

void print_help() {
printf(
"\
Usage: z_delete [OPTIONS]\n\n\
Options:\n\
-k <KEY> (optional, string, default='%s'): The key expression to write to\n",
DEFAULT_KEYEXPR);
printf(COMMON_HELP);
printf(
"\
-h: print help\n");
}

struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
if (parse_opt(argc, argv, "h", false)) {
print_help();
exit(1);
}
const char* keyexpr = parse_opt(argc, argv, "k", true);
if (!keyexpr) {
keyexpr = DEFAULT_KEYEXPR;
}
parse_zenoh_common_args(argc, argv, config);
const char* arg = check_unknown_opts(argc, argv);
if (arg) {
printf("Unknown option %s\n", arg);
exit(-1);
}
char** pos_args = parse_pos_args(argc, argv, 1);
if (!pos_args || pos_args[0]) {
printf("Unexpected positional arguments\n");
free(pos_args);
exit(-1);
}
free(pos_args);
return (struct args_t){.keyexpr = (char*)keyexpr};
}
Loading