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)};
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()}};
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]]
232 std::tuple<I...> m_info;
238template <
typename T,
typename... I>
267 m_buf{
std::move(rhs.m_buf)},
269 rhs.m_schemaPublished.load(
std::memory_order_relaxed)},
270 m_info{
std::move(rhs.m_info)} {}
274 m_buf = std::move(rhs.m_buf);
275 m_schemaPublished.store(
276 rhs.m_schemaPublished.load(std::memory_order_relaxed),
277 std::memory_order_relaxed);
278 m_info = std::move(rhs.m_info);
288 template <
typename U>
289#if __cpp_lib_ranges >= 201911L
290 requires std::ranges::range<U> &&
291 std::convertible_to<std::ranges::range_value_t<U>, T>
293 void Set(U&& value, int64_t time = 0) {
295 [&](
const I&... info) {
296 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
300 std::forward<U>(value),
313 void Set(std::span<const T> value, int64_t time = 0) {
315 [&](
const I&... info) {
316 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
334 template <
typename U>
335#if __cpp_lib_ranges >= 201911L
336 requires std::ranges::range<U> &&
337 std::convertible_to<std::ranges::range_value_t<U>, T>
341 [&](
const I&... info) {
342 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
346 std::forward<U>(value),
362 [&](
const I&... info) {
363 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
381 [&](
const I&... info) {
390 std::atomic_bool m_schemaPublished{
false};
391 [[no_unique_address]]
392 std::tuple<I...> m_info;
400template <
typename T,
typename... I>
423 template <
typename U>
424#if __cpp_lib_ranges >= 201911L
425 requires std::ranges::range<U> &&
426 std::convertible_to<std::ranges::range_value_t<U>, T>
465template <
typename T,
typename... I>
467class StructArrayTopic final :
public Topic {
486 :
Topic{handle}, m_info{
std::move(info)...} {}
495 :
Topic{topic}, m_info{
std::move(info)...} {}
512 template <
typename U>
513#if __cpp_lib_ranges >= 201911L
514 requires std::ranges::range<U> &&
515 std::convertible_to<std::ranges::range_value_t<U>, T>
521 [&](
const I&... info) {
528 defaultValue, info...};
550 std::span<const T> defaultValue,
553 [&](
const I&... info) {
560 defaultValue, info...};
583 [&](
const I&... info) {
614 const wpi::json& properties,
617 [&](
const I&... info) {
623 properties, options),
649 template <
typename U>
650#if __cpp_lib_ranges >= 201911L
651 requires std::ranges::range<U> &&
652 std::convertible_to<std::ranges::range_value_t<U>, T>
658 [&](
const I&... info) {
665 defaultValue, info...};
694 [&](
const I&... info) {
701 defaultValue, info...};
707 [[no_unique_address]]
708 std::tuple<I...> m_info;
This file defines the SmallVector class.
NetworkTables publisher.
Definition Topic.h:393
Publisher & operator=(const Publisher &)=delete
NT_Publisher m_pubHandle
NetworkTables handle.
Definition Topic.h:441
NetworkTables struct-encoded value array entry.
Definition StructArrayTopic.h:403
StructArrayEntry(NT_Entry handle, U &&defaultValue, const I &... info)
Construct from an entry handle; recommended to use StructTopic::GetEntry() instead.
Definition StructArrayTopic.h:428
std::span< const T > ParamType
Definition StructArrayTopic.h:409
TopicType GetTopic() const
Get the corresponding topic.
Definition StructArrayTopic.h:452
std::vector< T > ValueType
Definition StructArrayTopic.h:408
void Unpublish()
Stops publishing the entry if it's published.
Definition StructArrayTopic.h:459
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition StructArrayTopic.h:445
StructArrayEntry()=default
NetworkTables struct-encoded value array publisher.
Definition StructArrayTopic.h:240
StructArrayPublisher()=default
void Set(U &&value, int64_t time=0)
Publish a new value.
Definition StructArrayTopic.h:293
StructArrayPublisher & operator=(const StructArrayPublisher &)=delete
void Set(std::span< const T > value, int64_t time=0)
Publish a new value.
Definition StructArrayTopic.h:313
std::vector< T > ValueType
Definition StructArrayTopic.h:245
void SetDefault(U &&value)
Publish a default value.
Definition StructArrayTopic.h:339
StructArrayPublisher(NT_Publisher handle, I... info)
Construct from a publisher handle; recommended to use StructTopic::Publish() instead.
Definition StructArrayTopic.h:259
StructArrayPublisher & operator=(StructArrayPublisher &&rhs)
Definition StructArrayTopic.h:272
StructArrayPublisher(StructArrayPublisher &&rhs)
Definition StructArrayTopic.h:265
void SetDefault(std::span< const T > value)
Publish a default value.
Definition StructArrayTopic.h:360
StructArrayPublisher(const StructArrayPublisher &)=delete
std::span< const T > ParamType
Definition StructArrayTopic.h:246
TopicType GetTopic() const
Get the corresponding topic.
Definition StructArrayTopic.h:379
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 NetworkTable.h:42
StructArrayTopic(NT_Topic handle, I... info)
Construct from a topic handle; recommended to use NetworkTableInstance::GetStructTopic() instead.
Definition StructArrayTopic.h:485
SubscriberType Subscribe(std::span< const T > defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition StructArrayTopic.h:549
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:613
std::span< const T > ParamType
Definition StructArrayTopic.h:473
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition StructArrayTopic.h:581
SubscriberType Subscribe(U &&defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition StructArrayTopic.h:518
EntryType GetEntry(std::span< const T > defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition StructArrayTopic.h:691
EntryType GetEntry(U &&defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition StructArrayTopic.h:655
StructArrayTopic(Topic topic, I... info)
Construct from a generic topic.
Definition StructArrayTopic.h:494
StructArrayTopic()=default
std::vector< T > ValueType
Definition StructArrayTopic.h:472
NetworkTables subscriber.
Definition Topic.h:321
NT_Subscriber m_subHandle
Definition Topic.h:386
NetworkTables Topic.
Definition Topic.h:28
NT_Topic m_handle
Definition Topic.h:317
NetworkTableInstance GetInstance() const
Gets the instance for the topic.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1212
void Write(U &&data, F &&func, const I &... info)
Definition Struct.h:457
Specifies that a type is capable of raw struct serialization and deserialization.
Definition Struct.h:69
bool SetDefaultRaw(NT_Handle pubentry, std::span< const uint8_t > defaultValue)
Publish a default value.
std::vector< TimestampedRaw > ReadQueueRaw(NT_Handle subentry)
Get an array of all value changes since the last call to ReadQueue.
bool SetRaw(NT_Handle pubentry, std::span< const uint8_t > value, int64_t time=0)
Publish a new value.
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: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:58
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.
NetworkTables (ntcore) namespace.
Definition ntcore_cpp.h:36
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
constexpr auto MakeStructArrayTypeString(const I &... info)
Definition Struct.h:370
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:39