21#include "wpi/util/SmallVector.hpp"
28template <
typename T,
typename... I>
29 requires wpi::util::StructSerializable<T, I...>
35template <
typename T,
typename... I>
36 requires wpi::util::StructSerializable<T, I...>
57#if __cpp_lib_ranges >= 201911L
58 requires std::ranges::range<U> &&
59 std::convertible_to<std::ranges::range_value_t<U>, T>
63 m_defaultValue{defaultValue.begin(), defaultValue.end()},
64 m_info{
std::move(info)...} {
85#if __cpp_lib_ranges >= 201911L
86 requires std::ranges::range<U> &&
87 std::convertible_to<std::ranges::range_value_t<U>, T>
122 template <
typename U>
123#if __cpp_lib_ranges >= 201911L
124 requires std::ranges::range<U> &&
125 std::convertible_to<std::ranges::range_value_t<U>, T>
128 wpi::util::SmallVector<uint8_t, 128> buf;
129 size_t size = std::apply(S::GetSize, m_info);
131 if (view.
value.size() == 0 || (view.
value.size() % size) != 0) {
132 return {0, 0, std::forward<U>(defaultValue)};
136 for (
auto in = view.
value.begin(), end = view.
value.end(); in != end;
139 [&](
const I&... info) {
140 rv.
value.emplace_back(S::Unpack(
141 std::span<const uint8_t>{std::to_address(in), size}, info...));
157 wpi::util::SmallVector<uint8_t, 128> buf;
158 size_t size = std::apply(S::GetSize, m_info);
160 if (view.
value.size() == 0 || (view.
value.size() % size) != 0) {
161 return {0, 0, {defaultValue.begin(), defaultValue.end()}};
165 for (
auto in = view.
value.begin(), end = view.
value.end(); in != end;
168 [&](
const I&... info) {
169 rv.
value.emplace_back(S::Unpack(
170 std::span<const uint8_t>{std::to_address(in), size}, info...));
190 std::vector<TimestampedValueType> rv;
191 rv.reserve(raw.size());
192 size_t size = std::apply(S::GetSize, m_info);
193 for (
auto&& r : raw) {
194 if (r.value.size() == 0 || (r.value.size() % size) != 0) {
197 std::vector<T> values;
198 values.reserve(r.value.size() / size);
199 for (
auto in = r.value.begin(), end = r.value.end(); in != end;
202 [&](
const I&... info) {
204 S::Unpack(std::span<const uint8_t>{std::to_address(in), size},
209 rv.emplace_back(r.time, r.serverTime, std::move(values));
221 [&](
const I&... info) {
230 [[no_unique_address]]
231 std::tuple<I...> m_info;
237template <
typename T,
typename... I>
266 m_buf{
std::move(rhs.m_buf)},
268 rhs.m_schemaPublished.load(
std::memory_order_relaxed)},
269 m_info{
std::move(rhs.m_info)} {}
273 m_buf = std::move(rhs.m_buf);
274 m_schemaPublished.store(
275 rhs.m_schemaPublished.load(std::memory_order_relaxed),
276 std::memory_order_relaxed);
277 m_info = std::move(rhs.m_info);
287 template <
typename U>
288#if __cpp_lib_ranges >= 201911L
289 requires std::ranges::range<U> &&
290 std::convertible_to<std::ranges::range_value_t<U>, T>
292 void Set(U&& value, int64_t time = 0) {
294 [&](
const I&... info) {
295 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
299 std::forward<U>(value),
312 void Set(std::span<const T> value, int64_t time = 0) {
314 [&](
const I&... info) {
315 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
333 template <
typename U>
334#if __cpp_lib_ranges >= 201911L
335 requires std::ranges::range<U> &&
336 std::convertible_to<std::ranges::range_value_t<U>, T>
340 [&](
const I&... info) {
341 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
345 std::forward<U>(value),
361 [&](
const I&... info) {
362 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
380 [&](
const I&... info) {
389 std::atomic_bool m_schemaPublished{
false};
390 [[no_unique_address]]
391 std::tuple<I...> m_info;
399template <
typename T,
typename... I>
422 template <
typename U>
423#if __cpp_lib_ranges >= 201911L
424 requires std::ranges::range<U> &&
425 std::convertible_to<std::ranges::range_value_t<U>, T>
464template <
typename T,
typename... I>
485 :
Topic{handle}, m_info{
std::move(info)...} {}
494 :
Topic{topic}, m_info{
std::move(info)...} {}
511 template <
typename U>
512#if __cpp_lib_ranges >= 201911L
513 requires std::ranges::range<U> &&
514 std::convertible_to<std::ranges::range_value_t<U>, T>
520 [&](
const I&... info) {
527 defaultValue, info...};
549 std::span<const T> defaultValue,
552 [&](
const I&... info) {
559 defaultValue, info...};
582 [&](
const I&... info) {
613 const wpi::util::json& properties,
616 [&](
const I&... info) {
622 properties, options),
648 template <
typename U>
649#if __cpp_lib_ranges >= 201911L
650 requires std::ranges::range<U> &&
651 std::convertible_to<std::ranges::range_value_t<U>, T>
657 [&](
const I&... info) {
664 defaultValue, info...};
693 [&](
const I&... info) {
700 defaultValue, info...};
706 [[no_unique_address]]
707 std::tuple<I...> m_info;
Publisher(const Publisher &)=delete
Publisher & operator=(const Publisher &)=delete
NT_Publisher m_pubHandle
NetworkTables handle.
Definition Topic.hpp:440
NetworkTables struct-encoded value array entry.
Definition StructArrayTopic.hpp:402
TopicType GetTopic() const
Get the corresponding topic.
Definition StructArrayTopic.hpp:451
StructArraySubscriber< T, I... > SubscriberType
Definition StructArrayTopic.hpp:404
StructArrayEntry()=default
StructArrayEntry(NT_Entry handle, U &&defaultValue, const I &... info)
Construct from an entry handle; recommended to use StructTopic::GetEntry() instead.
Definition StructArrayTopic.hpp:427
std::vector< T > ValueType
Definition StructArrayTopic.hpp:407
void Unpublish()
Stops publishing the entry if it's published.
Definition StructArrayTopic.hpp:458
Timestamped< ValueType > TimestampedValueType
Definition StructArrayTopic.hpp:410
StructArrayTopic< T, I... > TopicType
Definition StructArrayTopic.hpp:406
StructArrayPublisher< T, I... > PublisherType
Definition StructArrayTopic.hpp:405
std::span< const T > ParamType
Definition StructArrayTopic.hpp:408
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition StructArrayTopic.hpp:444
NetworkTables struct-encoded value array publisher.
Definition StructArrayTopic.hpp:239
std::vector< T > ValueType
Definition StructArrayTopic.hpp:244
void SetDefault(U &&value)
Publish a default value.
Definition StructArrayTopic.hpp:338
StructArrayPublisher(NT_Publisher handle, I... info)
Construct from a publisher handle; recommended to use StructTopic::Publish() instead.
Definition StructArrayTopic.hpp:258
TopicType GetTopic() const
Get the corresponding topic.
Definition StructArrayTopic.hpp:378
std::span< const T > ParamType
Definition StructArrayTopic.hpp:245
StructArrayTopic< T, I... > TopicType
Definition StructArrayTopic.hpp:243
Timestamped< ValueType > TimestampedValueType
Definition StructArrayTopic.hpp:247
void Set(U &&value, int64_t time=0)
Publish a new value.
Definition StructArrayTopic.hpp:292
StructArrayPublisher & operator=(StructArrayPublisher &&rhs)
Definition StructArrayTopic.hpp:271
StructArrayPublisher()=default
void SetDefault(std::span< const T > value)
Publish a default value.
Definition StructArrayTopic.hpp:359
void Set(std::span< const T > value, int64_t time=0)
Publish a new value.
Definition StructArrayTopic.hpp:312
StructArrayPublisher(StructArrayPublisher &&rhs)
Definition StructArrayTopic.hpp:264
StructArrayPublisher & operator=(const StructArrayPublisher &)=delete
StructArrayPublisher(const StructArrayPublisher &)=delete
NetworkTables struct-encoded value array subscriber.
Definition StructArrayTopic.hpp:37
ValueType Get() const
Get the last published value.
Definition StructArrayTopic.hpp:74
TimestampedValueType GetAtomic(std::span< const T > defaultValue) const
Get the last published value along with its timestamp.
Definition StructArrayTopic.hpp:156
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published or the value can...
Definition StructArrayTopic.hpp:112
std::vector< T > ValueType
Definition StructArrayTopic.hpp:42
ValueType Get(std::span< const T > defaultValue) const
Get the last published value.
Definition StructArrayTopic.hpp:101
ValueType Get(U &&defaultValue) const
Get the last published value.
Definition StructArrayTopic.hpp:89
StructArraySubscriber(NT_Subscriber handle, U &&defaultValue, I... info)
Construct from a subscriber handle; recommended to use StructTopic::Subscribe() instead.
Definition StructArrayTopic.hpp:61
StructArrayTopic< T, I... > TopicType
Definition StructArrayTopic.hpp:41
TimestampedValueType GetAtomic(U &&defaultValue) const
Get the last published value along with its timestamp.
Definition StructArrayTopic.hpp:127
std::span< const T > ParamType
Definition StructArrayTopic.hpp:43
Timestamped< ValueType > TimestampedValueType
Definition StructArrayTopic.hpp:44
StructArraySubscriber()=default
std::vector< TimestampedValueType > ReadQueue()
Get an array of all valid value changes since the last call to ReadQueue.
Definition StructArrayTopic.hpp:188
TopicType GetTopic() const
Get the corresponding topic.
Definition StructArrayTopic.hpp:219
NetworkTables struct-encoded value array topic.
Definition StructArrayTopic.hpp:466
SubscriberType Subscribe(std::span< const T > defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition StructArrayTopic.hpp:548
SubscriberType Subscribe(U &&defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition StructArrayTopic.hpp:517
StructArrayTopic(Topic topic, I... info)
Construct from a generic topic.
Definition StructArrayTopic.hpp:493
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition StructArrayTopic.hpp:580
StructArrayEntry< T, I... > EntryType
Definition StructArrayTopic.hpp:470
EntryType GetEntry(U &&defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition StructArrayTopic.hpp:654
StructArrayTopic()=default
PublisherType PublishEx(const wpi::util::json &properties, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic, with type string and initial properties.
Definition StructArrayTopic.hpp:612
EntryType GetEntry(std::span< const T > defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition StructArrayTopic.hpp:690
StructArrayTopic(NT_Topic handle, I... info)
Construct from a topic handle; recommended to use NetworkTableInstance::GetStructTopic() instead.
Definition StructArrayTopic.hpp:484
std::span< const T > ParamType
Definition StructArrayTopic.hpp:472
std::vector< T > ValueType
Definition StructArrayTopic.hpp:471
StructArrayPublisher< T, I... > PublisherType
Definition StructArrayTopic.hpp:469
StructArraySubscriber< T, I... > SubscriberType
Definition StructArrayTopic.hpp:468
Timestamped< ValueType > TimestampedValueType
Definition StructArrayTopic.hpp:473
Subscriber(const Subscriber &)=delete
NT_Subscriber m_subHandle
Definition Topic.hpp:385
NT_Topic m_handle
Definition Topic.hpp:316
NetworkTableInstance GetInstance() const
Gets the instance for the topic.
Definition Struct.hpp:437
Specifies that a type is capable of raw struct serialization and deserialization.
Definition Struct.hpp:69
bool SetRaw(NT_Handle pubentry, std::span< const uint8_t > value, int64_t time=0)
Publish a new value.
std::vector< TimestampedRaw > ReadQueueRaw(NT_Handle subentry)
Get an array of all value changes since the last call to ReadQueue.
TimestampedRaw GetAtomicRaw(NT_Handle subentry, std::span< const uint8_t > defaultValue)
Get the last published value along with its timestamp.
bool SetDefaultRaw(NT_Handle pubentry, std::span< const uint8_t > defaultValue)
Publish a default value.
NT_Handle NT_Topic
Definition ntcore_c.h:42
NT_Handle NT_Subscriber
Definition ntcore_c.h:43
NT_Handle NT_Publisher
Definition ntcore_c.h:44
NT_Handle NT_Entry
Definition ntcore_c.h:37
@ NT_RAW
Definition ntcore_c.h:55
Timestamped< std::span< uint8_t > > TimestampedRawView
Timestamped Raw view (for wpi::util::SmallVector-taking functions).
Definition ntcore_cpp_types.hpp:469
constexpr PubSubOptions kDefaultPubSubOptions
Default publish/subscribe options.
Definition ntcore_cpp.hpp:388
NT_Entry GetEntry(NT_Inst inst, std::string_view name)
Get Entry Handle.
NT_Topic GetTopicFromHandle(NT_Handle pubsubentry)
Gets the topic handle from an entry/subscriber/publisher handle.
NT_Publisher Publish(NT_Topic topic, NT_Type type, std::string_view typeStr, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new publisher to a topic.
void Unpublish(NT_Handle pubentry)
Stops publisher.
NT_Publisher PublishEx(NT_Topic topic, NT_Type type, std::string_view typeStr, const wpi::util::json &properties, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new publisher to a topic.
NT_Subscriber Subscribe(NT_Topic topic, NT_Type type, std::string_view typeStr, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new subscriber to value changes on a topic.
Definition StringMap.hpp:773
NetworkTables (ntcore) namespace.
Definition NTSendable.hpp:9
constexpr auto MakeStructArrayTypeString(const I &... info)
Definition Struct.hpp:370
NetworkTables publish/subscribe options.
Definition ntcore_cpp.hpp:303
Timestamped value.
Definition ntcore_cpp_types.hpp:30
int64_t serverTime
Time in server time base.
Definition ntcore_cpp_types.hpp:43
T value
Value.
Definition ntcore_cpp_types.hpp:48
int64_t time
Time in local time base.
Definition ntcore_cpp_types.hpp:38
Struct serialization template.
Definition Struct.hpp:39