17#include "wpi/util/SmallVector.hpp"
27template <wpi::util::ProtobufSerializable T>
33template <wpi::util::ProtobufSerializable T>
54 m_msg{
std::move(msg)},
55 m_defaultValue{
std::move(defaultValue)} {}
62 m_msg{
std::move(rhs.m_msg)},
63 m_defaultValue{
std::move(rhs.m_defaultValue)} {}
67 m_msg = std::move(rhs.m_msg);
68 m_defaultValue = std::move(rhs.m_defaultValue);
102 wpi::util::SmallVector<uint8_t, 128> buf;
104 if (view.
value.empty()) {
107 std::scoped_lock lock{m_mutex};
108 return m_msg.UnpackInto(out, view.
value);
130 wpi::util::SmallVector<uint8_t, 128> buf;
132 if (!view.
value.empty()) {
133 std::scoped_lock lock{m_mutex};
134 if (
auto optval = m_msg.Unpack(view.
value)) {
138 return {0, 0, defaultValue};
154 std::vector<TimestampedValueType> rv;
155 rv.reserve(raw.size());
156 std::scoped_lock lock{m_mutex};
157 for (
auto&& r : raw) {
158 if (
auto optval = m_msg.Unpack(r.value)) {
159 rv.emplace_back(r.time, r.serverTime, *optval);
183template <wpi::util::ProtobufSerializable T>
210 m_msg{
std::move(rhs.m_msg)},
211 m_schemaPublished{rhs.m_schemaPublished.load()} {}
215 m_msg = std::move(rhs.m_msg);
216 m_schemaPublished.store(
217 rhs.m_schemaPublished.load(std::memory_order_relaxed),
218 std::memory_order_relaxed);
228 void Set(
const T& value, int64_t time = 0) {
229 wpi::util::SmallVector<uint8_t, 128> buf;
231 std::scoped_lock lock{m_mutex};
232 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
235 m_msg.Pack(buf, value);
248 wpi::util::SmallVector<uint8_t, 128> buf;
250 std::scoped_lock lock{m_mutex};
251 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
254 m_msg.Pack(buf, value);
271 std::atomic_bool m_schemaPublished{
false};
279template <wpi::util::ProtobufSerializable T>
338template <wpi::util::ProtobufSerializable T>
387 std::move(msg), std::move(defaultValue)};
470 std::move(msg), std::move(defaultValue)};
NetworkTables protobuf-encoded value entry.
Definition ProtobufTopic.hpp:281
const T & ParamType
Definition ProtobufTopic.hpp:287
ProtobufPublisher< T > PublisherType
Definition ProtobufTopic.hpp:284
void Unpublish()
Stops publishing the entry if it's published.
Definition ProtobufTopic.hpp:332
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition ProtobufTopic.hpp:318
TopicType GetTopic() const
Get the corresponding topic.
Definition ProtobufTopic.hpp:325
Timestamped< T > TimestampedValueType
Definition ProtobufTopic.hpp:289
ProtobufEntry(NT_Entry handle, wpi::util::ProtobufMessage< T > msg, T defaultValue)
Construct from an entry handle; recommended to use ProtobufTopic::GetEntry() instead.
Definition ProtobufTopic.hpp:301
T ValueType
Definition ProtobufTopic.hpp:286
ProtobufTopic< T > TopicType
Definition ProtobufTopic.hpp:285
ProtobufSubscriber< T > SubscriberType
Definition ProtobufTopic.hpp:283
NetworkTables protobuf-encoded value publisher.
Definition ProtobufTopic.hpp:184
const T & ParamType
Definition ProtobufTopic.hpp:188
ProtobufPublisher(const ProtobufPublisher &)=delete
ProtobufPublisher(ProtobufPublisher &&rhs)
Definition ProtobufTopic.hpp:208
Timestamped< T > TimestampedValueType
Definition ProtobufTopic.hpp:190
ProtobufPublisher & operator=(const ProtobufPublisher &)=delete
ProtobufTopic< T > TopicType
Definition ProtobufTopic.hpp:186
T ValueType
Definition ProtobufTopic.hpp:187
void Set(const T &value, int64_t time=0)
Publish a new value.
Definition ProtobufTopic.hpp:228
void SetDefault(const T &value)
Publish a default value.
Definition ProtobufTopic.hpp:247
ProtobufPublisher(NT_Publisher handle, wpi::util::ProtobufMessage< T > msg)
Construct from a publisher handle; recommended to use ProtobufTopic::Publish() instead.
Definition ProtobufTopic.hpp:201
TopicType GetTopic() const
Get the corresponding topic.
Definition ProtobufTopic.hpp:264
ProtobufPublisher & operator=(ProtobufPublisher &&rhs)
Definition ProtobufTopic.hpp:213
ProtobufPublisher()=default
NetworkTables protobuf-encoded value subscriber.
Definition ProtobufTopic.hpp:34
ProtobufSubscriber & operator=(const ProtobufSubscriber &)=delete
ValueType Get(const T &defaultValue) const
Get the last published value.
Definition ProtobufTopic.hpp:89
bool GetInto(T *out)
Get the last published value, replacing the contents in place of an existing object.
Definition ProtobufTopic.hpp:101
const T & ParamType
Definition ProtobufTopic.hpp:38
std::vector< TimestampedValueType > ReadQueue()
Get an array of all valid value changes since the last call to ReadQueue.
Definition ProtobufTopic.hpp:152
ProtobufSubscriber(const ProtobufSubscriber &)=delete
ProtobufTopic< T > TopicType
Definition ProtobufTopic.hpp:36
ProtobufSubscriber()=default
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published or the value can...
Definition ProtobufTopic.hpp:119
Timestamped< T > TimestampedValueType
Definition ProtobufTopic.hpp:39
T ValueType
Definition ProtobufTopic.hpp:37
TimestampedValueType GetAtomic(const T &defaultValue) const
Get the last published value along with its timestamp.
Definition ProtobufTopic.hpp:129
ProtobufSubscriber & operator=(ProtobufSubscriber &&rhs)
Definition ProtobufTopic.hpp:65
ProtobufSubscriber(ProtobufSubscriber &&rhs)
Definition ProtobufTopic.hpp:60
ProtobufSubscriber(NT_Subscriber handle, wpi::util::ProtobufMessage< T > msg, T defaultValue)
Construct from a subscriber handle; recommended to use ProtobufTopic::Subscribe() instead.
Definition ProtobufTopic.hpp:51
TopicType GetTopic() const
Get the corresponding topic.
Definition ProtobufTopic.hpp:170
ValueType Get() const
Get the last published value.
Definition ProtobufTopic.hpp:79
NetworkTables protobuf-encoded value topic.
Definition ProtobufTopic.hpp:339
ProtobufEntry< T > EntryType
Definition ProtobufTopic.hpp:343
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 ProtobufTopic.hpp:433
SubscriberType Subscribe(T defaultValue, const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Create a new subscriber to the topic.
Definition ProtobufTopic.hpp:381
const T & ParamType
Definition ProtobufTopic.hpp:345
PublisherType Publish(const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Create a new publisher to the topic.
Definition ProtobufTopic.hpp:406
ProtobufTopic(Topic topic)
Construct from a generic topic.
Definition ProtobufTopic.hpp:363
ProtobufTopic(NT_Topic handle)
Construct from a topic handle; recommended to use NetworkTableInstance::GetProtobufTopic() instead.
Definition ProtobufTopic.hpp:356
EntryType GetEntry(T defaultValue, const PubSubOptions &options=DEFAULT_PUB_SUB_OPTIONS)
Create a new entry for the topic.
Definition ProtobufTopic.hpp:464
T ValueType
Definition ProtobufTopic.hpp:344
ProtobufSubscriber< T > SubscriberType
Definition ProtobufTopic.hpp:341
Timestamped< T > TimestampedValueType
Definition ProtobufTopic.hpp:346
ProtobufPublisher< T > PublisherType
Definition ProtobufTopic.hpp:342
Publisher(const Publisher &)=delete
Publisher & operator=(const Publisher &)=delete
NT_Publisher m_pubHandle
NetworkTables handle.
Definition Topic.hpp:443
Subscriber(const Subscriber &)=delete
NT_Subscriber m_subHandle
Definition Topic.hpp:388
Subscriber & operator=(const Subscriber &)=delete
NT_Topic m_handle
Definition Topic.hpp:319
NetworkTableInstance GetInstance() const
Gets the instance for the topic.
Ease of use wrapper to make nanopb streams more opaque to the user.
Definition Protobuf.hpp:309
std::string GetTypeString() const
Gets the type string for the message.
Definition Protobuf.hpp:372
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
::std::mutex mutex
Definition mutex.hpp:17
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