From 562178d958df9943c7e64012a5ce567b010efcdf Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Tue, 27 Aug 2024 18:05:52 +0200 Subject: [PATCH 1/9] move interop functionality under separatre namespace and make it public --- include/zenoh/api/base.hxx | 52 ----- include/zenoh/api/bytes.hxx | 70 +++--- include/zenoh/api/channels.hxx | 15 +- include/zenoh/api/config.hxx | 12 +- include/zenoh/api/encoding.hxx | 12 +- include/zenoh/api/hello.hxx | 9 +- include/zenoh/api/interop.hxx | 208 ++++++++++++++++++ include/zenoh/api/keyexpr.hxx | 39 ++-- include/zenoh/api/liveliness.hxx | 4 +- include/zenoh/api/publisher.hxx | 22 +- include/zenoh/api/query.hxx | 55 ++--- include/zenoh/api/reply.hxx | 20 +- include/zenoh/api/sample.hxx | 34 +-- include/zenoh/api/scout.hxx | 4 +- include/zenoh/api/session.hxx | 144 ++++++------ include/zenoh/api/shm/buffer/zshm.hxx | 7 +- include/zenoh/api/shm/buffer/zshmmut.hxx | 6 +- .../api/shm/client_storage/client_storage.hxx | 13 +- .../posix/posix_shm_provider.hxx | 2 +- .../zenoh/api/shm/provider/alloc_layout.hxx | 17 +- .../zenoh/api/shm/provider/shm_provider.hxx | 22 +- .../api/shm/provider/shm_provider_backend.hxx | 9 +- include/zenoh/api/shm/provider/types.hxx | 4 +- include/zenoh/api/source_info.hxx | 8 +- include/zenoh/api/subscriber.hxx | 4 +- include/zenoh/api/timestamp.hxx | 2 +- include/zenoh/detail/closures_concrete.hxx | 12 +- include/zenoh/detail/interop.hxx | 115 ---------- tests/universal/network/keyexpr.cxx | 10 +- tests/zenohc/shm_api.cxx | 20 +- 30 files changed, 512 insertions(+), 439 deletions(-) create mode 100644 include/zenoh/api/interop.hxx delete mode 100644 include/zenoh/detail/interop.hxx diff --git a/include/zenoh/api/base.hxx b/include/zenoh/api/base.hxx index a92789cc..49b62d94 100644 --- a/include/zenoh/api/base.hxx +++ b/include/zenoh/api/base.hxx @@ -22,27 +22,6 @@ namespace zenoh { -namespace detail { -template -struct is_loan_available : std::false_type {}; - -template -struct is_loan_available()))>> : std::true_type {}; - -template -inline constexpr bool is_loan_available_v = is_loan_available::value; - -template -struct is_loan_mut_available : std::false_type {}; - -template -struct is_loan_mut_available()))>> : std::true_type {}; - -template -inline constexpr bool is_loan_mut_available_v = is_loan_mut_available::value; - -} // namespace detail - /// @brief Error code returned by Zenoh API typedef ::z_result_t ZResult; @@ -118,39 +97,8 @@ class Owned { /// Destructor drops owned value using z_drop from zenoh API ~Owned() { ::z_drop(::z_move(_0)); } - /// @name Methods - - /// Take out zenoh structure and leave owned object in a null state. - OwnedType take() && { - auto r = this->_0; - ::z_internal_null(&this->_0); - return r; - } - - /// Check object validity uzing zenoh API - /// This is internal function made public for testing purposes - bool internal_check() const { return ::z_internal_check(_0); } - protected: OwnedType _0; - - template , - class L = typename OtL::type, // SFINAE here if no loaned type declared - class LAvail = std::enable_if, L>, - class T = typename LAvail::type // SFINAE here if immutable loan is not available - > - const T* loan() const { - return ::z_loan(_0); - } - - template , - class L = typename OtL::type, // SFINAE here if no loaned type declared - class LAvail = std::enable_if, L>, - class T = typename LAvail::type // SFINAE here if mutable loan is not available - > - T* loan() { - return ::z_loan_mut(_0); - } }; } // namespace zenoh \ No newline at end of file diff --git a/include/zenoh/api/bytes.hxx b/include/zenoh/api/bytes.hxx index 4d861d61..5417e035 100644 --- a/include/zenoh/api/bytes.hxx +++ b/include/zenoh/api/bytes.hxx @@ -15,9 +15,9 @@ #include "../detail/closures.hxx" #include "../detail/commons.hxx" -#include "../detail/interop.hxx" #include "base.hxx" #include "closures.hxx" +#include "interop.hxx" #if (defined(SHARED_MEMORY) && defined(UNSTABLE)) #include "shm/buffer/buffer.hxx" #endif @@ -67,17 +67,17 @@ class Bytes : public Owned<::z_owned_bytes_t> { /// @brief Construct a shallow copy of this data. Bytes clone() const { Bytes b; - ::z_bytes_clone(&b._0, this->loan()); + ::z_bytes_clone(&b._0, interop::as_loaned_c_ptr(*this)); return b; } /// @brief Construct an empty data. - Bytes() : Owned(nullptr) { ::z_bytes_empty(detail::as_owned_c_ptr(*this)); } + Bytes() : Owned(nullptr) { ::z_bytes_empty(interop::as_owned_c_ptr(*this)); } /// @name Methods /// @brief Get number of bytes in the pyload. - size_t size() const { return ::z_bytes_len(this->loan()); } + size_t size() const { return ::z_bytes_len(interop::as_loaned_c_ptr(*this)); } /// @brief Serialize specified type. /// @@ -111,7 +111,7 @@ class Bytes : public Owned<::z_owned_bytes_t> { // increment current, in case iterator dereference might invalidate it, which happens // for map/set elements extraction while serializing std::move'd map/set auto it = current++; - *b = Bytes::serialize(*it, codec).take(); + *b = interop::move_to_c_obj(Bytes::serialize(*it, codec)); return true; }; using F = decltype(f); @@ -119,7 +119,7 @@ class Bytes : public Owned<::z_owned_bytes_t> { using ClosureType = typename detail::closures::Closure; auto closure = ClosureType(std::forward(f), closures::none); - ::z_bytes_from_iter(detail::as_owned_c_ptr(out), detail::closures::_zenoh_encode_iter, closure.as_context()); + ::z_bytes_from_iter(interop::as_owned_c_ptr(out), detail::closures::_zenoh_encode_iter, closure.as_context()); return out; } @@ -210,7 +210,7 @@ class Bytes : public Owned<::z_owned_bytes_t> { /// @brief Create data reader. /// @return reader instance. - Reader reader() const { return Reader(::z_bytes_get_reader(this->loan())); } + Reader reader() const { return Reader(::z_bytes_get_reader(interop::as_loaned_c_ptr(*this))); } /// @brief A writer for Zenoh-serialized data. class Writer : public Copyable<::z_bytes_writer_t> { @@ -256,7 +256,7 @@ class Bytes : public Owned<::z_owned_bytes_t> { /// It is the user responsibility to ensure that there is at most one active writer at /// a given moment of time for a given ``Bytes`` instance. /// @return writer instance. - Writer writer() { return Writer(::z_bytes_get_writer(this->loan())); } + Writer writer() { return Writer(::z_bytes_get_writer(interop::as_loaned_c_ptr(*this))); } }; /// @brief An iterator over multi-element serialized data. @@ -270,14 +270,16 @@ class Bytes::Iterator : Copyable<::z_bytes_iterator_t> { /// @return next element of serialized data, if the iterator reached the end, an empty optional will be returned. std::optional next() { std::optional b(std::in_place); - if (!::z_bytes_iterator_next(&this->_0, detail::as_owned_c_ptr(b.value()))) { + if (!::z_bytes_iterator_next(&this->_0, interop::as_owned_c_ptr(b.value()))) { b.reset(); } return b; } }; -inline Bytes::Iterator Bytes::iter() const { return Bytes::Iterator(::z_bytes_get_iterator(this->loan())); } +inline Bytes::Iterator Bytes::iter() const { + return Bytes::Iterator(::z_bytes_get_iterator(interop::as_loaned_c_ptr(*this))); +} namespace detail { @@ -301,7 +303,7 @@ struct ZenohDeserializer { static ZShm deserialize(const Bytes& b, ZResult* err = nullptr) { ZShm shm(nullptr); __ZENOH_RESULT_CHECK( - ::z_bytes_deserialize_into_owned_shm(detail::as_loaned_c_ptr(b), detail::as_owned_c_ptr(shm)), err, + ::z_bytes_deserialize_into_owned_shm(interop::as_loaned_c_ptr(b), interop::as_owned_c_ptr(shm)), err, "Failed to deserialize into ZShm!"); return shm; } @@ -323,8 +325,8 @@ template struct ZenohDeserializer> { static std::pair deserialize(const Bytes& b, ZResult* err = nullptr) { zenoh::Bytes ba, bb; - __ZENOH_RESULT_CHECK(::z_bytes_deserialize_into_pair(detail::as_loaned_c_ptr(b), detail::as_owned_c_ptr(ba), - detail::as_owned_c_ptr(bb)), + __ZENOH_RESULT_CHECK(::z_bytes_deserialize_into_pair(interop::as_loaned_c_ptr(b), interop::as_owned_c_ptr(ba), + interop::as_owned_c_ptr(bb)), err, "Failed to deserialize into std::pair"); return {ZenohDeserializer::deserialize(ba, err), ZenohDeserializer::deserialize(bb, err)}; } @@ -408,15 +410,15 @@ struct ZenohDeserializer> { } }; -#define __ZENOH_DESERIALIZE_ARITHMETIC(TYPE, EXT) \ - template <> \ - struct ZenohDeserializer { \ - static TYPE deserialize(const Bytes& b, ZResult* err = nullptr) { \ - TYPE t; \ - __ZENOH_RESULT_CHECK(::z_bytes_deserialize_into_##EXT(detail::as_loaned_c_ptr(b), &t), err, \ - "Failed to deserialize into " #TYPE); \ - return t; \ - } \ +#define __ZENOH_DESERIALIZE_ARITHMETIC(TYPE, EXT) \ + template <> \ + struct ZenohDeserializer { \ + static TYPE deserialize(const Bytes& b, ZResult* err = nullptr) { \ + TYPE t; \ + __ZENOH_RESULT_CHECK(::z_bytes_deserialize_into_##EXT(interop::as_loaned_c_ptr(b), &t), err, \ + "Failed to deserialize into " #TYPE); \ + return t; \ + } \ }; __ZENOH_DESERIALIZE_ARITHMETIC(uint8_t, uint8); @@ -469,7 +471,7 @@ class ZenohCodec { Bytes serialize(const Slice& s) const { Bytes b; if (this->_alias_guard == nullptr) { - ::z_bytes_serialize_from_buf(detail::as_owned_c_ptr(b), s.data, s.len); + ::z_bytes_serialize_from_buf(interop::as_owned_c_ptr(b), s.data, s.len); } else { auto deleter = [ptr = this->_alias_guard](void*) mutable { ptr.reset(); }; b = serialize(make_owned_slice(const_cast(s.data), s.len, std::move(deleter))); @@ -488,7 +490,7 @@ class ZenohCodec { using Dval = std::remove_reference_t; using DroppableType = typename detail::closures::Droppable; auto drop = DroppableType::into_context(std::forward(d)); - ::z_bytes_from_buf(detail::as_owned_c_ptr(b), data, len, detail::closures::_zenoh_drop_with_context, drop); + ::z_bytes_from_buf(interop::as_owned_c_ptr(b), data, len, detail::closures::_zenoh_drop_with_context, drop); return b; } @@ -513,14 +515,14 @@ class ZenohCodec { #if (defined(SHARED_MEMORY) && defined(UNSTABLE)) Bytes serialize(ZShm&& shm, ZResult* err = nullptr) const { Bytes b; - __ZENOH_RESULT_CHECK(::z_bytes_serialize_from_shm(detail::as_owned_c_ptr(b), detail::as_moved_c_ptr(shm)), err, - "Failed to serialize ZShm"); + __ZENOH_RESULT_CHECK(::z_bytes_serialize_from_shm(interop::as_owned_c_ptr(b), interop::as_moved_c_ptr(shm)), + err, "Failed to serialize ZShm"); return b; } Bytes serialize(ZShmMut&& shm, ZResult* err = nullptr) const { Bytes b; - __ZENOH_RESULT_CHECK(::z_bytes_serialize_from_shm_mut(detail::as_owned_c_ptr(b), detail::as_moved_c_ptr(shm)), + __ZENOH_RESULT_CHECK(::z_bytes_serialize_from_shm_mut(interop::as_owned_c_ptr(b), interop::as_moved_c_ptr(shm)), err, "Failed to serialize ZShmMut"); return b; } @@ -628,7 +630,7 @@ class ZenohCodec { auto ba = serialize(s.first); auto bb = serialize(s.second); Bytes b; - ::z_bytes_from_pair(detail::as_owned_c_ptr(b), detail::as_moved_c_ptr(ba), detail::as_moved_c_ptr(bb)); + ::z_bytes_from_pair(interop::as_owned_c_ptr(b), interop::as_moved_c_ptr(ba), interop::as_moved_c_ptr(bb)); return b; } @@ -637,7 +639,7 @@ class ZenohCodec { auto ba = serialize(std::move(s.first)); auto bb = serialize(std::move(s.second)); Bytes b; - ::z_bytes_from_pair(detail::as_owned_c_ptr(b), detail::as_moved_c_ptr(ba), detail::as_moved_c_ptr(bb)); + ::z_bytes_from_pair(interop::as_owned_c_ptr(b), interop::as_moved_c_ptr(ba), interop::as_moved_c_ptr(bb)); return b; } @@ -653,11 +655,11 @@ class ZenohCodec { } } -#define __ZENOH_SERIALIZE_ARITHMETIC(TYPE, EXT) \ - Bytes serialize(TYPE t) const { \ - Bytes b; \ - ::z_bytes_serialize_from_##EXT(detail::as_owned_c_ptr(b), t); \ - return b; \ +#define __ZENOH_SERIALIZE_ARITHMETIC(TYPE, EXT) \ + Bytes serialize(TYPE t) const { \ + Bytes b; \ + ::z_bytes_serialize_from_##EXT(interop::as_owned_c_ptr(b), t); \ + return b; \ } __ZENOH_SERIALIZE_ARITHMETIC(uint8_t, uint8); diff --git a/include/zenoh/api/channels.hxx b/include/zenoh/api/channels.hxx index 018b1797..a0e54424 100644 --- a/include/zenoh/api/channels.hxx +++ b/include/zenoh/api/channels.hxx @@ -18,8 +18,8 @@ #pragma once #include -#include "../detail/interop.hxx" #include "base.hxx" +#include "interop.hxx" #include "query.hxx" #include "reply.hxx" #include "sample.hxx" @@ -110,7 +110,7 @@ class FifoHandler : public Owned::handler_ty /// @return received data entry if there were any in the buffer, a receive error otherwise. std::variant recv() const { std::variant v(T(nullptr)); - z_result_t res = ::z_recv(this->loan(), zenoh::detail::as_owned_c_ptr(std::get(v))); + z_result_t res = ::z_recv(interop::as_loaned_c_ptr(*this), zenoh::interop::as_owned_c_ptr(std::get(v))); if (res == Z_OK) { return v; } else { @@ -122,7 +122,7 @@ class FifoHandler : public Owned::handler_ty /// @return received data entry if there were any in the buffer, a receive error otherwise. std::variant try_recv() const { std::variant v(T(nullptr)); - z_result_t res = ::z_try_recv(this->loan(), zenoh::detail::as_owned_c_ptr(std::get(v))); + z_result_t res = ::z_try_recv(interop::as_loaned_c_ptr(*this), zenoh::interop::as_owned_c_ptr(std::get(v))); if (res == Z_OK) { return v; } else if (res == Z_CHANNEL_NODATA) { @@ -146,7 +146,8 @@ class RingHandler : public Owned::handler_ty /// @return received data entry if there were any in the buffer, a receive error otherwise. std::variant recv() const { std::variant v(T(nullptr)); - z_result_t res = ::z_recv(this->loan(), zenoh::detail::as_owned_c_ptr(std::get(v))); + z_result_t res = + ::z_recv(zenoh::interop::as_loaned_c_ptr(*this), zenoh::interop::as_owned_c_ptr(std::get(v))); if (res == Z_OK) { return v; } else { @@ -158,7 +159,7 @@ class RingHandler : public Owned::handler_ty /// @return received data entry if there were any in the buffer, a receive error otherwise. std::variant try_recv() const { std::variant v(T(nullptr)); - z_result_t res = ::z_try_recv(this->loan(), zenoh::detail::as_owned_c_ptr(std::get(v))); + z_result_t res = ::z_try_recv(interop::as_loaned_c_ptr(*this), zenoh::interop::as_owned_c_ptr(std::get(v))); if (res == Z_OK) { return v; } else if (res == Z_CHANNEL_NODATA) { @@ -191,7 +192,7 @@ class FifoChannel { std::pair::closure_type, HandlerType> into_cb_handler_pair() const { typename detail::FifoHandlerData::closure_type c_closure; FifoHandler h(nullptr); - detail::FifoHandlerData::create_cb_handler_pair(&c_closure, zenoh::detail::as_owned_c_ptr(h), _capacity); + detail::FifoHandlerData::create_cb_handler_pair(&c_closure, zenoh::interop::as_owned_c_ptr(h), _capacity); return {c_closure, std::move(h)}; } }; @@ -218,7 +219,7 @@ class RingChannel { std::pair::closure_type, HandlerType> into_cb_handler_pair() const { typename detail::RingHandlerData::closure_type c_closure; RingHandler h(nullptr); - detail::RingHandlerData::create_cb_handler_pair(&c_closure, zenoh::detail::as_owned_c_ptr(h), _capacity); + detail::RingHandlerData::create_cb_handler_pair(&c_closure, zenoh::interop::as_owned_c_ptr(h), _capacity); return {c_closure, std::move(h)}; } }; diff --git a/include/zenoh/api/config.hxx b/include/zenoh/api/config.hxx index a3ed0030..19fe4164 100644 --- a/include/zenoh/api/config.hxx +++ b/include/zenoh/api/config.hxx @@ -138,8 +138,8 @@ class Config : public Owned<::z_owned_config_t> { /// @note zenoh-c only. std::string get(std::string_view key, ZResult* err = nullptr) const { ::z_owned_string_t s; - __ZENOH_RESULT_CHECK(::zc_config_get_from_substr(this->loan(), key.data(), key.size(), &s), err, - std::string("Failed to get config value for the key: ").append(key)); + __ZENOH_RESULT_CHECK(::zc_config_get_from_substr(interop::as_loaned_c_ptr(*this), key.data(), key.size(), &s), + err, std::string("Failed to get config value for the key: ").append(key)); std::string out = std::string(::z_string_data(::z_loan(s)), ::z_string_len(::z_loan(s))); ::z_drop(::z_move(s)); return out; @@ -150,7 +150,7 @@ class Config : public Owned<::z_owned_config_t> { /// @note zenoh-c only. std::string to_string() const { ::z_owned_string_t s; - ::zc_config_to_string(this->loan(), &s); + ::zc_config_to_string(interop::as_loaned_c_ptr(*this), &s); std::string out = std::string(::z_string_data(::z_loan(s)), ::z_string_len(::z_loan(s))); ::z_drop(::z_move(s)); return out; @@ -164,7 +164,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return true if the parameter was inserted, false otherwise. /// @note zenoh-c only. void insert_json(const std::string& key, const std::string& value, ZResult* err = nullptr) { - __ZENOH_RESULT_CHECK(::zc_config_insert_json(loan(), key.c_str(), value.c_str()), err, + __ZENOH_RESULT_CHECK(::zc_config_insert_json(interop::as_loaned_c_ptr(*this), key.c_str(), value.c_str()), err, std::string("Failed to insert '") .append(value) .append("' for the key '") @@ -180,7 +180,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return pointer to the null-terminated string value of the config parameter. /// @note zenoh-pico only. const char* get(uint8_t key, ZResult* err = nullptr) const { - const char* c = ::zp_config_get(loan(), key); + const char* c = ::zp_config_get(interop::as_loaned_c_ptr(*this), key); __ZENOH_RESULT_CHECK((c == nullptr ? -1 : Z_OK), err, std::string("Failed to get config value for the key: ").append(std::to_string(key))); return c; @@ -193,7 +193,7 @@ class Config : public Owned<::z_owned_config_t> { /// thrown in case of error. /// @note zenoh-pico only. void insert(uint8_t key, const char* value, ZResult* err = nullptr) { - __ZENOH_RESULT_CHECK(zp_config_insert(this->loan(), key, value), err, + __ZENOH_RESULT_CHECK(zp_config_insert(interop::as_loaned_c_ptr(*this), key, value), err, std::string("Failed to insert '") .append(value) .append("' for the key '") diff --git a/include/zenoh/api/encoding.hxx b/include/zenoh/api/encoding.hxx index 3b4aaa6a..5688333f 100644 --- a/include/zenoh/api/encoding.hxx +++ b/include/zenoh/api/encoding.hxx @@ -16,6 +16,7 @@ #include "../zenohc.hxx" #include "base.hxx" +#include "interop.hxx" namespace zenoh { @@ -36,7 +37,7 @@ class Encoding : public Owned<::z_owned_encoding_t> { } /// @brief Copy contructor - Encoding(const Encoding& other) : Encoding() { ::z_encoding_clone(&this->_0, other.loan()); }; + Encoding(const Encoding& other) : Encoding() { ::z_encoding_clone(&this->_0, interop::as_loaned_c_ptr(other)); }; Encoding(Encoding&& other) = default; @@ -45,7 +46,7 @@ class Encoding : public Owned<::z_owned_encoding_t> { /// @brief Get string representation of encoding. std::string as_string() const { ::z_owned_string_t s; - ::z_encoding_to_string(this->loan(), &s); + ::z_encoding_to_string(interop::as_loaned_c_ptr(*this), &s); std::string out = std::string(::z_string_data(::z_loan(s)), ::z_string_len(::z_loan(s))); ::z_drop(::z_move(s)); return out; @@ -56,8 +57,9 @@ class Encoding : public Owned<::z_owned_encoding_t> { /// Zenoh does not define what a schema is and its semantics is left to the implementer. /// E.g. a common schema for `text/plain` encoding is `utf-8`. void set_schema(std::string_view schema, ZResult* err = nullptr) { - __ZENOH_RESULT_CHECK(::z_encoding_set_schema_from_substr(this->loan(), schema.data(), schema.size()), err, - "Failed to set encoding schema"); + __ZENOH_RESULT_CHECK( + ::z_encoding_set_schema_from_substr(interop::as_loaned_c_ptr(*this), schema.data(), schema.size()), err, + "Failed to set encoding schema"); } /// @name Operators @@ -66,7 +68,7 @@ class Encoding : public Owned<::z_owned_encoding_t> { Encoding& operator=(const Encoding& other) { if (this != &other) { ::z_drop(z_move(this->_0)); - ::z_encoding_clone(&this->_0, other.loan()); + ::z_encoding_clone(&this->_0, interop::as_loaned_c_ptr(other)); } return *this; }; diff --git a/include/zenoh/api/hello.hxx b/include/zenoh/api/hello.hxx index c7f01477..1fcbea84 100644 --- a/include/zenoh/api/hello.hxx +++ b/include/zenoh/api/hello.hxx @@ -15,6 +15,7 @@ #include "../zenohc.hxx" #include "base.hxx" #include "enums.hxx" +#include "interop.hxx" #if defined UNSTABLE #include "id.hxx" #endif @@ -34,22 +35,22 @@ class Hello : public Owned<::z_owned_hello_t> { #if defined UNSTABLE /// @brief Get ``Id`` of the entity. /// @return ``Id`` of the entity. - Id get_id() const { return Id(::z_hello_zid(this->loan())); }; + Id get_id() const { return Id(::z_hello_zid(interop::as_loaned_c_ptr(*this))); }; #endif /// @brief Get the type of the entity. /// @return ``zenoh::WhatAmI`` of the entity. - WhatAmI get_whatami() const { return ::z_hello_whatami(this->loan()); } + WhatAmI get_whatami() const { return ::z_hello_whatami(interop::as_loaned_c_ptr(*this)); } /// @brief Get the array of locators of the entity. /// @return the array of locators of the entity. std::vector get_locators() const { #ifdef ZENOHCXX_ZENOHC ::z_owned_string_array_t out; - ::z_hello_locators(this->loan(), &out); + ::z_hello_locators(interop::as_loaned_c_ptr(*this), &out); auto out_loaned = ::z_loan(out); #else - auto out_loaned = ::z_hello_locators(this->loan()); + auto out_loaned = ::z_hello_locators(interop::as_loaned_c_ptr(*this)); #endif std::vector locators(::z_string_array_len(out_loaned)); for (size_t i = 0; i < ::z_string_array_len(out_loaned); i++) { diff --git a/include/zenoh/api/interop.hxx b/include/zenoh/api/interop.hxx new file mode 100644 index 00000000..691b85de --- /dev/null +++ b/include/zenoh/api/interop.hxx @@ -0,0 +1,208 @@ +// +// 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, +#pragma once + +#include + +#include "base.hxx" + +namespace zenoh::detail { +template +struct is_loan_available : std::false_type {}; + +template +struct is_loan_available()))>> : std::true_type {}; + +template +inline constexpr bool is_loan_available_v = is_loan_available::value; + +template +struct is_loan_mut_available : std::false_type {}; + +template +struct is_loan_mut_available()))>> : std::true_type {}; + +template +inline constexpr bool is_loan_mut_available_v = is_loan_mut_available::value; + +} // namespace zenoh::detail + +namespace zenoh::interop { + +/// @brief Get C representation of trivially copyable Zenoh-object. +template +CopyableType* as_copyable_c_ptr(Copyable& cpp_obj) { + return reinterpret_cast(&cpp_obj); +} + +/// @brief Get C representation of trivially copyable Zenoh-object. +template +const CopyableType* as_copyable_c_ptr(const Copyable& cpp_obj) { + return reinterpret_cast(&cpp_obj); +} + +/// @brief Get C representation of std::optional of trivially copyable Zenoh-object. +/// @return Pointer to C representation of trivially copyable Zenoh-object, or NULL if cpp_obj is empty. +template +auto* as_copyable_c_ptr(std::optional& cpp_obj) { + return cpp_obj.has_value() ? as_copyable_c_ptr(cpp_obj.value()) : nullptr; +} + +/// @brief Get C representation of std::optional of trivially copyable Zenoh-object. +/// @return Pointer to C representation of trivially copyable Zenoh-object, or NULL if cpp_obj is empty. +template +const auto* as_copyable_c_ptr(const std::optional& cpp_obj) { + return cpp_obj.has_value() ? as_copyable_c_ptr(cpp_obj.value()) : nullptr; +} + +/// @brief Get owned C representation of owned Zenoh-object. +template +OwnedType* as_owned_c_ptr(Owned& cpp_obj) { + return reinterpret_cast(&cpp_obj); +} + +/// @brief Get owned C representation of owned Zenoh-object. +template +const OwnedType* as_owned_c_ptr(const Owned& cpp_obj) { + return reinterpret_cast(&cpp_obj); +} + +/// @brief Get owned C representation of owned Zenoh-object. +/// @return Pointer to owned C representation of owned Zenoh-object, or NULL if cpp_obj is empty. +template +auto* as_owned_c_ptr(std::optional& cpp_obj) { + return cpp_obj.has_value() ? as_owned_c_ptr(cpp_obj.value()) : nullptr; +} + +/// @brief Get owned C representation of owned Zenoh-object. +/// @return Pointer to owned C representation of owned Zenoh-object, or NULL if cpp_obj is empty. +template +const auto* as_owned_c_ptr(const std::optional& cpp_obj) { + return cpp_obj.has_value() ? as_owned_c_ptr(cpp_obj.value()) : nullptr; +} + +/// @brief Get loaned C representation of owned Zenoh-object. +template < + class OwnedType, + class Loaned = typename ::z_owned_to_loaned_type_t::type, // SFINAE here if no loaned type declared + class LoanAvailable = std::enable_if_t, Loaned> // SFINAE here if immutable + // loan is not available + > +const Loaned* as_loaned_c_ptr(const Owned& cpp_obj) { + return ::z_loan(*as_owned_c_ptr(cpp_obj)); +} + +/// @brief Get loaned C representation of owned Zenoh-object. +template ::type, // SFINAE here if no loaned type + // declared + class LoanAvailable = std::enable_if_t, + Loaned> // SFINAE here if immutable loan is not available + > +Loaned* as_loaned_c_ptr(Owned& cpp_obj) { + return ::z_loan_mut(*as_owned_c_ptr(cpp_obj)); +} + +/// @brief Get moved C representation of owned Zenoh-object. +template +auto* as_moved_c_ptr(Owned& cpp_obj) { + return ::z_move(*as_owned_c_ptr(cpp_obj)); +} + +/// @brief Get moved C representation of std::optional of owned Zenoh-object. +/// @return Pointer to C representation of moved owned Zenoh-object, or NULL if cpp_obj is empty. +template +auto* as_moved_c_ptr(std::optional& cpp_obj) { + return cpp_obj.has_value() ? ::z_move(*as_owned_c_ptr(cpp_obj.value())) : nullptr; +} + +/// @brief Get C++ representation of owned Zenoh-c struct, from its loaned ptr. +template ::type // SFINAE here if no loaned type + // declared + > +const auto& as_owned_cpp_ref(const LoanedType* loaned_c_obj) { + static_assert(sizeof(OwnedType) == sizeof(LoanedType) && alignof(OwnedType) == alignof(LoanedType), + "Loaned and owned classes must have the same layout"); + static_assert(sizeof(T) == sizeof(LoanedType) && alignof(T) == alignof(LoanedType), + "Loaned and Target classes must have the same layout"); + static_assert(std::is_base_of_v, T>, "Target class should be derived from an owned class"); + const OwnedType* o = reinterpret_cast(loaned_c_obj); + const zenoh::Owned* o_cpp = reinterpret_cast*>(o); + return *reinterpret_cast(o_cpp); +} + +/// @brief Get C++ representation of owned Zenoh-c struct, from its loaned ptr. +template ::type // SFINAE here if no loaned type + // declared + > +auto& as_owned_cpp_ref(LoanedType* loaned_c_obj) { + static_assert(sizeof(OwnedType) == sizeof(LoanedType) && alignof(OwnedType) == alignof(LoanedType), + "Loaned and owned classes must have the same layout"); + static_assert(sizeof(T) == sizeof(LoanedType) && alignof(T) == alignof(LoanedType), + "Loaned and Target classes must have the same layout"); + static_assert(std::is_base_of_v, T>, "Target class should be derived from an owned class"); + OwnedType* o = reinterpret_cast(loaned_c_obj); + zenoh::Owned* o_cpp = reinterpret_cast*>(o); + return *reinterpret_cast(o_cpp); +} + +/// @brief Get C++ representation of owned Zenoh-c struct. +template +auto& as_owned_cpp_ref(OwnedType* owned_c_obj) { + static_assert(sizeof(T) == sizeof(OwnedType) && alignof(T) == alignof(OwnedType), + "Owned and Target classes must have the same layout"); + static_assert(std::is_base_of_v, T>, "Target class should be derived from an owned class"); + zenoh::Owned* o_cpp = reinterpret_cast*>(owned_c_obj); + return *reinterpret_cast(o_cpp); +} + +/// @brief Get C++ representation of copyable Zenoh-c struct. +template +const auto& as_copyable_cpp_ref(const CopyableType* copyable_c_obj) { + static_assert(sizeof(T) == sizeof(CopyableType) && alignof(T) == alignof(CopyableType), + "Copyable and Target classes must have the same layout"); + static_assert(std::is_base_of_v, T>, + "Target class should be derived from a copyable class"); + const zenoh::Copyable* c_cpp = reinterpret_cast*>(copyable_c_obj); + return *reinterpret_cast(c_cpp); +} + +/// @brief Get C++ representation of copyable Zenoh-c struct. +template +auto& as_copyable_cpp_ref(CopyableType* copyable_c_obj) { + static_assert(sizeof(T) == sizeof(CopyableType) && alignof(T) == alignof(CopyableType), + "Copyable and Target classes must have the same layout"); + static_assert(std::is_base_of_v, T>, + "Target class should be derived from a copyable class"); + zenoh::Copyable* c_cpp = reinterpret_cast*>(copyable_c_obj); + return *reinterpret_cast(c_cpp); +} + +/// @brief Move owned Zenoh C++ object object into Zenoh-c struct. +template +OwnedType move_to_c_obj(Owned&& owned_cpp_obj) { + OwnedType o = *as_owned_c_ptr(owned_cpp_obj); + ::z_internal_null(as_owned_c_ptr(owned_cpp_obj)); + return o; +} + +namespace detail { +template +bool check(const Owned& owned_cpp_obj) { + return ::z_internal_check(*as_owned_c_ptr(owned_cpp_obj)); +} +} // namespace detail + +} // namespace zenoh::interop \ No newline at end of file diff --git a/include/zenoh/api/keyexpr.hxx b/include/zenoh/api/keyexpr.hxx index fb237f82..b4c323b1 100644 --- a/include/zenoh/api/keyexpr.hxx +++ b/include/zenoh/api/keyexpr.hxx @@ -16,6 +16,7 @@ #include "../zenohc.hxx" #include "base.hxx" +#include "interop.hxx" namespace zenoh { class Session; @@ -72,7 +73,7 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// @brief Get underlying key expression string. std::string_view as_string_view() const { ::z_view_string_t s; - ::z_keyexpr_as_view_string(this->loan(), &s); + ::z_keyexpr_as_view_string(interop::as_loaned_c_ptr(*this), &s); return std::string_view(reinterpret_cast(::z_string_data(::z_loan(s))), ::z_string_len(::z_loan(s))); } @@ -81,7 +82,9 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// @param other the ``KeyExpr`` to compare with /// @return ``true`` if current key expression includes ``other``, i.e. contains every key belonging to the /// ``other``. - bool includes(const KeyExpr& other) { return ::z_keyexpr_includes(this->loan(), other.loan()); } + bool includes(const KeyExpr& other) { + return ::z_keyexpr_includes(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(other)); + } /// @brief Construct new key expression by concatenating the current one with a string. /// @param s A string to concatenate with the key expression. @@ -91,7 +94,7 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { KeyExpr concat(std::string_view s, ZResult* err = nullptr) const { KeyExpr k; __ZENOH_RESULT_CHECK( - ::z_keyexpr_concat(&k._0, this->loan(), s.data(), s.size()), err, + ::z_keyexpr_concat(&k._0, interop::as_loaned_c_ptr(*this), s.data(), s.size()), err, std::string("Failed to concatenate KeyExpr: ").append(this->as_string_view()).append(" with ").append(s)); return k; } @@ -102,7 +105,8 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// thrown in case of error. KeyExpr join(const KeyExpr& other, ZResult* err = nullptr) const { KeyExpr k; - __ZENOH_RESULT_CHECK(::z_keyexpr_join(&k._0, this->loan(), other.loan()), err, + __ZENOH_RESULT_CHECK(::z_keyexpr_join(&k._0, interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(other)), + err, std::string("Failed to join KeyExpr: ") .append(this->as_string_view()) .append(" with ") @@ -113,7 +117,9 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// @brief Check if 2 key expressions intersect. /// /// @return true if there is at least one non-empty key that is contained in both key expressions, false otherwise. - bool intersects(const KeyExpr& other) const { return ::z_keyexpr_intersects(this->loan(), other.loan()); } + bool intersects(const KeyExpr& other) const { + return ::z_keyexpr_intersects(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(other)); + } #if defined(UNSTABLE) /// /// Intersection level of 2 key expressions. @@ -127,7 +133,9 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// typedef ::z_keyexpr_intersection_level_t IntersectionLevel; /// @brief Returns the relation between `this` and `other` from `this`'s point of view. - IntersectionLevel relation_to(const KeyExpr& other) { return ::z_keyexpr_relation_to(this->loan(), other.loan()); } + IntersectionLevel relation_to(const KeyExpr& other) { + return ::z_keyexpr_relation_to(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(other)); + } #endif /// @brief Check if the string represents a canonical key expression static bool is_canon(std::string_view s) { return ::z_keyexpr_is_canon(s.data(), s.size()) == Z_OK; } @@ -137,10 +145,7 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// @brief Key expression to string equality relation. /// @param other a string to compare with. /// @return true if the key expression string representation is equal to other, false otherwise. - bool operator==(std::string_view other) const { - if (!(this->internal_check())) return false; - return as_string_view() == other; - } + bool operator==(std::string_view other) const { return as_string_view() == other; } /// @brief Key expression to string inequality relation. /// @param other a string to compare with. @@ -150,10 +155,7 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// @brief Key expression to string equality relation. /// @param other a string to compare with. /// @return true if the key expression string representation is equal to other, false otherwise. - bool operator==(const std::string& other) const { - if (!(this->internal_check())) return false; - return as_string_view() == other; - } + bool operator==(const std::string& other) const { return as_string_view() == other; } /// @brief Key expression to string inequality relation. /// @param other a string to compare with. @@ -163,10 +165,7 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// @brief Key expression to string equality relation. /// @param other a null-terminated string to compare with. /// @return true if the key expression string representation is equal to other, false otherwise. - bool operator==(const char* other) const { - if (!(this->internal_check())) return false; - return as_string_view() == other; - } + bool operator==(const char* other) const { return as_string_view() == other; } /// @brief Key expression to string inequality relation. /// @param other a null-terminated string to compare with. @@ -176,7 +175,9 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// @brief Equality relation. /// @param other a key expression to compare with. /// @return true if both key expressions are equal (i.e. they represent the same set of resources), false otherwise. - bool operator==(const KeyExpr& other) const { return ::z_keyexpr_equals(this->loan(), other.loan()); } + bool operator==(const KeyExpr& other) const { + return ::z_keyexpr_equals(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(other)); + } /// @brief Inequality relation. /// @param other a key expression to compare with. diff --git a/include/zenoh/api/liveliness.hxx b/include/zenoh/api/liveliness.hxx index a98aaa5a..27651d99 100644 --- a/include/zenoh/api/liveliness.hxx +++ b/include/zenoh/api/liveliness.hxx @@ -13,8 +13,8 @@ #pragma once -#include "../detail/interop.hxx" #include "base.hxx" +#include "interop.hxx" namespace zenoh { /// @brief // A liveliness token that can be used to provide the network with information about connectivity to its @@ -33,7 +33,7 @@ class LivelinessToken : public Owned<::zc_owned_liveliness_token_t> { /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. void undeclare(ZResult* err = nullptr) && { - __ZENOH_RESULT_CHECK(::zc_liveliness_undeclare_token(detail::as_moved_c_ptr(*this)), err, + __ZENOH_RESULT_CHECK(::zc_liveliness_undeclare_token(interop::as_moved_c_ptr(*this)), err, "Failed to undeclare liveliness token"); } }; diff --git a/include/zenoh/api/publisher.hxx b/include/zenoh/api/publisher.hxx index 351dde4b..d2ee7290 100644 --- a/include/zenoh/api/publisher.hxx +++ b/include/zenoh/api/publisher.hxx @@ -13,11 +13,11 @@ #pragma once -#include "../detail/interop.hxx" #include "base.hxx" #include "bytes.hxx" #include "encoding.hxx" #include "enums.hxx" +#include "interop.hxx" #include "keyexpr.hxx" #include "timestamp.hxx" #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) @@ -69,17 +69,17 @@ class Publisher : public Owned<::z_owned_publisher_t> { /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. void put(Bytes&& payload, PutOptions&& options = PutOptions::create_default(), ZResult* err = nullptr) const { - auto payload_ptr = detail::as_moved_c_ptr(payload); + auto payload_ptr = interop::as_moved_c_ptr(payload); ::z_publisher_put_options_t opts; z_publisher_put_options_default(&opts); - opts.encoding = detail::as_moved_c_ptr(options.encoding); + opts.encoding = interop::as_moved_c_ptr(options.encoding); #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) - opts.source_info = detail::as_moved_c_ptr(options.source_info); + opts.source_info = interop::as_moved_c_ptr(options.source_info); #endif - opts.attachment = detail::as_moved_c_ptr(options.attachment); - opts.timestamp = detail::as_copyable_c_ptr(options.timestamp); + opts.attachment = interop::as_moved_c_ptr(options.attachment); + opts.timestamp = interop::as_copyable_c_ptr(options.timestamp); - __ZENOH_RESULT_CHECK(::z_publisher_put(this->loan(), payload_ptr, &opts), err, + __ZENOH_RESULT_CHECK(::z_publisher_put(interop::as_loaned_c_ptr(*this), payload_ptr, &opts), err, "Failed to perform put operation"); } @@ -90,8 +90,8 @@ class Publisher : public Owned<::z_owned_publisher_t> { void delete_resource(DeleteOptions&& options = DeleteOptions::create_default(), ZResult* err = nullptr) const { ::z_publisher_delete_options_t opts; z_publisher_delete_options_default(&opts); - opts.timestamp = detail::as_copyable_c_ptr(options.timestamp); - __ZENOH_RESULT_CHECK(::z_publisher_delete(this->loan(), &opts), err, + opts.timestamp = interop::as_copyable_c_ptr(options.timestamp); + __ZENOH_RESULT_CHECK(::z_publisher_delete(interop::as_loaned_c_ptr(*this), &opts), err, "Failed to perform delete_resource operation"); } @@ -99,13 +99,13 @@ class Publisher : public Owned<::z_owned_publisher_t> { /// @brief Get the key expression of the publisher. /// @note zenoh-c only. const KeyExpr& get_keyexpr() const { - return detail::as_owned_cpp_obj(::z_publisher_keyexpr(this->loan())); + return interop::as_owned_cpp_ref(::z_publisher_keyexpr(interop::as_loaned_c_ptr(*this))); } #endif #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) /// @brief Get the id of the publisher. /// @return id of this publisher. - EntityGlobalId get_id() const { return EntityGlobalId(::z_publisher_id(this->loan())); } + EntityGlobalId get_id() const { return EntityGlobalId(::z_publisher_id(interop::as_loaned_c_ptr(*this))); } #endif }; } // namespace zenoh diff --git a/include/zenoh/api/query.hxx b/include/zenoh/api/query.hxx index 784341cc..0ee44120 100644 --- a/include/zenoh/api/query.hxx +++ b/include/zenoh/api/query.hxx @@ -17,11 +17,11 @@ #include #include -#include "../detail/interop.hxx" #include "base.hxx" #include "bytes.hxx" #include "encoding.hxx" #include "enums.hxx" +#include "interop.hxx" #include "keyexpr.hxx" #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) #include "source_info.hxx" @@ -38,7 +38,9 @@ class Query : public Owned<::z_owned_query_t> { /// @brief Get the key expression of the query. /// @return ``KeyExpr`` of the query. - const KeyExpr& get_keyexpr() const { return detail::as_owned_cpp_obj(::z_query_keyexpr(this->loan())); } + const KeyExpr& get_keyexpr() const { + return interop::as_owned_cpp_ref(::z_query_keyexpr(interop::as_loaned_c_ptr(*this))); + } /// @brief Get query parameters. See Selector for more information. @@ -46,34 +48,34 @@ class Query : public Owned<::z_owned_query_t> { /// @return parameters string. std::string_view get_parameters() const { ::z_view_string_t p; - ::z_query_parameters(this->loan(), &p); + ::z_query_parameters(interop::as_loaned_c_ptr(*this), &p); return std::string_view(::z_string_data(::z_loan(p)), ::z_string_len(::z_loan(p))); } /// @brief Get the payload of the query. /// @return payload of the query. std::optional> get_payload() const { - auto payload = ::z_query_payload(this->loan()); + auto payload = ::z_query_payload(interop::as_loaned_c_ptr(*this)); if (payload == nullptr) return {}; - return std::cref(detail::as_owned_cpp_obj(payload)); + return std::cref(interop::as_owned_cpp_ref(payload)); } /// @brief Get the encoding of the query. /// @return encoding of the query. std::optional> get_encoding() const { - auto encoding = ::z_query_encoding(this->loan()); + auto encoding = ::z_query_encoding(interop::as_loaned_c_ptr(*this)); if (encoding == nullptr) return {}; - return std::cref(detail::as_owned_cpp_obj(::z_query_encoding(this->loan()))); + return std::cref(interop::as_owned_cpp_ref(::z_query_encoding(interop::as_loaned_c_ptr(*this)))); } /// @brief Get the attachment of the query. /// @return attachment of the query. std::optional> get_attachment() const { - auto attachment = ::z_query_attachment(this->loan()); + auto attachment = ::z_query_attachment(interop::as_loaned_c_ptr(*this)); if (attachment == nullptr) { return {}; } - return std::cref(detail::as_owned_cpp_obj(attachment)); + return std::cref(interop::as_owned_cpp_ref(attachment)); } /// @brief Options passed to the ``Query::reply`` operation. @@ -109,21 +111,22 @@ class Query : public Owned<::z_owned_query_t> { /// thrown in case of error. void reply(const KeyExpr& key_expr, Bytes&& payload, ReplyOptions&& options = ReplyOptions::create_default(), ZResult* err = nullptr) const { - auto payload_ptr = detail::as_moved_c_ptr(payload); + auto payload_ptr = interop::as_moved_c_ptr(payload); ::z_query_reply_options_t opts; z_query_reply_options_default(&opts); - opts.encoding = detail::as_moved_c_ptr(options.encoding); + opts.encoding = interop::as_moved_c_ptr(options.encoding); opts.priority = options.priority; opts.congestion_control = options.congestion_control; opts.is_express = options.is_express; - opts.timestamp = detail::as_copyable_c_ptr(options.timestamp); + opts.timestamp = interop::as_copyable_c_ptr(options.timestamp); #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) - opts.source_info = detail::as_moved_c_ptr(options.source_info); + opts.source_info = interop::as_moved_c_ptr(options.source_info); #endif - opts.attachment = detail::as_moved_c_ptr(options.attachment); + opts.attachment = interop::as_moved_c_ptr(options.attachment); - __ZENOH_RESULT_CHECK(::z_query_reply(this->loan(), detail::as_loaned_c_ptr(key_expr), payload_ptr, &opts), err, - "Failed to send reply"); + __ZENOH_RESULT_CHECK( + ::z_query_reply(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), payload_ptr, &opts), + err, "Failed to send reply"); } /// @brief Options passed to the ``Query::reply_err`` operation. @@ -145,12 +148,13 @@ class Query : public Owned<::z_owned_query_t> { /// thrown in case of error. void reply_err(Bytes&& payload, ReplyErrOptions&& options = ReplyErrOptions::create_default(), ZResult* err = nullptr) const { - auto payload_ptr = detail::as_moved_c_ptr(payload); + auto payload_ptr = interop::as_moved_c_ptr(payload); ::z_query_reply_err_options_t opts; z_query_reply_err_options_default(&opts); - opts.encoding = detail::as_moved_c_ptr(options.encoding); + opts.encoding = interop::as_moved_c_ptr(options.encoding); - __ZENOH_RESULT_CHECK(::z_query_reply_err(this->loan(), payload_ptr, &opts), err, "Failed to send reply error"); + __ZENOH_RESULT_CHECK(::z_query_reply_err(interop::as_loaned_c_ptr(*this), payload_ptr, &opts), err, + "Failed to send reply error"); } /// @brief Options passed to the ``Query::reply_del`` operation. @@ -190,14 +194,15 @@ class Query : public Owned<::z_owned_query_t> { opts.priority = options.priority; opts.congestion_control = options.congestion_control; opts.is_express = options.is_express; - opts.timestamp = detail::as_copyable_c_ptr(options.timestamp); + opts.timestamp = interop::as_copyable_c_ptr(options.timestamp); #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) - opts.source_info = detail::as_moved_c_ptr(options.source_info); + opts.source_info = interop::as_moved_c_ptr(options.source_info); #endif - opts.attachment = detail::as_moved_c_ptr(options.attachment); + opts.attachment = interop::as_moved_c_ptr(options.attachment); - __ZENOH_RESULT_CHECK(::z_query_reply_del(this->loan(), detail::as_loaned_c_ptr(key_expr), &opts), err, - "Failed to send reply del"); + __ZENOH_RESULT_CHECK( + ::z_query_reply_del(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), &opts), err, + "Failed to send reply del"); } /// @brief Construct a a shallow copy of this Query. @@ -205,7 +210,7 @@ class Query : public Owned<::z_owned_query_t> { /// The query responses will be sent only when the last clone is destroyed. Query clone() const { Query q(nullptr); - ::z_query_clone(&q._0, this->loan()); + ::z_query_clone(&q._0, interop::as_loaned_c_ptr(*this)); return q; }; }; diff --git a/include/zenoh/api/reply.hxx b/include/zenoh/api/reply.hxx index f35f66d3..4f705337 100644 --- a/include/zenoh/api/reply.hxx +++ b/include/zenoh/api/reply.hxx @@ -13,9 +13,9 @@ #pragma once -#include "../detail/interop.hxx" #include "base.hxx" #include "bytes.hxx" +#include "interop.hxx" #include "sample.hxx" #if defined UNSTABLE #include "id.hxx" @@ -31,12 +31,14 @@ class ReplyError : public Owned<::z_owned_reply_err_t> { /// @brief The payload of this error. /// @return Error payload. - const Bytes& get_payload() const { return detail::as_owned_cpp_obj(::z_reply_err_payload(this->loan())); } + const Bytes& get_payload() const { + return interop::as_owned_cpp_ref(::z_reply_err_payload(interop::as_loaned_c_ptr(*this))); + } /// @brief The encoding of this error. /// @return Error encoding. const Encoding& get_encoding() const { - return detail::as_owned_cpp_obj(::z_reply_err_encoding(this->loan())); + return interop::as_owned_cpp_ref(::z_reply_err_encoding(interop::as_loaned_c_ptr(*this))); } }; @@ -49,24 +51,24 @@ class Reply : public Owned<::z_owned_reply_t> { /// @brief Check if the reply is OK (and contains a sample). /// @return ``true`` if the reply is OK, ``false`` if contains a error. - bool is_ok() const { return ::z_reply_is_ok(this->loan()); } + bool is_ok() const { return ::z_reply_is_ok(interop::as_loaned_c_ptr(*this)); } /// @brief Get the reply sample. Will throw a ZException if ``Reply::is_ok`` returns ``false``. /// @return Reply sample. const Sample& get_ok() const { - if (!::z_reply_is_ok(this->loan())) { + if (!::z_reply_is_ok(interop::as_loaned_c_ptr(*this))) { throw ZException("Reply data sample was requested, but reply contains error", Z_EINVAL); } - return detail::as_owned_cpp_obj(::z_reply_ok(this->loan())); + return interop::as_owned_cpp_ref(::z_reply_ok(interop::as_loaned_c_ptr(*this))); } /// @brief Get the reply error. Will throw a ZException if ``Reply::is_ok`` returns ``true``. /// @return Reply error. const ReplyError& get_err() const { - if (::z_reply_is_ok(this->loan())) { + if (::z_reply_is_ok(interop::as_loaned_c_ptr(*this))) { throw ZException("Reply error was requested, but reply contains data sample", Z_EINVAL); } - return detail::as_owned_cpp_obj(::z_reply_err(this->loan())); + return interop::as_owned_cpp_ref(::z_reply_err(interop::as_loaned_c_ptr(*this))); } #if defined(UNSTABLE) @@ -74,7 +76,7 @@ class Reply : public Owned<::z_owned_reply_t> { /// @return Zenoh instance id, or an empty optional if the id was not set. std::optional get_replier_id() const { ::z_id_t z_id; - if (::z_reply_replier_id(this->loan(), &z_id)) { + if (::z_reply_replier_id(interop::as_loaned_c_ptr(*this), &z_id)) { return Id(z_id); } return {}; diff --git a/include/zenoh/api/sample.hxx b/include/zenoh/api/sample.hxx index c74ffb10..beb67421 100644 --- a/include/zenoh/api/sample.hxx +++ b/include/zenoh/api/sample.hxx @@ -15,11 +15,11 @@ #include -#include "../detail/interop.hxx" #include "base.hxx" #include "bytes.hxx" #include "encoding.hxx" #include "enums.hxx" +#include "interop.hxx" #include "keyexpr.hxx" #include "timestamp.hxx" #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) @@ -38,61 +38,67 @@ class Sample : public Owned<::z_owned_sample_t> { /// @brief Get the resource key of this sample. /// @return ``KeyExpr`` object representing the resource key. - const KeyExpr& get_keyexpr() const { return detail::as_owned_cpp_obj(::z_sample_keyexpr(this->loan())); } + const KeyExpr& get_keyexpr() const { + return interop::as_owned_cpp_ref(::z_sample_keyexpr(interop::as_loaned_c_ptr(*this))); + } /// @brief Get the data of this sample. /// @return ``Bytes`` object representing the sample payload. - const Bytes& get_payload() const { return detail::as_owned_cpp_obj(::z_sample_payload(this->loan())); } + const Bytes& get_payload() const { + return interop::as_owned_cpp_ref(::z_sample_payload(interop::as_loaned_c_ptr(*this))); + } /// @brief Get the encoding of the data of this sample. /// @return ``Encoding`` object. const Encoding& get_encoding() const { - return detail::as_owned_cpp_obj(::z_sample_encoding(this->loan())); + return interop::as_owned_cpp_ref(::z_sample_encoding(interop::as_loaned_c_ptr(*this))); } /// @brief Get the kind of this sample. /// @return ``zenoh::SampleKind`` value (PUT or DELETE). - SampleKind get_kind() const { return ::z_sample_kind(this->loan()); } + SampleKind get_kind() const { return ::z_sample_kind(interop::as_loaned_c_ptr(*this)); } /// @brief Get the attachment of this sample. /// @return ``Bytes`` object representing sample attachment. std::optional> get_attachment() const { - auto attachment = ::z_sample_attachment(this->loan()); + auto attachment = ::z_sample_attachment(interop::as_loaned_c_ptr(*this)); if (attachment == nullptr) return {}; - return std::cref(detail::as_owned_cpp_obj(attachment)); + return std::cref(interop::as_owned_cpp_ref(attachment)); } /// @brief Get the timestamp of this sample. /// @return ``Timestamp`` object. std::optional get_timestamp() const { - const ::z_timestamp_t* t = ::z_sample_timestamp(this->loan()); + const ::z_timestamp_t* t = ::z_sample_timestamp(interop::as_loaned_c_ptr(*this)); if (t == nullptr) { return {}; } - return detail::as_copyable_cpp_obj(t); + return interop::as_copyable_cpp_ref(t); } /// @brief Get the priority this sample was sent with. /// @return ``Priority`` value. - Priority get_priority() const { return ::z_sample_priority(this->loan()); } + Priority get_priority() const { return ::z_sample_priority(interop::as_loaned_c_ptr(*this)); } /// @brief Get the congestion control setting this sample was sent with. /// @return ``CongestionControl`` value. - CongestionControl get_congestion_control() const { return ::z_sample_congestion_control(this->loan()); } + CongestionControl get_congestion_control() const { + return ::z_sample_congestion_control(interop::as_loaned_c_ptr(*this)); + } /// @brief Get the express setting this sample was sent with. /// @return ``CongestionControl`` value. - bool get_express() const { return ::z_sample_express(this->loan()); } + bool get_express() const { return ::z_sample_express(interop::as_loaned_c_ptr(*this)); } #if defined(ZENOHCXX) && defined(UNSTABLE) /// @brief Get the source info of this sample. const SourceInfo& get_source_info() const { - return detail::as_owned_cpp_obj(::z_sample_source_info(this->loan())); + return interop::as_owned_cpp_ref(::z_sample_source_info(interop::as_loaned_c_ptr(*this))); } #endif /// @brief Construct a shallow copy of this sample. Sample clone() const { Sample s(nullptr); - ::z_sample_clone(&s._0, this->loan()); + ::z_sample_clone(&s._0, interop::as_loaned_c_ptr(*this)); return s; }; }; diff --git a/include/zenoh/api/scout.hxx b/include/zenoh/api/scout.hxx index 601267cc..901b9b9c 100644 --- a/include/zenoh/api/scout.hxx +++ b/include/zenoh/api/scout.hxx @@ -3,10 +3,10 @@ #pragma once #include "../detail/closures_concrete.hxx" -#include "../detail/interop.hxx" #include "base.hxx" #include "config.hxx" #include "enums.hxx" +#include "interop.hxx" namespace zenoh { @@ -46,7 +46,7 @@ void scout(Config&& config, C&& on_hello, D&& on_drop, ScoutOptions&& options = opts.timeout_ms = options.timeout_ms; opts.what = options.what; - __ZENOH_RESULT_CHECK(::z_scout(detail::as_moved_c_ptr(config), ::z_move(c_closure), &opts), err, + __ZENOH_RESULT_CHECK(::z_scout(interop::as_moved_c_ptr(config), ::z_move(c_closure), &opts), err, "Failed to perform scout operation"); } diff --git a/include/zenoh/api/session.hxx b/include/zenoh/api/session.hxx index e27820d8..bda14fb2 100644 --- a/include/zenoh/api/session.hxx +++ b/include/zenoh/api/session.hxx @@ -17,11 +17,11 @@ #include #include "../detail/closures_concrete.hxx" -#include "../detail/interop.hxx" #include "base.hxx" #include "closures.hxx" #include "config.hxx" #include "enums.hxx" +#include "interop.hxx" #include "timestamp.hxx" #if defined UNSTABLE #include "id.hxx" @@ -66,7 +66,7 @@ class Session : public Owned<::z_owned_session_t> { /// thrown in case of error. Session(Config&& config, SessionOptions&& options = SessionOptions::create_default(), ZResult* err = nullptr) : Owned(nullptr) { - __ZENOH_RESULT_CHECK(::z_open(&this->_0, detail::as_moved_c_ptr(config)), err, "Failed to open session"); + __ZENOH_RESULT_CHECK(::z_open(&this->_0, interop::as_moved_c_ptr(config)), err, "Failed to open session"); #ifdef ZENOHCXX_ZENOHPICO if (err != nullptr && *err != Z_OK) return; if (options.start_background_tasks) { @@ -91,8 +91,8 @@ class Session : public Owned<::z_owned_session_t> { /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. Session(Config&& config, const ShmClientStorage& shm_storage, ZResult* err = nullptr) : Owned(nullptr) { - __ZENOH_RESULT_CHECK(::z_open_with_custom_shm_clients(&this->_0, detail::as_moved_c_ptr(config), - detail::as_loaned_c_ptr(shm_storage)), + __ZENOH_RESULT_CHECK(::z_open_with_custom_shm_clients(&this->_0, interop::as_moved_c_ptr(config), + interop::as_loaned_c_ptr(shm_storage)), err, "Failed to open session"); } #endif @@ -126,14 +126,14 @@ class Session : public Owned<::z_owned_session_t> { /// @return a new ``Session`` instance. Session clone() const { Session s(nullptr); - ::z_session_clone(&s._0, this->loan()); + ::z_session_clone(&s._0, interop::as_loaned_c_ptr(*this)); return s; } #if defined UNSTABLE /// @brief Get the unique identifier of the zenoh node associated to this ``Session``. /// @return the unique identifier ``Id``. - Id get_zid() const { return Id(::z_info_zid(this->loan())); } + Id get_zid() const { return Id(::z_info_zid(interop::as_loaned_c_ptr(*this))); } #endif /// @brief Create ``KeyExpr`` instance with numeric id registered in ``Session`` routing tables (to reduce bandwith @@ -144,9 +144,9 @@ class Session : public Owned<::z_owned_session_t> { /// @return Declared ``KeyExpr`` instance. KeyExpr declare_keyexpr(const KeyExpr& key_expr, ZResult* err = nullptr) const { KeyExpr k; - __ZENOH_RESULT_CHECK( - ::z_declare_keyexpr(detail::as_owned_c_ptr(k), this->loan(), detail::as_loaned_c_ptr(key_expr)), err, - std::string("Failed to declare key expression: ").append(k.as_string_view())); + __ZENOH_RESULT_CHECK(::z_declare_keyexpr(interop::as_owned_c_ptr(k), interop::as_loaned_c_ptr(*this), + interop::as_loaned_c_ptr(key_expr)), + err, std::string("Failed to declare key expression: ").append(k.as_string_view())); return k; } @@ -155,8 +155,8 @@ class Session : public Owned<::z_owned_session_t> { /// thrown in case of error. /// @param key_expr ``KeyExpr`` instance to undeclare, that was previously returned by ``Session::declare_keyexpr``. void undeclare_keyexpr(KeyExpr&& key_expr, ZResult* err = nullptr) const { - __ZENOH_RESULT_CHECK(::z_undeclare_keyexpr(detail::as_moved_c_ptr(key_expr), this->loan()), err, - "Failed to undeclare key expression"); + __ZENOH_RESULT_CHECK(::z_undeclare_keyexpr(interop::as_moved_c_ptr(key_expr), interop::as_loaned_c_ptr(*this)), + err, "Failed to undeclare key expression"); } /// @brief Options passed to the ``get`` operation. @@ -220,17 +220,17 @@ class Session : public Owned<::z_owned_session_t> { opts.congestion_control = options.congestion_control; opts.priority = options.priority; opts.is_express = options.is_express; - opts.payload = detail::as_moved_c_ptr(options.payload); - opts.encoding = detail::as_moved_c_ptr(options.encoding); + opts.payload = interop::as_moved_c_ptr(options.payload); + opts.encoding = interop::as_moved_c_ptr(options.encoding); #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) - opts.source_info = detail::as_moved_c_ptr(options.source_info); + opts.source_info = interop::as_moved_c_ptr(options.source_info); #endif - opts.attachment = detail::as_moved_c_ptr(options.attachment); + opts.attachment = interop::as_moved_c_ptr(options.attachment); opts.timeout_ms = options.timeout_ms; - __ZENOH_RESULT_CHECK( - ::z_get(this->loan(), detail::as_loaned_c_ptr(key_expr), parameters.c_str(), ::z_move(c_closure), &opts), - err, "Failed to perform get operation"); + __ZENOH_RESULT_CHECK(::z_get(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), + parameters.c_str(), ::z_move(c_closure), &opts), + err, "Failed to perform get operation"); } /// @brief Query data from the matching queryables in the system. Replies are provided through a channel. @@ -253,18 +253,18 @@ class Session : public Owned<::z_owned_session_t> { z_get_options_default(&opts); opts.target = options.target; opts.consolidation = static_cast(options.consolidation); - opts.payload = detail::as_moved_c_ptr(options.payload); - opts.encoding = detail::as_moved_c_ptr(options.encoding); + opts.payload = interop::as_moved_c_ptr(options.payload); + opts.encoding = interop::as_moved_c_ptr(options.encoding); #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) - opts.source_info = detail::as_moved_c_ptr(options.source_info); + opts.source_info = interop::as_moved_c_ptr(options.source_info); #endif - opts.attachment = detail::as_moved_c_ptr(options.attachment); + opts.attachment = interop::as_moved_c_ptr(options.attachment); opts.timeout_ms = options.timeout_ms; - ZResult res = ::z_get(this->loan(), detail::as_loaned_c_ptr(key_expr), parameters.c_str(), + ZResult res = ::z_get(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), parameters.c_str(), ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to perform get operation"); - if (res != Z_OK) ::z_drop(detail::as_moved_c_ptr(cb_handler_pair.second)); + if (res != Z_OK) ::z_drop(interop::as_moved_c_ptr(cb_handler_pair.second)); return std::move(cb_handler_pair.second); } /// @brief Options to be passed to ``delete_resource`` operation @@ -298,8 +298,8 @@ class Session : public Owned<::z_owned_session_t> { opts.priority = options.priority; opts.is_express = options.is_express; - __ZENOH_RESULT_CHECK(::z_delete(this->loan(), detail::as_loaned_c_ptr(key_expr), &opts), err, - "Failed to perform delete operation"); + __ZENOH_RESULT_CHECK(::z_delete(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), &opts), + err, "Failed to perform delete operation"); } /// @brief Options passed to the ``Session::put`` operation. @@ -342,21 +342,22 @@ class Session : public Owned<::z_owned_session_t> { ZResult* err = nullptr) const { ::z_put_options_t opts; z_put_options_default(&opts); - opts.encoding = detail::as_moved_c_ptr(options.encoding); + opts.encoding = interop::as_moved_c_ptr(options.encoding); opts.congestion_control = options.congestion_control; opts.priority = options.priority; opts.is_express = options.is_express; #if defined(UNSTABLE) - opts.source_info = detail::as_moved_c_ptr(options.source_info); + opts.source_info = interop::as_moved_c_ptr(options.source_info); #endif - opts.attachment = detail::as_moved_c_ptr(options.attachment); - opts.timestamp = detail::as_copyable_c_ptr(options.timestamp); + opts.attachment = interop::as_moved_c_ptr(options.attachment); + opts.timestamp = interop::as_copyable_c_ptr(options.timestamp); #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) opts.allowed_destination = options.allowed_destination; #endif - auto payload_ptr = detail::as_moved_c_ptr(payload); - __ZENOH_RESULT_CHECK(::z_put(this->loan(), detail::as_loaned_c_ptr(key_expr), payload_ptr, &opts), err, - "Failed to perform put operation"); + auto payload_ptr = interop::as_moved_c_ptr(payload); + __ZENOH_RESULT_CHECK( + ::z_put(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), payload_ptr, &opts), err, + "Failed to perform put operation"); } /// @brief Options to be passed when declaring a ``Queryable`` @@ -399,8 +400,8 @@ class Session : public Owned<::z_owned_session_t> { opts.complete = options.complete; Queryable q(nullptr); - ZResult res = ::z_declare_queryable(detail::as_owned_c_ptr(q), this->loan(), detail::as_loaned_c_ptr(key_expr), - ::z_move(c_closure), &opts); + ZResult res = ::z_declare_queryable(interop::as_owned_c_ptr(q), interop::as_loaned_c_ptr(*this), + interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Queryable"); return q; } @@ -424,10 +425,10 @@ class Session : public Owned<::z_owned_session_t> { opts.complete = options.complete; QueryableBase q(nullptr); - ZResult res = ::z_declare_queryable(detail::as_owned_c_ptr(q), this->loan(), detail::as_loaned_c_ptr(key_expr), - ::z_move(cb_handler_pair.first), &opts); + ZResult res = ::z_declare_queryable(interop::as_owned_c_ptr(q), interop::as_loaned_c_ptr(*this), + interop::as_loaned_c_ptr(key_expr), ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Queryable"); - if (res != Z_OK) ::z_drop(detail::as_moved_c_ptr(cb_handler_pair.second)); + if (res != Z_OK) ::z_drop(interop::as_moved_c_ptr(cb_handler_pair.second)); return Queryable>(std::move(q), std::move(cb_handler_pair.second)); } @@ -478,8 +479,8 @@ class Session : public Owned<::z_owned_session_t> { (void)options; #endif Subscriber s(nullptr); - ZResult res = ::z_declare_subscriber(detail::as_owned_c_ptr(s), this->loan(), detail::as_loaned_c_ptr(key_expr), - ::z_move(c_closure), &opts); + ZResult res = ::z_declare_subscriber(interop::as_owned_c_ptr(s), interop::as_loaned_c_ptr(*this), + interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Subscriber"); return s; } @@ -507,10 +508,11 @@ class Session : public Owned<::z_owned_session_t> { (void)options; #endif SubscriberBase s(nullptr); - ZResult res = ::z_declare_subscriber(detail::as_owned_c_ptr(s), this->loan(), detail::as_loaned_c_ptr(key_expr), - ::z_move(cb_handler_pair.first), &opts); + ZResult res = + ::z_declare_subscriber(interop::as_owned_c_ptr(s), interop::as_loaned_c_ptr(*this), + interop::as_loaned_c_ptr(key_expr), ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Subscriber"); - if (res != Z_OK) ::z_drop(detail::as_moved_c_ptr(cb_handler_pair.second)); + if (res != Z_OK) ::z_drop(interop::as_moved_c_ptr(cb_handler_pair.second)); return Subscriber>(std::move(s), std::move(cb_handler_pair.second)); } @@ -554,11 +556,11 @@ class Session : public Owned<::z_owned_session_t> { #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) opts.allowed_destination = options.allowed_destination; #endif - opts.encoding = detail::as_moved_c_ptr(options.encoding); + opts.encoding = interop::as_moved_c_ptr(options.encoding); Publisher p(nullptr); - ZResult res = - ::z_declare_publisher(detail::as_owned_c_ptr(p), this->loan(), detail::as_loaned_c_ptr(key_expr), &opts); + ZResult res = ::z_declare_publisher(interop::as_owned_c_ptr(p), interop::as_loaned_c_ptr(*this), + interop::as_loaned_c_ptr(key_expr), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Publisher"); return p; } @@ -576,7 +578,7 @@ class Session : public Owned<::z_owned_session_t> { using ClosureType = typename detail::closures::Closure; auto closure = ClosureType::into_context(std::forward(f), closures::none); ::z_closure(&c_closure, detail::closures::_zenoh_on_id_call, detail::closures::_zenoh_on_drop, closure); - __ZENOH_RESULT_CHECK(::z_info_routers_zid(this->loan(), ::z_move(c_closure)), err, + __ZENOH_RESULT_CHECK(::z_info_routers_zid(interop::as_loaned_c_ptr(*this), ::z_move(c_closure)), err, "Failed to fetch router Ids"); return out; } @@ -593,7 +595,8 @@ class Session : public Owned<::z_owned_session_t> { auto closure = detail::closures::Closure::into_context(std::forward(f), closures::none); ::z_closure(&c_closure, detail::closures::_zenoh_on_id_call, detail::closures::_zenoh_on_drop, closure); - __ZENOH_RESULT_CHECK(::z_info_peers_zid(this->loan(), ::z_move(c_closure)), err, "Failed to fetch peer Ids"); + __ZENOH_RESULT_CHECK(::z_info_peers_zid(interop::as_loaned_c_ptr(*this), ::z_move(c_closure)), err, + "Failed to fetch peer Ids"); return out; } #endif @@ -604,7 +607,8 @@ class Session : public Owned<::z_owned_session_t> { /// thrown in case of error. /// @note zenoh-pico only void start_read_task(ZResult* err = nullptr) { - __ZENOH_RESULT_CHECK(zp_start_read_task(this->loan(), nullptr), err, "Failed to start read task"); + __ZENOH_RESULT_CHECK(zp_start_read_task(interop::as_loaned_c_ptr(*this), nullptr), err, + "Failed to start read task"); } /// @brief Stop the read task. @@ -612,7 +616,7 @@ class Session : public Owned<::z_owned_session_t> { /// thrown in case of error. /// @note zenoh-pico only void stop_read_task(ZResult* err = nullptr) { - __ZENOH_RESULT_CHECK(zp_stop_read_task(this->loan()), err, "Failed to stop read task"); + __ZENOH_RESULT_CHECK(zp_stop_read_task(interop::as_loaned_c_ptr(*this)), err, "Failed to stop read task"); } /// @brief Start a separate task to handle the session lease. This task will send KeepAlive messages when needed @@ -622,7 +626,8 @@ class Session : public Owned<::z_owned_session_t> { /// thrown in case of error. /// @note zenoh-pico only void start_lease_task(ZResult* err = nullptr) { - __ZENOH_RESULT_CHECK(zp_start_lease_task(this->loan(), NULL), err, "Failed to start lease task"); + __ZENOH_RESULT_CHECK(zp_start_lease_task(interop::as_loaned_c_ptr(*this), NULL), err, + "Failed to start lease task"); } /// @brief Stop the lease task. @@ -630,7 +635,7 @@ class Session : public Owned<::z_owned_session_t> { /// thrown in case of error. /// @note zenoh-pico only void stop_lease_task(ZResult* err = nullptr) { - __ZENOH_RESULT_CHECK(zp_stop_lease_task(this->loan()), err, "Failed to stop lease task"); + __ZENOH_RESULT_CHECK(zp_stop_lease_task(interop::as_loaned_c_ptr(*this)), err, "Failed to stop lease task"); } /// @brief Triggers a single execution of reading procedure from the network and processes of any received the @@ -639,7 +644,7 @@ class Session : public Owned<::z_owned_session_t> { /// thrown in case of error. /// @note zenoh-pico only void read(ZResult* err = nullptr) { - __ZENOH_RESULT_CHECK(zp_read(this->loan(), nullptr), err, "Failed to perform read"); + __ZENOH_RESULT_CHECK(zp_read(interop::as_loaned_c_ptr(*this), nullptr), err, "Failed to perform read"); } /// @brief Triggers a single execution of keep alive procedure. It will send KeepAlive messages when needed and @@ -647,14 +652,16 @@ class Session : public Owned<::z_owned_session_t> { /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. void send_keep_alive(ZResult* err = nullptr) { - __ZENOH_RESULT_CHECK(zp_send_keep_alive(this->loan(), nullptr), err, "Failed to perform send_keep_alive"); + __ZENOH_RESULT_CHECK(zp_send_keep_alive(interop::as_loaned_c_ptr(*this), nullptr), err, + "Failed to perform send_keep_alive"); } /// @brief Triggers a single execution of join procedure: send the Join message. /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. void send_join(ZResult* err = nullptr) { - __ZENOH_RESULT_CHECK(zp_send_join(this->loan(), nullptr), err, "Failed to perform send_join"); + __ZENOH_RESULT_CHECK(zp_send_join(interop::as_loaned_c_ptr(*this), nullptr), err, + "Failed to perform send_join"); } #endif @@ -686,8 +693,8 @@ class Session : public Owned<::z_owned_session_t> { ::zc_liveliness_declaration_options_t opts; zc_liveliness_declaration_options_default(&opts); (void)options; - __ZENOH_RESULT_CHECK(::zc_liveliness_declare_token(detail::as_owned_c_ptr(t), this->loan(), - detail::as_loaned_c_ptr(key_expr), &opts), + __ZENOH_RESULT_CHECK(::zc_liveliness_declare_token(interop::as_owned_c_ptr(t), interop::as_loaned_c_ptr(*this), + interop::as_loaned_c_ptr(key_expr), &opts), err, "Failed to perform liveliness_declare_token operation"); return t; } @@ -731,8 +738,9 @@ class Session : public Owned<::z_owned_session_t> { zc_liveliness_subscriber_options_default(&opts); (void)options; Subscriber s(nullptr); - ZResult res = ::zc_liveliness_declare_subscriber(detail::as_owned_c_ptr(s), this->loan(), - detail::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts); + ZResult res = + ::zc_liveliness_declare_subscriber(interop::as_owned_c_ptr(s), interop::as_loaned_c_ptr(*this), + interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Liveliness Token Subscriber"); return s; } @@ -756,11 +764,11 @@ class Session : public Owned<::z_owned_session_t> { zc_liveliness_subscriber_options_default(&opts); (void)options; SubscriberBase s(nullptr); - ZResult res = ::zc_liveliness_declare_subscriber(detail::as_owned_c_ptr(s), this->loan(), - detail::as_loaned_c_ptr(key_expr), + ZResult res = ::zc_liveliness_declare_subscriber(interop::as_owned_c_ptr(s), interop::as_loaned_c_ptr(*this), + interop::as_loaned_c_ptr(key_expr), ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Liveliness Token Subscriber"); - if (res != Z_OK) ::z_drop(::z_move(*detail::as_moved_c_ptr(cb_handler_pair.second))); + if (res != Z_OK) ::z_drop(::z_move(*interop::as_moved_c_ptr(cb_handler_pair.second))); return Subscriber>(std::move(s), std::move(cb_handler_pair.second)); } @@ -804,9 +812,9 @@ class Session : public Owned<::z_owned_session_t> { zc_liveliness_get_options_default(&opts); opts.timeout_ms = options.timeout_ms; - __ZENOH_RESULT_CHECK( - ::zc_liveliness_get(this->loan(), detail::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts), err, - "Failed to perform liveliness_get operation"); + __ZENOH_RESULT_CHECK(::zc_liveliness_get(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), + ::z_move(c_closure), &opts), + err, "Failed to perform liveliness_get operation"); } /// @brief Queries liveliness tokens currently on the network with a key expression intersecting with `key_expr`. @@ -827,10 +835,10 @@ class Session : public Owned<::z_owned_session_t> { zc_liveliness_get_options_default(&opts); opts.timeout_ms = options.timeout_ms; - ZResult res = ::zc_liveliness_get(this->loan(), detail::as_loaned_c_ptr(key_expr), + ZResult res = ::zc_liveliness_get(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to perform liveliness_get operation"); - if (res != Z_OK) ::z_drop(detail::as_moved_c_ptr(cb_handler_pair.second)); + if (res != Z_OK) ::z_drop(interop::as_moved_c_ptr(cb_handler_pair.second)); return std::move(cb_handler_pair.second); } @@ -840,7 +848,7 @@ class Session : public Owned<::z_owned_session_t> { /// thrown in case of error. Timestamp new_timestamp(ZResult* err = nullptr) { ::z_timestamp_t t; - __ZENOH_RESULT_CHECK(z_timestamp_new(&t, this->loan()), err, "Failed to create timestamp"); + __ZENOH_RESULT_CHECK(z_timestamp_new(&t, interop::as_loaned_c_ptr(*this)), err, "Failed to create timestamp"); return Timestamp(t); } }; diff --git a/include/zenoh/api/shm/buffer/zshm.hxx b/include/zenoh/api/shm/buffer/zshm.hxx index 7b14968a..dceee487 100644 --- a/include/zenoh/api/shm/buffer/zshm.hxx +++ b/include/zenoh/api/shm/buffer/zshm.hxx @@ -17,6 +17,7 @@ #include #include "../../base.hxx" +#include "../../interop.hxx" #include "zshmmut.hxx" namespace zenoh { @@ -36,15 +37,15 @@ class ZShm : public Owned<::z_owned_shm_t> { /// @brief Create a new ZShm from ZShm by performing a shallow SHM reference copy. /// @param other ZShm to copy - ZShm(const ZShm& other) : Owned(nullptr) { ::z_shm_clone(&this->_0, other.loan()); } + ZShm(const ZShm& other) : Owned(nullptr) { ::z_shm_clone(&this->_0, interop::as_loaned_c_ptr(other)); } /// @brief Get buffer's const data. It is completely unsafe to to modify SHM data without using ZShmMut interface. /// @return pointer to the underlying data - const uint8_t* data() const { return ::z_shm_data(this->loan()); } + const uint8_t* data() const { return ::z_shm_data(interop::as_loaned_c_ptr(*this)); } /// @brief Get buffer's data size. /// @return underlying data size - std::size_t len() const { return ::z_shm_len(this->loan()); } + std::size_t len() const { return ::z_shm_len(interop::as_loaned_c_ptr(*this)); } /// @brief Create a new ZShmMut from ZShm. /// @param immut immutable buffer diff --git a/include/zenoh/api/shm/buffer/zshmmut.hxx b/include/zenoh/api/shm/buffer/zshmmut.hxx index 1c79046c..c20976f5 100644 --- a/include/zenoh/api/shm/buffer/zshmmut.hxx +++ b/include/zenoh/api/shm/buffer/zshmmut.hxx @@ -27,15 +27,15 @@ class ZShmMut : public Owned<::z_owned_shm_mut_t> { /// @brief Get buffer's const data /// @return pointer to the underlying data - const uint8_t* data() const { return ::z_shm_mut_data(this->loan()); } + const uint8_t* data() const { return ::z_shm_mut_data(interop::as_loaned_c_ptr(*this)); } /// @brief Get buffer's data /// @return pointer to the underlying data - uint8_t* data() { return ::z_shm_mut_data_mut(this->loan()); } + uint8_t* data() { return ::z_shm_mut_data_mut(interop::as_loaned_c_ptr(*this)); } /// @brief Get buffer's data size. /// @return underlying data size - std::size_t len() const { return ::z_shm_mut_len(this->loan()); } + std::size_t len() const { return ::z_shm_mut_len(interop::as_loaned_c_ptr(*this)); } }; } // end of namespace zenoh diff --git a/include/zenoh/api/shm/client_storage/client_storage.hxx b/include/zenoh/api/shm/client_storage/client_storage.hxx index 6e027d4a..70c4410f 100644 --- a/include/zenoh/api/shm/client_storage/client_storage.hxx +++ b/include/zenoh/api/shm/client_storage/client_storage.hxx @@ -17,6 +17,7 @@ #include #include "../../base.hxx" +#include "../../interop.hxx" #include "../client/shm_client.hxx" namespace zenoh { @@ -69,18 +70,20 @@ class ShmClientStorage : public Owned<::z_owned_shm_client_storage_t> { // fill list with clients for (std::move_iterator it = begin; it != end; ++it) { - __ZENOH_RESULT_CHECK(zc_shm_client_list_add_client(it->first, z_move(it->second._0), list.loan()), err, - "Failed to form list of SHM clients"); + __ZENOH_RESULT_CHECK( + zc_shm_client_list_add_client(it->first, z_move(it->second._0), interop::as_loaned_c_ptr(list)), err, + "Failed to form list of SHM clients"); } // create client storage from the list - __ZENOH_RESULT_CHECK(z_shm_client_storage_new(&this->_0, list.loan(), add_default_client_set), err, - "Failed to create SHM client storage!"); + __ZENOH_RESULT_CHECK( + z_shm_client_storage_new(&this->_0, interop::as_loaned_c_ptr(list), add_default_client_set), err, + "Failed to create SHM client storage!"); } /// @brief Performs a shallow copy of ShmClientStorage ShmClientStorage(const ShmClientStorage& other) : Owned(nullptr) { - z_shm_client_storage_clone(&this->_0, other.loan()); + z_shm_client_storage_clone(&this->_0, interop::as_loaned_c_ptr(other)); } private: diff --git a/include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx b/include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx index c86490f0..7650e00f 100644 --- a/include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx +++ b/include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx @@ -30,7 +30,7 @@ class PosixShmProvider : public ShmProvider { /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. PosixShmProvider(const MemoryLayout& layout, ZResult* err = nullptr) : ShmProvider(nullptr) { - __ZENOH_RESULT_CHECK(::z_posix_shm_provider_new(&this->_0, layout.loan()), err, + __ZENOH_RESULT_CHECK(::z_posix_shm_provider_new(&this->_0, interop::as_loaned_c_ptr(layout)), err, "Failed to create POSIX SHM provider"); } }; diff --git a/include/zenoh/api/shm/provider/alloc_layout.hxx b/include/zenoh/api/shm/provider/alloc_layout.hxx index 151b802e..68ee88e6 100644 --- a/include/zenoh/api/shm/provider/alloc_layout.hxx +++ b/include/zenoh/api/shm/provider/alloc_layout.hxx @@ -55,37 +55,37 @@ class AllocLayout : public Owned<::z_owned_alloc_layout_t> { /// @brief Create a new Alloc Layout for SHM Provider. AllocLayout(const ShmProvider& owner_provider, std::size_t size, AllocAlignment alignment, ZResult* err = nullptr) : Owned(nullptr) { - __ZENOH_RESULT_CHECK(::z_alloc_layout_new(&this->_0, owner_provider.loan(), size, alignment), err, - "Failed to create SHM Alloc Layout"); + __ZENOH_RESULT_CHECK(::z_alloc_layout_new(&this->_0, interop::as_loaned_c_ptr(owner_provider), size, alignment), + err, "Failed to create SHM Alloc Layout"); } BufAllocResult alloc() const { z_buf_alloc_result_t result; - ::z_alloc_layout_alloc(&result, this->loan()); + ::z_alloc_layout_alloc(&result, interop::as_loaned_c_ptr(*this)); return Converters::from(result); } BufAllocResult alloc_gc() const { z_buf_alloc_result_t result; - ::z_alloc_layout_alloc_gc(&result, this->loan()); + ::z_alloc_layout_alloc_gc(&result, interop::as_loaned_c_ptr(*this)); return Converters::from(result); } BufAllocResult alloc_gc_defrag() const { z_buf_alloc_result_t result; - ::z_alloc_layout_alloc_gc_defrag(&result, this->loan()); + ::z_alloc_layout_alloc_gc_defrag(&result, interop::as_loaned_c_ptr(*this)); return Converters::from(result); } BufAllocResult alloc_gc_defrag_dealloc() const { z_buf_alloc_result_t result; - ::z_alloc_layout_alloc_gc_defrag_dealloc(&result, this->loan()); + ::z_alloc_layout_alloc_gc_defrag_dealloc(&result, interop::as_loaned_c_ptr(*this)); return Converters::from(result); } BufAllocResult alloc_gc_defrag_blocking() const { z_buf_alloc_result_t result; - ::z_alloc_layout_alloc_gc_defrag_blocking(&result, this->loan()); + ::z_alloc_layout_alloc_gc_defrag_blocking(&result, interop::as_loaned_c_ptr(*this)); return Converters::from(result); } @@ -93,7 +93,8 @@ class AllocLayout : public Owned<::z_owned_alloc_layout_t> { auto rcv = receiver.release(); ::zc_threadsafe_context_t context = {{rcv}, &shm::provider::closures::_z_alloc_layout_async_interface_drop_fn}; return ::z_alloc_layout_threadsafe_alloc_gc_defrag_async( - &rcv->_result, this->loan(), context, shm::provider::closures::_z_alloc_layout_async_interface_result_fn); + &rcv->_result, interop::as_loaned_c_ptr(*this), context, + shm::provider::closures::_z_alloc_layout_async_interface_result_fn); } }; } // end of namespace zenoh diff --git a/include/zenoh/api/shm/provider/shm_provider.hxx b/include/zenoh/api/shm/provider/shm_provider.hxx index dc9201da..a7de148d 100644 --- a/include/zenoh/api/shm/provider/shm_provider.hxx +++ b/include/zenoh/api/shm/provider/shm_provider.hxx @@ -54,31 +54,31 @@ class ShmProvider : public Owned<::z_owned_shm_provider_t> { BufLayoutAllocResult alloc(size_t size, AllocAlignment alignment) const { z_buf_layout_alloc_result_t result; - ::z_shm_provider_alloc(&result, this->loan(), size, alignment); + ::z_shm_provider_alloc(&result, interop::as_loaned_c_ptr(*this), size, alignment); return Converters::from(result); } BufLayoutAllocResult alloc_gc(size_t size, AllocAlignment alignment) const { z_buf_layout_alloc_result_t result; - ::z_shm_provider_alloc_gc(&result, this->loan(), size, alignment); + ::z_shm_provider_alloc_gc(&result, interop::as_loaned_c_ptr(*this), size, alignment); return Converters::from(result); } BufLayoutAllocResult alloc_gc_defrag(size_t size, AllocAlignment alignment) const { z_buf_layout_alloc_result_t result; - ::z_shm_provider_alloc_gc_defrag(&result, this->loan(), size, alignment); + ::z_shm_provider_alloc_gc_defrag(&result, interop::as_loaned_c_ptr(*this), size, alignment); return Converters::from(result); } BufLayoutAllocResult alloc_gc_defrag_dealloc(size_t size, AllocAlignment alignment) const { z_buf_layout_alloc_result_t result; - ::z_shm_provider_alloc_gc_defrag_dealloc(&result, this->loan(), size, alignment); + ::z_shm_provider_alloc_gc_defrag_dealloc(&result, interop::as_loaned_c_ptr(*this), size, alignment); return Converters::from(result); } BufLayoutAllocResult alloc_gc_defrag_blocking(size_t size, AllocAlignment alignment) const { z_buf_layout_alloc_result_t result; - ::z_shm_provider_alloc_gc_defrag_blocking(&result, this->loan(), size, alignment); + ::z_shm_provider_alloc_gc_defrag_blocking(&result, interop::as_loaned_c_ptr(*this), size, alignment); return Converters::from(result); } @@ -86,19 +86,19 @@ class ShmProvider : public Owned<::z_owned_shm_provider_t> { std::unique_ptr receiver) const { auto rcv = receiver.release(); ::zc_threadsafe_context_t context = {{rcv}, &ShmProviderAsyncInterface::drop}; - return ::z_shm_provider_alloc_gc_defrag_async(&rcv->_result, this->loan(), size, alignment, context, - ShmProviderAsyncInterface::result); + return ::z_shm_provider_alloc_gc_defrag_async(&rcv->_result, interop::as_loaned_c_ptr(*this), size, alignment, + context, ShmProviderAsyncInterface::result); } - void defragment() const { ::z_shm_provider_defragment(this->loan()); } + void defragment() const { ::z_shm_provider_defragment(interop::as_loaned_c_ptr(*this)); } - std::size_t garbage_collect() const { return ::z_shm_provider_garbage_collect(this->loan()); } + std::size_t garbage_collect() const { return ::z_shm_provider_garbage_collect(interop::as_loaned_c_ptr(*this)); } - std::size_t available() const { return ::z_shm_provider_available(this->loan()); } + std::size_t available() const { return ::z_shm_provider_available(interop::as_loaned_c_ptr(*this)); } ZShmMut map(AllocatedChunk&& chunk, std::size_t len) const { z_owned_shm_mut_t result; - ::z_shm_provider_map(&result, this->loan(), chunk, len); + ::z_shm_provider_map(&result, interop::as_loaned_c_ptr(*this), chunk, len); return ZShmMut(&result); } }; diff --git a/include/zenoh/api/shm/provider/shm_provider_backend.hxx b/include/zenoh/api/shm/provider/shm_provider_backend.hxx index 67408f90..7db0f6c8 100644 --- a/include/zenoh/api/shm/provider/shm_provider_backend.hxx +++ b/include/zenoh/api/shm/provider/shm_provider_backend.hxx @@ -14,8 +14,8 @@ #pragma once -#include "../../../detail/interop.hxx" #include "../../base.hxx" +#include "../../interop.hxx" #include "chunk.hxx" #include "types.hxx" @@ -41,9 +41,8 @@ extern "C" { inline void _z_cpp_shm_provider_backend_drop_fn(void *context) { delete static_cast(context); } inline void _z_cpp_shm_provider_backend_alloc_fn(struct z_owned_chunk_alloc_result_t *out_result, const struct z_loaned_memory_layout_t *layout, void *context) { - *out_result = static_cast(context) - ->alloc(detail::as_owned_cpp_obj(layout)) - .take(); + *out_result = interop::move_to_c_obj(static_cast(context)->alloc( + interop::as_owned_cpp_ref(layout))); } inline void _z_cpp_shm_provider_backend_free_fn(const struct z_chunk_descriptor_t *chunk, void *context) { static_cast(context)->free(*chunk); @@ -56,7 +55,7 @@ inline size_t _z_cpp_shm_provider_backend_available_fn(void *context) { } inline void _z_cpp_shm_provider_backend_layout_for_fn(struct z_owned_memory_layout_t *layout, void *context) { static_cast(context)->layout_for( - detail::as_cpp_obj_mut(layout)); + interop::as_owned_cpp_ref(layout)); } } } // namespace shm::provider_backend::closures diff --git a/include/zenoh/api/shm/provider/types.hxx b/include/zenoh/api/shm/provider/types.hxx index 1f1069ff..5c8ece8d 100644 --- a/include/zenoh/api/shm/provider/types.hxx +++ b/include/zenoh/api/shm/provider/types.hxx @@ -61,14 +61,14 @@ class MemoryLayout : public Owned<::z_owned_memory_layout_t> { size_t size() const { size_t size; AllocAlignment alignment; - z_memory_layout_get_data(&size, &alignment, this->loan()); + z_memory_layout_get_data(&size, &alignment, interop::as_loaned_c_ptr(*this)); return size; } AllocAlignment alignment() const { size_t size; AllocAlignment alignment; - z_memory_layout_get_data(&size, &alignment, this->loan()); + z_memory_layout_get_data(&size, &alignment, interop::as_loaned_c_ptr(*this)); return alignment; } }; diff --git a/include/zenoh/api/source_info.hxx b/include/zenoh/api/source_info.hxx index 86d93f5a..e8659632 100644 --- a/include/zenoh/api/source_info.hxx +++ b/include/zenoh/api/source_info.hxx @@ -13,9 +13,9 @@ #pragma once -#include "../detail/interop.hxx" #include "../zenohc.hxx" #include "base.hxx" +#include "interop.hxx" #if defined UNSTABLE #include "id.hxx" #endif @@ -44,16 +44,16 @@ class SourceInfo : public Owned<::z_owned_source_info_t> { /// @brief Construct from global id and sequence number. SourceInfo(const EntityGlobalId& id, uint64_t sn) : Owned(nullptr) { - ::z_source_info_new(&this->_0, detail::as_copyable_c_ptr(id), sn); + ::z_source_info_new(&this->_0, interop::as_copyable_c_ptr(id), sn); } /// @name Methods /// @brief Get the source id. - EntityGlobalId id() const { return EntityGlobalId(::z_source_info_id(this->loan())); } + EntityGlobalId id() const { return EntityGlobalId(::z_source_info_id(interop::as_loaned_c_ptr(*this))); } /// @brief Get the sequence number of the sample from the given source. - uint64_t sn() const { return ::z_source_info_sn(this->loan()); } + uint64_t sn() const { return ::z_source_info_sn(interop::as_loaned_c_ptr(*this)); } }; #endif } // namespace zenoh diff --git a/include/zenoh/api/subscriber.hxx b/include/zenoh/api/subscriber.hxx index 11d77dbf..842d04e8 100644 --- a/include/zenoh/api/subscriber.hxx +++ b/include/zenoh/api/subscriber.hxx @@ -13,8 +13,8 @@ #pragma once -#include "../detail/interop.hxx" #include "base.hxx" +#include "interop.hxx" #include "keyexpr.hxx" namespace zenoh { @@ -27,7 +27,7 @@ class SubscriberBase : public Owned<::z_owned_subscriber_t> { /// @brief Get the key expression of the subscriber /// @note zenoh-c only. const KeyExpr& get_keyexpr() const { - return detail::as_owned_cpp_obj(::z_subscriber_keyexpr(this->loan())); + return interop::as_owned_cpp_ref(::z_subscriber_keyexpr(interop::as_loaned_c_ptr(*this))); } #endif }; diff --git a/include/zenoh/api/timestamp.hxx b/include/zenoh/api/timestamp.hxx index 744581c3..f158404a 100644 --- a/include/zenoh/api/timestamp.hxx +++ b/include/zenoh/api/timestamp.hxx @@ -12,9 +12,9 @@ // ZettaScale Zenoh Team, #pragma once -#include "../detail/interop.hxx" #include "../zenohc.hxx" #include "base.hxx" +#include "interop.hxx" #if defined UNSTABLE #include "id.hxx" #endif diff --git a/include/zenoh/detail/closures_concrete.hxx b/include/zenoh/detail/closures_concrete.hxx index e7860f96..c10f4bf1 100644 --- a/include/zenoh/detail/closures_concrete.hxx +++ b/include/zenoh/detail/closures_concrete.hxx @@ -13,10 +13,10 @@ #pragma once +#include "../api/interop.hxx" #include "../api/query.hxx" #include "../api/reply.hxx" #include "../api/sample.hxx" -#include "../detail/interop.hxx" #include "../zenohc.hxx" #include "closures.hxx" #if defined UNSTABLE @@ -30,25 +30,25 @@ extern "C" { inline void _zenoh_on_drop(void* context) { IDroppable::delete_from_context(context); } inline void _zenoh_on_reply_call(const ::z_loaned_reply_t* reply, void* context) { - IClosure::call_from_context(context, detail::as_owned_cpp_obj(reply)); + IClosure::call_from_context(context, interop::as_owned_cpp_ref(reply)); } inline void _zenoh_on_sample_call(const ::z_loaned_sample_t* sample, void* context) { - IClosure::call_from_context(context, detail::as_owned_cpp_obj(sample)); + IClosure::call_from_context(context, interop::as_owned_cpp_ref(sample)); } inline void _zenoh_on_query_call(const ::z_loaned_query_t* query, void* context) { - IClosure::call_from_context(context, detail::as_owned_cpp_obj(query)); + IClosure::call_from_context(context, interop::as_owned_cpp_ref(query)); } #if defined UNSTABLE inline void _zenoh_on_id_call(const ::z_id_t* z_id, void* context) { - IClosure::call_from_context(context, detail::as_copyable_cpp_obj(z_id)); + IClosure::call_from_context(context, interop::as_copyable_cpp_ref(z_id)); } #endif inline void _zenoh_on_hello_call(const ::z_loaned_hello_t* hello, void* context) { - IClosure::call_from_context(context, detail::as_owned_cpp_obj(hello)); + IClosure::call_from_context(context, interop::as_owned_cpp_ref(hello)); } } } // namespace zenoh::detail::closures diff --git a/include/zenoh/detail/interop.hxx b/include/zenoh/detail/interop.hxx deleted file mode 100644 index 7f234f6a..00000000 --- a/include/zenoh/detail/interop.hxx +++ /dev/null @@ -1,115 +0,0 @@ -// -// 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, -#pragma once - -#include - -#include "../api/base.hxx" - -namespace zenoh::detail { - -template -CopyableType* as_copyable_c_ptr(Copyable& c) { - return reinterpret_cast(&c); -} - -template -const CopyableType* as_copyable_c_ptr(const Copyable& c) { - return reinterpret_cast(&c); -} - -template -auto* as_copyable_c_ptr(std::optional& c) { - return c.has_value() ? as_copyable_c_ptr(c.value()) : nullptr; -} - -template -const auto* as_copyable_c_ptr(const std::optional& c) { - return c.has_value() ? as_copyable_c_ptr(c.value()) : nullptr; -} - -template -OwnedType* as_owned_c_ptr(Owned& o) { - return reinterpret_cast(&o); -} - -template -const OwnedType* as_owned_c_ptr(const Owned& o) { - return reinterpret_cast(&o); -} - -template -auto* as_owned_c_ptr(std::optional& o) { - return o.has_value() ? as_owned_c_ptr(o.value()) : nullptr; -} - -template -const auto* as_owned_c_ptr(const std::optional& o) { - return o.has_value() ? as_owned_c_ptr(o.value()) : nullptr; -} - -template -auto as_loaned_c_ptr(const OwnedType& o) { - return ::z_loan(*as_owned_c_ptr(o)); -} - -template -auto as_loaned_c_ptr(OwnedType& o) { - return ::z_loan_mut(*as_owned_c_ptr(o)); -} - -template -auto as_moved_c_ptr(OwnedType& o) { - return ::z_move(*as_owned_c_ptr(o)); -} - -template -auto& to_owned_cpp_ref(const OwnedType* o) { - static_assert(sizeof(OwnedType) == sizeof(T) && alignof(OwnedType) == alignof(T), - "Target and owned classes must have the same layout"); - return *reinterpret_cast(o); -} - -template -const auto& as_owned_cpp_obj(const LoanedType* l) { - typedef typename z_loaned_to_owned_type_t::type OwnedType; - static_assert(sizeof(OwnedType) == sizeof(LoanedType) && alignof(OwnedType) == alignof(LoanedType), - "Loaned and owned classes must have the same layout"); - static_assert(sizeof(T) == sizeof(LoanedType) && alignof(T) == alignof(LoanedType), - "Loaned and Target classes must have the same layout"); - static_assert(std::is_base_of_v, T>, "Target class should be derived from an owned class"); - const OwnedType* o = reinterpret_cast(l); - const zenoh::Owned* o_cpp = reinterpret_cast*>(o); - return *reinterpret_cast(o_cpp); -} - -template -auto& as_cpp_obj_mut(OwnedType* o) { - static_assert(sizeof(T) == sizeof(OwnedType) && alignof(T) == alignof(OwnedType), - "Owned and Target classes must have the same layout"); - static_assert(std::is_base_of_v, T>, "Target class should be derived from an owned class"); - zenoh::Owned* o_cpp = reinterpret_cast*>(o); - return *reinterpret_cast(o_cpp); -} - -template -const auto& as_copyable_cpp_obj(const CopyableType* c) { - static_assert(sizeof(T) == sizeof(CopyableType) && alignof(T) == alignof(CopyableType), - "Copyable and Target classes must have the same layout"); - static_assert(std::is_base_of_v, T>, - "Target class should be derived from a copyable class"); - const zenoh::Copyable* c_cpp = reinterpret_cast*>(c); - return *reinterpret_cast(c_cpp); -} - -} // namespace zenoh::detail \ No newline at end of file diff --git a/tests/universal/network/keyexpr.cxx b/tests/universal/network/keyexpr.cxx index 16129445..6eea781e 100644 --- a/tests/universal/network/keyexpr.cxx +++ b/tests/universal/network/keyexpr.cxx @@ -19,7 +19,7 @@ using namespace zenoh; void key_expr() { KeyExpr foo("FOO"); - assert(foo.internal_check()); + assert(interop::detail::check(foo)); assert(foo.as_string_view() == "FOO"); } @@ -35,13 +35,13 @@ void canonize() { // do not force canoniozation on keyexpr construction KeyExpr k_err(non_canon, false, &err); #ifdef ZENOHCXX_ZENOHC // Pico does not validate key expressions yet. - assert(!k_err.internal_check()); + assert(!interop::detail::check(k_err)); assert(err < 0); #endif // enforce canonization KeyExpr k_ok(non_canon, true, &err); - assert(k_ok.internal_check()); + assert(interop::detail::check(k_ok)); assert(err == 0); assert(k_ok.as_string_view() == canon); } @@ -93,14 +93,14 @@ void declare(Session& s) { KeyExpr foobar("FOO/BAR"); KeyExpr foostar("FOO/*"); auto declared = s.declare_keyexpr(foobar); - assert(declared.internal_check()); + assert(interop::detail::check(declared)); assert(declared.as_string_view() == "FOO/BAR"); assert(declared == foobar); assert(foostar.includes(declared)); assert(declared.intersects(foobar)); s.undeclare_keyexpr(std::move(declared)); - assert(!declared.internal_check()); + assert(!interop::detail::check(declared)); } int main(int argc, char** argv) { diff --git a/tests/zenohc/shm_api.cxx b/tests/zenohc/shm_api.cxx index 171848e9..086475f1 100644 --- a/tests/zenohc/shm_api.cxx +++ b/tests/zenohc/shm_api.cxx @@ -41,16 +41,16 @@ using namespace std::chrono_literals; return -300; \ } -#define ASSERT_VALID(expr) \ - if (!(expr.internal_check())) { \ - assert(false); \ - return -300; \ +#define ASSERT_VALID(expr) \ + if (!interop::detail::check(expr)) { \ + assert(false); \ + return -300; \ } -#define ASSERT_NULL(expr) \ - if (expr.internal_check()) { \ - assert(false); \ - return -300; \ +#define ASSERT_NULL(expr) \ + if (interop::detail::check(expr)) { \ + assert(false); \ + return -300; \ } int test_shm_buffer(ZShmMut&& buf) { @@ -160,7 +160,7 @@ class TestShmProviderBackend : public CppShmProviderBackend { private: virtual ChunkAllocResult alloc(const MemoryLayout& layout) override { - assert(layout.internal_check()); + assert(interop::detail::check(layout)); // check size and alignment const auto size = layout.size(); @@ -208,7 +208,7 @@ class TestShmProviderBackend : public CppShmProviderBackend { virtual size_t available() const override { return this->bytes_available; } virtual void layout_for(MemoryLayout& layout) override { - assert(layout.internal_check()); + assert(interop::detail::check(layout)); // check size and alignment const auto size = layout.size(); From d8632fb3d2cc086cbe30d3c76cecfc57dffeeb21 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 29 Aug 2024 19:04:23 +0200 Subject: [PATCH 2/9] no longer expose constructors of cpp owned objects from c ones --- include/zenoh/api/base.hxx | 19 ++++----- include/zenoh/api/bytes.hxx | 2 +- include/zenoh/api/channels.hxx | 28 ++++++++----- include/zenoh/api/config.hxx | 20 ++++----- include/zenoh/api/encoding.hxx | 2 - include/zenoh/api/hello.hxx | 2 - include/zenoh/api/interop.hxx | 10 +++++ include/zenoh/api/liveliness.hxx | 7 +++- include/zenoh/api/publisher.hxx | 7 +++- include/zenoh/api/query.hxx | 6 +-- include/zenoh/api/queryable.hxx | 29 ++++++++----- include/zenoh/api/reply.hxx | 3 -- include/zenoh/api/sample.hxx | 6 +-- include/zenoh/api/session.hxx | 41 +++++++++---------- include/zenoh/api/shm/buffer/zshm.hxx | 6 +-- include/zenoh/api/shm/buffer/zshmmut.hxx | 2 - include/zenoh/api/shm/client/shm_client.hxx | 2 - .../api/shm/client_storage/client_storage.hxx | 2 +- .../posix/posix_shm_provider.hxx | 2 +- .../zenoh/api/shm/provider/alloc_layout.hxx | 2 - .../zenoh/api/shm/provider/shm_provider.hxx | 11 ++--- .../api/shm/provider/shm_provider_backend.hxx | 7 ++-- include/zenoh/api/shm/provider/types.hxx | 5 +-- include/zenoh/api/shm/provider/types_impl.hxx | 5 ++- include/zenoh/api/source_info.hxx | 2 - include/zenoh/api/subscriber.hxx | 25 +++++------ tests/zenohc/shm_api.cxx | 2 +- 27 files changed, 132 insertions(+), 123 deletions(-) diff --git a/include/zenoh/api/base.hxx b/include/zenoh/api/base.hxx index 49b62d94..76d161c2 100644 --- a/include/zenoh/api/base.hxx +++ b/include/zenoh/api/base.hxx @@ -72,17 +72,6 @@ class Owned { typedef ZC_OWNED_TYPE OwnedType; public: - /// @name Constructors - /// @brief Construct from owned zenoh-c struct. - /// @param pv Pointer to valid owned zenoh-c struct. The ownership is transferred - /// to the constructed object. - explicit Owned(OwnedType* pv) { - if (pv) { - _0 = *pv; - ::z_internal_null(pv); - } else - ::z_internal_null(&this->_0); - } /// Move constructor from other object Owned(Owned&& v) : Owned(&v._0) {} /// Move assignment from other object @@ -99,6 +88,14 @@ class Owned { protected: OwnedType _0; + + explicit Owned(OwnedType* pv) { + if (pv) { + _0 = *pv; + ::z_internal_null(pv); + } else + ::z_internal_null(&this->_0); + } }; } // namespace zenoh \ No newline at end of file diff --git a/include/zenoh/api/bytes.hxx b/include/zenoh/api/bytes.hxx index 5417e035..f7534582 100644 --- a/include/zenoh/api/bytes.hxx +++ b/include/zenoh/api/bytes.hxx @@ -301,7 +301,7 @@ struct ZenohDeserializer { template <> struct ZenohDeserializer { static ZShm deserialize(const Bytes& b, ZResult* err = nullptr) { - ZShm shm(nullptr); + ZShm shm = interop::detail::null(); __ZENOH_RESULT_CHECK( ::z_bytes_deserialize_into_owned_shm(interop::as_loaned_c_ptr(b), interop::as_owned_c_ptr(shm)), err, "Failed to deserialize into ZShm!"); diff --git a/include/zenoh/api/channels.hxx b/include/zenoh/api/channels.hxx index a0e54424..1e81ae00 100644 --- a/include/zenoh/api/channels.hxx +++ b/include/zenoh/api/channels.hxx @@ -97,19 +97,21 @@ struct RingHandlerData { }; } // namespace detail +class FifoChannel; + /// @brief A FIFO channel handler /// @tparam T data entry type template class FifoHandler : public Owned::handler_type> { - public: - using Owned::handler_type>::Owned; + FifoHandler() : Owned::handler_type>(nullptr){}; + public: /// @name Methods /// @brief Fetch a data entry from the handler's buffer. If buffer is empty will block until new data entry arrives. /// @return received data entry if there were any in the buffer, a receive error otherwise. std::variant recv() const { - std::variant v(T(nullptr)); + std::variant v(interop::detail::null()); z_result_t res = ::z_recv(interop::as_loaned_c_ptr(*this), zenoh::interop::as_owned_c_ptr(std::get(v))); if (res == Z_OK) { return v; @@ -121,7 +123,7 @@ class FifoHandler : public Owned::handler_ty /// @brief Fetch a data entry from the handler's buffer. If buffer is empty will immediately return. /// @return received data entry if there were any in the buffer, a receive error otherwise. std::variant try_recv() const { - std::variant v(T(nullptr)); + std::variant v(interop::detail::null()); z_result_t res = ::z_try_recv(interop::as_loaned_c_ptr(*this), zenoh::interop::as_owned_c_ptr(std::get(v))); if (res == Z_OK) { return v; @@ -131,21 +133,25 @@ class FifoHandler : public Owned::handler_ty return RecvError::Z_DISCONNECTED; } } + + friend class FifoChannel; }; +class RingChannel; + /// @brief A circular buffer channel handler. /// @tparam T data entry type. template class RingHandler : public Owned::handler_type> { - public: - using Owned::handler_type>::Owned; + RingHandler() : Owned::handler_type>(nullptr){}; + public: /// @name Methods /// @brief Fetch a data entry from the handler's buffer. If buffer is empty will block until new data entry arrives. /// @return received data entry if there were any in the buffer, a receive error otherwise. std::variant recv() const { - std::variant v(T(nullptr)); + std::variant v(interop::detail::null()); z_result_t res = ::z_recv(zenoh::interop::as_loaned_c_ptr(*this), zenoh::interop::as_owned_c_ptr(std::get(v))); if (res == Z_OK) { @@ -158,7 +164,7 @@ class RingHandler : public Owned::handler_ty /// @brief Fetch a data entry from the handler's buffer. If buffer is empty will immediately return. /// @return received data entry if there were any in the buffer, a receive error otherwise. std::variant try_recv() const { - std::variant v(T(nullptr)); + std::variant v(interop::detail::null()); z_result_t res = ::z_try_recv(interop::as_loaned_c_ptr(*this), zenoh::interop::as_owned_c_ptr(std::get(v))); if (res == Z_OK) { return v; @@ -168,6 +174,8 @@ class RingHandler : public Owned::handler_ty return RecvError::Z_DISCONNECTED; } } + + friend class RingChannel; }; /// @brief A FIFO channel. @@ -191,7 +199,7 @@ class FifoChannel { template std::pair::closure_type, HandlerType> into_cb_handler_pair() const { typename detail::FifoHandlerData::closure_type c_closure; - FifoHandler h(nullptr); + FifoHandler h; detail::FifoHandlerData::create_cb_handler_pair(&c_closure, zenoh::interop::as_owned_c_ptr(h), _capacity); return {c_closure, std::move(h)}; } @@ -218,7 +226,7 @@ class RingChannel { template std::pair::closure_type, HandlerType> into_cb_handler_pair() const { typename detail::RingHandlerData::closure_type c_closure; - RingHandler h(nullptr); + RingHandler h; detail::RingHandlerData::create_cb_handler_pair(&c_closure, zenoh::interop::as_owned_c_ptr(h), _capacity); return {c_closure, std::move(h)}; } diff --git a/include/zenoh/api/config.hxx b/include/zenoh/api/config.hxx index 19fe4164..53068b04 100644 --- a/include/zenoh/api/config.hxx +++ b/include/zenoh/api/config.hxx @@ -21,16 +21,16 @@ namespace zenoh { /// A Zenoh Session config class Config : public Owned<::z_owned_config_t> { - public: - using Owned::Owned; + Config() : Owned(nullptr){}; + public: /// @name Constructors /// @brief Create a default configuration. /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. static Config create_default(ZResult* err = nullptr) { - Config c(nullptr); + Config c; __ZENOH_RESULT_CHECK(::z_config_default(&c._0), err, std::string("Failed to create default configuration")); return c; } @@ -42,7 +42,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-c only. static Config peer(ZResult* err = nullptr) { - Config c(nullptr); + Config c; __ZENOH_RESULT_CHECK(::z_config_peer(&c._0), err, std::string("Failed to create peer configuration")); return c; } @@ -54,7 +54,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-c only. static Config from_file(const std::string& path, ZResult* err = nullptr) { - Config c(nullptr); + Config c; __ZENOH_RESULT_CHECK(::zc_config_from_file(&c._0, path.data()), err, std::string("Failed to create config from: ").append(path)); return c; @@ -67,7 +67,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-c only. static Config from_str(const std::string& s, ZResult* err = nullptr) { - Config c(nullptr); + Config c; __ZENOH_RESULT_CHECK(::zc_config_from_str(&c._0, s.data()), err, std::string("Failed to create config from: ").append(s)); return c; @@ -79,7 +79,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-c only. static Config client(const std::vector& peers, ZResult* err = nullptr) { - Config c(nullptr); + Config c; std::vector p; p.reserve(peers.size()); for (const auto& peer : peers) { @@ -95,7 +95,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-pico only. static Config from_env(ZResult* err = nullptr) { - Config c(nullptr); + Config c; __ZENOH_RESULT_CHECK(::zc_config_from_env(&c._0), err, "Failed to create config from environment variable"); return c; } @@ -109,7 +109,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-pico only. static Config peer(const char* locator, ZResult* err = nullptr) { - Config c(nullptr); + Config c; __ZENOH_RESULT_CHECK(::z_config_peer(&c._0, locator), err, "Failed to create client config"); return c; } @@ -121,7 +121,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-pico only. static Config client(const char* peer, ZResult* err = nullptr) { - Config c(nullptr); + Config c; __ZENOH_RESULT_CHECK(::z_config_client(&c._0, peer), err, "Failed to create client config"); return c; } diff --git a/include/zenoh/api/encoding.hxx b/include/zenoh/api/encoding.hxx index 5688333f..9434a074 100644 --- a/include/zenoh/api/encoding.hxx +++ b/include/zenoh/api/encoding.hxx @@ -23,8 +23,6 @@ namespace zenoh { /// @brief The encoding of Zenoh data. class Encoding : public Owned<::z_owned_encoding_t> { public: - using Owned::Owned; - /// @name Constructors /// @brief Construct default encoding. diff --git a/include/zenoh/api/hello.hxx b/include/zenoh/api/hello.hxx index 1fcbea84..db07b068 100644 --- a/include/zenoh/api/hello.hxx +++ b/include/zenoh/api/hello.hxx @@ -28,8 +28,6 @@ namespace zenoh { /// message. class Hello : public Owned<::z_owned_hello_t> { public: - using Owned::Owned; - /// @name Methods #if defined UNSTABLE diff --git a/include/zenoh/api/interop.hxx b/include/zenoh/api/interop.hxx index 691b85de..15617d64 100644 --- a/include/zenoh/api/interop.hxx +++ b/include/zenoh/api/interop.hxx @@ -13,6 +13,7 @@ #pragma once #include +#include #include "base.hxx" @@ -203,6 +204,15 @@ template bool check(const Owned& owned_cpp_obj) { return ::z_internal_check(*as_owned_c_ptr(owned_cpp_obj)); } + +template +T null() { + using ZCtype = std::remove_cv_t()))>>; + ZCtype z; + ::z_internal_null(&z); + return std::move(as_owned_cpp_ref(&z)); +} + } // namespace detail } // namespace zenoh::interop \ No newline at end of file diff --git a/include/zenoh/api/liveliness.hxx b/include/zenoh/api/liveliness.hxx index 27651d99..194329bf 100644 --- a/include/zenoh/api/liveliness.hxx +++ b/include/zenoh/api/liveliness.hxx @@ -17,6 +17,9 @@ #include "interop.hxx" namespace zenoh { + +class Session; + /// @brief // A liveliness token that can be used to provide the network with information about connectivity to its /// declarer. /// @@ -26,9 +29,9 @@ namespace zenoh { /// between the subscriber and the token's creator is lost. /// @note zenoh-c only. class LivelinessToken : public Owned<::zc_owned_liveliness_token_t> { - public: - using Owned::Owned; + LivelinessToken() : Owned(nullptr){}; + public: /// Undeclares liveliness token, resetting it to gravestone state. /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. diff --git a/include/zenoh/api/publisher.hxx b/include/zenoh/api/publisher.hxx index d2ee7290..c5601f87 100644 --- a/include/zenoh/api/publisher.hxx +++ b/include/zenoh/api/publisher.hxx @@ -26,11 +26,14 @@ #include namespace zenoh { + +class Session; + /// A Zenoh publisher. Constructed by ``Session::declare_publisher`` method. class Publisher : public Owned<::z_owned_publisher_t> { - public: - using Owned::Owned; + Publisher() : Owned(nullptr){}; + public: /// @brief Options to be passed to ``Publisher::put`` operation. struct PutOptions { /// @name Fields diff --git a/include/zenoh/api/query.hxx b/include/zenoh/api/query.hxx index 0ee44120..cc9dd39b 100644 --- a/include/zenoh/api/query.hxx +++ b/include/zenoh/api/query.hxx @@ -31,9 +31,9 @@ namespace zenoh { /// The query to be answered by a ``Queryable``. class Query : public Owned<::z_owned_query_t> { - public: - using Owned::Owned; + Query() : Owned(nullptr){}; + public: /// @name Methods /// @brief Get the key expression of the query. @@ -209,7 +209,7 @@ class Query : public Owned<::z_owned_query_t> { /// /// The query responses will be sent only when the last clone is destroyed. Query clone() const { - Query q(nullptr); + Query q; ::z_query_clone(&q._0, interop::as_loaned_c_ptr(*this)); return q; }; diff --git a/include/zenoh/api/queryable.hxx b/include/zenoh/api/queryable.hxx index 7ffa35f2..9508f278 100644 --- a/include/zenoh/api/queryable.hxx +++ b/include/zenoh/api/queryable.hxx @@ -16,34 +16,41 @@ #include "base.hxx" namespace zenoh { + +class Session; +namespace detail { class QueryableBase : public Owned<::z_owned_queryable_t> { - public: - using Owned::Owned; + protected: + QueryableBase() : Owned(nullptr){}; + + friend class zenoh::Session; }; +} // namespace detail /// A Zenoh queryable. Constructed by ``Session::declare_queryable`` method. template -class Queryable : public QueryableBase { +class Queryable : public detail::QueryableBase { Handler _handler; - - public: - /// @name Constructors - - /// @internal - /// @brief Construct from queryable and handler. - Queryable(QueryableBase queryable, Handler handler) + Queryable(detail::QueryableBase queryable, Handler handler) : QueryableBase(std::move(queryable)), _handler(std::move(handler)) {} + public: /// @name Methods /// @brief Return handler to queryable data stream. const Handler& handler() const { return _handler; }; + + friend class Session; }; template <> -class Queryable : public QueryableBase { +class Queryable : public detail::QueryableBase { + Queryable() : QueryableBase(){}; + public: using QueryableBase::QueryableBase; + + friend class Session; }; } // namespace zenoh \ No newline at end of file diff --git a/include/zenoh/api/reply.hxx b/include/zenoh/api/reply.hxx index 4f705337..43bd0cdd 100644 --- a/include/zenoh/api/reply.hxx +++ b/include/zenoh/api/reply.hxx @@ -26,7 +26,6 @@ namespace zenoh { /// @brief Reply error data. class ReplyError : public Owned<::z_owned_reply_err_t> { public: - using Owned::Owned; /// @name Methods /// @brief The payload of this error. @@ -45,8 +44,6 @@ class ReplyError : public Owned<::z_owned_reply_err_t> { /// A reply from queryable to ``Session::get`` operation. class Reply : public Owned<::z_owned_reply_t> { public: - using Owned::Owned; - /// @name Methods /// @brief Check if the reply is OK (and contains a sample). diff --git a/include/zenoh/api/sample.hxx b/include/zenoh/api/sample.hxx index beb67421..9b3a6916 100644 --- a/include/zenoh/api/sample.hxx +++ b/include/zenoh/api/sample.hxx @@ -31,9 +31,9 @@ namespace zenoh { /// /// A sample is the value associated to a given resource at a given point in time. class Sample : public Owned<::z_owned_sample_t> { - public: - using Owned::Owned; + Sample() : Owned(nullptr){}; + public: /// @name Methods /// @brief Get the resource key of this sample. @@ -97,7 +97,7 @@ class Sample : public Owned<::z_owned_sample_t> { #endif /// @brief Construct a shallow copy of this sample. Sample clone() const { - Sample s(nullptr); + Sample s; ::z_sample_clone(&s._0, interop::as_loaned_c_ptr(*this)); return s; }; diff --git a/include/zenoh/api/session.hxx b/include/zenoh/api/session.hxx index bda14fb2..70a86dc8 100644 --- a/include/zenoh/api/session.hxx +++ b/include/zenoh/api/session.hxx @@ -41,9 +41,9 @@ namespace zenoh { /// A Zenoh session. class Session : public Owned<::z_owned_session_t> { - public: - using Owned::Owned; + Session() : Owned(nullptr){}; + public: /// @brief Options to be passed when opening a ``Session``. struct SessionOptions { /// @name Fields @@ -125,7 +125,7 @@ class Session : public Owned<::z_owned_session_t> { /// @brief Create a shallow copy of the session. /// @return a new ``Session`` instance. Session clone() const { - Session s(nullptr); + Session s; ::z_session_clone(&s._0, interop::as_loaned_c_ptr(*this)); return s; } @@ -399,7 +399,7 @@ class Session : public Owned<::z_owned_session_t> { z_queryable_options_default(&opts); opts.complete = options.complete; - Queryable q(nullptr); + Queryable q; ZResult res = ::z_declare_queryable(interop::as_owned_c_ptr(q), interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Queryable"); @@ -424,7 +424,7 @@ class Session : public Owned<::z_owned_session_t> { z_queryable_options_default(&opts); opts.complete = options.complete; - QueryableBase q(nullptr); + detail::QueryableBase q; ZResult res = ::z_declare_queryable(interop::as_owned_c_ptr(q), interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Queryable"); @@ -478,7 +478,7 @@ class Session : public Owned<::z_owned_session_t> { #else (void)options; #endif - Subscriber s(nullptr); + Subscriber s; ZResult res = ::z_declare_subscriber(interop::as_owned_c_ptr(s), interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Subscriber"); @@ -507,14 +507,13 @@ class Session : public Owned<::z_owned_session_t> { #else (void)options; #endif - SubscriberBase s(nullptr); - ZResult res = - ::z_declare_subscriber(interop::as_owned_c_ptr(s), interop::as_loaned_c_ptr(*this), - interop::as_loaned_c_ptr(key_expr), ::z_move(cb_handler_pair.first), &opts); + ::z_owned_subscriber_t s; + ZResult res = ::z_declare_subscriber(&s, interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), + ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Subscriber"); if (res != Z_OK) ::z_drop(interop::as_moved_c_ptr(cb_handler_pair.second)); - return Subscriber>(std::move(s), - std::move(cb_handler_pair.second)); + return Subscriber>( + std::move(interop::as_owned_cpp_ref(&s)), std::move(cb_handler_pair.second)); } /// @brief Options to be passed when declaring a ``Publisher``. @@ -558,7 +557,7 @@ class Session : public Owned<::z_owned_session_t> { #endif opts.encoding = interop::as_moved_c_ptr(options.encoding); - Publisher p(nullptr); + Publisher p = interop::detail::null(); ZResult res = ::z_declare_publisher(interop::as_owned_c_ptr(p), interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Publisher"); @@ -689,7 +688,7 @@ class Session : public Owned<::z_owned_session_t> { const KeyExpr& key_expr, LivelinessDeclarationOptions&& options = LivelinessDeclarationOptions::create_default(), ZResult* err = nullptr) { - LivelinessToken t(nullptr); + LivelinessToken t = interop::detail::null(); ::zc_liveliness_declaration_options_t opts; zc_liveliness_declaration_options_default(&opts); (void)options; @@ -737,7 +736,7 @@ class Session : public Owned<::z_owned_session_t> { ::zc_liveliness_subscriber_options_t opts; zc_liveliness_subscriber_options_default(&opts); (void)options; - Subscriber s(nullptr); + Subscriber s; ZResult res = ::zc_liveliness_declare_subscriber(interop::as_owned_c_ptr(s), interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts); @@ -763,14 +762,14 @@ class Session : public Owned<::z_owned_session_t> { ::zc_liveliness_subscriber_options_t opts; zc_liveliness_subscriber_options_default(&opts); (void)options; - SubscriberBase s(nullptr); - ZResult res = ::zc_liveliness_declare_subscriber(interop::as_owned_c_ptr(s), interop::as_loaned_c_ptr(*this), - interop::as_loaned_c_ptr(key_expr), - ::z_move(cb_handler_pair.first), &opts); + ::z_owned_subscriber_t s; + ZResult res = + ::zc_liveliness_declare_subscriber(&s, interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), + ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Liveliness Token Subscriber"); if (res != Z_OK) ::z_drop(::z_move(*interop::as_moved_c_ptr(cb_handler_pair.second))); - return Subscriber>(std::move(s), - std::move(cb_handler_pair.second)); + return Subscriber>( + std::move(interop::as_owned_cpp_ref(&s)), std::move(cb_handler_pair.second)); } /// @brief Options to pass to ``Session::liveliness_get``. diff --git a/include/zenoh/api/shm/buffer/zshm.hxx b/include/zenoh/api/shm/buffer/zshm.hxx index dceee487..58f24b53 100644 --- a/include/zenoh/api/shm/buffer/zshm.hxx +++ b/include/zenoh/api/shm/buffer/zshm.hxx @@ -27,8 +27,6 @@ class ZShm : public Owned<::z_owned_shm_t> { friend class ZShmMut; public: - using Owned::Owned; - /// @name Constructors /// @brief Create a new ZShm from ZShmMut. @@ -53,8 +51,8 @@ class ZShm : public Owned<::z_owned_shm_t> { static std::optional try_mutate(ZShm&& immut) { z_owned_shm_mut_t mut_inner; ::z_shm_mut_try_from_immut(&mut_inner, z_move(immut._0)); - if (z_internal_check(mut_inner)) { - return ZShmMut(&mut_inner); + if (::z_internal_check(mut_inner)) { + return std::move(interop::as_owned_cpp_ref(&mut_inner)); } return std::nullopt; } diff --git a/include/zenoh/api/shm/buffer/zshmmut.hxx b/include/zenoh/api/shm/buffer/zshmmut.hxx index c20976f5..e7cb8d5d 100644 --- a/include/zenoh/api/shm/buffer/zshmmut.hxx +++ b/include/zenoh/api/shm/buffer/zshmmut.hxx @@ -23,8 +23,6 @@ class ZShmMut : public Owned<::z_owned_shm_mut_t> { friend class ZShm; public: - using Owned::Owned; - /// @brief Get buffer's const data /// @return pointer to the underlying data const uint8_t* data() const { return ::z_shm_mut_data(interop::as_loaned_c_ptr(*this)); } diff --git a/include/zenoh/api/shm/client/shm_client.hxx b/include/zenoh/api/shm/client/shm_client.hxx index 681433e4..8c182b0e 100644 --- a/include/zenoh/api/shm/client/shm_client.hxx +++ b/include/zenoh/api/shm/client/shm_client.hxx @@ -51,8 +51,6 @@ class ShmClient : public Owned<::z_owned_shm_client_t> { friend class ShmClientStorage; public: - using Owned::Owned; - /// @name Constructors /// @brief Create a new CPP-defined ShmClient. diff --git a/include/zenoh/api/shm/client_storage/client_storage.hxx b/include/zenoh/api/shm/client_storage/client_storage.hxx index 70c4410f..04e87f41 100644 --- a/include/zenoh/api/shm/client_storage/client_storage.hxx +++ b/include/zenoh/api/shm/client_storage/client_storage.hxx @@ -25,9 +25,9 @@ namespace zenoh { /// @brief A storage for SHM clients. Session constructed with instance of this type /// gets capabilities to read SHM buffers for Protocols added to this instance. class ShmClientStorage : public Owned<::z_owned_shm_client_storage_t> { - public: using Owned::Owned; + public: /// @name Constructors /// @brief Create ShmClientStorage referencing a default global list of SHM clients diff --git a/include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx b/include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx index 7650e00f..16e64b33 100644 --- a/include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx +++ b/include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx @@ -29,7 +29,7 @@ class PosixShmProvider : public ShmProvider { /// @param layout layout for POSIX shared memory segment to be allocated and used by the provider /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. - PosixShmProvider(const MemoryLayout& layout, ZResult* err = nullptr) : ShmProvider(nullptr) { + PosixShmProvider(const MemoryLayout& layout, ZResult* err = nullptr) : ShmProvider() { __ZENOH_RESULT_CHECK(::z_posix_shm_provider_new(&this->_0, interop::as_loaned_c_ptr(layout)), err, "Failed to create POSIX SHM provider"); } diff --git a/include/zenoh/api/shm/provider/alloc_layout.hxx b/include/zenoh/api/shm/provider/alloc_layout.hxx index 68ee88e6..730d4ed9 100644 --- a/include/zenoh/api/shm/provider/alloc_layout.hxx +++ b/include/zenoh/api/shm/provider/alloc_layout.hxx @@ -48,8 +48,6 @@ inline void _z_alloc_layout_async_interface_drop_fn(void* context) { class AllocLayout : public Owned<::z_owned_alloc_layout_t> { public: - using Owned::Owned; - /// @name Constructors /// @brief Create a new Alloc Layout for SHM Provider. diff --git a/include/zenoh/api/shm/provider/shm_provider.hxx b/include/zenoh/api/shm/provider/shm_provider.hxx index a7de148d..61c85219 100644 --- a/include/zenoh/api/shm/provider/shm_provider.hxx +++ b/include/zenoh/api/shm/provider/shm_provider.hxx @@ -49,9 +49,10 @@ class ShmProviderAsyncInterface { class ShmProvider : public Owned<::z_owned_shm_provider_t> { friend class AllocLayout; - public: - using Owned::Owned; + protected: + ShmProvider() : Owned(nullptr){}; + public: BufLayoutAllocResult alloc(size_t size, AllocAlignment alignment) const { z_buf_layout_alloc_result_t result; ::z_shm_provider_alloc(&result, interop::as_loaned_c_ptr(*this), size, alignment); @@ -99,7 +100,7 @@ class ShmProvider : public Owned<::z_owned_shm_provider_t> { ZShmMut map(AllocatedChunk&& chunk, std::size_t len) const { z_owned_shm_mut_t result; ::z_shm_provider_map(&result, interop::as_loaned_c_ptr(*this), chunk, len); - return ZShmMut(&result); + return std::move(interop::as_owned_cpp_ref(&result)); } }; @@ -112,7 +113,7 @@ class CppShmProvider : public ShmProvider { /// @name Constructors /// @brief Create a new CPP-defined ShmProvider. - CppShmProvider(ProtocolId id, std::unique_ptr backend) : ShmProvider(nullptr) { + CppShmProvider(ProtocolId id, std::unique_ptr backend) : ShmProvider() { // init context zc_context_t context = {backend.release(), &shm::provider_backend::closures::_z_cpp_shm_provider_backend_drop_fn}; @@ -130,7 +131,7 @@ class CppShmProvider : public ShmProvider { } /// @brief Create a new CPP-defined threadsafe ShmProvider. - CppShmProvider(ProtocolId id, std::unique_ptr backend) : ShmProvider(nullptr) { + CppShmProvider(ProtocolId id, std::unique_ptr backend) : ShmProvider() { // init context ::zc_threadsafe_context_t context = {{backend.release()}, &shm::provider_backend::closures::_z_cpp_shm_provider_backend_drop_fn}; diff --git a/include/zenoh/api/shm/provider/shm_provider_backend.hxx b/include/zenoh/api/shm/provider/shm_provider_backend.hxx index 7db0f6c8..5bcc9de9 100644 --- a/include/zenoh/api/shm/provider/shm_provider_backend.hxx +++ b/include/zenoh/api/shm/provider/shm_provider_backend.hxx @@ -41,8 +41,8 @@ extern "C" { inline void _z_cpp_shm_provider_backend_drop_fn(void *context) { delete static_cast(context); } inline void _z_cpp_shm_provider_backend_alloc_fn(struct z_owned_chunk_alloc_result_t *out_result, const struct z_loaned_memory_layout_t *layout, void *context) { - *out_result = interop::move_to_c_obj(static_cast(context)->alloc( - interop::as_owned_cpp_ref(layout))); + *out_result = interop::move_to_c_obj( + static_cast(context)->alloc(interop::as_owned_cpp_ref(layout))); } inline void _z_cpp_shm_provider_backend_free_fn(const struct z_chunk_descriptor_t *chunk, void *context) { static_cast(context)->free(*chunk); @@ -54,8 +54,7 @@ inline size_t _z_cpp_shm_provider_backend_available_fn(void *context) { return static_cast(context)->available(); } inline void _z_cpp_shm_provider_backend_layout_for_fn(struct z_owned_memory_layout_t *layout, void *context) { - static_cast(context)->layout_for( - interop::as_owned_cpp_ref(layout)); + static_cast(context)->layout_for(interop::as_owned_cpp_ref(layout)); } } } // namespace shm::provider_backend::closures diff --git a/include/zenoh/api/shm/provider/types.hxx b/include/zenoh/api/shm/provider/types.hxx index 5c8ece8d..93a89556 100644 --- a/include/zenoh/api/shm/provider/types.hxx +++ b/include/zenoh/api/shm/provider/types.hxx @@ -16,6 +16,7 @@ #include +#include "../../interop.hxx" #include "../buffer/zshmmut.hxx" namespace zenoh { @@ -44,8 +45,6 @@ class MemoryLayout : public Owned<::z_owned_memory_layout_t> { friend class PosixShmProvider; public: - using Owned::Owned; - /// @name Constructors /// @brief Create a new MemoryLayout. @@ -78,8 +77,6 @@ class ChunkAllocResult : public Owned<::z_owned_chunk_alloc_result_t> { friend class CppShmProviderBackend; public: - using Owned::Owned; - /// @name Constructors /// @brief Create a new ChunkAllocResult that carries successfuly allocated chunk. diff --git a/include/zenoh/api/shm/provider/types_impl.hxx b/include/zenoh/api/shm/provider/types_impl.hxx index a486d7c5..0a5f99f8 100644 --- a/include/zenoh/api/shm/provider/types_impl.hxx +++ b/include/zenoh/api/shm/provider/types_impl.hxx @@ -14,6 +14,7 @@ #pragma once +#include "../../interop.hxx" #include "types.hxx" namespace zenoh { @@ -22,7 +23,7 @@ struct Converters { static inline BufLayoutAllocResult from(z_buf_layout_alloc_result_t& c_result) { switch (c_result.status) { case zc_buf_layout_alloc_status_t::ZC_BUF_LAYOUT_ALLOC_STATUS_OK: - return ZShmMut(&c_result.buf); + return std::move(interop::as_owned_cpp_ref(&c_result.buf)); case zc_buf_layout_alloc_status_t::ZC_BUF_LAYOUT_ALLOC_STATUS_LAYOUT_ERROR: return c_result.layout_error; case zc_buf_layout_alloc_status_t::ZC_BUF_LAYOUT_ALLOC_STATUS_ALLOC_ERROR: @@ -34,7 +35,7 @@ struct Converters { static inline BufAllocResult from(z_buf_alloc_result_t& c_result) { switch (c_result.status) { case zc_buf_alloc_status_t::ZC_BUF_ALLOC_STATUS_OK: - return ZShmMut(&c_result.buf); + return std::move(interop::as_owned_cpp_ref(&c_result.buf)); case zc_buf_alloc_status_t::ZC_BUF_ALLOC_STATUS_ALLOC_ERROR: default: return c_result.error; diff --git a/include/zenoh/api/source_info.hxx b/include/zenoh/api/source_info.hxx index e8659632..6f35f1eb 100644 --- a/include/zenoh/api/source_info.hxx +++ b/include/zenoh/api/source_info.hxx @@ -38,8 +38,6 @@ class EntityGlobalId : public Copyable<::z_entity_global_id_t> { /// @brief Informations on the Zenoh source. class SourceInfo : public Owned<::z_owned_source_info_t> { public: - using Owned::Owned; - /// @name Constructors /// @brief Construct from global id and sequence number. diff --git a/include/zenoh/api/subscriber.hxx b/include/zenoh/api/subscriber.hxx index 842d04e8..6aefb5b1 100644 --- a/include/zenoh/api/subscriber.hxx +++ b/include/zenoh/api/subscriber.hxx @@ -19,10 +19,13 @@ namespace zenoh { +class Session; +namespace detail { class SubscriberBase : public Owned<::z_owned_subscriber_t> { - public: - using Owned::Owned; + protected: + SubscriberBase() : Owned(nullptr){}; + public: #ifdef ZENOHCXX_ZENOHC /// @brief Get the key expression of the subscriber /// @note zenoh-c only. @@ -30,22 +33,18 @@ class SubscriberBase : public Owned<::z_owned_subscriber_t> { return interop::as_owned_cpp_ref(::z_subscriber_keyexpr(interop::as_loaned_c_ptr(*this))); } #endif + friend class zenoh::Session; }; - +} // namespace detail /// A Zenoh subscriber. Destroying subscriber cancels the subscription. /// Constructed by ``Session::declare_subscriber`` method. template -class Subscriber : public SubscriberBase { +class Subscriber : public detail::SubscriberBase { Handler _handler; - - public: - /// @name Constructors - - /// @internal - /// @brief Construct from subscriber and handler. Subscriber(SubscriberBase subscriber, Handler handler) : SubscriberBase(std::move(subscriber)), _handler(std::move(handler)) {} + public: /// @name Methods #ifdef ZENOHCXX_ZENOHC @@ -53,12 +52,14 @@ class Subscriber : public SubscriberBase { #endif /// @brief Return the handler to subscriber data stream. const Handler& handler() const { return _handler; }; + friend class Session; }; template <> -class Subscriber : public SubscriberBase { - public: +class Subscriber : public detail::SubscriberBase { + protected: using SubscriberBase::SubscriberBase; + friend class Session; }; } // namespace zenoh \ No newline at end of file diff --git a/tests/zenohc/shm_api.cxx b/tests/zenohc/shm_api.cxx index 086475f1..2b1725e8 100644 --- a/tests/zenohc/shm_api.cxx +++ b/tests/zenohc/shm_api.cxx @@ -216,7 +216,7 @@ class TestShmProviderBackend : public CppShmProviderBackend { // incorrect layout will be invalidated if (size != 1 || alignment.pow != 0) { - layout = MemoryLayout(nullptr); + layout = interop::detail::null(); } } }; From 8e7801fedcafd39ee69b13b118b375a688f4ff78 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 29 Aug 2024 19:07:37 +0200 Subject: [PATCH 3/9] keyexpr fix --- include/zenoh/api/keyexpr.hxx | 2 -- include/zenoh/api/session.hxx | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/include/zenoh/api/keyexpr.hxx b/include/zenoh/api/keyexpr.hxx index b4c323b1..29b5f919 100644 --- a/include/zenoh/api/keyexpr.hxx +++ b/include/zenoh/api/keyexpr.hxx @@ -19,7 +19,6 @@ #include "interop.hxx" namespace zenoh { -class Session; /// @brief A Zenoh key expression . /// @@ -28,7 +27,6 @@ class Session; /// when transporting key expressions. class KeyExpr : public Owned<::z_owned_keyexpr_t> { - friend Session; KeyExpr() : Owned(nullptr){}; public: diff --git a/include/zenoh/api/session.hxx b/include/zenoh/api/session.hxx index 70a86dc8..7e7828b8 100644 --- a/include/zenoh/api/session.hxx +++ b/include/zenoh/api/session.hxx @@ -143,7 +143,7 @@ class Session : public Owned<::z_owned_session_t> { /// thrown in case of error. /// @return Declared ``KeyExpr`` instance. KeyExpr declare_keyexpr(const KeyExpr& key_expr, ZResult* err = nullptr) const { - KeyExpr k; + KeyExpr k = interop::detail::null(); __ZENOH_RESULT_CHECK(::z_declare_keyexpr(interop::as_owned_c_ptr(k), interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr)), err, std::string("Failed to declare key expression: ").append(k.as_string_view())); From 4694861f653a23e9e5237cfb27f99df4de68def0 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Fri, 30 Aug 2024 11:57:35 +0200 Subject: [PATCH 4/9] hide copyable constructor from c objects and expose it through interop --- include/zenoh/api/base.hxx | 8 +------- include/zenoh/api/bytes.hxx | 21 ++++++++++++++------- include/zenoh/api/hello.hxx | 2 +- include/zenoh/api/id.hxx | 4 +++- include/zenoh/api/interop.hxx | 22 ++++++++++++++++++---- include/zenoh/api/keyexpr.hxx | 1 + include/zenoh/api/liveliness.hxx | 1 + include/zenoh/api/publisher.hxx | 5 ++++- include/zenoh/api/query.hxx | 1 + include/zenoh/api/query_consolidation.hxx | 4 +++- include/zenoh/api/reply.hxx | 5 ++++- include/zenoh/api/sample.hxx | 1 + include/zenoh/api/session.hxx | 8 ++++---- include/zenoh/api/shm/buffer/zshm.hxx | 2 ++ include/zenoh/api/shm/provider/types.hxx | 2 ++ include/zenoh/api/source_info.hxx | 10 +++++++--- include/zenoh/api/timestamp.hxx | 5 +++-- 17 files changed, 70 insertions(+), 32 deletions(-) diff --git a/include/zenoh/api/base.hxx b/include/zenoh/api/base.hxx index 76d161c2..d5e5b616 100644 --- a/include/zenoh/api/base.hxx +++ b/include/zenoh/api/base.hxx @@ -55,13 +55,7 @@ class Copyable { InnerType& inner() { return this->_0; } const InnerType& inner() const { return this->_0; } - - public: - /// @name Constructors - /// Construct from wrapped zenoh-c / zenoh-pico structure explicit Copyable(const InnerType& v) : _0(v) {} - explicit operator const ZC_COPYABLE_TYPE&() const { return inner(); } - explicit operator ZC_COPYABLE_TYPE&() { return inner(); } }; /// Base type for C++ wrappers of Zenoh owned structures @@ -98,4 +92,4 @@ class Owned { } }; -} // namespace zenoh \ No newline at end of file +} // namespace zenoh diff --git a/include/zenoh/api/bytes.hxx b/include/zenoh/api/bytes.hxx index f7534582..5d4fd94e 100644 --- a/include/zenoh/api/bytes.hxx +++ b/include/zenoh/api/bytes.hxx @@ -154,9 +154,10 @@ class Bytes : public Owned<::z_owned_bytes_t> { /// @brief A reader for Zenoh-serialized data. class Reader : public Copyable<::z_bytes_reader_t> { - public: using Copyable::Copyable; + friend struct interop::detail::Converter; + public: /// @name Methods /// @brief Read data into specified destination. @@ -210,13 +211,16 @@ class Bytes : public Owned<::z_owned_bytes_t> { /// @brief Create data reader. /// @return reader instance. - Reader reader() const { return Reader(::z_bytes_get_reader(interop::as_loaned_c_ptr(*this))); } + Reader reader() const { + return interop::into_copyable_cpp_obj(::z_bytes_get_reader(interop::as_loaned_c_ptr(*this))); + } /// @brief A writer for Zenoh-serialized data. class Writer : public Copyable<::z_bytes_writer_t> { - public: using Copyable::Copyable; + friend struct interop::detail::Converter; + public: /// @name Methods /// @brief Copy data from sepcified source into underlying ``Bytes`` instance. @@ -256,14 +260,17 @@ class Bytes : public Owned<::z_owned_bytes_t> { /// It is the user responsibility to ensure that there is at most one active writer at /// a given moment of time for a given ``Bytes`` instance. /// @return writer instance. - Writer writer() { return Writer(::z_bytes_get_writer(interop::as_loaned_c_ptr(*this))); } + Writer writer() { + return interop::into_copyable_cpp_obj(::z_bytes_get_writer(interop::as_loaned_c_ptr(*this))); + } }; /// @brief An iterator over multi-element serialized data. class Bytes::Iterator : Copyable<::z_bytes_iterator_t> { - public: using Copyable::Copyable; + friend struct interop::detail::Converter; + public: /// @name Methods /// @brief Return next element of serialized data. @@ -278,7 +285,7 @@ class Bytes::Iterator : Copyable<::z_bytes_iterator_t> { }; inline Bytes::Iterator Bytes::iter() const { - return Bytes::Iterator(::z_bytes_get_iterator(interop::as_loaned_c_ptr(*this))); + return interop::into_copyable_cpp_obj(::z_bytes_get_iterator(interop::as_loaned_c_ptr(*this))); } namespace detail { @@ -442,7 +449,7 @@ struct Slice { size_t len; }; -auto make_slice(const uint8_t* data, size_t len) { return Slice{data, len}; }; +inline auto make_slice(const uint8_t* data, size_t len) { return Slice{data, len}; }; template struct OwnedSlice { diff --git a/include/zenoh/api/hello.hxx b/include/zenoh/api/hello.hxx index db07b068..e247b649 100644 --- a/include/zenoh/api/hello.hxx +++ b/include/zenoh/api/hello.hxx @@ -33,7 +33,7 @@ class Hello : public Owned<::z_owned_hello_t> { #if defined UNSTABLE /// @brief Get ``Id`` of the entity. /// @return ``Id`` of the entity. - Id get_id() const { return Id(::z_hello_zid(interop::as_loaned_c_ptr(*this))); }; + Id get_id() const { return interop::into_copyable_cpp_obj(::z_hello_zid(interop::as_loaned_c_ptr(*this))); }; #endif /// @brief Get the type of the entity. diff --git a/include/zenoh/api/id.hxx b/include/zenoh/api/id.hxx index 704f8a65..59c3e4b5 100644 --- a/include/zenoh/api/id.hxx +++ b/include/zenoh/api/id.hxx @@ -19,15 +19,17 @@ #include "../zenohc.hxx" #include "base.hxx" +#include "interop.hxx" namespace zenoh { /// @brief A representation a Zenoh ID. /// /// In general, valid Zenoh IDs are LSB-first 128bit unsigned and non-zero integers. class Id : public Copyable<::z_id_t> { - public: using Copyable::Copyable; + friend struct interop::detail::Converter; + public: /// @name Methods /// Check if the ID is valid. diff --git a/include/zenoh/api/interop.hxx b/include/zenoh/api/interop.hxx index 15617d64..cf2a7706 100644 --- a/include/zenoh/api/interop.hxx +++ b/include/zenoh/api/interop.hxx @@ -205,14 +205,28 @@ bool check(const Owned& owned_cpp_obj) { return ::z_internal_check(*as_owned_c_ptr(owned_cpp_obj)); } +struct Converter { + template + static T null_owned() { + return T(); + } + template + static T copyable_to_cpp(const Inner& i) { + return T(i); + } +}; + template T null() { - using ZCtype = std::remove_cv_t()))>>; - ZCtype z; - ::z_internal_null(&z); - return std::move(as_owned_cpp_ref(&z)); + return Converter::null_owned(); } } // namespace detail +/// @brief Copy copyable Zenoh-c struct into corresponding C++ object. +template +T into_copyable_cpp_obj(const CopyableType& copyable_c_obj) { + return detail::Converter::copyable_to_cpp(copyable_c_obj); +} + } // namespace zenoh::interop \ No newline at end of file diff --git a/include/zenoh/api/keyexpr.hxx b/include/zenoh/api/keyexpr.hxx index 29b5f919..6988ea6b 100644 --- a/include/zenoh/api/keyexpr.hxx +++ b/include/zenoh/api/keyexpr.hxx @@ -28,6 +28,7 @@ namespace zenoh { class KeyExpr : public Owned<::z_owned_keyexpr_t> { KeyExpr() : Owned(nullptr){}; + friend struct interop::detail::Converter; public: /// @name Constructors diff --git a/include/zenoh/api/liveliness.hxx b/include/zenoh/api/liveliness.hxx index 194329bf..75a7b456 100644 --- a/include/zenoh/api/liveliness.hxx +++ b/include/zenoh/api/liveliness.hxx @@ -30,6 +30,7 @@ class Session; /// @note zenoh-c only. class LivelinessToken : public Owned<::zc_owned_liveliness_token_t> { LivelinessToken() : Owned(nullptr){}; + friend struct interop::detail::Converter; public: /// Undeclares liveliness token, resetting it to gravestone state. diff --git a/include/zenoh/api/publisher.hxx b/include/zenoh/api/publisher.hxx index c5601f87..a3921662 100644 --- a/include/zenoh/api/publisher.hxx +++ b/include/zenoh/api/publisher.hxx @@ -32,6 +32,7 @@ class Session; /// A Zenoh publisher. Constructed by ``Session::declare_publisher`` method. class Publisher : public Owned<::z_owned_publisher_t> { Publisher() : Owned(nullptr){}; + friend struct interop::detail::Converter; public: /// @brief Options to be passed to ``Publisher::put`` operation. @@ -108,7 +109,9 @@ class Publisher : public Owned<::z_owned_publisher_t> { #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) /// @brief Get the id of the publisher. /// @return id of this publisher. - EntityGlobalId get_id() const { return EntityGlobalId(::z_publisher_id(interop::as_loaned_c_ptr(*this))); } + EntityGlobalId get_id() const { + return interop::into_copyable_cpp_obj(::z_publisher_id(interop::as_loaned_c_ptr(*this))); + } #endif }; } // namespace zenoh diff --git a/include/zenoh/api/query.hxx b/include/zenoh/api/query.hxx index cc9dd39b..98c6fef0 100644 --- a/include/zenoh/api/query.hxx +++ b/include/zenoh/api/query.hxx @@ -32,6 +32,7 @@ namespace zenoh { /// The query to be answered by a ``Queryable``. class Query : public Owned<::z_owned_query_t> { Query() : Owned(nullptr){}; + friend struct interop::detail::Converter; public: /// @name Methods diff --git a/include/zenoh/api/query_consolidation.hxx b/include/zenoh/api/query_consolidation.hxx index 9dab3790..a8e054e5 100644 --- a/include/zenoh/api/query_consolidation.hxx +++ b/include/zenoh/api/query_consolidation.hxx @@ -15,14 +15,16 @@ #include "base.hxx" #include "enums.hxx" +#include "interop.hxx" namespace zenoh { /// Replies consolidation mode to apply on replies of get operation. class QueryConsolidation : public Copyable<::z_query_consolidation_t> { - public: using Copyable::Copyable; + friend struct interop::detail::Converter; + public: /// @name Constructors /// @brief Create a new default ``QueryConsolidation`` value. diff --git a/include/zenoh/api/reply.hxx b/include/zenoh/api/reply.hxx index 43bd0cdd..2b5218ab 100644 --- a/include/zenoh/api/reply.hxx +++ b/include/zenoh/api/reply.hxx @@ -43,6 +43,9 @@ class ReplyError : public Owned<::z_owned_reply_err_t> { /// A reply from queryable to ``Session::get`` operation. class Reply : public Owned<::z_owned_reply_t> { + Reply() : Owned(nullptr){}; + friend struct interop::detail::Converter; + public: /// @name Methods @@ -74,7 +77,7 @@ class Reply : public Owned<::z_owned_reply_t> { std::optional get_replier_id() const { ::z_id_t z_id; if (::z_reply_replier_id(interop::as_loaned_c_ptr(*this), &z_id)) { - return Id(z_id); + return interop::into_copyable_cpp_obj(z_id); } return {}; } diff --git a/include/zenoh/api/sample.hxx b/include/zenoh/api/sample.hxx index 9b3a6916..55e1ec9b 100644 --- a/include/zenoh/api/sample.hxx +++ b/include/zenoh/api/sample.hxx @@ -32,6 +32,7 @@ namespace zenoh { /// A sample is the value associated to a given resource at a given point in time. class Sample : public Owned<::z_owned_sample_t> { Sample() : Owned(nullptr){}; + friend struct interop::detail::Converter; public: /// @name Methods diff --git a/include/zenoh/api/session.hxx b/include/zenoh/api/session.hxx index 7e7828b8..d2dd8955 100644 --- a/include/zenoh/api/session.hxx +++ b/include/zenoh/api/session.hxx @@ -133,7 +133,7 @@ class Session : public Owned<::z_owned_session_t> { #if defined UNSTABLE /// @brief Get the unique identifier of the zenoh node associated to this ``Session``. /// @return the unique identifier ``Id``. - Id get_zid() const { return Id(::z_info_zid(interop::as_loaned_c_ptr(*this))); } + Id get_zid() const { return interop::into_copyable_cpp_obj(::z_info_zid(interop::as_loaned_c_ptr(*this))); } #endif /// @brief Create ``KeyExpr`` instance with numeric id registered in ``Session`` routing tables (to reduce bandwith @@ -216,7 +216,7 @@ class Session : public Owned<::z_owned_session_t> { ::z_get_options_t opts; z_get_options_default(&opts); opts.target = options.target; - opts.consolidation = static_cast(options.consolidation); + opts.consolidation = *interop::as_copyable_c_ptr(options.consolidation); opts.congestion_control = options.congestion_control; opts.priority = options.priority; opts.is_express = options.is_express; @@ -252,7 +252,7 @@ class Session : public Owned<::z_owned_session_t> { ::z_get_options_t opts; z_get_options_default(&opts); opts.target = options.target; - opts.consolidation = static_cast(options.consolidation); + opts.consolidation = *interop::as_copyable_c_ptr(options.consolidation); opts.payload = interop::as_moved_c_ptr(options.payload); opts.encoding = interop::as_moved_c_ptr(options.encoding); #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) @@ -848,7 +848,7 @@ class Session : public Owned<::z_owned_session_t> { Timestamp new_timestamp(ZResult* err = nullptr) { ::z_timestamp_t t; __ZENOH_RESULT_CHECK(z_timestamp_new(&t, interop::as_loaned_c_ptr(*this)), err, "Failed to create timestamp"); - return Timestamp(t); + return interop::as_copyable_cpp_ref(&t); } }; } // namespace zenoh \ No newline at end of file diff --git a/include/zenoh/api/shm/buffer/zshm.hxx b/include/zenoh/api/shm/buffer/zshm.hxx index 58f24b53..a6187e54 100644 --- a/include/zenoh/api/shm/buffer/zshm.hxx +++ b/include/zenoh/api/shm/buffer/zshm.hxx @@ -25,6 +25,8 @@ namespace zenoh { /// @brief An immutable SHM buffer class ZShm : public Owned<::z_owned_shm_t> { friend class ZShmMut; + ZShm() : Owned(nullptr){}; + friend struct interop::detail::Converter; public: /// @name Constructors diff --git a/include/zenoh/api/shm/provider/types.hxx b/include/zenoh/api/shm/provider/types.hxx index 93a89556..f7b2221c 100644 --- a/include/zenoh/api/shm/provider/types.hxx +++ b/include/zenoh/api/shm/provider/types.hxx @@ -43,6 +43,8 @@ typedef ::z_alloc_alignment_t AllocAlignment; class MemoryLayout : public Owned<::z_owned_memory_layout_t> { friend class PosixShmProvider; + MemoryLayout() : Owned(nullptr){}; + friend struct interop::detail::Converter; public: /// @name Constructors diff --git a/include/zenoh/api/source_info.hxx b/include/zenoh/api/source_info.hxx index 6f35f1eb..2dbfef1d 100644 --- a/include/zenoh/api/source_info.hxx +++ b/include/zenoh/api/source_info.hxx @@ -24,12 +24,14 @@ namespace zenoh { #ifdef ZENOHCXX_ZENOHC /// The global unique id of a Zenoh entity. class EntityGlobalId : public Copyable<::z_entity_global_id_t> { - public: using Copyable::Copyable; + friend struct interop::detail::Converter; + + public: /// @name Methods /// Get Zenoh id. - Id id() const { return Id(::z_entity_global_id_zid(&this->inner())); } + Id id() const { return interop::into_copyable_cpp_obj(::z_entity_global_id_zid(&this->inner())); } /// Get eid. uint32_t eid() const { return ::z_entity_global_id_eid(&this->inner()); } @@ -48,7 +50,9 @@ class SourceInfo : public Owned<::z_owned_source_info_t> { /// @name Methods /// @brief Get the source id. - EntityGlobalId id() const { return EntityGlobalId(::z_source_info_id(interop::as_loaned_c_ptr(*this))); } + EntityGlobalId id() const { + return interop::into_copyable_cpp_obj(::z_source_info_id(interop::as_loaned_c_ptr(*this))); + } /// @brief Get the sequence number of the sample from the given source. uint64_t sn() const { return ::z_source_info_sn(interop::as_loaned_c_ptr(*this)); } diff --git a/include/zenoh/api/timestamp.hxx b/include/zenoh/api/timestamp.hxx index f158404a..d79c3ede 100644 --- a/include/zenoh/api/timestamp.hxx +++ b/include/zenoh/api/timestamp.hxx @@ -22,9 +22,10 @@ namespace zenoh { /// Zenoh Timestamp. class Timestamp : public Copyable<::z_timestamp_t> { - public: using Copyable::Copyable; + friend struct interop::detail::Converter; + public: /// @name Methods /// @brief Get the NTP64 time part of the timestamp. @@ -34,7 +35,7 @@ class Timestamp : public Copyable<::z_timestamp_t> { #if defined UNSTABLE /// @brief Get the unique id of the timestamp. /// @return Id associated with this timestamp. - Id get_id() const { return Id(::z_timestamp_id(&this->inner())); } + Id get_id() const { return interop::into_copyable_cpp_obj(::z_timestamp_id(&this->inner())); } #endif }; From b4da312d81af729e4d97686d9db65cf6aecd4d21 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Fri, 30 Aug 2024 12:11:40 +0200 Subject: [PATCH 5/9] docs update --- docs/api.rst | 3 ++- docs/interop.rst | 19 +++++++++++++++++++ include/zenoh/api/interop.hxx | 34 +++++++++++++++++----------------- 3 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 docs/interop.rst diff --git a/docs/api.rst b/docs/api.rst index 47002471..f9f5223a 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -28,4 +28,5 @@ API Reference publish_subscribe query_reply serialization_deserialization - channels \ No newline at end of file + channels + interop \ No newline at end of file diff --git a/docs/interop.rst b/docs/interop.rst new file mode 100644 index 00000000..31593983 --- /dev/null +++ b/docs/interop.rst @@ -0,0 +1,19 @@ +.. +.. 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, +.. + +Interoperability with Zenoh-c / Zenoh-pico +========================================== + +.. doxygennamespace:: zenoh::interop + :content-only: diff --git a/include/zenoh/api/interop.hxx b/include/zenoh/api/interop.hxx index cf2a7706..693b3366 100644 --- a/include/zenoh/api/interop.hxx +++ b/include/zenoh/api/interop.hxx @@ -40,59 +40,59 @@ inline constexpr bool is_loan_mut_available_v = is_loan_mut_available::value; namespace zenoh::interop { -/// @brief Get C representation of trivially copyable Zenoh-object. +/// @brief Get C representation of trivially copyable Zenoh-c++ object. template CopyableType* as_copyable_c_ptr(Copyable& cpp_obj) { return reinterpret_cast(&cpp_obj); } -/// @brief Get C representation of trivially copyable Zenoh-object. +/// @brief Get C representation of trivially copyable Zenoh-c++ object. template const CopyableType* as_copyable_c_ptr(const Copyable& cpp_obj) { return reinterpret_cast(&cpp_obj); } -/// @brief Get C representation of std::optional of trivially copyable Zenoh-object. -/// @return Pointer to C representation of trivially copyable Zenoh-object, or NULL if cpp_obj is empty. +/// @brief Get C representation of std::optional of trivially copyable Zenoh-c++ object. +/// @return Pointer to C representation of trivially copyable Zenoh-c++ object, or NULL if cpp_obj is empty. template auto* as_copyable_c_ptr(std::optional& cpp_obj) { return cpp_obj.has_value() ? as_copyable_c_ptr(cpp_obj.value()) : nullptr; } -/// @brief Get C representation of std::optional of trivially copyable Zenoh-object. -/// @return Pointer to C representation of trivially copyable Zenoh-object, or NULL if cpp_obj is empty. +/// @brief Get C representation of std::optional of trivially copyable Zenoh-c++ object. +/// @return Pointer to C representation of trivially copyable Zenoh-c++ object, or NULL if cpp_obj is empty. template const auto* as_copyable_c_ptr(const std::optional& cpp_obj) { return cpp_obj.has_value() ? as_copyable_c_ptr(cpp_obj.value()) : nullptr; } -/// @brief Get owned C representation of owned Zenoh-object. +/// @brief Get owned C representation of owned Zenoh-c++ object. template OwnedType* as_owned_c_ptr(Owned& cpp_obj) { return reinterpret_cast(&cpp_obj); } -/// @brief Get owned C representation of owned Zenoh-object. +/// @brief Get owned C representation of owned Zenoh-c++ object. template const OwnedType* as_owned_c_ptr(const Owned& cpp_obj) { return reinterpret_cast(&cpp_obj); } -/// @brief Get owned C representation of owned Zenoh-object. -/// @return Pointer to owned C representation of owned Zenoh-object, or NULL if cpp_obj is empty. +/// @brief Get owned C representation of owned Zenoh-c++ object. +/// @return Pointer to owned C representation of owned Zenoh-c++ object, or NULL if cpp_obj is empty. template auto* as_owned_c_ptr(std::optional& cpp_obj) { return cpp_obj.has_value() ? as_owned_c_ptr(cpp_obj.value()) : nullptr; } -/// @brief Get owned C representation of owned Zenoh-object. -/// @return Pointer to owned C representation of owned Zenoh-object, or NULL if cpp_obj is empty. +/// @brief Get owned C representation of owned Zenoh-c++ object. +/// @return Pointer to owned C representation of owned Zenoh-c++ object, or NULL if cpp_obj is empty. template const auto* as_owned_c_ptr(const std::optional& cpp_obj) { return cpp_obj.has_value() ? as_owned_c_ptr(cpp_obj.value()) : nullptr; } -/// @brief Get loaned C representation of owned Zenoh-object. +/// @brief Get loaned C representation of owned Zenoh-c++ object. template < class OwnedType, class Loaned = typename ::z_owned_to_loaned_type_t::type, // SFINAE here if no loaned type declared @@ -103,7 +103,7 @@ const Loaned* as_loaned_c_ptr(const Owned& cpp_obj) { return ::z_loan(*as_owned_c_ptr(cpp_obj)); } -/// @brief Get loaned C representation of owned Zenoh-object. +/// @brief Get loaned C representation of owned Zenoh-c++ object. template ::type, // SFINAE here if no loaned type // declared @@ -114,14 +114,14 @@ Loaned* as_loaned_c_ptr(Owned& cpp_obj) { return ::z_loan_mut(*as_owned_c_ptr(cpp_obj)); } -/// @brief Get moved C representation of owned Zenoh-object. +/// @brief Get moved C representation of owned Zenoh-c++ object. template auto* as_moved_c_ptr(Owned& cpp_obj) { return ::z_move(*as_owned_c_ptr(cpp_obj)); } -/// @brief Get moved C representation of std::optional of owned Zenoh-object. -/// @return Pointer to C representation of moved owned Zenoh-object, or NULL if cpp_obj is empty. +/// @brief Get moved C representation of std::optional of owned Zenoh-c++ object. +/// @return Pointer to C representation of moved owned Zenoh-c++ object, or NULL if cpp_obj is empty. template auto* as_moved_c_ptr(std::optional& cpp_obj) { return cpp_obj.has_value() ? ::z_move(*as_owned_c_ptr(cpp_obj.value())) : nullptr; From 6d377bf9cd522bd63d8822c5776bb073d612b0a6 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Fri, 30 Aug 2024 14:11:20 +0200 Subject: [PATCH 6/9] interop for subscriber and queryable with handlers --- include/zenoh/api/interop.hxx | 14 ------ include/zenoh/api/queryable.hxx | 86 ++++++++++++++++++++++++++++---- include/zenoh/api/session.hxx | 13 ++--- include/zenoh/api/subscriber.hxx | 86 +++++++++++++++++++++++++++++--- 4 files changed, 162 insertions(+), 37 deletions(-) diff --git a/include/zenoh/api/interop.hxx b/include/zenoh/api/interop.hxx index 693b3366..6f8db4f2 100644 --- a/include/zenoh/api/interop.hxx +++ b/include/zenoh/api/interop.hxx @@ -78,20 +78,6 @@ const OwnedType* as_owned_c_ptr(const Owned& cpp_obj) { return reinterpret_cast(&cpp_obj); } -/// @brief Get owned C representation of owned Zenoh-c++ object. -/// @return Pointer to owned C representation of owned Zenoh-c++ object, or NULL if cpp_obj is empty. -template -auto* as_owned_c_ptr(std::optional& cpp_obj) { - return cpp_obj.has_value() ? as_owned_c_ptr(cpp_obj.value()) : nullptr; -} - -/// @brief Get owned C representation of owned Zenoh-c++ object. -/// @return Pointer to owned C representation of owned Zenoh-c++ object, or NULL if cpp_obj is empty. -template -const auto* as_owned_c_ptr(const std::optional& cpp_obj) { - return cpp_obj.has_value() ? as_owned_c_ptr(cpp_obj.value()) : nullptr; -} - /// @brief Get loaned C representation of owned Zenoh-c++ object. template < class OwnedType, diff --git a/include/zenoh/api/queryable.hxx b/include/zenoh/api/queryable.hxx index 9508f278..7e40cc15 100644 --- a/include/zenoh/api/queryable.hxx +++ b/include/zenoh/api/queryable.hxx @@ -22,19 +22,43 @@ namespace detail { class QueryableBase : public Owned<::z_owned_queryable_t> { protected: QueryableBase() : Owned(nullptr){}; + QueryableBase(::z_owned_queryable_t* q) : Owned(q){}; friend class zenoh::Session; }; } // namespace detail +template +class Queryable; + +template <> +class Queryable : public detail::QueryableBase { + Queryable() : QueryableBase(){}; + + public: + using QueryableBase::QueryableBase; + + friend class Session; +}; + /// A Zenoh queryable. Constructed by ``Session::declare_queryable`` method. +/// @tparam Handler Streaming handler exposing data. If `void`, no handler access is provided and instead data is being +/// processed inside the callback. template class Queryable : public detail::QueryableBase { Handler _handler; - Queryable(detail::QueryableBase queryable, Handler handler) - : QueryableBase(std::move(queryable)), _handler(std::move(handler)) {} public: + /// @name Constructors + + /// @brief Construct stream queryable from callback queryable and handler. + /// + /// @param q Callback queryable, that should expose data to the handler in its callback. + /// @param handler Handler to access data exposed by q. Zenoh handlers implement + /// recv and try_recv methods, for blocking and non-blocking message reception. But user is free to define his own + /// interface. + Queryable(Queryable&& q, Handler handler) + : QueryableBase(interop::as_owned_c_ptr(q)), _handler(std::move(handler)) {} /// @name Methods /// @brief Return handler to queryable data stream. @@ -43,14 +67,58 @@ class Queryable : public detail::QueryableBase { friend class Session; }; -template <> -class Queryable : public detail::QueryableBase { - Queryable() : QueryableBase(){}; +namespace interop { +/// @brief Return a pair of pointers to owned C representations of queryable and its callback. +template >> +auto as_owned_c_ptr(Queryable& q) { + return make_pair(as_owned_c_ptr(static_cast(q)), + as_owned_c_ptr(const_cast(q.handler()))); +} - public: - using QueryableBase::QueryableBase; +/// @brief Return a pair of pointers to owned C representations of queryable and its handler. +template >> +auto as_owned_c_ptr(const Queryable& q) { + return make_pair(as_owned_c_ptr(static_cast(q)), as_owned_c_ptr(q.handler())); +} - friend class Session; -}; +/// @brief Return a pair of pointers to loaned C representations of queryable and its handler. +template >> +auto as_loaned_c_ptr(Queryable& q) { + return make_pair(as_loaned_c_ptr(static_cast(q)), + as_loaned_c_ptr(const_cast(q.handler()))); +} + +/// @brief Return a pair of pointers to loaned C representation of queryable and its handler. +template >> +auto as_loaned_c_ptr(const Queryable& q) { + return make_pair(as_loaned_c_ptr(static_cast(q)), + as_loaned_c_ptr(q.handler())); +} + +/// @brief Return a pair of pointers to moved C representation of queryable and its handler. +template >> +auto as_moved_c_ptr(Queryable& q) { + return make_pair(as_moved_c_ptr(static_cast(q)), + as_moved_c_ptr(const_cast(q.handler()))); +} + +/// @brief Return a pair of pointers to moved C representation of queryable and its handler. +/// Will return a pair of null pointers if optional is empty. +template >> +auto as_moved_c_ptr(std::optional>& q) -> decltype(as_moved_c_ptr(q.value())) { + if (!q.has_value()) { + return as_moved_c_ptr(q.value()); + } else { + return {}; + } +} + +/// @brief Move queryable and its handler to a pair containing corresponding Zenoh-c structs. +template >> +auto move_to_c_obj(Queryable&& q) { + return make_pair(move_to_c_obj(std::move(static_cast(q))), + move_to_c_obj(std::move(const_cast(q)))); +} +} // namespace interop } // namespace zenoh \ No newline at end of file diff --git a/include/zenoh/api/session.hxx b/include/zenoh/api/session.hxx index d2dd8955..ab3d1a37 100644 --- a/include/zenoh/api/session.hxx +++ b/include/zenoh/api/session.hxx @@ -424,7 +424,7 @@ class Session : public Owned<::z_owned_session_t> { z_queryable_options_default(&opts); opts.complete = options.complete; - detail::QueryableBase q; + Queryable q; ZResult res = ::z_declare_queryable(interop::as_owned_c_ptr(q), interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Queryable"); @@ -507,13 +507,14 @@ class Session : public Owned<::z_owned_session_t> { #else (void)options; #endif - ::z_owned_subscriber_t s; - ZResult res = ::z_declare_subscriber(&s, interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), - ::z_move(cb_handler_pair.first), &opts); + Subscriber s; + ZResult res = + ::z_declare_subscriber(interop::as_owned_c_ptr(s), interop::as_loaned_c_ptr(*this), + interop::as_loaned_c_ptr(key_expr), ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Subscriber"); if (res != Z_OK) ::z_drop(interop::as_moved_c_ptr(cb_handler_pair.second)); - return Subscriber>( - std::move(interop::as_owned_cpp_ref(&s)), std::move(cb_handler_pair.second)); + return Subscriber>(std::move(s), + std::move(cb_handler_pair.second)); } /// @brief Options to be passed when declaring a ``Publisher``. diff --git a/include/zenoh/api/subscriber.hxx b/include/zenoh/api/subscriber.hxx index 6aefb5b1..e7bd1090 100644 --- a/include/zenoh/api/subscriber.hxx +++ b/include/zenoh/api/subscriber.hxx @@ -24,6 +24,7 @@ namespace detail { class SubscriberBase : public Owned<::z_owned_subscriber_t> { protected: SubscriberBase() : Owned(nullptr){}; + SubscriberBase(::z_owned_subscriber_t* s) : Owned(s){}; public: #ifdef ZENOHCXX_ZENOHC @@ -35,16 +36,38 @@ class SubscriberBase : public Owned<::z_owned_subscriber_t> { #endif friend class zenoh::Session; }; + } // namespace detail + +template +class Subscriber; +template <> +class Subscriber : public detail::SubscriberBase { + protected: + using SubscriberBase::SubscriberBase; + friend class Session; +}; + /// A Zenoh subscriber. Destroying subscriber cancels the subscription. /// Constructed by ``Session::declare_subscriber`` method. +/// @tparam Handler Streaming handler exposing data. If `void`, no handler access is provided and instead data is being +/// processed inside the callback. template class Subscriber : public detail::SubscriberBase { Handler _handler; - Subscriber(SubscriberBase subscriber, Handler handler) - : SubscriberBase(std::move(subscriber)), _handler(std::move(handler)) {} public: + /// @name Constructors + + /// @brief Construct stream subscriber from callback subscriber and handler. + /// + /// @param s Callback subscriber, that should expose data to the handler in its callback. + /// @param handler Handler to access data exposed by s. Zenoh handlers implement + /// recv and try_recv methods, for blocking and non-blocking message reception. But user is free to define his own + /// interface. + Subscriber(Subscriber&& s, Handler handler) + : SubscriberBase(interop::as_owned_c_ptr(s)), _handler(std::move(handler)) {} + /// @name Methods #ifdef ZENOHCXX_ZENOHC @@ -55,11 +78,58 @@ class Subscriber : public detail::SubscriberBase { friend class Session; }; -template <> -class Subscriber : public detail::SubscriberBase { - protected: - using SubscriberBase::SubscriberBase; - friend class Session; -}; +namespace interop { +/// @brief Return a pair of pointers to owned C representations of subscriber and its callback. +template >> +auto as_owned_c_ptr(Subscriber& s) { + return make_pair(as_owned_c_ptr(static_cast(s)), + as_owned_c_ptr(const_cast(s.handler()))); +} + +/// @brief Return a pair of pointers to owned C representations of subscriber and its callback. +template >> +auto as_owned_c_ptr(const Subscriber& s) { + return make_pair(as_owned_c_ptr(static_cast(s)), as_owned_c_ptr(s.handler())); +} + +/// @brief Return a pair of pointers to loaned C representations of subscriber and its callback. +template >> +auto as_loaned_c_ptr(Subscriber& s) { + return make_pair(as_loaned_c_ptr(static_cast(s)), + as_loaned_c_ptr(const_cast(s.handler()))); +} + +/// @brief Return a pair of pointers to loaned C representations of subscriber and its callback. +template >> +auto as_loaned_c_ptr(const Subscriber& s) { + return make_pair(as_loaned_c_ptr(static_cast(s)), + as_loaned_c_ptr(s.handler())); +} + +/// @brief Return a pair of pointers to moved C representations of subscriber and its callback. +template >> +auto as_moved_c_ptr(Subscriber& s) { + return make_pair(as_moved_c_ptr(static_cast(s)), + as_moved_c_ptr(const_cast(s.handler()))); +} + +/// @brief Return a pair of pointers to moved C representations of subscriber and its callback. +/// Will return a pair of null pointers if option is empty. +template >> +auto as_moved_c_ptr(std::optional>& s) -> decltype(as_moved_c_ptr(s.value())) { + if (!s.has_value()) { + return as_moved_c_ptr(s.value()); + } else { + return {}; + } +} + +/// @brief Move subscriber and its handler to a pair containing corresponding Zenoh-c structs. +template >> +auto move_to_c_obj(Subscriber&& s) { + return make_pair(move_to_c_obj(std::move(static_cast(s))), + move_to_c_obj(std::move(const_cast(s)))); +} +} // namespace interop } // namespace zenoh \ No newline at end of file From da804d3cf67772507d1811ac6d85f3370ba23afa Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Fri, 30 Aug 2024 14:21:28 +0200 Subject: [PATCH 7/9] add missing prefix for std::pair --- include/zenoh/api/queryable.hxx | 26 +++++++++++++++----------- include/zenoh/api/subscriber.hxx | 25 ++++++++++++++----------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/include/zenoh/api/queryable.hxx b/include/zenoh/api/queryable.hxx index 7e40cc15..d58b19ef 100644 --- a/include/zenoh/api/queryable.hxx +++ b/include/zenoh/api/queryable.hxx @@ -13,7 +13,10 @@ #pragma once +#include + #include "base.hxx" +#include "interop.hxx" namespace zenoh { @@ -71,35 +74,36 @@ namespace interop { /// @brief Return a pair of pointers to owned C representations of queryable and its callback. template >> auto as_owned_c_ptr(Queryable& q) { - return make_pair(as_owned_c_ptr(static_cast(q)), - as_owned_c_ptr(const_cast(q.handler()))); + return std::make_pair(as_owned_c_ptr(static_cast(q)), + as_owned_c_ptr(const_cast(q.handler()))); } /// @brief Return a pair of pointers to owned C representations of queryable and its handler. template >> auto as_owned_c_ptr(const Queryable& q) { - return make_pair(as_owned_c_ptr(static_cast(q)), as_owned_c_ptr(q.handler())); + return std::make_pair(as_owned_c_ptr(static_cast(q)), + as_owned_c_ptr(q.handler())); } /// @brief Return a pair of pointers to loaned C representations of queryable and its handler. template >> auto as_loaned_c_ptr(Queryable& q) { - return make_pair(as_loaned_c_ptr(static_cast(q)), - as_loaned_c_ptr(const_cast(q.handler()))); + return std::make_pair(as_loaned_c_ptr(static_cast(q)), + as_loaned_c_ptr(const_cast(q.handler()))); } /// @brief Return a pair of pointers to loaned C representation of queryable and its handler. template >> auto as_loaned_c_ptr(const Queryable& q) { - return make_pair(as_loaned_c_ptr(static_cast(q)), - as_loaned_c_ptr(q.handler())); + return std::make_pair(as_loaned_c_ptr(static_cast(q)), + as_loaned_c_ptr(q.handler())); } /// @brief Return a pair of pointers to moved C representation of queryable and its handler. template >> auto as_moved_c_ptr(Queryable& q) { - return make_pair(as_moved_c_ptr(static_cast(q)), - as_moved_c_ptr(const_cast(q.handler()))); + return std::make_pair(as_moved_c_ptr(static_cast(q)), + as_moved_c_ptr(const_cast(q.handler()))); } /// @brief Return a pair of pointers to moved C representation of queryable and its handler. @@ -116,8 +120,8 @@ auto as_moved_c_ptr(std::optional>& q) -> decltype(as_moved_c /// @brief Move queryable and its handler to a pair containing corresponding Zenoh-c structs. template >> auto move_to_c_obj(Queryable&& q) { - return make_pair(move_to_c_obj(std::move(static_cast(q))), - move_to_c_obj(std::move(const_cast(q)))); + return std::make_pair(move_to_c_obj(std::move(static_cast(q))), + move_to_c_obj(std::move(const_cast(q)))); } } // namespace interop diff --git a/include/zenoh/api/subscriber.hxx b/include/zenoh/api/subscriber.hxx index e7bd1090..78ca9778 100644 --- a/include/zenoh/api/subscriber.hxx +++ b/include/zenoh/api/subscriber.hxx @@ -13,6 +13,8 @@ #pragma once +#include + #include "base.hxx" #include "interop.hxx" #include "keyexpr.hxx" @@ -82,35 +84,36 @@ namespace interop { /// @brief Return a pair of pointers to owned C representations of subscriber and its callback. template >> auto as_owned_c_ptr(Subscriber& s) { - return make_pair(as_owned_c_ptr(static_cast(s)), - as_owned_c_ptr(const_cast(s.handler()))); + return std::make_pair(as_owned_c_ptr(static_cast(s)), + as_owned_c_ptr(const_cast(s.handler()))); } /// @brief Return a pair of pointers to owned C representations of subscriber and its callback. template >> auto as_owned_c_ptr(const Subscriber& s) { - return make_pair(as_owned_c_ptr(static_cast(s)), as_owned_c_ptr(s.handler())); + return std::make_pair(as_owned_c_ptr(static_cast(s)), + as_owned_c_ptr(s.handler())); } /// @brief Return a pair of pointers to loaned C representations of subscriber and its callback. template >> auto as_loaned_c_ptr(Subscriber& s) { - return make_pair(as_loaned_c_ptr(static_cast(s)), - as_loaned_c_ptr(const_cast(s.handler()))); + return std::make_pair(as_loaned_c_ptr(static_cast(s)), + as_loaned_c_ptr(const_cast(s.handler()))); } /// @brief Return a pair of pointers to loaned C representations of subscriber and its callback. template >> auto as_loaned_c_ptr(const Subscriber& s) { - return make_pair(as_loaned_c_ptr(static_cast(s)), - as_loaned_c_ptr(s.handler())); + return std::make_pair(as_loaned_c_ptr(static_cast(s)), + as_loaned_c_ptr(s.handler())); } /// @brief Return a pair of pointers to moved C representations of subscriber and its callback. template >> auto as_moved_c_ptr(Subscriber& s) { - return make_pair(as_moved_c_ptr(static_cast(s)), - as_moved_c_ptr(const_cast(s.handler()))); + return std::make_pair(as_moved_c_ptr(static_cast(s)), + as_moved_c_ptr(const_cast(s.handler()))); } /// @brief Return a pair of pointers to moved C representations of subscriber and its callback. @@ -127,8 +130,8 @@ auto as_moved_c_ptr(std::optional>& s) -> decltype(as_moved_ /// @brief Move subscriber and its handler to a pair containing corresponding Zenoh-c structs. template >> auto move_to_c_obj(Subscriber&& s) { - return make_pair(move_to_c_obj(std::move(static_cast(s))), - move_to_c_obj(std::move(const_cast(s)))); + return std::make_pair(move_to_c_obj(std::move(static_cast(s))), + move_to_c_obj(std::move(const_cast(s)))); } } // namespace interop From c18d60f9110f4b16febab85227cc1d82493ee7c7 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Fri, 30 Aug 2024 16:10:38 +0200 Subject: [PATCH 8/9] replace empty null constructors with the one taking zenoh::detail::null_object_t --- include/zenoh/api/base.hxx | 5 +++++ include/zenoh/api/channels.hxx | 8 ++++---- include/zenoh/api/config.hxx | 18 +++++++++--------- include/zenoh/api/interop.hxx | 2 +- include/zenoh/api/keyexpr.hxx | 6 +++--- include/zenoh/api/liveliness.hxx | 2 +- include/zenoh/api/publisher.hxx | 2 +- include/zenoh/api/query.hxx | 4 ++-- include/zenoh/api/queryable.hxx | 4 ++-- include/zenoh/api/reply.hxx | 2 +- include/zenoh/api/sample.hxx | 4 ++-- include/zenoh/api/session.hxx | 14 +++++++------- include/zenoh/api/shm/buffer/zshm.hxx | 2 +- .../posix/posix_shm_provider.hxx | 2 +- .../zenoh/api/shm/provider/shm_provider.hxx | 8 +++++--- include/zenoh/api/shm/provider/types.hxx | 2 +- include/zenoh/api/subscriber.hxx | 2 +- 17 files changed, 47 insertions(+), 40 deletions(-) diff --git a/include/zenoh/api/base.hxx b/include/zenoh/api/base.hxx index d5e5b616..57c98569 100644 --- a/include/zenoh/api/base.hxx +++ b/include/zenoh/api/base.hxx @@ -92,4 +92,9 @@ class Owned { } }; +namespace detail { +struct null_object_t {}; +inline constexpr null_object_t null_object{}; +} // namespace detail + } // namespace zenoh diff --git a/include/zenoh/api/channels.hxx b/include/zenoh/api/channels.hxx index 1e81ae00..c929974a 100644 --- a/include/zenoh/api/channels.hxx +++ b/include/zenoh/api/channels.hxx @@ -103,7 +103,7 @@ class FifoChannel; /// @tparam T data entry type template class FifoHandler : public Owned::handler_type> { - FifoHandler() : Owned::handler_type>(nullptr){}; + FifoHandler(zenoh::detail::null_object_t) : Owned::handler_type>(nullptr){}; public: /// @name Methods @@ -143,7 +143,7 @@ class RingChannel; /// @tparam T data entry type. template class RingHandler : public Owned::handler_type> { - RingHandler() : Owned::handler_type>(nullptr){}; + RingHandler(zenoh::detail::null_object_t) : Owned::handler_type>(nullptr){}; public: /// @name Methods @@ -199,7 +199,7 @@ class FifoChannel { template std::pair::closure_type, HandlerType> into_cb_handler_pair() const { typename detail::FifoHandlerData::closure_type c_closure; - FifoHandler h; + FifoHandler h(zenoh::detail::null_object); detail::FifoHandlerData::create_cb_handler_pair(&c_closure, zenoh::interop::as_owned_c_ptr(h), _capacity); return {c_closure, std::move(h)}; } @@ -226,7 +226,7 @@ class RingChannel { template std::pair::closure_type, HandlerType> into_cb_handler_pair() const { typename detail::RingHandlerData::closure_type c_closure; - RingHandler h; + RingHandler h(zenoh::detail::null_object); detail::RingHandlerData::create_cb_handler_pair(&c_closure, zenoh::interop::as_owned_c_ptr(h), _capacity); return {c_closure, std::move(h)}; } diff --git a/include/zenoh/api/config.hxx b/include/zenoh/api/config.hxx index 53068b04..5c7f8c3b 100644 --- a/include/zenoh/api/config.hxx +++ b/include/zenoh/api/config.hxx @@ -21,7 +21,7 @@ namespace zenoh { /// A Zenoh Session config class Config : public Owned<::z_owned_config_t> { - Config() : Owned(nullptr){}; + Config(zenoh::detail::null_object_t) : Owned(nullptr){}; public: /// @name Constructors @@ -30,7 +30,7 @@ class Config : public Owned<::z_owned_config_t> { /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. static Config create_default(ZResult* err = nullptr) { - Config c; + Config c(zenoh::detail::null_object); __ZENOH_RESULT_CHECK(::z_config_default(&c._0), err, std::string("Failed to create default configuration")); return c; } @@ -42,7 +42,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-c only. static Config peer(ZResult* err = nullptr) { - Config c; + Config c(zenoh::detail::null_object); __ZENOH_RESULT_CHECK(::z_config_peer(&c._0), err, std::string("Failed to create peer configuration")); return c; } @@ -54,7 +54,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-c only. static Config from_file(const std::string& path, ZResult* err = nullptr) { - Config c; + Config c(zenoh::detail::null_object); __ZENOH_RESULT_CHECK(::zc_config_from_file(&c._0, path.data()), err, std::string("Failed to create config from: ").append(path)); return c; @@ -67,7 +67,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-c only. static Config from_str(const std::string& s, ZResult* err = nullptr) { - Config c; + Config c(zenoh::detail::null_object); __ZENOH_RESULT_CHECK(::zc_config_from_str(&c._0, s.data()), err, std::string("Failed to create config from: ").append(s)); return c; @@ -79,7 +79,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-c only. static Config client(const std::vector& peers, ZResult* err = nullptr) { - Config c; + Config c(zenoh::detail::null_object); std::vector p; p.reserve(peers.size()); for (const auto& peer : peers) { @@ -95,7 +95,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-pico only. static Config from_env(ZResult* err = nullptr) { - Config c; + Config c(zenoh::detail::null_object); __ZENOH_RESULT_CHECK(::zc_config_from_env(&c._0), err, "Failed to create config from environment variable"); return c; } @@ -109,7 +109,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-pico only. static Config peer(const char* locator, ZResult* err = nullptr) { - Config c; + Config c(zenoh::detail::null_object); __ZENOH_RESULT_CHECK(::z_config_peer(&c._0, locator), err, "Failed to create client config"); return c; } @@ -121,7 +121,7 @@ class Config : public Owned<::z_owned_config_t> { /// @return the ``Config`` object. /// @note zenoh-pico only. static Config client(const char* peer, ZResult* err = nullptr) { - Config c; + Config c(zenoh::detail::null_object); __ZENOH_RESULT_CHECK(::z_config_client(&c._0, peer), err, "Failed to create client config"); return c; } diff --git a/include/zenoh/api/interop.hxx b/include/zenoh/api/interop.hxx index 6f8db4f2..16b5ed28 100644 --- a/include/zenoh/api/interop.hxx +++ b/include/zenoh/api/interop.hxx @@ -194,7 +194,7 @@ bool check(const Owned& owned_cpp_obj) { struct Converter { template static T null_owned() { - return T(); + return T(zenoh::detail::null_object); } template static T copyable_to_cpp(const Inner& i) { diff --git a/include/zenoh/api/keyexpr.hxx b/include/zenoh/api/keyexpr.hxx index 6988ea6b..8f90d527 100644 --- a/include/zenoh/api/keyexpr.hxx +++ b/include/zenoh/api/keyexpr.hxx @@ -27,7 +27,7 @@ namespace zenoh { /// when transporting key expressions. class KeyExpr : public Owned<::z_owned_keyexpr_t> { - KeyExpr() : Owned(nullptr){}; + KeyExpr(zenoh::detail::null_object_t) : Owned(nullptr){}; friend struct interop::detail::Converter; public: @@ -91,7 +91,7 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// thrown in case of error. /// @return A new key expression. KeyExpr concat(std::string_view s, ZResult* err = nullptr) const { - KeyExpr k; + KeyExpr k(zenoh::detail::null_object); __ZENOH_RESULT_CHECK( ::z_keyexpr_concat(&k._0, interop::as_loaned_c_ptr(*this), s.data(), s.size()), err, std::string("Failed to concatenate KeyExpr: ").append(this->as_string_view()).append(" with ").append(s)); @@ -103,7 +103,7 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. KeyExpr join(const KeyExpr& other, ZResult* err = nullptr) const { - KeyExpr k; + KeyExpr k(zenoh::detail::null_object); __ZENOH_RESULT_CHECK(::z_keyexpr_join(&k._0, interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(other)), err, std::string("Failed to join KeyExpr: ") diff --git a/include/zenoh/api/liveliness.hxx b/include/zenoh/api/liveliness.hxx index 75a7b456..b8a5fa16 100644 --- a/include/zenoh/api/liveliness.hxx +++ b/include/zenoh/api/liveliness.hxx @@ -29,7 +29,7 @@ class Session; /// between the subscriber and the token's creator is lost. /// @note zenoh-c only. class LivelinessToken : public Owned<::zc_owned_liveliness_token_t> { - LivelinessToken() : Owned(nullptr){}; + LivelinessToken(zenoh::detail::null_object_t) : Owned(nullptr){}; friend struct interop::detail::Converter; public: diff --git a/include/zenoh/api/publisher.hxx b/include/zenoh/api/publisher.hxx index a3921662..cb812122 100644 --- a/include/zenoh/api/publisher.hxx +++ b/include/zenoh/api/publisher.hxx @@ -31,7 +31,7 @@ class Session; /// A Zenoh publisher. Constructed by ``Session::declare_publisher`` method. class Publisher : public Owned<::z_owned_publisher_t> { - Publisher() : Owned(nullptr){}; + Publisher(zenoh::detail::null_object_t) : Owned(nullptr){}; friend struct interop::detail::Converter; public: diff --git a/include/zenoh/api/query.hxx b/include/zenoh/api/query.hxx index 98c6fef0..52f9e1ba 100644 --- a/include/zenoh/api/query.hxx +++ b/include/zenoh/api/query.hxx @@ -31,7 +31,7 @@ namespace zenoh { /// The query to be answered by a ``Queryable``. class Query : public Owned<::z_owned_query_t> { - Query() : Owned(nullptr){}; + Query(zenoh::detail::null_object_t) : Owned(nullptr){}; friend struct interop::detail::Converter; public: @@ -210,7 +210,7 @@ class Query : public Owned<::z_owned_query_t> { /// /// The query responses will be sent only when the last clone is destroyed. Query clone() const { - Query q; + Query q(zenoh::detail::null_object); ::z_query_clone(&q._0, interop::as_loaned_c_ptr(*this)); return q; }; diff --git a/include/zenoh/api/queryable.hxx b/include/zenoh/api/queryable.hxx index d58b19ef..3c36fce3 100644 --- a/include/zenoh/api/queryable.hxx +++ b/include/zenoh/api/queryable.hxx @@ -24,7 +24,7 @@ class Session; namespace detail { class QueryableBase : public Owned<::z_owned_queryable_t> { protected: - QueryableBase() : Owned(nullptr){}; + QueryableBase(zenoh::detail::null_object_t) : Owned(nullptr){}; QueryableBase(::z_owned_queryable_t* q) : Owned(q){}; friend class zenoh::Session; @@ -36,7 +36,7 @@ class Queryable; template <> class Queryable : public detail::QueryableBase { - Queryable() : QueryableBase(){}; + Queryable(zenoh::detail::null_object_t) : QueryableBase(zenoh::detail::null_object){}; public: using QueryableBase::QueryableBase; diff --git a/include/zenoh/api/reply.hxx b/include/zenoh/api/reply.hxx index 2b5218ab..dece2865 100644 --- a/include/zenoh/api/reply.hxx +++ b/include/zenoh/api/reply.hxx @@ -43,7 +43,7 @@ class ReplyError : public Owned<::z_owned_reply_err_t> { /// A reply from queryable to ``Session::get`` operation. class Reply : public Owned<::z_owned_reply_t> { - Reply() : Owned(nullptr){}; + Reply(zenoh::detail::null_object_t) : Owned(nullptr){}; friend struct interop::detail::Converter; public: diff --git a/include/zenoh/api/sample.hxx b/include/zenoh/api/sample.hxx index 55e1ec9b..7d85d56e 100644 --- a/include/zenoh/api/sample.hxx +++ b/include/zenoh/api/sample.hxx @@ -31,7 +31,7 @@ namespace zenoh { /// /// A sample is the value associated to a given resource at a given point in time. class Sample : public Owned<::z_owned_sample_t> { - Sample() : Owned(nullptr){}; + Sample(zenoh::detail::null_object_t) : Owned(nullptr){}; friend struct interop::detail::Converter; public: @@ -98,7 +98,7 @@ class Sample : public Owned<::z_owned_sample_t> { #endif /// @brief Construct a shallow copy of this sample. Sample clone() const { - Sample s; + Sample s(zenoh::detail::null_object); ::z_sample_clone(&s._0, interop::as_loaned_c_ptr(*this)); return s; }; diff --git a/include/zenoh/api/session.hxx b/include/zenoh/api/session.hxx index ab3d1a37..b11369c4 100644 --- a/include/zenoh/api/session.hxx +++ b/include/zenoh/api/session.hxx @@ -41,7 +41,7 @@ namespace zenoh { /// A Zenoh session. class Session : public Owned<::z_owned_session_t> { - Session() : Owned(nullptr){}; + Session(zenoh::detail::null_object_t) : Owned(nullptr){}; public: /// @brief Options to be passed when opening a ``Session``. @@ -125,7 +125,7 @@ class Session : public Owned<::z_owned_session_t> { /// @brief Create a shallow copy of the session. /// @return a new ``Session`` instance. Session clone() const { - Session s; + Session s(zenoh::detail::null_object); ::z_session_clone(&s._0, interop::as_loaned_c_ptr(*this)); return s; } @@ -399,7 +399,7 @@ class Session : public Owned<::z_owned_session_t> { z_queryable_options_default(&opts); opts.complete = options.complete; - Queryable q; + Queryable q(zenoh::detail::null_object); ZResult res = ::z_declare_queryable(interop::as_owned_c_ptr(q), interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Queryable"); @@ -424,7 +424,7 @@ class Session : public Owned<::z_owned_session_t> { z_queryable_options_default(&opts); opts.complete = options.complete; - Queryable q; + Queryable q(zenoh::detail::null_object); ZResult res = ::z_declare_queryable(interop::as_owned_c_ptr(q), interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Queryable"); @@ -478,7 +478,7 @@ class Session : public Owned<::z_owned_session_t> { #else (void)options; #endif - Subscriber s; + Subscriber s(zenoh::detail::null_object); ZResult res = ::z_declare_subscriber(interop::as_owned_c_ptr(s), interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Subscriber"); @@ -507,7 +507,7 @@ class Session : public Owned<::z_owned_session_t> { #else (void)options; #endif - Subscriber s; + Subscriber s(zenoh::detail::null_object); ZResult res = ::z_declare_subscriber(interop::as_owned_c_ptr(s), interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), ::z_move(cb_handler_pair.first), &opts); @@ -737,7 +737,7 @@ class Session : public Owned<::z_owned_session_t> { ::zc_liveliness_subscriber_options_t opts; zc_liveliness_subscriber_options_default(&opts); (void)options; - Subscriber s; + Subscriber s(zenoh::detail::null_object); ZResult res = ::zc_liveliness_declare_subscriber(interop::as_owned_c_ptr(s), interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts); diff --git a/include/zenoh/api/shm/buffer/zshm.hxx b/include/zenoh/api/shm/buffer/zshm.hxx index a6187e54..923a9f16 100644 --- a/include/zenoh/api/shm/buffer/zshm.hxx +++ b/include/zenoh/api/shm/buffer/zshm.hxx @@ -25,7 +25,7 @@ namespace zenoh { /// @brief An immutable SHM buffer class ZShm : public Owned<::z_owned_shm_t> { friend class ZShmMut; - ZShm() : Owned(nullptr){}; + ZShm(zenoh::detail::null_object_t) : Owned(nullptr){}; friend struct interop::detail::Converter; public: diff --git a/include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx b/include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx index 16e64b33..6599cc83 100644 --- a/include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx +++ b/include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx @@ -29,7 +29,7 @@ class PosixShmProvider : public ShmProvider { /// @param layout layout for POSIX shared memory segment to be allocated and used by the provider /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. - PosixShmProvider(const MemoryLayout& layout, ZResult* err = nullptr) : ShmProvider() { + PosixShmProvider(const MemoryLayout& layout, ZResult* err = nullptr) : ShmProvider(zenoh::detail::null_object) { __ZENOH_RESULT_CHECK(::z_posix_shm_provider_new(&this->_0, interop::as_loaned_c_ptr(layout)), err, "Failed to create POSIX SHM provider"); } diff --git a/include/zenoh/api/shm/provider/shm_provider.hxx b/include/zenoh/api/shm/provider/shm_provider.hxx index 61c85219..325499c9 100644 --- a/include/zenoh/api/shm/provider/shm_provider.hxx +++ b/include/zenoh/api/shm/provider/shm_provider.hxx @@ -50,7 +50,7 @@ class ShmProvider : public Owned<::z_owned_shm_provider_t> { friend class AllocLayout; protected: - ShmProvider() : Owned(nullptr){}; + ShmProvider(zenoh::detail::null_object_t) : Owned(nullptr){}; public: BufLayoutAllocResult alloc(size_t size, AllocAlignment alignment) const { @@ -113,7 +113,8 @@ class CppShmProvider : public ShmProvider { /// @name Constructors /// @brief Create a new CPP-defined ShmProvider. - CppShmProvider(ProtocolId id, std::unique_ptr backend) : ShmProvider() { + CppShmProvider(ProtocolId id, std::unique_ptr backend) + : ShmProvider(zenoh::detail::null_object) { // init context zc_context_t context = {backend.release(), &shm::provider_backend::closures::_z_cpp_shm_provider_backend_drop_fn}; @@ -131,7 +132,8 @@ class CppShmProvider : public ShmProvider { } /// @brief Create a new CPP-defined threadsafe ShmProvider. - CppShmProvider(ProtocolId id, std::unique_ptr backend) : ShmProvider() { + CppShmProvider(ProtocolId id, std::unique_ptr backend) + : ShmProvider(zenoh::detail::null_object) { // init context ::zc_threadsafe_context_t context = {{backend.release()}, &shm::provider_backend::closures::_z_cpp_shm_provider_backend_drop_fn}; diff --git a/include/zenoh/api/shm/provider/types.hxx b/include/zenoh/api/shm/provider/types.hxx index f7b2221c..84fa1e4f 100644 --- a/include/zenoh/api/shm/provider/types.hxx +++ b/include/zenoh/api/shm/provider/types.hxx @@ -43,7 +43,7 @@ typedef ::z_alloc_alignment_t AllocAlignment; class MemoryLayout : public Owned<::z_owned_memory_layout_t> { friend class PosixShmProvider; - MemoryLayout() : Owned(nullptr){}; + MemoryLayout(zenoh::detail::null_object_t) : Owned(nullptr){}; friend struct interop::detail::Converter; public: diff --git a/include/zenoh/api/subscriber.hxx b/include/zenoh/api/subscriber.hxx index 78ca9778..fccc68f6 100644 --- a/include/zenoh/api/subscriber.hxx +++ b/include/zenoh/api/subscriber.hxx @@ -25,7 +25,7 @@ class Session; namespace detail { class SubscriberBase : public Owned<::z_owned_subscriber_t> { protected: - SubscriberBase() : Owned(nullptr){}; + SubscriberBase(zenoh::detail::null_object_t) : Owned(nullptr){}; SubscriberBase(::z_owned_subscriber_t* s) : Owned(s){}; public: From 7a8b5013fcb639e059501f90ef68768fb14dd5e1 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Fri, 30 Aug 2024 17:00:36 +0200 Subject: [PATCH 9/9] session clean up --- include/zenoh/api/session.hxx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/zenoh/api/session.hxx b/include/zenoh/api/session.hxx index b11369c4..83c59e6e 100644 --- a/include/zenoh/api/session.hxx +++ b/include/zenoh/api/session.hxx @@ -763,14 +763,14 @@ class Session : public Owned<::z_owned_session_t> { ::zc_liveliness_subscriber_options_t opts; zc_liveliness_subscriber_options_default(&opts); (void)options; - ::z_owned_subscriber_t s; - ZResult res = - ::zc_liveliness_declare_subscriber(&s, interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), - ::z_move(cb_handler_pair.first), &opts); + Subscriber s(zenoh::detail::null_object); + ZResult res = ::zc_liveliness_declare_subscriber(interop::as_owned_c_ptr(s), interop::as_loaned_c_ptr(*this), + interop::as_loaned_c_ptr(key_expr), + ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Liveliness Token Subscriber"); if (res != Z_OK) ::z_drop(::z_move(*interop::as_moved_c_ptr(cb_handler_pair.second))); - return Subscriber>( - std::move(interop::as_owned_cpp_ref(&s)), std::move(cb_handler_pair.second)); + return Subscriber>(std::move(s), + std::move(cb_handler_pair.second)); } /// @brief Options to pass to ``Session::liveliness_get``. @@ -849,7 +849,7 @@ class Session : public Owned<::z_owned_session_t> { Timestamp new_timestamp(ZResult* err = nullptr) { ::z_timestamp_t t; __ZENOH_RESULT_CHECK(z_timestamp_new(&t, interop::as_loaned_c_ptr(*this)), err, "Failed to create timestamp"); - return interop::as_copyable_cpp_ref(&t); + return interop::into_copyable_cpp_obj(t); } }; } // namespace zenoh \ No newline at end of file