From 8398e6d05385609b16cc0ca0606de5a1fe67dced Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Thu, 22 Aug 2024 00:13:32 +0200 Subject: [PATCH] update to latest api with move protection, compilation on windows fixes --- examples/universal/z_get.cxx | 8 ++++ examples/universal/z_get_attachment.cxx | 8 ++++ examples/universal/z_get_channel.cxx | 7 +++ .../universal/z_get_channel_non_blocking.cxx | 7 +++ examples/universal/z_pub.cxx | 6 +++ examples/universal/z_pub_attachment.cxx | 7 +++ examples/universal/z_pub_thr.cxx | 6 +++ examples/universal/z_put.cxx | 8 +++- examples/universal/z_queryable.cxx | 6 +++ examples/universal/z_queryable_attachment.cxx | 7 +++ include/zenoh/api/base.hxx | 12 +++-- include/zenoh/api/bytes.hxx | 16 +++---- include/zenoh/api/keyexpr.hxx | 12 ++--- include/zenoh/api/liveliness.hxx | 2 +- include/zenoh/api/publisher.hxx | 8 ++-- include/zenoh/api/query.hxx | 16 +++---- include/zenoh/api/scout.hxx | 2 +- include/zenoh/api/session.hxx | 47 ++++++++++--------- include/zenoh/detail/interop.hxx | 5 ++ tests/build/warnings.cxx | 11 +++++ tests/universal/network/implicit.cxx | 4 +- tests/universal/network/keyexpr.cxx | 10 ++-- tests/universal/network/queryable_get.cxx | 30 ++++++++---- zenoh-c | 2 +- zenoh-pico | 2 +- 25 files changed, 175 insertions(+), 74 deletions(-) diff --git a/examples/universal/z_get.cxx b/examples/universal/z_get.cxx index e73200be..b8aabea7 100644 --- a/examples/universal/z_get.cxx +++ b/examples/universal/z_get.cxx @@ -86,7 +86,15 @@ int _main(int argc, char **argv) { done_signal.notify_all(); }; +#if __cplusplus >= 201703L session.get(keyexpr, "", on_reply, on_done, {.target = Z_QUERY_TARGET_ALL, .payload = Bytes::serialize(value)}); +#else + Session::GetOptions options; + options.target = Z_QUERY_TARGET_ALL; + options.payload = Bytes::serialize(value); + session.get(keyexpr, "", on_reply, on_done, std::move(options)); +#endif + std::unique_lock lock(m); done_signal.wait(lock, [&done] { return done; }); diff --git a/examples/universal/z_get_attachment.cxx b/examples/universal/z_get_attachment.cxx index 80c21347..3f8f7814 100644 --- a/examples/universal/z_get_attachment.cxx +++ b/examples/universal/z_get_attachment.cxx @@ -78,9 +78,17 @@ int _main(int argc, char **argv) { std::unordered_map attachment = {{"Source", "C++"}}; +#if __cplusplus >= 201703L session.get( keyexpr, "", on_reply, on_done, {.target = Z_QUERY_TARGET_ALL, .payload = Bytes::serialize(value), .attachment = Bytes::serialize(attachment)}); +#else + Session::GetOptions options; + options.target = QueryTarget::Z_QUERY_TARGET_ALL; + options.payload = Bytes::serialize(value); + options.attachment = Bytes::serialize(attachment); + session.get(keyexpr, "", on_reply, on_done, std::move(options)); +#endif std::unique_lock lock(m); done_signal.wait(lock, [&done] { return done; }); diff --git a/examples/universal/z_get_channel.cxx b/examples/universal/z_get_channel.cxx index 9caa8d63..e4361c09 100644 --- a/examples/universal/z_get_channel.cxx +++ b/examples/universal/z_get_channel.cxx @@ -61,9 +61,16 @@ int _main(int argc, char **argv) { auto session = Session::open(std::move(config)); std::cout << "Sending Query '" << expr << "'...\n"; +#if __cplusplus >= 201703L auto replies = session.get(keyexpr, "", channels::FifoChannel(16), {.target = QueryTarget::Z_QUERY_TARGET_ALL, .payload = Bytes::serialize("Get from C++")}); +#else + Session::GetOptions options; + options.target = QueryTarget::Z_QUERY_TARGET_ALL; + options.payload = Bytes::serialize("Get from C++"); + auto replies = session.get(keyexpr, "", channels::FifoChannel(16), std::move(options)); +#endif for (auto res = replies.recv(); std::holds_alternative(res); res = replies.recv()) { const auto &sample = std::get(res).get_ok(); diff --git a/examples/universal/z_get_channel_non_blocking.cxx b/examples/universal/z_get_channel_non_blocking.cxx index 62b67b9f..9cbdf415 100644 --- a/examples/universal/z_get_channel_non_blocking.cxx +++ b/examples/universal/z_get_channel_non_blocking.cxx @@ -64,9 +64,16 @@ int _main(int argc, char **argv) { std::cout << "Sending Query '" << expr << "'...\n"; +#if __cplusplus >= 201703L auto replies = session.get(keyexpr, "", channels::FifoChannel(16), {.target = QueryTarget::Z_QUERY_TARGET_ALL, .payload = Bytes::serialize("Get from C++")}); +#else + Session::GetOptions options; + options.target = QueryTarget::Z_QUERY_TARGET_ALL; + options.payload = Bytes::serialize("Get from C++"); + auto replies = session.get(keyexpr, "", channels::FifoChannel(16), std::move(options)); +#endif while (true) { auto res = replies.try_recv(); diff --git a/examples/universal/z_pub.cxx b/examples/universal/z_pub.cxx index 02b88526..1ba9c4fc 100644 --- a/examples/universal/z_pub.cxx +++ b/examples/universal/z_pub.cxx @@ -88,7 +88,13 @@ int _main(int argc, char **argv) { ss << "[" << idx << "] " << value; auto s = ss.str(); // in C++20 use .view() instead std::cout << "Putting Data ('" << keyexpr << "': '" << s << "')...\n"; +#if __cplusplus >= 201703L pub.put(Bytes::serialize(s), {.encoding = Encoding("text/plain")}); +#else + auto put_options = Publisher::PutOptions{}; + put_options.encoding = Encoding("text/plain"); + pub.put(Bytes::serialize(s), std::move(put_options)); +#endif } return 0; } diff --git a/examples/universal/z_pub_attachment.cxx b/examples/universal/z_pub_attachment.cxx index 38a89229..e2cdae28 100644 --- a/examples/universal/z_pub_attachment.cxx +++ b/examples/universal/z_pub_attachment.cxx @@ -75,8 +75,15 @@ int _main(int argc, char **argv) { std::cout << "Putting Data ('" << keyexpr << "': '" << s << "')...\n"; // add some other attachment value attachment_map["index"] = std::to_string(idx); +#if __cplusplus >= 201703L pub.put(Bytes::serialize(s), {.encoding = Encoding("text/plain"), .attachment = Bytes::serialize(attachment_map)}); +#else + Publisher::PutOptions options; + options.encoding = Encoding("text/plain"); + options.attachment = Bytes::serialize(attachment_map); + pub.put(Bytes::serialize(s), std::move(options)); +#endif } return 0; } diff --git a/examples/universal/z_pub_thr.cxx b/examples/universal/z_pub_thr.cxx index d62fc916..5008723c 100644 --- a/examples/universal/z_pub_thr.cxx +++ b/examples/universal/z_pub_thr.cxx @@ -67,7 +67,13 @@ int _main(int argc, char **argv) { auto session = Session::open(std::move(config)); std::cout << "Declaring Publisher on " << keyexpr << "...\n"; +#if __cplusplus >= 201703L auto pub = session.declare_publisher(KeyExpr(keyexpr), {.congestion_control = Z_CONGESTION_CONTROL_BLOCK}); +#else + auto pub_options = Session::PublisherOptions::create_default(); + pub_options.congestion_control = Z_CONGESTION_CONTROL_BLOCK; + auto pub = session.declare_publisher(KeyExpr(keyexpr), std::move(pub_options)); +#endif printf("Press CTRL-C to quit...\n"); while (1) pub.put(payload.clone()); diff --git a/examples/universal/z_put.cxx b/examples/universal/z_put.cxx index fb36e2d7..fdb64779 100644 --- a/examples/universal/z_put.cxx +++ b/examples/universal/z_put.cxx @@ -73,9 +73,15 @@ int _main(int argc, char **argv) { std::unordered_map attachment_map = {{"serial_number", "123"}, {"coordinates", "48.7082,2.1498"}}; - +#if __cplusplus >= 201703L session.put(KeyExpr(keyexpr), Bytes::serialize(value), {.encoding = Encoding("text/plain"), .attachment = Bytes::serialize(attachment_map)}); +#else + auto put_options = Session::PutOptions::create_default(); + put_options.encoding = Encoding("text/plain"); + put_options.attachment = Bytes::serialize(attachment_map); + session.put(KeyExpr(keyexpr), Bytes::serialize(value), std::move(put_options)); +#endif return 0; } diff --git a/examples/universal/z_queryable.cxx b/examples/universal/z_queryable.cxx index 689c5f2a..80175e98 100644 --- a/examples/universal/z_queryable.cxx +++ b/examples/universal/z_queryable.cxx @@ -83,7 +83,13 @@ int _main(int argc, char **argv) { std::cout << "' value = '" << payload->get().deserialize(); } std::cout << "'\n"; +#if __cplusplus >= 201703L query.reply(KeyExpr(expr), Bytes::serialize(value), {.encoding = Encoding("text/plain")}); +#else + Query::ReplyOptions reply_options; + reply_options.encoding = Encoding("text/plain"); + query.reply(KeyExpr(expr), Bytes::serialize(value), std::move(reply_options)); +#endif }; auto on_drop_queryable = []() { std::cout << "Destroying queryable\n"; }; diff --git a/examples/universal/z_queryable_attachment.cxx b/examples/universal/z_queryable_attachment.cxx index a4854992..6159976f 100644 --- a/examples/universal/z_queryable_attachment.cxx +++ b/examples/universal/z_queryable_attachment.cxx @@ -76,8 +76,15 @@ int _main(int argc, char **argv) { std::cout << " attachment: " << key << ": '" << value << "'\n"; } } +#if __cplusplus >= 201703L query.reply(KeyExpr(expr), Bytes::serialize(value), {.encoding = Encoding("text/palin"), .attachment = Bytes::serialize(attachment_map)}); +#else + Query::ReplyOptions options; + options.encoding = Encoding("text/plain"); + options.attachment = Bytes::serialize(attachment_map); + query.reply(KeyExpr(expr), Bytes::serialize(value), std::move(options)); +#endif }; auto on_drop_queryable = []() { std::cout << "Destroying queryable\n"; }; diff --git a/include/zenoh/api/base.hxx b/include/zenoh/api/base.hxx index 957c60e1..a92789cc 100644 --- a/include/zenoh/api/base.hxx +++ b/include/zenoh/api/base.hxx @@ -100,9 +100,9 @@ class Owned { explicit Owned(OwnedType* pv) { if (pv) { _0 = *pv; - ::z_null(pv); + ::z_internal_null(pv); } else - ::z_null(&this->_0); + ::z_internal_null(&this->_0); } /// Move constructor from other object Owned(Owned&& v) : Owned(&v._0) {} @@ -111,7 +111,7 @@ class Owned { if (this != &v) { ::z_drop(::z_move(this->_0)); _0 = v._0; - ::z_null(&v._0); + ::z_internal_null(&v._0); } return *this; } @@ -123,11 +123,13 @@ class Owned { /// Take out zenoh structure and leave owned object in a null state. OwnedType take() && { auto r = this->_0; - ::z_null(&this->_0); + ::z_internal_null(&this->_0); return r; } + /// Check object validity uzing zenoh API - explicit operator bool() const { return ::z_check(_0); } + /// This is internal function made public for testing purposes + bool internal_check() const { return ::z_internal_check(_0); } protected: OwnedType _0; diff --git a/include/zenoh/api/bytes.hxx b/include/zenoh/api/bytes.hxx index b443e13e..25718b9d 100644 --- a/include/zenoh/api/bytes.hxx +++ b/include/zenoh/api/bytes.hxx @@ -105,7 +105,7 @@ class Bytes : public Owned<::z_owned_bytes_t> { Bytes out; auto f = [current = begin, end, &codec](z_owned_bytes_t* b) mutable { if (current == end) { - ::z_null(b); + ::z_internal_null(b); return false; } // increment current, in case iterator dereference might invalidate it, which happens @@ -300,7 +300,7 @@ template <> struct ZenohDeserializer { static ZShm deserialize(const Bytes& b, ZResult* err = nullptr) { ZShm shm(nullptr); - __ZENOH_RESULT_CHECK(::z_bytes_deserialize_into_owned_shm(detail::loan(b), detail::as_owned_c_ptr(shm)), err, + __ZENOH_RESULT_CHECK(::z_bytes_deserialize_into_owned_shm(detail::loan(b), detail::move_(shm)), err, "Failed to deserialize into ZShm!"); return shm; } @@ -512,15 +512,15 @@ 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_owned_c_ptr(shm)), err, + __ZENOH_RESULT_CHECK(::z_bytes_serialize_from_shm(detail::as_owned_c_ptr(b), detail::move_(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_owned_c_ptr(shm)), - err, "Failed to serialize ZShmMut"); + __ZENOH_RESULT_CHECK(::z_bytes_serialize_from_shm_mut(detail::as_owned_c_ptr(b), detail::move_(shm)), err, + "Failed to serialize ZShmMut"); return b; } #endif @@ -627,8 +627,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), ::z_move(*detail::as_owned_c_ptr(ba)), - ::z_move(*detail::as_owned_c_ptr(bb))); + ::z_bytes_from_pair(detail::as_owned_c_ptr(b), detail::move_(ba), detail::move_(bb)); return b; } @@ -637,8 +636,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), ::z_move(*detail::as_owned_c_ptr(ba)), - ::z_move(*detail::as_owned_c_ptr(bb))); + ::z_bytes_from_pair(detail::as_owned_c_ptr(b), detail::move_(ba), detail::move_(bb)); return b; } diff --git a/include/zenoh/api/keyexpr.hxx b/include/zenoh/api/keyexpr.hxx index a9cbed5d..2d074864 100644 --- a/include/zenoh/api/keyexpr.hxx +++ b/include/zenoh/api/keyexpr.hxx @@ -28,7 +28,7 @@ class Session; class KeyExpr : public Owned<::z_owned_keyexpr_t> { friend Session; - KeyExpr() : Owned(nullptr){}; + KeyExpr() : Owned(nullptr) {}; public: /// @name Constructors @@ -57,7 +57,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(const std::string& key_expr, bool autocanonize = true, ZResult* err = nullptr) - : KeyExpr(static_cast(key_expr), autocanonize, err){}; + : KeyExpr(static_cast(key_expr), autocanonize, err) {}; /// @brief Create a new instance from a null-terminated string. /// @@ -66,7 +66,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(const char* key_expr, bool autocanonize = true, ZResult* err = nullptr) - : KeyExpr(std::string_view(key_expr), autocanonize, err){}; + : KeyExpr(std::string_view(key_expr), autocanonize, err) {}; /// @name Methods /// @brief Get underlying key expression string. @@ -138,7 +138,7 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// @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)) return false; + if (!(this->internal_check())) return false; return as_string_view() == other; } @@ -151,7 +151,7 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// @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)) return false; + if (!(this->internal_check())) return false; return as_string_view() == other; } @@ -164,7 +164,7 @@ class KeyExpr : public Owned<::z_owned_keyexpr_t> { /// @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)) return false; + if (!(this->internal_check())) return false; return as_string_view() == other; } diff --git a/include/zenoh/api/liveliness.hxx b/include/zenoh/api/liveliness.hxx index f8fc0a0a..a71b9996 100644 --- a/include/zenoh/api/liveliness.hxx +++ b/include/zenoh/api/liveliness.hxx @@ -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_owned_c_ptr(*this)), err, + __ZENOH_RESULT_CHECK(::zc_liveliness_undeclare_token(detail::move_(*this)), err, "Failed to undeclare liveliness token"); } }; diff --git a/include/zenoh/api/publisher.hxx b/include/zenoh/api/publisher.hxx index 6ff4b4e5..5258aebc 100644 --- a/include/zenoh/api/publisher.hxx +++ b/include/zenoh/api/publisher.hxx @@ -69,14 +69,14 @@ 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_owned_c_ptr(payload); + auto payload_ptr = detail::move_(payload); ::z_publisher_put_options_t opts; z_publisher_put_options_default(&opts); - opts.encoding = detail::as_owned_c_ptr(options.encoding); + opts.encoding = detail::move_(options.encoding); #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) - opts.source_info = detail::as_owned_c_ptr(options.source_info); + opts.source_info = detail::move_(options.source_info); #endif - opts.attachment = detail::as_owned_c_ptr(options.attachment); + opts.attachment = detail::move_(options.attachment); opts.timestamp = detail::as_copyable_c_ptr(options.timestamp); __ZENOH_RESULT_CHECK(::z_publisher_put(this->loan(), payload_ptr, &opts), err, diff --git a/include/zenoh/api/query.hxx b/include/zenoh/api/query.hxx index 6ff2fad5..96e4b599 100644 --- a/include/zenoh/api/query.hxx +++ b/include/zenoh/api/query.hxx @@ -109,18 +109,18 @@ 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_owned_c_ptr(payload); + auto payload_ptr = detail::move_(payload); ::z_query_reply_options_t opts; z_query_reply_options_default(&opts); - opts.encoding = detail::as_owned_c_ptr(options.encoding); + opts.encoding = detail::move_(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); #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) - opts.source_info = detail::as_owned_c_ptr(options.source_info); + opts.source_info = detail::move_(options.source_info); #endif - opts.attachment = detail::as_owned_c_ptr(options.attachment); + opts.attachment = detail::move_(options.attachment); __ZENOH_RESULT_CHECK(::z_query_reply(this->loan(), detail::loan(key_expr), payload_ptr, &opts), err, "Failed to send reply"); @@ -145,10 +145,10 @@ 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_owned_c_ptr(payload); + auto payload_ptr = detail::move_(payload); ::z_query_reply_err_options_t opts; z_query_reply_err_options_default(&opts); - opts.encoding = detail::as_owned_c_ptr(options.encoding); + opts.encoding = detail::move_(options.encoding); __ZENOH_RESULT_CHECK(::z_query_reply_err(this->loan(), payload_ptr, &opts), err, "Failed to send reply error"); } @@ -192,9 +192,9 @@ class Query : public Owned<::z_owned_query_t> { opts.is_express = options.is_express; opts.timestamp = detail::as_copyable_c_ptr(options.timestamp); #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) - opts.source_info = detail::as_owned_c_ptr(options.source_info); + opts.source_info = detail::move_(options.source_info); #endif - opts.attachment = detail::as_owned_c_ptr(options.attachment); + opts.attachment = detail::move_(options.attachment); __ZENOH_RESULT_CHECK(::z_query_reply_del(this->loan(), detail::loan(key_expr), &opts), err, "Failed to send reply del"); diff --git a/include/zenoh/api/scout.hxx b/include/zenoh/api/scout.hxx index 2b91026d..959554a8 100644 --- a/include/zenoh/api/scout.hxx +++ b/include/zenoh/api/scout.hxx @@ -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_owned_c_ptr(config), ::z_move(c_closure), &opts), err, + __ZENOH_RESULT_CHECK(::z_scout(detail::move_(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 9394f410..329c0c4e 100644 --- a/include/zenoh/api/session.hxx +++ b/include/zenoh/api/session.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_owned_c_ptr(config)), err, "Failed to open session"); + __ZENOH_RESULT_CHECK(::z_open(&this->_0, detail::move_(config)), err, "Failed to open session"); #ifdef ZENOHCXX_ZENOHPICO if (err != nullptr && *err != Z_OK) return; if (options.start_background_tasks) { @@ -92,7 +92,7 @@ class Session : public Owned<::z_owned_session_t> { /// 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_owned_c_ptr(config), detail::loan(shm_storage)), err, + ::z_open_with_custom_shm_clients(&this->_0, detail::move_(config), detail::loan(shm_storage)), err, "Failed to open session"); } #endif @@ -154,7 +154,7 @@ 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_owned_c_ptr(key_expr), this->loan()), err, + __ZENOH_RESULT_CHECK(::z_undeclare_keyexpr(detail::move_(key_expr), this->loan()), err, "Failed to undeclare key expression"); } @@ -219,12 +219,12 @@ 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_owned_c_ptr(options.payload); - opts.encoding = detail::as_owned_c_ptr(options.encoding); + opts.payload = detail::move_(options.payload); + opts.encoding = detail::move_(options.encoding); #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) - opts.source_info = detail::as_owned_c_ptr(options.source_info); + opts.source_info = detail::move_(options.source_info); #endif - opts.attachment = detail::as_owned_c_ptr(options.attachment); + opts.attachment = detail::move_(options.attachment); opts.timeout_ms = options.timeout_ms; __ZENOH_RESULT_CHECK( @@ -252,18 +252,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_owned_c_ptr(options.payload); - opts.encoding = detail::as_owned_c_ptr(options.encoding); + opts.payload = detail::move_(options.payload); + opts.encoding = detail::move_(options.encoding); #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) - opts.source_info = detail::as_owned_c_ptr(options.source_info); + opts.source_info = detail::move_(options.source_info); #endif - opts.attachment = detail::as_owned_c_ptr(options.attachment); + opts.attachment = detail::move_(options.attachment); opts.timeout_ms = options.timeout_ms; ZResult res = ::z_get(this->loan(), detail::loan(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(::z_move(*detail::as_owned_c_ptr(cb_handler_pair.second))); + if (res != Z_OK) ::z_drop(detail::move_(cb_handler_pair.second)); return std::move(cb_handler_pair.second); } /// @brief Options to be passed to ``delete_resource`` operation @@ -341,19 +341,19 @@ 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_owned_c_ptr(options.encoding); + opts.encoding = detail::move_(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_owned_c_ptr(options.source_info); + opts.source_info = detail::move_(options.source_info); #endif - opts.attachment = detail::as_owned_c_ptr(options.attachment); + opts.attachment = detail::move_(options.attachment); opts.timestamp = detail::as_copyable_c_ptr(options.timestamp); #if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE) opts.allowed_destination = options.allowed_destination; #endif - auto payload_ptr = detail::as_owned_c_ptr(payload); + auto payload_ptr = detail::move_(payload); __ZENOH_RESULT_CHECK(::z_put(this->loan(), detail::loan(key_expr), payload_ptr, &opts), err, "Failed to perform put operation"); } @@ -426,7 +426,7 @@ class Session : public Owned<::z_owned_session_t> { ZResult res = ::z_declare_queryable(detail::as_owned_c_ptr(q), this->loan(), detail::loan(key_expr), ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Queryable"); - if (res != Z_OK) ::z_drop(::z_move(*detail::as_owned_c_ptr(cb_handler_pair.second))); + if (res != Z_OK) ::z_drop(detail::move_(cb_handler_pair.second)); return Queryable>(std::move(q), std::move(cb_handler_pair.second)); } @@ -501,7 +501,7 @@ class Session : public Owned<::z_owned_session_t> { ZResult res = ::z_declare_subscriber(detail::as_owned_c_ptr(s), this->loan(), detail::loan(key_expr), ::z_move(cb_handler_pair.first), &opts); __ZENOH_RESULT_CHECK(res, err, "Failed to declare Subscriber"); - if (res != Z_OK) ::z_drop(::z_move(*detail::as_owned_c_ptr(cb_handler_pair.second))); + if (res != Z_OK) ::z_drop(detail::move_(cb_handler_pair.second)); return Subscriber>(std::move(s), std::move(cb_handler_pair.second)); } @@ -545,7 +545,7 @@ 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_owned_c_ptr(options.encoding); + opts.encoding = detail::move_(options.encoding); Publisher p(nullptr); ZResult res = ::z_declare_publisher(detail::as_owned_c_ptr(p), this->loan(), detail::loan(key_expr), &opts); @@ -566,7 +566,8 @@ 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(), &c_closure), err, "Failed to fetch router Ids"); + __ZENOH_RESULT_CHECK(::z_info_routers_zid(this->loan(), ::z_move(c_closure)), err, + "Failed to fetch router Ids"); return out; } @@ -582,7 +583,7 @@ 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(), &c_closure), err, "Failed to fetch peer Ids"); + __ZENOH_RESULT_CHECK(::z_info_peers_zid(this->loan(), ::z_move(c_closure)), err, "Failed to fetch peer Ids"); return out; } #endif @@ -748,7 +749,7 @@ class Session : public Owned<::z_owned_session_t> { ZResult res = ::zc_liveliness_declare_subscriber( detail::as_owned_c_ptr(s), this->loan(), detail::loan(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_owned_c_ptr(cb_handler_pair.second))); + if (res != Z_OK) ::z_drop(::z_move(*detail::move_(cb_handler_pair.second))); return Subscriber>(std::move(s), std::move(cb_handler_pair.second)); } @@ -816,7 +817,7 @@ class Session : public Owned<::z_owned_session_t> { ZResult res = ::zc_liveliness_get(this->loan(), detail::loan(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(::z_move(*detail::as_owned_c_ptr(cb_handler_pair.second))); + if (res != Z_OK) ::z_drop(detail::move_(cb_handler_pair.second)); return std::move(cb_handler_pair.second); } diff --git a/include/zenoh/detail/interop.hxx b/include/zenoh/detail/interop.hxx index a5e4dcc0..7fe039c3 100644 --- a/include/zenoh/detail/interop.hxx +++ b/include/zenoh/detail/interop.hxx @@ -68,6 +68,11 @@ auto loan(OwnedType& o) { return ::z_loan_mut(*as_owned_c_ptr(o)); } +template +auto move_(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), diff --git a/tests/build/warnings.cxx b/tests/build/warnings.cxx index 0697130a..b98afc6f 100644 --- a/tests/build/warnings.cxx +++ b/tests/build/warnings.cxx @@ -13,15 +13,26 @@ // // Disable 'old-style-cast` warning for C headers only +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4996) +#else #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wold-style-cast" +#endif + #ifdef ZENOHCXX_ZENOHPICO #include "zenoh-pico.h" #endif #ifdef ZENOHCXX_ZENOHC #include "zenoh.h" #endif + +#ifdef _MSC_VER +#pragma warning(pop) +#else #pragma GCC diagnostic pop +#endif #include "zenoh.hxx" diff --git a/tests/universal/network/implicit.cxx b/tests/universal/network/implicit.cxx index ca12391a..98799054 100644 --- a/tests/universal/network/implicit.cxx +++ b/tests/universal/network/implicit.cxx @@ -18,7 +18,9 @@ void put_sub() { std::this_thread::sleep_for(1s); - session1.put("zenoh/test", "data", {.attachment = std::vector{-1, 10, 15000}}); + Session::PutOptions options; + options.attachment = std::vector{-1, 10, 15000}; + session1.put("zenoh/test", "data", std::move(options)); std::this_thread::sleep_for(1s); diff --git a/tests/universal/network/keyexpr.cxx b/tests/universal/network/keyexpr.cxx index 7ebc9a9e..16129445 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(static_cast(foo)); + assert(foo.internal_check()); 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); + assert(!k_err.internal_check()); assert(err < 0); #endif // enforce canonization KeyExpr k_ok(non_canon, true, &err); - assert(static_cast(k_ok)); + assert(k_ok.internal_check()); 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(static_cast(declared)); + assert(declared.internal_check()); 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); + assert(!declared.internal_check()); } int main(int argc, char** argv) { diff --git a/tests/universal/network/queryable_get.cxx b/tests/universal/network/queryable_get.cxx index b8a33c2b..1c39a55f 100644 --- a/tests/universal/network/queryable_get.cxx +++ b/tests/universal/network/queryable_get.cxx @@ -47,9 +47,11 @@ void queryable_get() { ke, [&queries](const Query& q) { auto payload = q.get_payload()->get().deserialize(); - queries.push_back(QueryData{.key = std::string(q.get_keyexpr().as_string_view()), - .params = std::string(q.get_parameters()), - .payload = payload}); + QueryData qd; + qd.key = std::string(q.get_keyexpr().as_string_view()); + qd.params = std::string(q.get_parameters()); + qd.payload = payload; + queries.push_back(std::move(qd)); if (q.get_parameters() == "ok") { q.reply(q.get_keyexpr(), Bytes::serialize(std::to_string(payload))); } else { @@ -68,11 +70,19 @@ void queryable_get() { }; auto on_drop = [&queries_processed]() { queries_processed++; }; - session2.get(selector, "ok", on_reply, on_drop, {.payload = Bytes::serialize(1)}); + Session::GetOptions opt1; + opt1.payload = Bytes::serialize(1); + session2.get(selector, "ok", on_reply, on_drop, std::move(opt1)); std::this_thread::sleep_for(1s); - session2.get(selector, "ok", on_reply, on_drop, {.payload = Bytes::serialize(2)}); + + Session::GetOptions opt2; + opt2.payload = Bytes::serialize(2); + session2.get(selector, "ok", on_reply, on_drop, std::move(opt2)); std::this_thread::sleep_for(1s); - session2.get(selector, "err", on_reply, on_drop, {.payload = Bytes::serialize(3)}); + + Session::GetOptions opt3; + opt3.payload = Bytes::serialize(3); + session2.get(selector, "err", on_reply, on_drop, std::move(opt3)); std::this_thread::sleep_for(1s); } @@ -106,7 +116,9 @@ void queryable_get_channel() { auto queryable = session1.declare_queryable(ke, channels::FifoChannel(3)); std::this_thread::sleep_for(1s); - auto replies = session2.get(selector, "ok", channels::FifoChannel(3), {.payload = Bytes::serialize(1)}); + Session::GetOptions opt1; + opt1.payload = Bytes::serialize(1); + auto replies = session2.get(selector, "ok", channels::FifoChannel(3), std::move(opt1)); { auto res = queryable.handler().recv(); assert(std::holds_alternative(res)); @@ -127,7 +139,9 @@ void queryable_get_channel() { assert(std::holds_alternative(res)); assert(std::get(res) == channels::RecvError::Z_DISCONNECTED); - replies = session2.get(selector, "err", channels::FifoChannel(3), {.payload = Bytes::serialize(3)}); + Session::GetOptions opt3; + opt3.payload = Bytes::serialize(3); + replies = session2.get(selector, "err", channels::FifoChannel(3), std::move(opt3)); { auto res = queryable.handler().recv(); assert(std::holds_alternative(res)); diff --git a/zenoh-c b/zenoh-c index 5a82ecfd..9e30b343 160000 --- a/zenoh-c +++ b/zenoh-c @@ -1 +1 @@ -Subproject commit 5a82ecfd6154adf30ff96efe9b7838018134a528 +Subproject commit 9e30b3430b6801286d630de269a69ff76c390d7d diff --git a/zenoh-pico b/zenoh-pico index 00f1c00b..cf629260 160000 --- a/zenoh-pico +++ b/zenoh-pico @@ -1 +1 @@ -Subproject commit 00f1c00b37cd0f4af8eb36f52046b6c9db4b30b7 +Subproject commit cf6292601ea6bc595d47d1fb75dc11de0ba79fa4