17#include "wpi/util/SmallVector.hpp"
24template <wpi::util::ProtobufSerializable T>
30template <wpi::util::ProtobufSerializable T>
51 m_msg{
std::move(msg)},
52 m_defaultValue{
std::move(defaultValue)} {}
59 m_msg{
std::move(rhs.m_msg)},
60 m_defaultValue{
std::move(rhs.m_defaultValue)} {}
64 m_msg = std::move(rhs.m_msg);
65 m_defaultValue = std::move(rhs.m_defaultValue);
99 wpi::util::SmallVector<uint8_t, 128> buf;
101 if (view.
value.empty()) {
104 std::scoped_lock lock{m_mutex};
105 return m_msg.UnpackInto(out, view.
value);
127 wpi::util::SmallVector<uint8_t, 128> buf;
129 if (!view.
value.empty()) {
130 std::scoped_lock lock{m_mutex};
131 if (
auto optval = m_msg.Unpack(view.
value)) {
135 return {0, 0, defaultValue};
151 std::vector<TimestampedValueType> rv;
152 rv.reserve(raw.size());
153 std::scoped_lock lock{m_mutex};
154 for (
auto&& r : raw) {
155 if (
auto optval = m_msg.Unpack(r.value)) {
156 rv.emplace_back(r.time, r.serverTime, *optval);
180template <wpi::util::ProtobufSerializable T>
207 m_msg{
std::move(rhs.m_msg)},
208 m_schemaPublished{rhs.m_schemaPublished.load()} {}
212 m_msg = std::move(rhs.m_msg);
213 m_schemaPublished.store(
214 rhs.m_schemaPublished.load(std::memory_order_relaxed),
215 std::memory_order_relaxed);
225 void Set(
const T& value, int64_t time = 0) {
226 wpi::util::SmallVector<uint8_t, 128> buf;
228 std::scoped_lock lock{m_mutex};
229 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
232 m_msg.Pack(buf, value);
245 wpi::util::SmallVector<uint8_t, 128> buf;
247 std::scoped_lock lock{m_mutex};
248 if (!m_schemaPublished.exchange(
true, std::memory_order_relaxed)) {
251 m_msg.Pack(buf, value);
268 std::atomic_bool m_schemaPublished{
false};
276template <wpi::util::ProtobufSerializable T>
335template <wpi::util::ProtobufSerializable T>
384 std::move(msg), std::move(defaultValue)};
430 const wpi::util::json& properties,
466 std::move(msg), std::move(defaultValue)};
NetworkTables protobuf-encoded value entry.
Definition ProtobufTopic.hpp:278
const T & ParamType
Definition ProtobufTopic.hpp:284
ProtobufPublisher< T > PublisherType
Definition ProtobufTopic.hpp:281
void Unpublish()
Stops publishing the entry if it's published.
Definition ProtobufTopic.hpp:329
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition ProtobufTopic.hpp:315
TopicType GetTopic() const
Get the corresponding topic.
Definition ProtobufTopic.hpp:322
Timestamped< T > TimestampedValueType
Definition ProtobufTopic.hpp:286
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:298
T ValueType
Definition ProtobufTopic.hpp:283
ProtobufTopic< T > TopicType
Definition ProtobufTopic.hpp:282
ProtobufSubscriber< T > SubscriberType
Definition ProtobufTopic.hpp:280
NetworkTables protobuf-encoded value publisher.
Definition ProtobufTopic.hpp:181
const T & ParamType
Definition ProtobufTopic.hpp:185
ProtobufPublisher(const ProtobufPublisher &)=delete
ProtobufPublisher(ProtobufPublisher &&rhs)
Definition ProtobufTopic.hpp:205
Timestamped< T > TimestampedValueType
Definition ProtobufTopic.hpp:187
ProtobufPublisher & operator=(const ProtobufPublisher &)=delete
ProtobufTopic< T > TopicType
Definition ProtobufTopic.hpp:183
T ValueType
Definition ProtobufTopic.hpp:184
void Set(const T &value, int64_t time=0)
Publish a new value.
Definition ProtobufTopic.hpp:225
void SetDefault(const T &value)
Publish a default value.
Definition ProtobufTopic.hpp:244
ProtobufPublisher(NT_Publisher handle, wpi::util::ProtobufMessage< T > msg)
Construct from a publisher handle; recommended to use ProtobufTopic::Publish() instead.
Definition ProtobufTopic.hpp:198
TopicType GetTopic() const
Get the corresponding topic.
Definition ProtobufTopic.hpp:261
ProtobufPublisher & operator=(ProtobufPublisher &&rhs)
Definition ProtobufTopic.hpp:210
ProtobufPublisher()=default
NetworkTables protobuf-encoded value subscriber.
Definition ProtobufTopic.hpp:31
ProtobufSubscriber & operator=(const ProtobufSubscriber &)=delete
ValueType Get(const T &defaultValue) const
Get the last published value.
Definition ProtobufTopic.hpp:86
bool GetInto(T *out)
Get the last published value, replacing the contents in place of an existing object.
Definition ProtobufTopic.hpp:98
const T & ParamType
Definition ProtobufTopic.hpp:35
std::vector< TimestampedValueType > ReadQueue()
Get an array of all valid value changes since the last call to ReadQueue.
Definition ProtobufTopic.hpp:149
ProtobufSubscriber(const ProtobufSubscriber &)=delete
ProtobufTopic< T > TopicType
Definition ProtobufTopic.hpp:33
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:116
Timestamped< T > TimestampedValueType
Definition ProtobufTopic.hpp:36
T ValueType
Definition ProtobufTopic.hpp:34
TimestampedValueType GetAtomic(const T &defaultValue) const
Get the last published value along with its timestamp.
Definition ProtobufTopic.hpp:126
ProtobufSubscriber & operator=(ProtobufSubscriber &&rhs)
Definition ProtobufTopic.hpp:62
ProtobufSubscriber(ProtobufSubscriber &&rhs)
Definition ProtobufTopic.hpp:57
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:48
TopicType GetTopic() const
Get the corresponding topic.
Definition ProtobufTopic.hpp:167
ValueType Get() const
Get the last published value.
Definition ProtobufTopic.hpp:76
NetworkTables protobuf-encoded value topic.
Definition ProtobufTopic.hpp:336
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition ProtobufTopic.hpp:403
ProtobufEntry< T > EntryType
Definition ProtobufTopic.hpp:340
const T & ParamType
Definition ProtobufTopic.hpp:342
ProtobufTopic(Topic topic)
Construct from a generic topic.
Definition ProtobufTopic.hpp:360
PublisherType PublishEx(const wpi::util::json &properties, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic, with type string and initial properties.
Definition ProtobufTopic.hpp:429
ProtobufTopic(NT_Topic handle)
Construct from a topic handle; recommended to use NetworkTableInstance::GetProtobufTopic() instead.
Definition ProtobufTopic.hpp:353
T ValueType
Definition ProtobufTopic.hpp:341
ProtobufSubscriber< T > SubscriberType
Definition ProtobufTopic.hpp:338
SubscriberType Subscribe(T defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition ProtobufTopic.hpp:378
EntryType GetEntry(T defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition ProtobufTopic.hpp:460
Timestamped< T > TimestampedValueType
Definition ProtobufTopic.hpp:343
ProtobufPublisher< T > PublisherType
Definition ProtobufTopic.hpp:339
Publisher(const Publisher &)=delete
Publisher & operator=(const Publisher &)=delete
NT_Publisher m_pubHandle
NetworkTables handle.
Definition Topic.hpp:440
Subscriber(const Subscriber &)=delete
NT_Subscriber m_subHandle
Definition Topic.hpp:385
Subscriber & operator=(const Subscriber &)=delete
NT_Topic m_handle
Definition Topic.hpp:316
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:308
std::string GetTypeString() const
Gets the type string for the message.
Definition Protobuf.hpp:371
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 kDefaultPubSubOptions
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_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_Publisher PublishEx(NT_Topic topic, NT_Type type, std::string_view typeStr, const wpi::util::json &properties, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new publisher to a topic.
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.
Definition StringMap.hpp:773
NetworkTables (ntcore) namespace.
Definition NTSendable.hpp:9
::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