29template <
typename T,
typename... I>
31class StructArrayTopic;
36template <
typename T,
typename... I>
58#if __cpp_lib_ranges >= 201911L
59 requires std::ranges::range<U> &&
60 std::convertible_to<std::ranges::range_value_t<U>, T>
64 m_defaultValue{defaultValue.begin(), defaultValue.end()},
65 m_info{
std::move(info)...} {
86#if __cpp_lib_ranges >= 201911L
87 requires std::ranges::range<U> &&
88 std::convertible_to<std::ranges::range_value_t<U>, T>
123 template <
typename U>
124#if __cpp_lib_ranges >= 201911L
125 requires std::ranges::range<U> &&
126 std::convertible_to<std::ranges::range_value_t<U>, T>
130 size_t size = std::apply(S::GetSize, m_info);
132 if (view.
value.size() == 0 || (view.
value.size() % size) != 0) {
133 return {0, 0, std::forward<U>(defaultValue)};
136 rv.value.reserve(view.
value.size() / size);
137 for (
auto in = view.
value.begin(), end = view.
value.end();
in != end;
140 [&](
const I&... info) {
141 rv.value.emplace_back(S::Unpack(
142 std::span<const uint8_t>{std::to_address(
in), size}, info...));
159 size_t size = std::apply(S::GetSize, m_info);
161 if (view.
value.size() == 0 || (view.
value.size() % size) != 0) {
162 return {0, 0, {defaultValue.begin(), defaultValue.end()}};
165 rv.value.reserve(view.
value.size() / size);
166 for (
auto in = view.
value.begin(), end = view.
value.end();
in != end;
169 [&](
const I&... info) {
170 rv.value.emplace_back(S::Unpack(
171 std::span<const uint8_t>{std::to_address(
in), size}, info...));
191 std::vector<TimestampedValueType> rv;
192 rv.reserve(raw.size());
193 size_t size = std::apply(S::GetSize, m_info);
194 for (
auto&& r : raw) {
195 if (r.value.size() == 0 || (r.value.size() % size) != 0) {
198 std::vector<T> values;
199 values.reserve(r.value.size() / size);
200 for (
auto in = r.value.begin(), end = r.value.end();
in != end;
203 [&](
const I&... info) {
205 S::Unpack(std::span<const uint8_t>{std::to_address(
in), size},
210 rv.emplace_back(r.time, r.serverTime, std::move(values));
222 [&](
const I&... info) {
231 [[no_unique_address]] 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)) {
296 GetTopic().GetInstance().template AddStructSchema<T>(info...);
299 std::forward<U>(value),
300 [&](
auto bytes) { ::nt::SetRaw(m_pubHandle, bytes, time); },
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)) {
316 GetTopic().GetInstance().template AddStructSchema<T>(info...);
320 [&](
auto bytes) { ::nt::SetRaw(m_pubHandle, bytes, time); },
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)) {
342 GetTopic().GetInstance().template AddStructSchema<T>(info...);
345 std::forward<U>(value),
346 [&](
auto bytes) { ::nt::SetDefaultRaw(m_pubHandle, bytes); },
361 [&](
const I&... info) {
362 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
363 GetTopic().GetInstance().template AddStructSchema<T>(info...);
367 [&](
auto bytes) { ::nt::SetDefaultRaw(m_pubHandle, bytes); },
380 [&](
const I&... info) {
389 std::atomic_bool m_schemaPublished{
false};
390 [[no_unique_address]] std::tuple<I...> m_info;
398template <
typename T,
typename... I>
421 template <
typename U>
422#if __cpp_lib_ranges >= 201911L
423 requires std::ranges::range<U> &&
424 std::convertible_to<std::ranges::range_value_t<U>, T>
463template <
typename T,
typename... I>
484 :
Topic{handle}, m_info{
std::move(info)...} {}
493 :
Topic{topic}, m_info{
std::move(info)...} {}
510 template <
typename U>
511#if __cpp_lib_ranges >= 201911L
512 requires std::ranges::range<U> &&
513 std::convertible_to<std::ranges::range_value_t<U>, T>
519 [&](
const I&... info) {
523 wpi::MakeStructArrayTypeString<T, std::dynamic_extent>(
526 defaultValue, info...};
548 std::span<const T> defaultValue,
551 [&](
const I&... info) {
555 wpi::MakeStructArrayTypeString<T, std::dynamic_extent>(
558 defaultValue, info...};
581 [&](
const I&... info) {
585 wpi::MakeStructArrayTypeString<T, std::dynamic_extent>(
612 const wpi::json& properties,
615 [&](
const I&... info) {
619 wpi::MakeStructArrayTypeString<T, std::dynamic_extent>(
621 properties, options),
647 template <
typename U>
648#if __cpp_lib_ranges >= 201911L
649 requires std::ranges::range<U> &&
650 std::convertible_to<std::ranges::range_value_t<U>, T>
656 [&](
const I&... info) {
660 wpi::MakeStructArrayTypeString<T, std::dynamic_extent>(
663 defaultValue, info...};
692 [&](
const I&... info) {
696 wpi::MakeStructArrayTypeString<T, std::dynamic_extent>(
699 defaultValue, info...};
705 [[no_unique_address]] std::tuple<I...> m_info;
This file defines the SmallVector class.
Definition: format.h:4134
NetworkTables publisher.
Definition: Topic.h:364
Publisher & operator=(const Publisher &)=delete
NT_Publisher m_pubHandle
NetworkTables handle.
Definition: Topic.h:400
NetworkTables struct-encoded value array entry.
Definition: StructArrayTopic.h:401
StructArrayEntry(NT_Entry handle, U &&defaultValue, const I &... info)
Construct from an entry handle; recommended to use StructTopic::GetEntry() instead.
Definition: StructArrayTopic.h:426
std::span< const T > ParamType
Definition: StructArrayTopic.h:407
TopicType GetTopic() const
Get the corresponding topic.
Definition: StructArrayTopic.h:450
std::vector< T > ValueType
Definition: StructArrayTopic.h:406
void Unpublish()
Stops publishing the entry if it's published.
Definition: StructArrayTopic.h:457
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition: StructArrayTopic.h:443
StructArrayEntry()=default
NetworkTables struct-encoded value array publisher.
Definition: StructArrayTopic.h:239
StructArrayPublisher()=default
void Set(U &&value, int64_t time=0)
Publish a new value.
Definition: StructArrayTopic.h:292
StructArrayPublisher & operator=(const StructArrayPublisher &)=delete
void Set(std::span< const T > value, int64_t time=0)
Publish a new value.
Definition: StructArrayTopic.h:312
std::vector< T > ValueType
Definition: StructArrayTopic.h:244
void SetDefault(U &&value)
Publish a default value.
Definition: StructArrayTopic.h:338
StructArrayPublisher(NT_Publisher handle, I... info)
Construct from a publisher handle; recommended to use StructTopic::Publish() instead.
Definition: StructArrayTopic.h:258
StructArrayPublisher & operator=(StructArrayPublisher &&rhs)
Definition: StructArrayTopic.h:271
StructArrayPublisher(StructArrayPublisher &&rhs)
Definition: StructArrayTopic.h:264
void SetDefault(std::span< const T > value)
Publish a default value.
Definition: StructArrayTopic.h:359
StructArrayPublisher(const StructArrayPublisher &)=delete
std::span< const T > ParamType
Definition: StructArrayTopic.h:245
TopicType GetTopic() const
Get the corresponding topic.
Definition: StructArrayTopic.h:378
NetworkTables struct-encoded value array subscriber.
Definition: StructArrayTopic.h:38
ValueType Get(std::span< const T > defaultValue) const
Get the last published value.
Definition: StructArrayTopic.h:102
ValueType Get() const
Get the last published value.
Definition: StructArrayTopic.h:75
TopicType GetTopic() const
Get the corresponding topic.
Definition: StructArrayTopic.h:220
TimestampedValueType GetAtomic(U &&defaultValue) const
Get the last published value along with its timestamp.
Definition: StructArrayTopic.h:128
StructArraySubscriber(NT_Subscriber handle, U &&defaultValue, I... info)
Construct from a subscriber handle; recommended to use StructTopic::Subscribe() instead.
Definition: StructArrayTopic.h:62
std::span< const T > ParamType
Definition: StructArrayTopic.h:44
ValueType Get(U &&defaultValue) const
Get the last published value.
Definition: StructArrayTopic.h:90
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published or the value can...
Definition: StructArrayTopic.h:113
TimestampedValueType GetAtomic(std::span< const T > defaultValue) const
Get the last published value along with its timestamp.
Definition: StructArrayTopic.h:157
std::vector< T > ValueType
Definition: StructArrayTopic.h:43
std::vector< TimestampedValueType > ReadQueue()
Get an array of all valid value changes since the last call to ReadQueue.
Definition: StructArrayTopic.h:189
StructArraySubscriber()=default
NetworkTables struct-encoded value array topic.
Definition: StructArrayTopic.h:465
StructArrayTopic(NT_Topic handle, I... info)
Construct from a topic handle; recommended to use NetworkTableInstance::GetStructTopic() instead.
Definition: StructArrayTopic.h:483
SubscriberType Subscribe(std::span< const T > defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition: StructArrayTopic.h:547
PublisherType PublishEx(const wpi::json &properties, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic, with type string and initial properties.
Definition: StructArrayTopic.h:611
std::span< const T > ParamType
Definition: StructArrayTopic.h:471
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition: StructArrayTopic.h:579
SubscriberType Subscribe(U &&defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition: StructArrayTopic.h:516
EntryType GetEntry(std::span< const T > defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition: StructArrayTopic.h:689
EntryType GetEntry(U &&defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition: StructArrayTopic.h:653
StructArrayTopic(Topic topic, I... info)
Construct from a generic topic.
Definition: StructArrayTopic.h:492
StructArrayTopic()=default
std::vector< T > ValueType
Definition: StructArrayTopic.h:470
NetworkTables subscriber.
Definition: Topic.h:309
NT_Subscriber m_subHandle
Definition: Topic.h:360
NetworkTables Topic.
Definition: Topic.h:28
NT_Topic m_handle
Definition: Topic.h:305
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1202
void Write(U &&data, F &&func, const I &... info)
Definition: Struct.h:364
Specifies that a type is capable of raw struct serialization and deserialization.
Definition: Struct.h:68
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.
NT_Handle NT_Topic
Definition: ntcore_c.h:40
NT_Handle NT_Subscriber
Definition: ntcore_c.h:41
NT_Handle NT_Publisher
Definition: ntcore_c.h:42
NT_Handle NT_Entry
Definition: ntcore_c.h:35
@ NT_RAW
Definition: ntcore_c.h:56
constexpr PubSubOptions kDefaultPubSubOptions
Default publish/subscribe options.
Definition: ntcore_cpp.h:390
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 PublishEx(NT_Topic topic, NT_Type type, std::string_view typeStr, const wpi::json &properties, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new publisher to a topic.
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_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.
constexpr auto in(type t, int set) -> bool
Definition: core.h:611
NetworkTables (ntcore) namespace.
Definition: ntcore_cpp.h:36
NetworkTables publish/subscribe options.
Definition: ntcore_cpp.h:305
Timestamped value.
Definition: ntcore_cpp_types.h:30
int64_t time
Time in local time base.
Definition: ntcore_cpp_types.h:38
int64_t serverTime
Time in server time base.
Definition: ntcore_cpp_types.h:43
T value
Value.
Definition: ntcore_cpp_types.h:48
Struct serialization template.
Definition: Struct.h:38