21#include "wpi/util/SmallVector.hpp"
31template <
typename T,
typename... I>
32 requires wpi::util::StructSerializable<T, I...>
38template <
typename T,
typename... I>
39 requires wpi::util::StructSerializable<T, I...>
60#if __cpp_lib_ranges >= 201911L
61 requires std::ranges::range<U> &&
62 std::convertible_to<std::ranges::range_value_t<U>, T>
66 m_defaultValue{defaultValue.begin(), defaultValue.end()},
67 m_info{
std::move(info)...} {
88#if __cpp_lib_ranges >= 201911L
89 requires std::ranges::range<U> &&
90 std::convertible_to<std::ranges::range_value_t<U>, T>
125 template <
typename U>
126#if __cpp_lib_ranges >= 201911L
127 requires std::ranges::range<U> &&
128 std::convertible_to<std::ranges::range_value_t<U>, T>
131 wpi::util::SmallVector<uint8_t, 128> buf;
132 size_t size = std::apply(S::GetSize, m_info);
134 if (view.
value.size() == 0 || (view.
value.size() % size) != 0) {
135 return {0, 0, std::forward<U>(defaultValue)};
139 for (
auto in = view.
value.begin(), end = view.
value.end(); in != end;
142 [&](
const I&... info) {
143 rv.
value.emplace_back(S::Unpack(
144 std::span<const uint8_t>{std::to_address(in), size}, info...));
160 wpi::util::SmallVector<uint8_t, 128> buf;
161 size_t size = std::apply(S::GetSize, m_info);
163 if (view.
value.size() == 0 || (view.
value.size() % size) != 0) {
164 return {0, 0, {defaultValue.begin(), defaultValue.end()}};
168 for (
auto in = view.
value.begin(), end = view.
value.end(); in != end;
171 [&](
const I&... info) {
172 rv.
value.emplace_back(S::Unpack(
173 std::span<const uint8_t>{std::to_address(in), size}, info...));
193 std::vector<TimestampedValueType> rv;
194 rv.reserve(raw.size());
195 size_t size = std::apply(S::GetSize, m_info);
196 for (
auto&& r : raw) {
197 if (r.value.size() == 0 || (r.value.size() % size) != 0) {
200 std::vector<T> values;
201 values.reserve(r.value.size() / size);
202 for (
auto in = r.value.begin(), end = r.value.end(); in != end;
205 [&](
const I&... info) {
207 S::Unpack(std::span<const uint8_t>{std::to_address(in), size},
212 rv.emplace_back(r.time, r.serverTime, std::move(values));
224 [&](
const I&... info) {
233 [[no_unique_address]]
234 std::tuple<I...> m_info;
240template <
typename T,
typename... I>
269 m_buf{
std::move(rhs.m_buf)},
271 rhs.m_schemaPublished.load(
std::memory_order_relaxed)},
272 m_info{
std::move(rhs.m_info)} {}
276 m_buf = std::move(rhs.m_buf);
277 m_schemaPublished.store(
278 rhs.m_schemaPublished.load(std::memory_order_relaxed),
279 std::memory_order_relaxed);
280 m_info = std::move(rhs.m_info);
290 template <
typename U>
291#if __cpp_lib_ranges >= 201911L
292 requires std::ranges::range<U> &&
293 std::convertible_to<std::ranges::range_value_t<U>, T>
295 void Set(U&& value, int64_t time = 0) {
297 [&](
const I&... info) {
298 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
302 std::forward<U>(value),
315 void Set(std::span<const T> value, int64_t time = 0) {
317 [&](
const I&... info) {
318 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
336 template <
typename U>
337#if __cpp_lib_ranges >= 201911L
338 requires std::ranges::range<U> &&
339 std::convertible_to<std::ranges::range_value_t<U>, T>
343 [&](
const I&... info) {
344 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
348 std::forward<U>(value),
364 [&](
const I&... info) {
365 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
383 [&](
const I&... info) {
392 std::atomic_bool m_schemaPublished{
false};
393 [[no_unique_address]]
394 std::tuple<I...> m_info;
402template <
typename T,
typename... I>
425 template <
typename U>
426#if __cpp_lib_ranges >= 201911L
427 requires std::ranges::range<U> &&
428 std::convertible_to<std::ranges::range_value_t<U>, T>
467template <
typename T,
typename... I>
488 :
Topic{handle}, m_info{
std::move(info)...} {}
497 :
Topic{topic}, m_info{
std::move(info)...} {}
514 template <
typename U>
515#if __cpp_lib_ranges >= 201911L
516 requires std::ranges::range<U> &&
517 std::convertible_to<std::ranges::range_value_t<U>, T>
523 [&](
const I&... info) {
530 defaultValue, info...};
552 std::span<const T> defaultValue,
555 [&](
const I&... info) {
562 defaultValue, info...};
586 [&](
const I&... info) {
620 [&](
const I&... info) {
626 properties, options),
652 template <
typename U>
653#if __cpp_lib_ranges >= 201911L
654 requires std::ranges::range<U> &&
655 std::convertible_to<std::ranges::range_value_t<U>, T>
661 [&](
const I&... info) {
668 defaultValue, info...};
697 [&](
const I&... info) {
704 defaultValue, info...};
710 [[no_unique_address]]
711 std::tuple<I...> m_info;
Publisher(const Publisher &)=delete
Publisher & operator=(const Publisher &)=delete
NT_Publisher m_pubHandle
NetworkTables handle.
Definition Topic.hpp:443
NetworkTables struct-encoded value array entry.
Definition StructArrayTopic.hpp:405
TopicType GetTopic() const
Get the corresponding topic.
Definition StructArrayTopic.hpp:454
StructArraySubscriber< T, I... > SubscriberType
Definition StructArrayTopic.hpp:407
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:430
std::vector< T > ValueType
Definition StructArrayTopic.hpp:410
void Unpublish()
Stops publishing the entry if it's published.
Definition StructArrayTopic.hpp:461
Timestamped< ValueType > TimestampedValueType
Definition StructArrayTopic.hpp:413
StructArrayTopic< T, I... > TopicType
Definition StructArrayTopic.hpp:409
StructArrayPublisher< T, I... > PublisherType
Definition StructArrayTopic.hpp:408
std::span< const T > ParamType
Definition StructArrayTopic.hpp:411
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition StructArrayTopic.hpp:447
NetworkTables struct-encoded value array publisher.
Definition StructArrayTopic.hpp:242
std::vector< T > ValueType
Definition StructArrayTopic.hpp:247
void SetDefault(U &&value)
Publish a default value.
Definition StructArrayTopic.hpp:341
StructArrayPublisher(NT_Publisher handle, I... info)
Construct from a publisher handle; recommended to use StructTopic::Publish() instead.
Definition StructArrayTopic.hpp:261
TopicType GetTopic() const
Get the corresponding topic.
Definition StructArrayTopic.hpp:381
std::span< const T > ParamType
Definition StructArrayTopic.hpp:248
StructArrayTopic< T, I... > TopicType
Definition StructArrayTopic.hpp:246
Timestamped< ValueType > TimestampedValueType
Definition StructArrayTopic.hpp:250
void Set(U &&value, int64_t time=0)
Publish a new value.
Definition StructArrayTopic.hpp:295
StructArrayPublisher & operator=(StructArrayPublisher &&rhs)
Definition StructArrayTopic.hpp:274
StructArrayPublisher()=default
void SetDefault(std::span< const T > value)
Publish a default value.
Definition StructArrayTopic.hpp:362
void Set(std::span< const T > value, int64_t time=0)
Publish a new value.
Definition StructArrayTopic.hpp:315
StructArrayPublisher(StructArrayPublisher &&rhs)
Definition StructArrayTopic.hpp:267
StructArrayPublisher & operator=(const StructArrayPublisher &)=delete
StructArrayPublisher(const StructArrayPublisher &)=delete
NetworkTables struct-encoded value array subscriber.
Definition StructArrayTopic.hpp:40
ValueType Get() const
Get the last published value.
Definition StructArrayTopic.hpp:77
TimestampedValueType GetAtomic(std::span< const T > defaultValue) const
Get the last published value along with its timestamp.
Definition StructArrayTopic.hpp:159
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:115
std::vector< T > ValueType
Definition StructArrayTopic.hpp:45
ValueType Get(std::span< const T > defaultValue) const
Get the last published value.
Definition StructArrayTopic.hpp:104
ValueType Get(U &&defaultValue) const
Get the last published value.
Definition StructArrayTopic.hpp:92
StructArraySubscriber(NT_Subscriber handle, U &&defaultValue, I... info)
Construct from a subscriber handle; recommended to use StructTopic::Subscribe() instead.
Definition StructArrayTopic.hpp:64
StructArrayTopic< T, I... > TopicType
Definition StructArrayTopic.hpp:44
TimestampedValueType GetAtomic(U &&defaultValue) const
Get the last published value along with its timestamp.
Definition StructArrayTopic.hpp:130
std::span< const T > ParamType
Definition StructArrayTopic.hpp:46
Timestamped< ValueType > TimestampedValueType
Definition StructArrayTopic.hpp:47
StructArraySubscriber()=default
std::vector< TimestampedValueType > ReadQueue()
Get an array of all valid value changes since the last call to ReadQueue.
Definition StructArrayTopic.hpp:191
TopicType GetTopic() const
Get the corresponding topic.
Definition StructArrayTopic.hpp:222
NetworkTables struct-encoded value array topic.
Definition StructArrayTopic.hpp:469
SubscriberType Subscribe(std::span< const T > defaultValue, const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Create a new subscriber to the topic.
Definition StructArrayTopic.hpp:551
StructArrayTopic(Topic topic, I... info)
Construct from a generic topic.
Definition StructArrayTopic.hpp:496
EntryType GetEntry(std::span< const T > defaultValue, const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Create a new entry for the topic.
Definition StructArrayTopic.hpp:694
StructArrayEntry< T, I... > EntryType
Definition StructArrayTopic.hpp:473
EntryType GetEntry(U &&defaultValue, const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Create a new entry for the topic.
Definition StructArrayTopic.hpp:658
StructArrayTopic()=default
PublisherType Publish(const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Create a new publisher to the topic.
Definition StructArrayTopic.hpp:583
SubscriberType Subscribe(U &&defaultValue, const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Create a new subscriber to the topic.
Definition StructArrayTopic.hpp:520
StructArrayTopic(NT_Topic handle, I... info)
Construct from a topic handle; recommended to use NetworkTableInstance::GetStructTopic() instead.
Definition StructArrayTopic.hpp:487
std::span< const T > ParamType
Definition StructArrayTopic.hpp:475
std::vector< T > ValueType
Definition StructArrayTopic.hpp:474
PublisherType PublishEx(const wpi::util::json &properties, const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Create a new publisher to the topic, with type string and initial properties.
Definition StructArrayTopic.hpp:616
StructArrayPublisher< T, I... > PublisherType
Definition StructArrayTopic.hpp:472
StructArraySubscriber< T, I... > SubscriberType
Definition StructArrayTopic.hpp:471
Timestamped< ValueType > TimestampedValueType
Definition StructArrayTopic.hpp:476
Subscriber(const Subscriber &)=delete
NT_Subscriber m_subHandle
Definition Topic.hpp:388
NT_Topic m_handle
Definition Topic.hpp:319
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 DEFAULT_PUB_SUB_OPTIONS
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_Subscriber Subscribe(NT_Topic topic, NT_Type type, std::string_view typeStr, const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Creates a new subscriber to value changes on 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=DEFAULT_PUB_SUB_OPTIONS)
Creates a new publisher to a topic.
NT_Publisher Publish(NT_Topic topic, NT_Type type, std::string_view typeStr, const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Creates a new publisher to a topic.
Definition StringMap.hpp:773
NetworkTables (ntcore) namespace.
Definition NTSendable.hpp:9
Definition raw_os_ostream.hpp:19
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