Skip to content

Commit

Permalink
Merge pull request #2 from jean-roland/ft_reply_rc
Browse files Browse the repository at this point in the history
Switch query callback replies to rc
  • Loading branch information
jean-roland authored May 21, 2024
2 parents eb95230 + 9315dba commit 1fcd37f
Show file tree
Hide file tree
Showing 24 changed files with 244 additions and 152 deletions.
2 changes: 1 addition & 1 deletion examples/arduino/z_get.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void reply_dropper(void *ctx) {
Serial.println(" >> Received query final notification");
}

void reply_handler(z_owned_reply_t *oreply, void *ctx) {
void reply_handler(const z_loaned_reply_t *oreply, void *ctx) {
(void)(ctx);
if (z_reply_is_ok(oreply)) {
const z_loaned_sample_t *sample = z_reply_ok(oreply);
Expand Down
2 changes: 1 addition & 1 deletion examples/espidf/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void wifi_init_sta(void) {

void reply_dropper(void *ctx) { printf(" >> Received query final notification\n"); }

void reply_handler(z_owned_reply_t *oreply, void *ctx) {
void reply_handler(const z_loaned_reply_t *oreply, void *ctx) {
if (z_reply_is_ok(oreply)) {
const z_loaned_sample_t *sample = z_reply_ok(oreply);
z_owned_str_t keystr;
Expand Down
2 changes: 1 addition & 1 deletion examples/freertos_plus_tcp/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void reply_dropper(void *ctx) {
printf(">> Received query final notification\n");
}

void reply_handler(z_owned_reply_t *reply, void *ctx) {
void reply_handler(const z_loaned_reply_t *reply, void *ctx) {
(void)(ctx);
if (z_reply_is_ok(reply)) {
const z_loaned_sample_t *sample = z_reply_ok(reply);
Expand Down
2 changes: 1 addition & 1 deletion examples/mbed/z_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

void reply_dropper(void *ctx) { printf(" >> Received query final notification\n"); }

void reply_handler(z_owned_reply_t *oreply, void *ctx) {
void reply_handler(const z_loaned_reply_t *oreply, void *ctx) {
if (z_reply_is_ok(oreply)) {
const z_loaned_sample_t *sample = z_reply_ok(oreply);
z_owned_str_t keystr;
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c11/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int8_t attachment_handler(z_bytes_t key, z_bytes_t att_value, void *ctx) {
}
#endif

void reply_handler(z_owned_reply_t *reply, void *ctx) {
void reply_handler(const z_loaned_reply_t *reply, void *ctx) {
(void)(ctx);
if (z_reply_is_ok(reply)) {
const z_loaned_sample_t *sample = z_reply_ok(reply);
Expand Down
4 changes: 2 additions & 2 deletions examples/unix/c11/z_get_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ int main(int argc, char **argv) {
z_owned_reply_t reply;
z_null(&reply);
for (z_call(channel.recv, &reply); z_check(reply); z_call(channel.recv, &reply)) {
if (z_reply_is_ok(&reply)) {
const z_loaned_sample_t *sample = z_reply_ok(&reply);
if (z_reply_is_ok(z_loan(reply))) {
const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply));
z_owned_str_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len,
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c99/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void reply_dropper(void *ctx) {
z_condvar_free(&cond);
}

void reply_handler(z_owned_reply_t *reply, void *ctx) {
void reply_handler(const z_loaned_reply_t *reply, void *ctx) {
(void)(ctx);
if (z_reply_is_ok(reply)) {
const z_loaned_sample_t *sample = z_reply_ok(reply);
Expand Down
2 changes: 1 addition & 1 deletion examples/windows/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void reply_dropper(void *ctx) {
z_condvar_free(&cond);
}

void reply_handler(z_owned_reply_t *reply, void *ctx) {
void reply_handler(const z_loaned_reply_t *reply, void *ctx) {
(void)(ctx);
if (z_reply_is_ok(reply)) {
const z_loaned_sample_t *sample = z_reply_ok(reply);
Expand Down
2 changes: 1 addition & 1 deletion examples/zephyr/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

void reply_dropper(void *ctx) { printf(" >> Received query final notification\n"); }

void reply_handler(z_owned_reply_t *oreply, void *ctx) {
void reply_handler(const z_loaned_reply_t *oreply, void *ctx) {
if (z_reply_is_ok(oreply)) {
const z_loaned_sample_t *sample = z_reply_ok(oreply);
z_owned_str_t keystr;
Expand Down
14 changes: 7 additions & 7 deletions include/zenoh-pico/api/handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ z_owned_query_t *_z_query_to_owned_ptr(const z_loaned_query_t *src);

// -- Reply handler
void _z_owned_reply_move(z_owned_reply_t *dst, z_owned_reply_t *src);
z_owned_reply_t *_z_reply_clone(const z_owned_reply_t *src);
z_owned_reply_t *_z_reply_to_owned_ptr(const z_loaned_reply_t *src);

// -- Channel
#define _Z_CHANNEL_DEFINE(name, send_closure_name, recv_closure_name, send_type, recv_type, collection_type, \
Expand Down Expand Up @@ -112,13 +112,13 @@ _Z_CHANNEL_DEFINE(query_fifo_channel, closure_query, closure_owned_query, const
_z_owned_query_move, _z_query_to_owned_ptr, z_query_drop)

// z_owned_reply_ring_channel_t
_Z_CHANNEL_DEFINE(reply_ring_channel, closure_reply, closure_reply, z_owned_reply_t, z_owned_reply_t, _z_ring_mt_t,
_z_ring_mt_new, _z_ring_mt_free, _z_ring_mt_push, _z_ring_mt_pull, _z_ring_mt_try_pull,
_z_owned_reply_move, _z_reply_clone, z_reply_drop)
_Z_CHANNEL_DEFINE(reply_ring_channel, closure_reply, closure_owned_reply, const z_loaned_reply_t, z_owned_reply_t,
_z_ring_mt_t, _z_ring_mt_new, _z_ring_mt_free, _z_ring_mt_push, _z_ring_mt_pull, _z_ring_mt_try_pull,
_z_owned_reply_move, _z_reply_to_owned_ptr, z_reply_drop)

// z_owned_reply_fifo_channel_t
_Z_CHANNEL_DEFINE(reply_fifo_channel, closure_reply, closure_reply, z_owned_reply_t, z_owned_reply_t, _z_fifo_mt_t,
_z_fifo_mt_new, _z_fifo_mt_free, _z_fifo_mt_push, _z_fifo_mt_pull, _z_fifo_mt_try_pull,
_z_owned_reply_move, _z_reply_clone, z_reply_drop)
_Z_CHANNEL_DEFINE(reply_fifo_channel, closure_reply, closure_owned_reply, const z_loaned_reply_t, z_owned_reply_t,
_z_fifo_mt_t, _z_fifo_mt_new, _z_fifo_mt_free, _z_fifo_mt_push, _z_fifo_mt_pull, _z_fifo_mt_try_pull,
_z_owned_reply_move, _z_reply_to_owned_ptr, z_reply_drop)

#endif // INCLUDE_ZENOH_PICO_API_HANDLERS_H
17 changes: 10 additions & 7 deletions include/zenoh-pico/api/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
z_owned_hello_t : z_hello_check, \
z_owned_str_t : z_str_check, \
z_owned_str_array_t : z_str_array_check, \
z_owned_bytes_t : z_bytes_check, \
z_owned_bytes_t : z_bytes_check, \
z_owned_sample_t : z_sample_check, \
z_owned_query_t : z_query_check \
)(&x)
Expand All @@ -138,7 +138,8 @@
z_owned_closure_hello_t : z_closure_hello_call, \
z_owned_closure_zid_t : z_closure_zid_call, \
z_owned_closure_owned_sample_t : z_closure_owned_sample_call, \
z_owned_closure_owned_query_t : z_closure_owned_query_call \
z_owned_closure_owned_query_t : z_closure_owned_query_call, \
z_owned_closure_owned_reply_t : z_closure_owned_reply_call \
) (&x, __VA_ARGS__)

/**
Expand Down Expand Up @@ -350,16 +351,18 @@ inline bool z_check(const z_owned_hello_t& v) { return z_hello_check(&v); }
inline bool z_check(const z_owned_str_t& v) { return z_str_check(&v); }
inline bool z_check(const z_owned_str_t& v) { return z_sample_check(&v); }

inline void z_call(const z_owned_closure_sample_t &closure, const z_sample_t *sample)
inline void z_call(const z_owned_closure_sample_t &closure, const z_loaned_sample_t *sample)
{ z_closure_sample_call(&closure, sample); }
inline void z_call(const z_owned_closure_owned_sample_t &closure, const z_sample_t *sample)
inline void z_call(const z_owned_closure_owned_sample_t &closure, const z_owned_sample_t *sample)
{ z_closure_owned_sample_call(&closure, sample); }
inline void z_call(const z_owned_closure_query_t &closure, const z_query_t *query)
inline void z_call(const z_owned_closure_query_t &closure, const z_loaned_query_t *query)
{ z_closure_query_call(&closure, query); }
inline void z_call(const z_owned_closure_owned_query_t &closure, const z_query_t *query)
inline void z_call(const z_owned_closure_owned_query_t &closure, const z_owned_query_t *query)
{ z_closure_owned_query_call(&closure, query); }
inline void z_call(const z_owned_closure_reply_t &closure, z_owned_reply_t *reply)
inline void z_call(const z_owned_closure_reply_t &closure, const z_loaned_reply_t *reply)
{ z_closure_reply_call(&closure, reply); }
inline void z_call(const z_owned_closure_owned_reply_t &closure, z_owned_reply_t *reply)
{ z_closure_owned_reply_call(&closure, reply); }
inline void z_call(const z_owned_closure_hello_t &closure, z_owned_hello_t *hello)
{ z_closure_hello_call(&closure, hello); }
inline void z_call(const z_owned_closure_zid_t &closure, const z_id_t *zid)
Expand Down
53 changes: 41 additions & 12 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ const z_loaned_keyexpr_t *z_query_keyexpr(const z_loaned_query_t *query);
* Returns:
* Returns a new sample closure.
*/
int8_t z_closure_sample(z_owned_closure_sample_t *closure, _z_data_handler_t call, _z_dropper_handler_t drop,
int8_t z_closure_sample(z_owned_closure_sample_t *closure, z_data_handler_t call, z_dropper_handler_t drop,
void *context);

/**
Expand All @@ -608,8 +608,8 @@ int8_t z_closure_sample(z_owned_closure_sample_t *closure, _z_data_handler_t cal
* Returns:
* Returns a new sample closure.
*/
int8_t z_closure_owned_sample(z_owned_closure_owned_sample_t *closure, _z_owned_sample_handler_t call,
_z_dropper_handler_t drop, void *context);
int8_t z_closure_owned_sample(z_owned_closure_owned_sample_t *closure, z_owned_sample_handler_t call,
z_dropper_handler_t drop, void *context);

/**
* Return a new query closure.
Expand All @@ -636,7 +636,7 @@ int8_t z_closure_owned_sample(z_owned_closure_owned_sample_t *closure, _z_owned_
* Returns:
* Returns a new query closure.
*/
int8_t z_closure_query(z_owned_closure_query_t *closure, _z_queryable_handler_t call, _z_dropper_handler_t drop,
int8_t z_closure_query(z_owned_closure_query_t *closure, z_queryable_handler_t call, z_dropper_handler_t drop,
void *context);

/**
Expand All @@ -660,8 +660,8 @@ int8_t z_closure_query(z_owned_closure_query_t *closure, _z_queryable_handler_t
* Returns:
* Returns a new query closure.
*/
int8_t z_closure_owned_query(z_owned_closure_owned_query_t *closure, _z_owned_query_handler_t call,
_z_dropper_handler_t drop, void *context);
int8_t z_closure_owned_query(z_owned_closure_owned_query_t *closure, z_owned_query_handler_t call,
z_dropper_handler_t drop, void *context);

/**
* Return a new reply closure.
Expand All @@ -688,9 +688,37 @@ int8_t z_closure_owned_query(z_owned_closure_owned_query_t *closure, _z_owned_qu
* Returns:
* Returns a new reply closure.
*/
int8_t z_closure_reply(z_owned_closure_reply_t *closure, z_owned_reply_handler_t call, _z_dropper_handler_t drop,
int8_t z_closure_reply(z_owned_closure_reply_t *closure, z_reply_handler_t call, z_dropper_handler_t drop,
void *context);

/**
* Return a new reply closure.
* It consists on a structure that contains all the elements for stateful, memory-leak-free callbacks.
*
* Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_closure_reply_t` by loaning it using
* ``z_closure_reply_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, is
* equivalent to writing ``z_closure_reply_loan(&val)``.
*
* Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said
* instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code,
* consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but
* will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val``
* is valid.
*
* To check if ``val`` is still valid, you may use ``z_closure_reply_check(&val)`` or ``z_check(val)`` if your compiler
* supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise.
*
* Parameters:
* call: the typical callback function. ``context`` will be passed as its last argument.
* drop: allows the callback's state to be freed. ``context`` will be passed as its last argument.
* context: a pointer to an arbitrary state.
*
* Returns:
* Returns a new reply closure.
*/
int8_t z_closure_owned_reply(z_owned_closure_owned_reply_t *closure, z_owned_reply_handler_t call,
z_dropper_handler_t drop, void *context);

/**
* Return a new hello closure.
* It consists on a structure that contains all the elements for stateful, memory-leak-free callbacks.
Expand All @@ -716,7 +744,7 @@ int8_t z_closure_reply(z_owned_closure_reply_t *closure, z_owned_reply_handler_t
* Returns:
* Returns a new hello closure.
*/
int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_owned_hello_handler_t call, _z_dropper_handler_t drop,
int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_owned_hello_handler_t call, z_dropper_handler_t drop,
void *context);

/**
Expand Down Expand Up @@ -744,7 +772,7 @@ int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_owned_hello_handler_t
* Returns:
* Returns a new zid closure.
*/
int8_t z_closure_zid(z_owned_closure_zid_t *closure, z_id_handler_t call, _z_dropper_handler_t drop, void *context);
int8_t z_closure_zid(z_owned_closure_zid_t *closure, z_id_handler_t call, z_dropper_handler_t drop, void *context);

/**************** Loans ****************/
#define _OWNED_FUNCTIONS(loanedtype, ownedtype, name) \
Expand Down Expand Up @@ -782,6 +810,7 @@ _OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_sample_t, closure_owned_sample)
_OWNED_FUNCTIONS_CLOSURE(z_owned_closure_query_t, closure_query)
_OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_query_t, closure_owned_query)
_OWNED_FUNCTIONS_CLOSURE(z_owned_closure_reply_t, closure_reply)
_OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_reply_t, closure_owned_reply)
_OWNED_FUNCTIONS_CLOSURE(z_owned_closure_hello_t, closure_hello)
_OWNED_FUNCTIONS_CLOSURE(z_owned_closure_zid_t, closure_zid)

Expand Down Expand Up @@ -1159,7 +1188,7 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co
* Returns ``true`` if the queryable answered with an OK, which allows this value to be treated as a sample, or
* ``false`` otherwise.
*/
_Bool z_reply_is_ok(const z_owned_reply_t *reply);
_Bool z_reply_is_ok(const z_loaned_reply_t *reply);

/**
* Yields the contents of the reply by asserting it indicates a success.
Expand All @@ -1172,7 +1201,7 @@ _Bool z_reply_is_ok(const z_owned_reply_t *reply);
* Returns:
* Returns the :c:type:`z_loaned_sample_t` wrapped in the query reply.
*/
const z_loaned_sample_t *z_reply_ok(const z_owned_reply_t *reply);
const z_loaned_sample_t *z_reply_ok(const z_loaned_reply_t *reply);

/**
* Yields the contents of the reply by asserting it indicates a failure.
Expand All @@ -1185,7 +1214,7 @@ const z_loaned_sample_t *z_reply_ok(const z_owned_reply_t *reply);
* Returns:
* Returns the :c:type:`z_value_t` wrapped in the query reply.
*/
z_value_t z_reply_err(const z_owned_reply_t *reply);
z_value_t z_reply_err(const z_loaned_reply_t *reply);
#endif

#if Z_FEATURE_QUERYABLE == 1
Expand Down
Loading

0 comments on commit 1fcd37f

Please sign in to comment.