10#ifndef NROS_CPP_PUBLISHER_HPP
11#define NROS_CPP_PUBLISHER_HPP
22#include "nros_cpp_ffi.h"
59 return Result(nros_cpp_publish_raw(storage_, data, len));
77 } ctx{
static_cast<W&&
>(writer), total_len};
78 auto size_cb = [](
size_t* out_total,
void* user) {
79 auto* c =
static_cast<Ctx*
>(user);
80 *out_total = c->total;
82 auto chunk_cb = [](uint8_t* out_buf,
size_t cap,
size_t* out_written,
void* user) {
83 auto* c =
static_cast<Ctx*
>(user);
84 *out_written = c->writer(out_buf, cap);
86 return Result(nros_cpp_publisher_publish_streamed(storage_, size_cb, chunk_cb,
87 static_cast<void*
>(&ctx)));
99 Loan() : pub_(nullptr), buf_(nullptr), cap_(0), token_(nullptr) {}
100 Loan(
Loan&& o) : pub_(o.pub_), buf_(o.buf_), cap_(o.cap_), token_(o.token_) {
121 uint8_t*
data() {
return buf_; }
122 const uint8_t*
data()
const {
return buf_; }
125 bool is_valid()
const {
return token_ !=
nullptr; }
130 nros_cpp_ret_t ret = nros_cpp_publisher_commit(pub_, token_, actual_len);
151 Loan(
void* pub, uint8_t* buf,
size_t cap,
void* token)
152 : pub_(pub), buf_(buf), cap_(cap), token_(token) {}
156 if (token_ && pub_) {
157 nros_cpp_publisher_discard(pub_, token_);
173 uint8_t* buf =
nullptr;
175 void* token =
nullptr;
176 nros_cpp_ret_t ret = nros_cpp_publisher_loan(storage_, requested_len, &buf, &cap, &token);
190 nros_cpp_publisher_destroy(storage_);
191 initialized_ =
false;
198 if (other.initialized_) {
199 nros_cpp_publisher_relocate(other.storage_, storage_);
200 ::memcpy(topic_name_, other.topic_name_,
sizeof(topic_name_));
201 other.initialized_ =
false;
206 if (
this != &other) {
208 nros_cpp_publisher_destroy(storage_);
209 initialized_ =
false;
211 if (other.initialized_) {
212 nros_cpp_publisher_relocate(other.storage_, storage_);
213 ::memcpy(topic_name_, other.topic_name_,
sizeof(topic_name_));
215 other.initialized_ =
false;
223 Publisher() : storage_(), topic_name_{}, initialized_(false) {}
232 return Result(nros_cpp_publisher_set_liveliness_lost(storage_, cb, user_context));
238 void* user_context =
nullptr) {
240 return Result(nros_cpp_publisher_set_offered_deadline_missed(storage_, deadline_ms, cb,
249 return Result(nros_cpp_publisher_assert_liveliness(storage_));
258 alignas(8) uint8_t storage_[NROS_PUBLISHER_SIZE];
280 nros_cpp_ret_t ret = nros_cpp_publisher_create(&handle_, topic, M::TYPE_NAME, M::TYPE_HASH,
281 ffi_qos, out.storage_);
285 size_t topic_len = 0;
286 while (topic[topic_len] !=
'\0' && topic_len + 1 <
sizeof(out.topic_name_)) {
287 out.topic_name_[topic_len] = topic[topic_len];
290 out.topic_name_[topic_len] =
'\0';
291 out.initialized_ =
true;
303 const PublisherOptions& options) {
305 return create_publisher<M>(out, topic, qos);
Definition result.hpp:198
ErrorCode error() const
Definition result.hpp:221
bool ok() const
Definition result.hpp:214
Result create_publisher(Publisher< M > &out, const char *topic, const QoS &qos=QoS::default_profile())
Definition publisher.hpp:274
Definition publisher.hpp:97
Result commit(size_t actual_len)
Send actual_len bytes. Consumes the loan.
Definition publisher.hpp:128
Loan(Loan &&o)
Definition publisher.hpp:100
Loan(void *pub, uint8_t *buf, size_t cap, void *token)
Definition publisher.hpp:151
const uint8_t * data() const
Definition publisher.hpp:122
~Loan()
Definition publisher.hpp:118
Loan()
Definition publisher.hpp:99
Loan & operator=(const Loan &)=delete
size_t capacity() const
Definition publisher.hpp:123
Loan & operator=(Loan &&o)
Definition publisher.hpp:104
Result discard()
Abandon without sending. Consumes the loan.
Definition publisher.hpp:139
Loan(const Loan &)=delete
bool is_valid() const
Definition publisher.hpp:125
uint8_t * data()
Writable view of the loaned bytes.
Definition publisher.hpp:121
Definition publisher.hpp:48
Expected< Loan > loan(size_t requested_len)
Definition publisher.hpp:171
Result publish_streamed(size_t total_len, W &&writer)
Definition publisher.hpp:72
Publisher()
Definition publisher.hpp:223
Result publish_raw(const uint8_t *data, size_t len)
Publish raw CDR bytes.
Definition publisher.hpp:57
const char * get_topic_name() const
Get the topic name.
Definition publisher.hpp:182
Result on_liveliness_lost(nros_cpp_publisher_count_cb_t cb, void *user_context=nullptr)
Register a callback for liveliness-lost events on this publisher.
Definition publisher.hpp:230
Publisher(Publisher &&other)
Definition publisher.hpp:197
Result publish(const M &msg)
Definition publisher.hpp:54
~Publisher()
Destructor — releases publisher resources.
Definition publisher.hpp:188
Result on_offered_deadline_missed(uint32_t deadline_ms, nros_cpp_publisher_count_cb_t cb, void *user_context=nullptr)
Definition publisher.hpp:237
bool is_valid() const
Check if the publisher is initialized and valid.
Definition publisher.hpp:185
Result assert_liveliness()
Definition publisher.hpp:247
Publisher & operator=(Publisher &&other)
Definition publisher.hpp:205
static constexpr QoS default_profile()
Default profile: RELIABLE + VOLATILE + KEEP_LAST(10).
Definition qos.hpp:328
static constexpr Result success()
Named constructors.
Definition result.hpp:112
bool ok() const
Returns true if the operation succeeded.
Definition result.hpp:100
Inline storage-size macros for opaque entity buffers.
int nros_cpp_ret_t
Definition future.hpp:21
static constexpr size_t PUBLISHER_TOPIC_NAME_MAX
Definition publisher.hpp:28
bool ok()
Check if the nros session is initialized.
Definition node.hpp:997
nros::Node and global session helpers.
nros::Result, nros::ErrorCode, and the NROS_TRY macro.