Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::optional for optional arguments in options #117

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions include/zenoh/api.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <sstream>
#include <cstddef>
#include <cstdint>
#include <optional>

namespace zenoh {

Expand Down Expand Up @@ -459,12 +460,12 @@ public:
/// @return Attachment
decltype(auto) get_attachment() const { return detail::as_owned_cpp_obj<Bytes>(::z_query_attachment(this->loan())); }

/// Options passed to the ``Query::reply`` operation
/// @brief Options passed to the ``Query::reply`` operation
struct ReplyOptions {
/// @brief An optional encoding of this reply payload and/or attachment
Encoding encoding = Encoding(nullptr);
std::optional<Encoding> encoding = {};
/// @brief An optional attachment to this reply.
Bytes attachment = Bytes(nullptr);
std::optional<Bytes> attachment = {};

/// @brief Returns default option settings
static ReplyOptions create_default() { return {}; }
Expand All @@ -484,10 +485,10 @@ public:
);
}

/// Options passed to the ``Query::reply_err`` operation
/// @brief Options passed to the ``Query::reply_err()`` operation
struct ReplyErrOptions {
/// @brief An optional encoding of the reply error payload
Encoding encoding = Encoding(nullptr);
std::optional<Encoding> encoding = {};

/// @brief Returns default option settings
static ReplyErrOptions create_default() { return {}; }
Expand Down Expand Up @@ -695,18 +696,18 @@ class Publisher : public Owned<::z_owned_publisher_t> {
public:
using Owned::Owned;

/// Options to be passed to ``Publisher::put`` operation
/// @brief Options to be passed to ``Publisher::put()`` operation
struct PutOptions {
/// @brief The encoding of the data to publish.
Encoding encoding = Encoding(nullptr);
std::optional<Encoding> encoding = {};
/// @brief The attachment to attach to the publication.
Bytes attachment = Bytes(nullptr);
std::optional<Bytes> attachment = {};

/// @brief Returns default option settings
static PutOptions create_default() { return {}; }
};

/// Options to be passed to delete operation of a publisher
/// @brief Options to be passed to ``Publisher::delete_resource()`` operation
struct DeleteOptions {
uint8_t __dummy;

Expand Down Expand Up @@ -859,18 +860,18 @@ public:
);
}

/// Options passed to the get operation
/// @brief Options passed to the ``get()`` operation
struct GetOptions {
/// @brief The Queryables that should be target of the query.
QueryTarget target = QueryTarget::Z_QUERY_TARGET_ALL;
/// @brief The replies consolidation strategy to apply on replies to the query.
QueryConsolidation consolidation = QueryConsolidation();
/// @brief An optional payload of the query.
Bytes payload = Bytes(nullptr);
std::optional<Bytes> payload = {};
/// @brief An optional encoding of the query payload and/or attachment.
Encoding encoding = Encoding(nullptr);
/// An optional attachment to the query.
Bytes attachment = Bytes(nullptr);
std::optional<Encoding> encoding = {};
/// @brief An optional attachment to the query.
std::optional<Bytes> attachment = {};
/// @brief The timeout for the query in milliseconds. 0 means default query timeout from zenoh configuration.
uint64_t timeout_ms = 0;

Expand Down Expand Up @@ -950,7 +951,7 @@ public:
if (res != Z_OK) std::move(recv).take();
return recv;
}
/// Options to be passed to delete operation
/// @brief Options to be passed to ``delete_resource()`` operation
struct DeleteOptions {
/// @brief The priority of the delete message.
Priority priority = Priority::Z_PRIORITY_DATA;
Expand All @@ -976,7 +977,7 @@ public:
return ::z_delete(this->loan(), detail::loan(key_expr), &opts);
}

/// Options passed to the put operation
/// @brief Options passed to the ``put()`` operation
struct PutOptions {
/// @brief The priority of this message.
Priority priority = Priority::Z_PRIORITY_DATA;
Expand All @@ -985,9 +986,9 @@ public:
/// @brief Whether Zenoh will NOT wait to batch this message with others to reduce the bandwith.
bool is_express = false;
/// @brief An optional encoding of the message payload and/or attachment.
Encoding encoding = Encoding(nullptr);
/// An optional attachment to the message.
Bytes attachment = Bytes(nullptr);
std::optional<Encoding> encoding = {};
/// @brief An optional attachment to the message.
std::optional<Bytes> attachment = {};

/// @brief Returns default option settings
static PutOptions create_default() { return {}; }
Expand All @@ -1013,7 +1014,7 @@ public:
);
}

/// Options to be passed when declaring a ``Queryable``
/// @brief Options to be passed when declaring a ``Queryable``
struct QueryableOptions {
/// @brief The completeness of the Queryable.
bool complete = false;
Expand Down Expand Up @@ -1055,6 +1056,7 @@ public:
return q;
}

/// @brief Options to be passed when declaring a ``Subscriber``
struct SubscriberOptions {
/// @brief The subscription reliability.
Reliability reliability = Reliability::Z_RELIABILITY_BEST_EFFORT;
Expand Down Expand Up @@ -1096,6 +1098,7 @@ public:
return s;
}

/// @brief Options to be passed when declaring a ``Publisher``
struct PublisherOptions {
/// @brief The congestion control to apply when routing messages from this publisher.
CongestionControl congestion_control;
Expand All @@ -1104,6 +1107,7 @@ public:
/// @brief If true, Zenoh will not wait to batch this message with others to reduce the bandwith
bool is_express;
/// @brief Returns default option settings

static PublisherOptions create_default() { return {}; }
};

Expand Down Expand Up @@ -1262,11 +1266,13 @@ public:
}
};

/// @brief Options to be passed to ``scout()`` operation
struct ScoutOptions {
/// The maximum duration in ms the scouting can take.
/// @brief The maximum duration in ms the scouting can take.
size_t timeout_ms = 1000;
/// Type of entities to scout for.
/// @brief Type of entities to scout for.
WhatAmI what = WhatAmI::Z_WHATAMI_ROUTER_PEER;

/// @brief Returns default option settings
static ScoutOptions create_default() { return {}; }
};
Expand Down
11 changes: 11 additions & 0 deletions include/zenoh/internal.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "base.hxx"
#include <functional>
#include <optional>
namespace zenoh::detail {

template<class OwnedType>
Expand All @@ -26,6 +27,16 @@ const OwnedType* as_owned_c_ptr(const Owned<OwnedType>& o) {
return reinterpret_cast<const OwnedType*>(&o);
}

template<class OwnedCppObj>
auto* as_owned_c_ptr(std::optional<OwnedCppObj>& o) {
return o.has_value() ? as_owned_c_ptr(o.value()) : nullptr;
}

template<class OwnedCppObj>
const auto* as_owned_c_ptr(const std::optional<OwnedCppObj>& o) {
return o.has_value() ? as_owned_c_ptr(o.value()) : nullptr;
}

template<class OwnedType>
auto loan(const OwnedType& o) {
return ::z_loan(*as_owned_c_ptr(o));
Expand Down
Loading