21#include "wpi/util/SmallVector.hpp"
30template <
typename T,
typename... I>
31 requires wpi::util::StructSerializable<T, I...>
37template <
typename T,
typename... I>
38 requires wpi::util::StructSerializable<T, I...>
60 m_defaultValue{
std::move(defaultValue)},
61 m_info{
std::move(info)...} {}
93 wpi::util::SmallVector<uint8_t, 128> buf;
95 if (view.
value.size() < std::apply(S::GetSize, m_info)) {
99 [&](
const I&... info) {
125 wpi::util::SmallVector<uint8_t, 128> buf;
127 if (view.
value.size() < std::apply(S::GetSize, m_info)) {
128 return {0, 0, defaultValue};
133 [&](
const I&... info) {
return S::Unpack(view.
value, info...); },
151 std::vector<TimestampedValueType> rv;
152 rv.reserve(raw.size());
153 for (
auto&& r : raw) {
154 if (r.value.size() < std::apply(S::GetSize, m_info)) {
158 [&](
const I&... info) {
160 r.time, r.serverTime,
161 S::Unpack(std::span<const uint8_t>(r.value), info...));
176 [&](
const I&... info) {
185 [[no_unique_address]]
186 std::tuple<I...> m_info;
192template <
typename T,
typename... I>
212 rhs.m_schemaPublished.load(
std::memory_order_relaxed)},
213 m_info{
std::move(rhs.m_info)} {}
217 m_schemaPublished.store(
218 rhs.m_schemaPublished.load(std::memory_order_relaxed),
219 std::memory_order_relaxed);
220 m_info = std::move(rhs.m_info);
240 void Set(
const T& value, int64_t time = 0) {
241 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
243 [&](
const I&... info) {
248 if constexpr (
sizeof...(I) == 0) {
250 uint8_t buf[S::GetSize()];
256 wpi::util::SmallVector<uint8_t, 128> buf;
257 buf.resize_for_overwrite(std::apply(S::GetSize, m_info));
258 std::apply([&](
const I&... info) { S::Pack(buf, value, info...); }, m_info);
270 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
272 [&](
const I&... info) {
277 if constexpr (
sizeof...(I) == 0) {
279 uint8_t buf[S::GetSize()];
285 wpi::util::SmallVector<uint8_t, 128> buf;
286 buf.resize_for_overwrite(std::apply(S::GetSize, m_info));
287 std::apply([&](
const I&... info) { S::Pack(buf, value, info...); }, m_info);
298 [&](
const I&... info) {
306 std::atomic_bool m_schemaPublished{
false};
307 [[no_unique_address]]
308 std::tuple<I...> m_info;
316template <
typename T,
typename... I>
373template <
typename T,
typename... I>
394 :
Topic{handle}, m_info{
std::move(info)...} {}
403 :
Topic{topic}, m_info{
std::move(info)...} {}
424 [&](
const I&... info) {
429 std::move(defaultValue), info...};
453 [&](
const I&... info) {
485 [&](
const I&... info) {
520 [&](
const I&... info) {
525 std::move(defaultValue), info...};
531 [[no_unique_address]]
532 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 entry.
Definition StructTopic.hpp:319
T ValueType
Definition StructTopic.hpp:324
void Unpublish()
Stops publishing the entry if it's published.
Definition StructTopic.hpp:367
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition StructTopic.hpp:355
TopicType GetTopic() const
Get the corresponding topic.
Definition StructTopic.hpp:362
StructTopic< T, I... > TopicType
Definition StructTopic.hpp:323
const T & ParamType
Definition StructTopic.hpp:325
StructEntry(NT_Entry handle, T defaultValue, const I &... info)
Construct from an entry handle; recommended to use StructTopic::GetEntry() instead.
Definition StructTopic.hpp:339
Timestamped< T > TimestampedValueType
Definition StructTopic.hpp:327
StructPublisher< T, I... > PublisherType
Definition StructTopic.hpp:322
StructSubscriber< T, I... > SubscriberType
Definition StructTopic.hpp:321
NetworkTables struct-encoded value publisher.
Definition StructTopic.hpp:194
StructPublisher(StructPublisher &&rhs)
Definition StructTopic.hpp:209
void Set(const T &value, int64_t time=0)
Publish a new value.
Definition StructTopic.hpp:240
T ValueType
Definition StructTopic.hpp:199
StructPublisher()=default
void SetDefault(const T &value)
Publish a default value.
Definition StructTopic.hpp:269
StructPublisher & operator=(const StructPublisher &)=delete
const T & ParamType
Definition StructTopic.hpp:200
StructTopic< T, I... > TopicType
Definition StructTopic.hpp:198
Timestamped< T > TimestampedValueType
Definition StructTopic.hpp:202
StructPublisher(NT_Publisher handle, I... info)
Construct from a publisher handle; recommended to use StructTopic::Publish() instead.
Definition StructTopic.hpp:231
StructPublisher & operator=(StructPublisher &&rhs)
Definition StructTopic.hpp:215
StructPublisher(const StructPublisher &)=delete
TopicType GetTopic() const
Get the corresponding topic.
Definition StructTopic.hpp:296
NetworkTables struct-encoded value subscriber.
Definition StructTopic.hpp:39
StructTopic< T, I... > TopicType
Definition StructTopic.hpp:43
const T & ParamType
Definition StructTopic.hpp:45
bool GetInto(T *out)
Get the last published value, replacing the contents in place of an existing object.
Definition StructTopic.hpp:92
std::vector< TimestampedValueType > ReadQueue()
Get an array of all valid value changes since the last call to ReadQueue.
Definition StructTopic.hpp:149
Timestamped< T > TimestampedValueType
Definition StructTopic.hpp:46
StructSubscriber()=default
TimestampedValueType GetAtomic(const T &defaultValue) const
Get the last published value along with its timestamp.
Definition StructTopic.hpp:124
ValueType Get(const T &defaultValue) const
Get the last published value.
Definition StructTopic.hpp:80
T ValueType
Definition StructTopic.hpp:44
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published or the value can...
Definition StructTopic.hpp:114
TopicType GetTopic() const
Get the corresponding topic.
Definition StructTopic.hpp:174
StructSubscriber(NT_Subscriber handle, T defaultValue, I... info)
Construct from a subscriber handle; recommended to use StructTopic::Subscribe() instead.
Definition StructTopic.hpp:58
ValueType Get() const
Get the last published value.
Definition StructTopic.hpp:70
NetworkTables struct-encoded value topic.
Definition StructTopic.hpp:375
Timestamped< T > TimestampedValueType
Definition StructTopic.hpp:382
PublisherType Publish(const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Create a new publisher to the topic.
Definition StructTopic.hpp:450
StructTopic(NT_Topic handle, I... info)
Construct from a topic handle; recommended to use NetworkTableInstance::GetStructTopic() instead.
Definition StructTopic.hpp:393
EntryType GetEntry(T defaultValue, const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Create a new entry for the topic.
Definition StructTopic.hpp:517
StructEntry< T, I... > EntryType
Definition StructTopic.hpp:379
T ValueType
Definition StructTopic.hpp:380
StructTopic(Topic topic, I... info)
Construct from a generic topic.
Definition StructTopic.hpp:402
const T & ParamType
Definition StructTopic.hpp:381
StructPublisher< T, I... > PublisherType
Definition StructTopic.hpp:378
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 StructTopic.hpp:481
StructSubscriber< T, I... > SubscriberType
Definition StructTopic.hpp:377
SubscriberType Subscribe(T defaultValue, const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Create a new subscriber to the topic.
Definition StructTopic.hpp:421
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.
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
void UnpackStructInto(T *out, std::span< const uint8_t > data, const I &... info)
Unpack a serialized struct into an existing object, overwriting its contents.
Definition Struct.hpp:259
constexpr auto GetStructTypeString(const I &... info)
Get the type string for a raw struct serializable type.
Definition Struct.hpp:314
constexpr bool is_constexpr(Lambda)
Definition type_traits.hpp:81
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