Skip to content

Commit

Permalink
implement loaned take
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Oct 29, 2024
1 parent 9cc5d70 commit 6cd1de4
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 55 deletions.
19 changes: 16 additions & 3 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ See details at :ref:`owned_types_concept`
.. c:function:: z_loaned_slice_t * z_view_slice_loan_mut(z_view_slice_t * slice)
.. c:function:: const z_loaned_slice_t * z_slice_loan(const z_owned_slice_t * slice)
.. c:function:: z_loaned_slice_t * z_slice_loan_mut(z_owned_slice_t * slice)
.. c:function:: z_result_t z_slice_loaned_take(z_owned_slice_t *dst, z_loaned_slice_t *src)
String
------
Expand Down Expand Up @@ -108,7 +108,7 @@ See details at :ref:`owned_types_concept`
.. c:function:: z_loaned_string_t * z_view_string_loan_mut(z_view_string_t * string)
.. c:function:: const z_loaned_string_t * z_string_loan(const z_owned_string_t * string)
.. c:function:: z_loaned_string_t * z_string_loan_mut(z_owned_string_t * string)
.. c:function:: z_result_t z_string_loaned_take(z_owned_string_t *dst, z_loaned_string_t *src)
String Array
------------
Expand Down Expand Up @@ -142,6 +142,7 @@ See details at :ref:`owned_types_concept`
.. c:function:: void z_string_array_clone(z_owned_string_array_t * dst, const z_loaned_string_array_t * string_array)
.. c:function:: const z_loaned_string_array_t * z_string_array_loan(const z_owned_string_array_t * string_array)
.. c:function:: z_loaned_string_array_t * z_string_array_loan_mut(z_owned_string_array_t * string_array)
.. c:function:: z_result_t z_string_array_loaned_take(z_owned_string_array_t *dst, z_loaned_string_array_t *src)
Common
Expand Down Expand Up @@ -205,6 +206,7 @@ See details at :ref:`owned_types_concept`
.. c:function:: z_loaned_keyexpr_t * z_view_keyexpr_loan_mut(z_view_keyexpr_t * keyexpr)
.. c:function:: const z_loaned_keyexpr_t * z_keyexpr_loan(const z_owned_keyexpr_t * keyexpr)
.. c:function:: z_loaned_keyexpr_t * z_keyexpr_loan_mut(z_owned_keyexpr_t * keyexpr)
.. c:function:: z_result_t z_keyexpr_loaned_take(z_owned_keyexpr_t *dst, z_loaned_keyexpr_t *src)
Payload
-------
Expand Down Expand Up @@ -261,6 +263,7 @@ See details at :ref:`owned_types_concept`
.. c:function:: void z_bytes_clone(z_owned_bytes_t * dst, const z_loaned_bytes_t * bytes)
.. c:function:: const z_loaned_bytes_t * z_bytes_loan(const z_owned_bytes_t * bytes)
.. c:function:: z_loaned_bytes_t * z_bytes_loan_mut(z_owned_bytes_t * bytes)
.. c:function:: z_result_t z_bytes_loaned_take(z_owned_bytes_t *dst, z_loaned_bytes_t *src)
.. c:function:: void z_bytes_writer_drop(z_moved_bytes_writer_t * bytes_writer)
.. c:function:: void z_bytes_writer_clone(z_owned_bytes_writer_t * dst, const z_loaned_bytes_writer_t * bytes_writer)
Expand Down Expand Up @@ -300,6 +303,7 @@ See details at :ref:`owned_types_concept`
.. c:function:: void z_encoding_clone(z_owned_encoding_t * dst, const z_loaned_encoding_t * encoding)
.. c:function:: const z_loaned_encoding_t * z_encoding_loan(const z_owned_encoding_t * encoding)
.. c:function:: z_loaned_encoding_t * z_encoding_loan_mut(z_owned_encoding_t * encoding)
.. c:function:: z_result_t z_encoding_loaned_take(z_owned_encoding_t *dst, z_loaned_encoding_t *src)
Predefined Encodings
Expand Down Expand Up @@ -385,7 +389,8 @@ See details at :ref:`owned_types_concept`
.. c:function:: void z_reply_err_drop(z_moved_reply_err_t * reply_err)
.. c:function:: void z_reply_err_clone(z_owned_reply_err_t * dst, const z_loaned_reply_err_t * reply_err)
.. c:function:: const z_loaned_reply_err_t * z_reply_err_loan(const z_owned_reply_err_t * reply_err)
.. c:function:: z_loaned_reply_err_t * z_reply_err_loan_mut(z_owned_reply_err_t * reply_err)
.. c:function:: z_result_t z_reply_err_loaned_take(z_owned_reply_err_t *dst, z_loaned_reply_err_t *src)
Sample
------
Expand Down Expand Up @@ -424,6 +429,8 @@ See details at :ref:`owned_types_concept`
.. c:function:: void z_sample_drop(z_moved_sample_t * sample)
.. c:function:: void z_sample_clone(z_owned_sample_t * dst, const z_loaned_sample_t * sample)
.. c:function:: const z_loaned_sample_t * z_sample_loan(const z_owned_sample_t * sample)
.. c:function:: z_loaned_sample_t * z_sample_loan_mut(z_owned_sample_t * sample)
.. c:function:: z_result_t z_sample_loaned_take(z_owned_sample_t *dst, z_loaned_sample_t *src)
Timestamp
Expand Down Expand Up @@ -1103,6 +1110,8 @@ See details at :ref:`owned_types_concept`
.. c:function:: void z_query_drop(z_moved_query_t * query)
.. c:function:: void z_query_clone(z_owned_query_t * dst, const z_loaned_query_t * query)
.. c:function:: const z_loaned_query_t * z_query_loan(const z_owned_query_t * query)
.. c:function:: z_loaned_query_t * z_query_loan_mut(z_owned_query_t * query)
.. c:function:: z_result_t z_query_loaned_take(z_owned_query_t *dst, z_loaned_query_t *src)
Query
=====
Expand Down Expand Up @@ -1149,6 +1158,8 @@ See details at :ref:`owned_types_concept`
.. c:function:: void z_reply_drop(z_moved_reply_t * reply)
.. c:function:: void z_reply_clone(z_owned_reply_t * dst, const z_loaned_reply_t * reply)
.. c:function:: const z_loaned_reply_t * z_reply_loan(const z_owned_reply_t * reply)
.. c:function:: z_loaned_reply_t * z_reply_loan_mut(z_owned_reply_t * reply)
.. c:function:: z_result_t z_reply_loaned_take(z_owned_reply_t *dst, z_loaned_reply_t *src)
Scouting
Expand Down Expand Up @@ -1187,6 +1198,8 @@ See details at :ref:`owned_types_concept`
.. c:function:: void z_hello_drop(z_moved_hello_t * hello)
.. c:function:: void z_hello_clone(z_owned_hello_t * dst, const z_loaned_hello_t * hello)
.. c:function:: const z_loaned_hello_t * z_hello_loan(const z_owned_hello_t * hello)
.. c:function:: z_loaned_hello_t * z_hello_loan(z_owned_hello_t * hello)
.. c:function:: z_result_t z_hello_loaned_take(z_owned_hello_t *dst, z_loaned_hello_t *src)
Serialization
Expand Down
93 changes: 80 additions & 13 deletions include/zenoh-pico/api/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,32 @@
z_owned_slice_t* : z_slice_clone, \
z_owned_string_array_t* : z_string_array_clone, \
z_owned_config_t* : z_config_clone \
)(x, y)
)(dst, src)

/**
* Defines a generic function for moving of the ``z_owned_X_t`` types.
*
* Parameters:
* dst: The take destination.
* src: The instance to take contents from.
*
* Returns:
* `0` in case of success, negative error code otherwise.
*/
#define z_loaned_take(dst, src) _Generic((dst), \
z_owned_keyexpr_t* : z_keyexpr_loaned_take, \
z_owned_query_t* : z_query_loaned_take, \
z_owned_sample_t* : z_sample_loaned_take, \
z_owned_bytes_t* : z_bytes_loaned_take, \
z_owned_encoding_t* : z_encoding_loaned_take, \
z_owned_reply_err_t* : z_reply_err_loaned_take, \
z_owned_reply_t* : z_reply_loaned_take, \
z_owned_hello_t* : z_hello_loaned_take, \
z_owned_string_t* : z_string_loaned_take, \
z_owned_slice_t* : z_slice_loaned_take, \
z_owned_string_array_t* : z_string_array_loaned_take, \
z_owned_config_t* : z_config_loaned_take \
)(dst, src)

/**
* Defines a generic function for making null object of any of the ``z_owned_X_t`` types.
Expand Down Expand Up @@ -709,22 +734,64 @@ inline void z_take(z_owned_bytes_writer_t* this_, z_moved_bytes_writer_t* v) { z
inline void z_take(ze_owned_serializer_t* this_, ze_moved_serializer_t* v) { ze_serializer_take(this_, v); }

// 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_); }
inline z_result_t z_clone(z_owned_config_t* dst, z_loaned_config_t* this_) { return z_config_clone(dst, this_); }
inline z_result_t z_clone(z_owned_encoding_t* dst, z_loaned_encoding_t* this_) { return z_encoding_clone(dst, this_); }
inline z_result_t z_clone(z_owned_keyexpr_t* dst, z_loaned_keyexpr_t* this_) { return z_keyexpr_clone(dst, this_); }
inline z_result_t z_clone(z_owned_query_t* dst, z_loaned_query_t* this_) { return z_query_clone(dst, this_); }
inline z_result_t z_clone(z_owned_reply_t* dst, z_loaned_reply_t* this_) { return z_reply_clone(dst, this_); }
inline z_result_t z_clone(z_owned_reply_err_t* dst, z_loaned_reply_err_t* this_) {
inline z_result_t z_clone(z_owned_bytes_t* dst, const z_loaned_bytes_t* this_) { return z_bytes_clone(dst, this_); }
inline z_result_t z_clone(z_owned_config_t* dst, const z_loaned_config_t* this_) { return z_config_clone(dst, this_); }
inline z_result_t z_clone(z_owned_encoding_t* dst, const const z_loaned_encoding_t* this_) {
return z_encoding_clone(dst, this_);
}
inline z_result_t z_clone(z_owned_keyexpr_t* dst, const z_loaned_keyexpr_t* this_) {
return z_keyexpr_clone(dst, this_);
}
inline z_result_t z_clone(z_owned_query_t* dst, const z_loaned_query_t* this_) { return z_query_clone(dst, this_); }
inline z_result_t z_clone(z_owned_reply_t* dst, const z_loaned_reply_t* this_) { return z_reply_clone(dst, this_); }
inline z_result_t z_clone(z_owned_reply_err_t* dst, const z_loaned_reply_err_t* this_) {
return z_reply_err_clone(dst, this_);
}
inline z_result_t z_clone(z_owned_sample_t* dst, z_loaned_sample_t* this_) { return z_sample_clone(dst, this_); }
inline z_result_t z_clone(z_owned_slice_t* dst, z_loaned_slice_t* this_) { return z_slice_clone(dst, this_); }
inline z_result_t z_clone(z_owned_string_t* dst, z_loaned_string_t* this_) { return z_string_clone(dst, this_); }
inline z_result_t z_clone(z_owned_string_array_t* dst, z_loaned_string_array_t* this_) {
inline z_result_t z_clone(z_owned_sample_t* dst, const z_loaned_sample_t* this_) { return z_sample_clone(dst, this_); }
inline z_result_t z_clone(z_owned_slice_t* dst, const z_loaned_slice_t* this_) { return z_slice_clone(dst, this_); }
inline z_result_t z_clone(z_owned_string_t* dst, const z_loaned_string_t* this_) { return z_string_clone(dst, this_); }
inline z_result_t z_clone(z_owned_string_array_t* dst, const z_loaned_string_array_t* this_) {
return z_string_array_clone(dst, this_);
}
inline z_result_t z_clone(z_owned_hello_t* dst, z_loaned_hello_t* this_) { return z_hello_clone(dst, this_); }
inline z_result_t z_clone(z_owned_hello_t* dst, const z_loaned_hello_t* this_) { return z_hello_clone(dst, this_); }

// z_loaned_take definition
inline z_result_t z_loaned_take(z_owned_bytes_t* dst, z_loaned_bytes_t* this_) {
return z_bytes_loaned_take(dst, this_);
}
inline z_result_t z_loaned_take(z_owned_config_t* dst, z_loaned_config_t* this_) {
return z_config_loaned_take(dst, this_);
}
inline z_result_t z_loaned_take(z_owned_encoding_t* dst, z_loaned_encoding_t* this_) {
return z_encoding_loaned_take(dst, this_);
}
inline z_result_t z_loaned_take(z_owned_keyexpr_t* dst, z_loaned_keyexpr_t* this_) {
return z_keyexpr_loaned_take(dst, this_);
}
inline z_result_t z_loaned_take(z_owned_query_t* dst, z_loaned_query_t* this_) {
return z_query_loaned_take(dst, this_);
}
inline z_result_t z_loaned_take(z_owned_reply_t* dst, z_loaned_reply_t* this_) {
return z_reply_loaned_take(dst, this_);
}
inline z_result_t z_loaned_take(z_owned_reply_err_t* dst, z_loaned_reply_err_t* this_) {
return z_reply_err_loaned_take(dst, this_);
}
inline z_result_t z_loaned_take(z_owned_sample_t* dst, z_loaned_sample_t* this_) {
return z_sample_loaned_take(dst, this_);
}
inline z_result_t z_loaned_take(z_owned_slice_t* dst, z_loaned_slice_t* this_) {
return z_slice_loaned_take(dst, this_);
}
inline z_result_t z_loaned_take(z_owned_string_t* dst, z_loaned_string_t* this_) {
return z_string_loaned_take(dst, this_);
}
inline z_result_t z_loaned_take(z_owned_string_array_t* dst, z_loaned_string_array_t* this_) {
return z_string_array_loaned_take(dst, this_);
}
inline z_result_t z_loaned_take(z_owned_hello_t* dst, z_loaned_hello_t* this_) {
return z_hello_loaned_take(dst, this_);
}

template <class T>
struct z_loaned_to_owned_type_t {};
Expand Down
31 changes: 19 additions & 12 deletions include/zenoh-pico/api/olv_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@
void prefix##_##name##_take(prefix##_owned_##name##_t *obj, prefix##_moved_##name##_t *src); \
void prefix##_##name##_drop(prefix##_moved_##name##_t *obj);

#define _Z_OWNED_FUNCTIONS_DEF_PREFIX(prefix, name) \
_Z_OWNED_FUNCTIONS_NO_COPY_DEF_PREFIX(prefix, name) \
z_result_t prefix##_##name##_clone(prefix##_owned_##name##_t *obj, const prefix##_loaned_##name##_t *src);
#define _Z_OWNED_FUNCTIONS_DEF_PREFIX(prefix, name) \
_Z_OWNED_FUNCTIONS_NO_COPY_DEF_PREFIX(prefix, name) \
z_result_t prefix##_##name##_clone(prefix##_owned_##name##_t *obj, const prefix##_loaned_##name##_t *src); \
z_result_t prefix##_##name##_take_loaned(prefix##_owned_##name##_t *dst, prefix##_loaned_##name##_t *src);

#define _Z_OWNED_FUNCTIONS_NO_COPY_DEF(name) _Z_OWNED_FUNCTIONS_NO_COPY_DEF_PREFIX(z, name)
#define _Z_OWNED_FUNCTIONS_DEF(name) _Z_OWNED_FUNCTIONS_DEF_PREFIX(z, name)
Expand Down Expand Up @@ -117,24 +118,30 @@
if (obj != NULL) f_drop((&obj->_this._val)); \
}

#define _Z_OWNED_FUNCTIONS_VALUE_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_copy, f_drop, attribute) \
_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_drop, attribute) \
attribute prefix##_result_t prefix##_##name##_clone(prefix##_owned_##name##_t *obj, \
const prefix##_loaned_##name##_t *src) { \
return f_copy((&obj->_val), src); \
#define _Z_OWNED_FUNCTIONS_VALUE_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_copy, f_move, f_drop, \
attribute) \
_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_drop, attribute) \
attribute z_result_t prefix##_##name##_clone(prefix##_owned_##name##_t *obj, \
const prefix##_loaned_##name##_t *src) { \
return f_copy((&obj->_val), src); \
} \
attribute z_result_t prefix##_##name##_take_loaned(prefix##_owned_##name##_t *obj, \
prefix##_loaned_##name##_t *src) { \
f_move((&obj->_val), src); \
return _Z_RES_OK; \
}

#define _Z_OWNED_FUNCTIONS_VALUE_IMP_PREFIX(prefix, type, name, f_check, f_null, f_copy, f_drop) \
_Z_OWNED_FUNCTIONS_VALUE_IMPL_PREFIX_INNER(z, type, name, f_check, f_null, f_copy, f_drop, _ZP_NOTHING)
#define _Z_OWNED_FUNCTIONS_VALUE_IMP_PREFIX(prefix, type, name, f_check, f_null, f_copy, f_move, f_drop) \
_Z_OWNED_FUNCTIONS_VALUE_IMPL_PREFIX_INNER(z, type, name, f_check, f_null, f_copy, f_move, f_drop, _ZP_NOTHING)

#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX(prefix, type, name, f_check, f_null, f_drop) \
_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX_INNER(prefix, type, name, f_check, f_null, f_drop, _ZP_NOTHING)

#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL(type, name, f_check, f_null, f_drop) \
_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX(z, type, name, f_check, f_null, f_drop)

#define _Z_OWNED_FUNCTIONS_VALUE_IMPL(type, name, f_check, f_null, f_copy, f_drop) \
_Z_OWNED_FUNCTIONS_VALUE_IMP_PREFIX(z, type, name, f_check, f_null, f_copy, f_drop)
#define _Z_OWNED_FUNCTIONS_VALUE_IMPL(type, name, f_check, f_null, f_copy, f_move, f_drop) \
_Z_OWNED_FUNCTIONS_VALUE_IMP_PREFIX(z, type, name, f_check, f_null, f_copy, f_move, f_drop)

#define _Z_OWNED_FUNCTIONS_VALUE_NO_COPY_INLINE_IMPL(type, name, f_check, f_null, f_drop) \
_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL_PREFIX_INNER(z, type, name, f_check, f_null, f_drop, static inline)
Expand Down
2 changes: 2 additions & 0 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,8 @@ void z_query_reply_options_default(z_query_reply_options_t *options);
z_result_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, z_moved_bytes_t *payload,
const z_query_reply_options_t *options);

z_result_t z_query_loaned_take(z_owned_query_t *dst, z_loaned_query_t *src);

/**
* Builds a :c:type:`z_query_reply_del_options_t` with default values.
*
Expand Down
7 changes: 7 additions & 0 deletions include/zenoh-pico/collections/intmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ _z_int_void_map_t _z_int_void_map_clone(const _z_int_void_map_t *src, z_element_

void _z_int_void_map_clear(_z_int_void_map_t *map, z_element_free_f f);
void _z_int_void_map_free(_z_int_void_map_t **map, z_element_free_f f);
static inline void _z_int_void_map_move(_z_int_void_map_t *dst, _z_int_void_map_t *src) {
*dst = *src;
*src = _z_int_void_map_make(src->_capacity);
}

#define _Z_INT_MAP_DEFINE(name, type) \
typedef _z_int_void_map_entry_t name##_intmap_entry_t; \
Expand Down Expand Up @@ -115,6 +119,9 @@ void _z_int_void_map_free(_z_int_void_map_t **map, z_element_free_f f);
} \
static inline void name##_intmap_free(name##_intmap_t **m) { \
_z_int_void_map_free(m, name##_intmap_entry_elem_free); \
} \
static inline void name##_intmap_move(name##_intmap_t *dst, name##_intmap_t *src) { \
_z_int_void_map_move(dst, src); \
}

#ifdef __cplusplus
Expand Down
7 changes: 6 additions & 1 deletion include/zenoh-pico/collections/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ typedef struct {

_z_svec_t _z_svec_make(size_t capacity, size_t element_size);
bool _z_svec_copy(_z_svec_t *dst, const _z_svec_t *src, z_element_copy_f copy, size_t element_size);
static inline void _z_svec_move(_z_svec_t *dst, _z_svec_t *src) {
*dst = *src;
*src = _z_svec_make(0, 0);
}

size_t _z_svec_len(const _z_svec_t *v);
bool _z_svec_is_empty(const _z_svec_t *v);
Expand Down Expand Up @@ -116,7 +120,8 @@ void _z_svec_release(_z_svec_t *v);
} \
static inline void name##_svec_reset(name##_svec_t *v) { _z_svec_reset(v, name##_elem_clear, sizeof(type)); } \
static inline void name##_svec_clear(name##_svec_t *v) { _z_svec_clear(v, name##_elem_clear, sizeof(type)); } \
static inline void name##_svec_free(name##_svec_t **v) { _z_svec_free(v, name##_elem_clear, sizeof(type)); }
static inline void name##_svec_free(name##_svec_t **v) { _z_svec_free(v, name##_elem_clear, sizeof(type)); } \
static inline void name##_svec_move(name##_svec_t *dst, name##_svec_t *src) { _z_svec_move(dst, src); }

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion include/zenoh-pico/net/reply.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ typedef struct _z_reply_t {
_z_reply_data_t data;
} _z_reply_t;

_z_reply_t _z_reply_move(_z_reply_t *src_reply);
void _z_reply_move(_z_reply_t *dst, _z_reply_t *src);

_z_reply_t _z_reply_null(void);
void _z_reply_clear(_z_reply_t *src);
Expand Down
Loading

0 comments on commit 6cd1de4

Please sign in to comment.