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

Move and rename macros for define owned/loaned/view types #433

Merged
merged 1 commit into from
Jun 18, 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
138 changes: 138 additions & 0 deletions include/zenoh-pico/api/olv_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//
// 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]>
//
#ifndef INCLUDE_ZENOH_PICO_API_OLV_MACROS_H
#define INCLUDE_ZENOH_PICO_API_OLV_MACROS_H

// Owned/Loaned/View type macros
//
// !!! FOR INTERNAL USAGE ONLY !!!

// For pointer types
#define _Z_OWNED_TYPE_PTR(type, name) \
typedef struct { \
type *_val; \
} z_owned_##name##_t;

// For refcounted types
#define _Z_OWNED_TYPE_RC(type, name) \
typedef struct { \
type _rc; \
} z_owned_##name##_t;

#define _Z_LOANED_TYPE(type, name) typedef type z_loaned_##name##_t;

#define _Z_VIEW_TYPE(type, name) \
typedef struct { \
type _val; \
} z_view_##name##_t;

#define _Z_OWNED_FUNCTIONS_DEF(loanedtype, ownedtype, name) \
_Bool z_##name##_check(const ownedtype *obj); \
const loanedtype *z_##name##_loan(const ownedtype *obj); \
loanedtype *z_##name##_loan_mut(ownedtype *obj); \
ownedtype *z_##name##_move(ownedtype *obj); \
int8_t z_##name##_clone(ownedtype *obj, const loanedtype *src); \
void z_##name##_drop(ownedtype *obj); \
void z_##name##_null(ownedtype *obj);

#define _Z_VIEW_FUNCTIONS_DEF(loanedtype, viewtype, name) \
const loanedtype *z_view_##name##_loan(const viewtype *name); \
loanedtype *z_view_##name##_loan_mut(viewtype *name); \
void z_view_##name##_null(viewtype *name);

#define _Z_OWNED_FUNCTIONS_PTR_IMPL(type, name, f_copy, f_free) \
_Bool z_##name##_check(const z_owned_##name##_t *obj) { return obj->_val != NULL; } \
const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj) { return obj->_val; } \
z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj) { return obj->_val; } \
void z_##name##_null(z_owned_##name##_t *obj) { obj->_val = NULL; } \
z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj) { return obj; } \
int8_t z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src) { \
int8_t ret = _Z_RES_OK; \
obj->_val = (type *)z_malloc(sizeof(type)); \
if (obj->_val != NULL) { \
f_copy(obj->_val, src); \
} else { \
ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; \
} \
return ret; \
} \
void z_##name##_drop(z_owned_##name##_t *obj) { \
if ((obj != NULL) && (obj->_val != NULL)) { \
f_free(&obj->_val); \
} \
}

#define _Z_OWNED_FUNCTIONS_RC_IMPL(name) \
_Bool z_##name##_check(const z_owned_##name##_t *val) { return val->_rc.in != NULL; } \
const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *val) { return &val->_rc; } \
z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *val) { return &val->_rc; } \
void z_##name##_null(z_owned_##name##_t *val) { val->_rc.in = NULL; } \
z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *val) { return val; } \
int8_t z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src) { \
int8_t ret = _Z_RES_OK; \
obj->_rc = _z_##name##_rc_clone((z_loaned_##name##_t *)src); \
if (obj->_rc.in == NULL) { \
ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; \
} \
return ret; \
} \
void z_##name##_drop(z_owned_##name##_t *val) { \
if (val->_rc.in != NULL) { \
if (_z_##name##_rc_drop(&val->_rc)) { \
val->_rc.in = NULL; \
} \
} \
}

#define _Z_VIEW_FUNCTIONS_PTR_IMPL(type, name) \
const z_loaned_##name##_t *z_view_##name##_loan(const z_view_##name##_t *obj) { return &obj->_val; } \
z_loaned_##name##_t *z_view_##name##_loan_mut(z_view_##name##_t *obj) { return &obj->_val; }

#define _Z_OWNED_FUNCTIONS_CLOSURE_DEF(ownedtype, name) \
_Bool z_##name##_check(const ownedtype *val); \
ownedtype *z_##name##_move(ownedtype *val); \
void z_##name##_drop(ownedtype *val); \
void z_##name##_null(ownedtype *name);

#define _Z_OWNED_FUNCTIONS_CLOSURE_IMPL(ownedtype, name, f_call, f_drop) \
_Bool z_##name##_check(const ownedtype *val) { return val->call != NULL; } \
ownedtype *z_##name##_move(ownedtype *val) { return val; } \
void z_##name##_drop(ownedtype *val) { \
if (val->drop != NULL) { \
(val->drop)(val->context); \
val->drop = NULL; \
} \
val->call = NULL; \
val->context = NULL; \
} \
void z_##name##_null(ownedtype *val) { \
val->call = NULL; \
val->drop = NULL; \
val->context = NULL; \
} \
int8_t z_##name(ownedtype *closure, f_call call, f_drop drop, void *context) { \
closure->call = call; \
closure->drop = drop; \
closure->context = context; \
\
return _Z_RES_OK; \
}

// Gets internal value from refcounted type (e.g. z_loaned_session_t, z_query_t)
#define _Z_RC_IN_VAL(arg) ((arg)->in->val)

// Gets internal value from refcounted owned type (e.g. z_owned_session_t, z_owned_query_t)
#define _Z_OWNED_RC_IN_VAL(arg) ((arg)->_rc.in->val)

#endif /* INCLUDE_ZENOH_PICO_API_OLV_MACROS_H */
83 changes: 29 additions & 54 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <stdbool.h>
#include <stdint.h>

#include "olv_macros.h"
#include "zenoh-pico/api/types.h"
#include "zenoh-pico/net/query.h"
#include "zenoh-pico/net/session.h"
Expand Down Expand Up @@ -1029,60 +1030,34 @@ int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_loaned_hello_handler_
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) \
_Bool z_##name##_check(const ownedtype *obj); \
const loanedtype *z_##name##_loan(const ownedtype *obj); \
loanedtype *z_##name##_loan_mut(ownedtype *obj); \
ownedtype *z_##name##_move(ownedtype *obj); \
int8_t z_##name##_clone(ownedtype *obj, const loanedtype *src); \
void z_##name##_drop(ownedtype *obj); \
void z_##name##_null(ownedtype *obj);

_OWNED_FUNCTIONS(z_loaned_string_t, z_owned_string_t, string)
_OWNED_FUNCTIONS(z_loaned_keyexpr_t, z_owned_keyexpr_t, keyexpr)
_OWNED_FUNCTIONS(z_loaned_config_t, z_owned_config_t, config)
_OWNED_FUNCTIONS(z_loaned_scouting_config_t, z_owned_scouting_config_t, scouting_config)
_OWNED_FUNCTIONS(z_loaned_session_t, z_owned_session_t, session)
_OWNED_FUNCTIONS(z_loaned_subscriber_t, z_owned_subscriber_t, subscriber)
_OWNED_FUNCTIONS(z_loaned_publisher_t, z_owned_publisher_t, publisher)
_OWNED_FUNCTIONS(z_loaned_queryable_t, z_owned_queryable_t, queryable)
_OWNED_FUNCTIONS(z_loaned_hello_t, z_owned_hello_t, hello)
_OWNED_FUNCTIONS(z_loaned_reply_t, z_owned_reply_t, reply)
_OWNED_FUNCTIONS(z_loaned_string_array_t, z_owned_string_array_t, string_array)
_OWNED_FUNCTIONS(z_loaned_sample_t, z_owned_sample_t, sample)
_OWNED_FUNCTIONS(z_loaned_query_t, z_owned_query_t, query)
_OWNED_FUNCTIONS(z_loaned_slice_t, z_owned_slice_t, slice)
_OWNED_FUNCTIONS(z_loaned_bytes_t, z_owned_bytes_t, bytes)
_OWNED_FUNCTIONS(z_loaned_value_t, z_owned_value_t, value)

#define _OWNED_FUNCTIONS_CLOSURE(ownedtype, name) \
_Bool z_##name##_check(const ownedtype *val); \
ownedtype *z_##name##_move(ownedtype *val); \
void z_##name##_drop(ownedtype *val); \
void z_##name##_null(ownedtype *name);

_OWNED_FUNCTIONS_CLOSURE(z_owned_closure_sample_t, closure_sample)
_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)

#define _VIEW_FUNCTIONS(loanedtype, viewtype, name) \
const loanedtype *z_view_##name##_loan(const viewtype *name); \
loanedtype *z_view_##name##_loan_mut(viewtype *name); \
void z_view_##name##_null(viewtype *name);

_VIEW_FUNCTIONS(z_loaned_keyexpr_t, z_view_keyexpr_t, keyexpr)
_VIEW_FUNCTIONS(z_loaned_string_t, z_view_string_t, string)

// Gets internal value from refcounted type (e.g. z_loaned_session_t, z_query_t)
#define _Z_RC_IN_VAL(arg) ((arg)->in->val)

// Gets internal value from refcounted owned type (e.g. z_owned_session_t, z_owned_query_t)
#define _Z_OWNED_RC_IN_VAL(arg) ((arg)->_rc.in->val)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_string_t, z_owned_string_t, string)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_keyexpr_t, z_owned_keyexpr_t, keyexpr)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_config_t, z_owned_config_t, config)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_scouting_config_t, z_owned_scouting_config_t, scouting_config)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_session_t, z_owned_session_t, session)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_subscriber_t, z_owned_subscriber_t, subscriber)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_publisher_t, z_owned_publisher_t, publisher)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_queryable_t, z_owned_queryable_t, queryable)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_hello_t, z_owned_hello_t, hello)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_reply_t, z_owned_reply_t, reply)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_string_array_t, z_owned_string_array_t, string_array)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_sample_t, z_owned_sample_t, sample)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_query_t, z_owned_query_t, query)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_slice_t, z_owned_slice_t, slice)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_bytes_t, z_owned_bytes_t, bytes)
_Z_OWNED_FUNCTIONS_DEF(z_loaned_value_t, z_owned_value_t, value)

_Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_sample_t, closure_sample)
_Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_owned_sample_t, closure_owned_sample)
_Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_query_t, closure_query)
_Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_owned_query_t, closure_owned_query)
_Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_reply_t, closure_reply)
_Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_owned_reply_t, closure_owned_reply)
_Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_hello_t, closure_hello)
_Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_zid_t, closure_zid)

_Z_VIEW_FUNCTIONS_DEF(z_loaned_keyexpr_t, z_view_keyexpr_t, keyexpr)
_Z_VIEW_FUNCTIONS_DEF(z_loaned_string_t, z_view_string_t, string)

/**
* Loans a :c:type:`z_owned_sample_t`.
Expand Down
Loading
Loading