Skip to content

Commit

Permalink
make serialization stable (#701)
Browse files Browse the repository at this point in the history
* make serialization stable

* format
  • Loading branch information
DenisBiryukov91 authored Oct 1, 2024
1 parent 4d8e6ee commit 638f011
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 181 deletions.
8 changes: 4 additions & 4 deletions examples/unix/c11/z_bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ int main(void) {
z_drop(z_move(payload));
z_drop(z_move(output_string));
}
#if defined(Z_FEATURE_UNSTABLE_API)

// Serialization
{
// Arithmetic types: uint8, uint16, uint32, uint64, int8, int16, int32, int64, float, double
Expand All @@ -70,7 +70,7 @@ int main(void) {
// Corresponding encoding to be used in operations options like `z_put()`, `z_get()`, etc.
// const z_loaned_encoding* encoding = z_encoding_zenoh_uint32();
}
#endif

// Writer/reader for raw bytes
{
uint8_t input_writer[] = {0, 1, 2, 3, 4};
Expand All @@ -86,7 +86,7 @@ int main(void) {
assert(0 == memcmp(input_writer, output_reader, sizeof(output_reader)));
z_drop(z_move(payload));
}
#if defined(Z_FEATURE_UNSTABLE_API)

// Using serializer/deserializer for composite types
{
// A sequence of primitive types
Expand Down Expand Up @@ -193,7 +193,7 @@ int main(void) {
z_drop(z_move(c));
z_drop(z_move(payload));
}
#endif

// Slice iterator
{
/// Fill z_bytes with some data
Expand Down
5 changes: 1 addition & 4 deletions examples/unix/c11/z_get_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ void reply_handler(z_loaned_reply_t *reply, void *ctx) {
z_drop(z_move(replystr));
z_drop(z_move(encoding));

#if defined(Z_FEATURE_UNSTABLE_API)
// Check attachment
const z_loaned_bytes_t *attachment = z_sample_attachment(sample);
if (attachment == NULL) {
Expand All @@ -85,7 +84,6 @@ void reply_handler(z_loaned_reply_t *reply, void *ctx) {
}
drop_attachment(kvp, attachment_len);
free(kvp);
#endif
} else {
printf(">> Received an error\n");
}
Expand Down Expand Up @@ -172,7 +170,6 @@ int main(int argc, char **argv) {
opts.payload = z_bytes_move(&payload);
}

#if defined(Z_FEATURE_UNSTABLE_API)
// Add attachment value
kv_pair_t kvs[1];
z_string_from_str(&kvs[0].key, "test_key", NULL, NULL);
Expand All @@ -189,7 +186,7 @@ int main(int argc, char **argv) {
drop_attachment(kvs, 1);
ze_serializer_finish(z_move(serializer), &attachment);
opts.attachment = z_move(attachment);
#endif

// Add encoding value
z_owned_encoding_t encoding;
z_encoding_from_str(&encoding, "zenoh/string;utf8");
Expand Down
7 changes: 3 additions & 4 deletions examples/unix/c11/z_pub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ int main(int argc, char **argv) {
z_publisher_put_options_t options;
z_publisher_put_options_default(&options);

#if defined(Z_FEATURE_UNSTABLE_API)
// Allocate attachment
kv_pair_t kvs[2];
kvs[0] = (kv_pair_t){.key = "source", .value = "C"};
#endif

z_owned_bytes_t attachment;
z_bytes_empty(&attachment);

Expand All @@ -139,7 +138,7 @@ int main(int argc, char **argv) {
// Create payload
z_owned_bytes_t payload;
z_bytes_copy_from_str(&payload, buf);
#if defined(Z_FEATURE_UNSTABLE_API)

// Add attachment value
char buf_ind[16];
sprintf(buf_ind, "%d", idx);
Expand All @@ -153,7 +152,7 @@ int main(int argc, char **argv) {
}
ze_serializer_finish(z_move(serializer), &attachment);
options.attachment = z_move(attachment);
#endif

// Add encoding value
z_encoding_from_str(&encoding, "zenoh/string;utf8");
options.encoding = z_move(encoding);
Expand Down
7 changes: 3 additions & 4 deletions examples/unix/c11/z_queryable_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void query_handler(z_loaned_query_t *query, void *ctx) {
}
z_drop(z_move(payload_string));
z_drop(z_move(encoding));
#if defined(Z_FEATURE_UNSTABLE_API)

// Check attachment
const z_loaned_bytes_t *attachment = z_query_attachment(query);
if (attachment != NULL) {
Expand All @@ -83,15 +83,14 @@ void query_handler(z_loaned_query_t *query, void *ctx) {
drop_attachment(kvp, attachment_len);
free(kvp);
}
#endif

// Reply payload
z_owned_bytes_t reply_payload;
z_bytes_from_static_str(&reply_payload, value);

z_query_reply_options_t options;
z_query_reply_options_default(&options);
#if defined(Z_FEATURE_UNSTABLE_API)

// Reply attachment
z_owned_bytes_t reply_attachment;
kv_pair_t kvs[1];
Expand All @@ -105,7 +104,7 @@ void query_handler(z_loaned_query_t *query, void *ctx) {
ze_serializer_finish(z_move(serializer), &reply_attachment);
options.attachment = z_move(reply_attachment);
drop_attachment(kvs, 1);
#endif

// Reply encoding
z_owned_encoding_t reply_encoding;
z_encoding_from_str(&reply_encoding, "zenoh/string;utf8");
Expand Down
3 changes: 1 addition & 2 deletions examples/unix/c11/z_sub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void data_handler(z_loaned_sample_t *sample, void *ctx) {
printf(" with timestamp: %" PRIu64 "\n", z_timestamp_ntp64_time(ts));
}
// Check attachment
#if defined(Z_FEATURE_UNSTABLE_API)

const z_loaned_bytes_t *attachment = z_sample_attachment(sample);
if (attachment != NULL) {
ze_deserializer_t deserializer = ze_deserializer_from_bytes(attachment);
Expand All @@ -81,7 +81,6 @@ void data_handler(z_loaned_sample_t *sample, void *ctx) {
drop_attachment(kvp, attachment_len);
free(kvp);
}
#endif

z_drop(z_move(value));
z_drop(z_move(encoding));
Expand Down
75 changes: 8 additions & 67 deletions include/zenoh-pico/api/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
#ifndef __cplusplus

// clang-format off
#if defined (Z_FEATURE_UNSTABLE_API)
#define __ZP_ZE_LOAN ze_owned_serializer_t : ze_serializer_loan,
#else
#define __ZP_ZE_LOAN
#endif

/**
* Defines a generic function for loaning any of the ``z_owned_X_t`` types.
*
Expand Down Expand Up @@ -73,17 +69,10 @@
z_owned_closure_query_t : z_closure_query_loan, \
z_owned_closure_hello_t : z_closure_hello_loan, \
z_owned_closure_zid_t : z_closure_zid_loan, \
__ZP_ZE_LOAN \
ze_owned_serializer_t : ze_serializer_loan, \
z_owned_bytes_writer_t : z_bytes_writer_loan \
)(&x)


#if defined (Z_FEATURE_UNSTABLE_API)
#define __ZP_ZE_LOAN_MUT ze_owned_serializer_t : ze_serializer_loan_mut ,
#else
#define __ZP_ZE_LOAN_MUT
#endif
\
#define z_loan_mut(x) _Generic((x), \
z_owned_keyexpr_t : z_keyexpr_loan_mut, \
z_owned_config_t : z_config_loan_mut, \
Expand All @@ -104,17 +93,10 @@
z_owned_mutex_t : z_mutex_loan_mut, \
z_owned_condvar_t : z_condvar_loan_mut, \
z_owned_reply_err_t : z_reply_err_loan_mut, \
__ZP_ZE_LOAN_MUT \
ze_owned_serializer_t : ze_serializer_loan_mut, \
z_owned_bytes_writer_t : z_bytes_writer_loan_mut \
)(&x)


#if defined (Z_FEATURE_UNSTABLE_API)
#define __ZP_ZE_DROP ze_moved_serializer_t* : ze_serializer_drop,
#else
#define __ZP_ZE_DROP
#endif

/**
* Defines a generic function for dropping any of the ``z_owned_X_t`` types.
*
Expand Down Expand Up @@ -152,17 +134,10 @@
z_moved_ring_handler_reply_t* : z_ring_handler_reply_drop, \
z_moved_ring_handler_sample_t* : z_ring_handler_sample_drop, \
z_moved_reply_err_t* : z_reply_err_drop, \
__ZP_ZE_DROP \
ze_moved_serializer_t* : ze_serializer_drop, \
z_moved_bytes_writer_t* : z_bytes_writer_drop \
)(x)


#if defined (Z_FEATURE_UNSTABLE_API)
#define __ZP_ZE_CHECK ze_owned_serializer_t : ze_internal_serializer_check,
#else
#define __ZP_ZE_CHECK
#endif

/**
* Defines a generic function for checking the validity of any of the ``z_owned_X_t`` types.
*
Expand Down Expand Up @@ -195,7 +170,7 @@
z_owned_sample_t : z_internal_sample_check, \
z_owned_query_t : z_internal_query_check, \
z_owned_encoding_t : z_internal_encoding_check, \
__ZP_ZE_CHECK \
ze_owned_serializer_t : ze_internal_serializer_check, \
z_owned_bytes_writer_t : z_internal_bytes_writer_check \
)(&x)

Expand Down Expand Up @@ -233,12 +208,6 @@
const z_loaned_ring_handler_sample_t* : z_ring_handler_sample_recv \
)(x, __VA_ARGS__)

#if defined (Z_FEATURE_UNSTABLE_API)
#define __ZP_ZE_MOVE ze_owned_serializer_t : ze_serializer_move,
#else
#define __ZP_ZE_MOVE
#endif

/**
* Defines a generic function for moving any of the ``z_owned_X_t`` types.
*
Expand Down Expand Up @@ -279,16 +248,10 @@
z_owned_fifo_handler_reply_t : z_fifo_handler_reply_move, \
z_owned_fifo_handler_sample_t : z_fifo_handler_sample_move, \
z_owned_reply_err_t : z_reply_err_move, \
__ZP_ZE_MOVE \
ze_owned_serializer_t : ze_serializer_move, \
z_owned_bytes_writer_t : z_bytes_writer_move \
)(&x)

#if defined (Z_FEATURE_UNSTABLE_API)
#define __ZP_ZE_TAKE ze_owned_serializer_t *: ze_serializer_take,
#else
#define __ZP_ZE_TAKE
#endif

/**
* Defines a generic function for extracting the ``z_owned_X_t`` type from ``z_moved_X_t``
*
Expand Down Expand Up @@ -330,7 +293,7 @@
z_owned_string_array_t *: z_string_array_take, \
z_owned_string_t *: z_string_take, \
z_owned_subscriber_t *: z_subscriber_take, \
__ZP_ZE_TAKE \
ze_owned_serializer_t *: ze_serializer_take, \
z_owned_bytes_writer_t *: z_bytes_writer_take \
)(this_, x)

Expand Down Expand Up @@ -359,12 +322,6 @@
z_owned_config_t* : z_config_clone \
)(x, y)

#if defined (Z_FEATURE_UNSTABLE_API)
#define __ZP_ZE_NULL ze_owned_serializer_t * : ze_internal_serializer_null,
#else
#define __ZP_ZE_NULL
#endif

/**
* Defines a generic function for making null object of any of the ``z_owned_X_t`` types.
*
Expand Down Expand Up @@ -393,7 +350,7 @@
z_owned_sample_t * : z_internal_sample_null, \
z_owned_encoding_t * : z_internal_encoding_null, \
z_owned_reply_err_t * : z_internal_reply_err_null, \
__ZP_ZE_NULL \
ze_owned_serializer_t * : ze_internal_serializer_null, \
z_owned_bytes_writer_t * : z_internal_bytes_writer_null \
)(x)

Expand Down Expand Up @@ -458,9 +415,7 @@ inline const z_loaned_ring_handler_query_t* z_loan(const z_owned_ring_handler_qu
inline const z_loaned_ring_handler_reply_t* z_loan(const z_owned_ring_handler_reply_t& x) { return z_ring_handler_reply_loan(&x); };
inline const z_loaned_ring_handler_sample_t* z_loan(const z_owned_ring_handler_sample_t& x) { return z_ring_handler_sample_loan(&x); };
inline const z_loaned_bytes_writer_t* z_loan(const z_owned_bytes_writer_t& x) { return z_bytes_writer_loan(&x); };
#if defined(Z_FEATURE_UNSTABLE_API)
inline const ze_loaned_serializer_t* z_loan(const ze_owned_serializer_t& x) { return ze_serializer_loan(&x); };
#endif

// z_loan_mut definition
inline z_loaned_keyexpr_t* z_loan_mut(z_owned_keyexpr_t& x) { return z_keyexpr_loan_mut(&x); }
Expand All @@ -485,9 +440,7 @@ inline z_loaned_mutex_t* z_loan_mut(z_owned_mutex_t& x) { return z_mutex_loan_mu
inline z_loaned_condvar_t* z_loan_mut(z_owned_condvar_t& x) { return z_condvar_loan_mut(&x); }
inline z_loaned_reply_err_t* z_loan_mut(z_owned_reply_err_t& x) { return z_reply_err_loan_mut(&x); }
inline z_loaned_bytes_writer_t* z_loan_mut(z_owned_bytes_writer_t& x) { return z_bytes_writer_loan_mut(&x); };
#if defined(Z_FEATURE_UNSTABLE_API)
inline ze_loaned_serializer_t* z_loan_mut(ze_owned_serializer_t& x) { return ze_serializer_loan_mut(&x); };
#endif

// z_drop definition
inline void z_drop(z_moved_session_t* v) { z_session_drop(v); }
Expand Down Expand Up @@ -520,9 +473,7 @@ inline void z_drop(z_moved_fifo_handler_query_t* v) { z_fifo_handler_query_drop(
inline void z_drop(z_moved_ring_handler_reply_t* v) { z_ring_handler_reply_drop(v); }
inline void z_drop(z_moved_fifo_handler_reply_t* v) { z_fifo_handler_reply_drop(v); }
inline void z_drop(z_moved_bytes_writer_t* v) { z_bytes_writer_drop(v); }
#if defined(Z_FEATURE_UNSTABLE_API)
inline void z_drop(ze_moved_serializer_t* v) { ze_serializer_drop(v); }
#endif

// z_internal_null definition
inline void z_internal_null(z_owned_session_t* v) { z_internal_session_null(v); }
Expand Down Expand Up @@ -551,9 +502,7 @@ inline void z_internal_null(z_owned_fifo_handler_query_t* v) { return z_internal
inline void z_internal_null(z_owned_fifo_handler_reply_t* v) { return z_internal_fifo_handler_reply_null(v); };
inline void z_internal_null(z_owned_fifo_handler_sample_t* v) { return z_internal_fifo_handler_sample_null(v); };
inline void z_internal_null(z_owned_bytes_writer_t* v) { return z_internal_bytes_writer_null(v); };
#if defined(Z_FEATURE_UNSTABLE_API)
inline void z_internal_null(ze_owned_serializer_t* v) { return ze_internal_serializer_null(v); };
#endif

// z_internal_check definition
inline bool z_internal_check(const z_owned_session_t& v) { return z_internal_session_check(&v); }
Expand All @@ -577,9 +526,7 @@ inline bool z_internal_check(const z_owned_ring_handler_query_t& v) { return z_i
inline bool z_internal_check(const z_owned_ring_handler_reply_t& v) { return z_internal_ring_handler_reply_check(&v); };
inline bool z_internal_check(const z_owned_ring_handler_sample_t& v) { return z_internal_ring_handler_sample_check(&v); };
inline bool z_internal_check(const z_owned_bytes_writer_t& v) { return z_internal_bytes_writer_check(&v); };
#if defined(Z_FEATURE_UNSTABLE_API)
inline bool z_internal_check(const ze_owned_serializer_t& v) { return ze_internal_serializer_check(&v); };
#endif

// z_call definition
inline void z_call(const z_loaned_closure_sample_t &closure, z_loaned_sample_t *sample)
Expand Down Expand Up @@ -712,9 +659,7 @@ inline z_moved_ring_handler_sample_t* z_move(z_owned_ring_handler_sample_t& x) {
return z_ring_handler_sample_move(&x);
};
inline z_moved_bytes_writer_t* z_move(z_owned_bytes_writer_t& x) { return z_bytes_writer_move(&x); };
#if defined(Z_FEATURE_UNSTABLE_API)
inline ze_moved_serializer_t* z_move(ze_owned_serializer_t& x) { return ze_serializer_move(&x); };
#endif

// z_take definition
inline void z_take(z_owned_session_t* this_, z_moved_session_t* v) { return z_session_take(this_, v); }
Expand Down Expand Up @@ -759,9 +704,7 @@ inline void z_take(z_owned_fifo_handler_reply_t* this_, z_moved_fifo_handler_rep
z_fifo_handler_reply_take(this_, v);
}
inline void z_take(z_owned_bytes_writer_t* this_, z_moved_bytes_writer_t* v) { z_bytes_writer_take(this_, v); }
#if defined(Z_FEATURE_UNSTABLE_API)
inline void z_take(ze_owned_serializer_t* this_, ze_moved_serializer_t* v) { ze_serializer_take(this_, v); }
#endif

// z_clone definition
inline z_result_t z_clone(z_owned_bytes_t* dst, z_loaned_bytes_t* this_) { return z_bytes_clone(dst, this_); };
Expand Down Expand Up @@ -1010,7 +953,6 @@ template <>
struct z_owned_to_loaned_type_t<z_owned_bytes_writer_t> {
typedef z_loaned_bytes_writer_t type;
};
#if defined(Z_FEATURE_UNSTABLE_API)
template <>
struct z_loaned_to_owned_type_t<ze_loaned_serializer_t> {
typedef ze_owned_serializer_t type;
Expand All @@ -1019,7 +961,6 @@ template <>
struct z_owned_to_loaned_type_t<ze_owned_serializer_t> {
typedef ze_loaned_serializer_t type;
};
#endif

#endif

Expand Down
Loading

0 comments on commit 638f011

Please sign in to comment.