Skip to content

Commit

Permalink
update to latest api with move protection, compilation on windows fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Aug 21, 2024
1 parent a080d26 commit 8398e6d
Show file tree
Hide file tree
Showing 25 changed files with 175 additions and 74 deletions.
8 changes: 8 additions & 0 deletions examples/universal/z_get.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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; });

Expand Down
8 changes: 8 additions & 0 deletions examples/universal/z_get_attachment.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,17 @@ int _main(int argc, char **argv) {

std::unordered_map<std::string, std::string> 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; });
Expand Down
7 changes: 7 additions & 0 deletions examples/universal/z_get_channel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Reply>(res); res = replies.recv()) {
const auto &sample = std::get<Reply>(res).get_ok();
Expand Down
7 changes: 7 additions & 0 deletions examples/universal/z_get_channel_non_blocking.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions examples/universal/z_pub.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 7 additions & 0 deletions examples/universal/z_pub_attachment.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions examples/universal/z_pub_thr.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
8 changes: 7 additions & 1 deletion examples/universal/z_put.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ int _main(int argc, char **argv) {

std::unordered_map<std::string, std::string> 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;
}
Expand Down
6 changes: 6 additions & 0 deletions examples/universal/z_queryable.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ int _main(int argc, char **argv) {
std::cout << "' value = '" << payload->get().deserialize<std::string>();
}
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"; };
Expand Down
7 changes: 7 additions & 0 deletions examples/universal/z_queryable_attachment.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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"; };
Expand Down
12 changes: 7 additions & 5 deletions include/zenoh/api/base.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
16 changes: 7 additions & 9 deletions include/zenoh/api/bytes.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -300,7 +300,7 @@ template <>
struct ZenohDeserializer<ZShm> {
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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
12 changes: 6 additions & 6 deletions include/zenoh/api/keyexpr.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<std::string_view>(key_expr), autocanonize, err){};
: KeyExpr(static_cast<std::string_view>(key_expr), autocanonize, err) {};

/// @brief Create a new instance from a null-terminated string.
///
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion include/zenoh/api/liveliness.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
};
Expand Down
8 changes: 4 additions & 4 deletions include/zenoh/api/publisher.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions include/zenoh/api/query.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
}
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion include/zenoh/api/scout.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
Loading

0 comments on commit 8398e6d

Please sign in to comment.