14#include <wpi/SmallVector.h>
25template <wpi::ProtobufSerializable T>
31template <wpi::ProtobufSerializable T>
52 m_msg{
std::move(msg)},
53 m_defaultValue{
std::move(defaultValue)} {}
60 m_msg{
std::move(rhs.m_msg)},
61 m_defaultValue{
std::move(rhs.m_defaultValue)} {}
65 m_msg = std::move(rhs.m_msg);
66 m_defaultValue = std::move(rhs.m_defaultValue);
100 wpi::SmallVector<uint8_t, 128> buf;
102 if (view.
value.empty()) {
105 std::scoped_lock lock{m_mutex};
106 return m_msg.UnpackInto(out, view.
value);
128 wpi::SmallVector<uint8_t, 128> buf;
130 if (!view.
value.empty()) {
131 std::scoped_lock lock{m_mutex};
132 if (
auto optval = m_msg.Unpack(view.
value)) {
136 return {0, 0, defaultValue};
152 std::vector<TimestampedValueType> rv;
153 rv.reserve(raw.size());
154 std::scoped_lock lock{m_mutex};
155 for (
auto&& r : raw) {
156 if (
auto optval = m_msg.Unpack(r.value)) {
157 rv.emplace_back(r.time, r.serverTime, *optval);
181template <wpi::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::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::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::ProtobufSerializable T>
334template <wpi::ProtobufSerializable T>
335class ProtobufTopic final :
public Topic {
383 std::move(defaultValue)};
428 const wpi::json& properties,
464 std::move(defaultValue)};
NetworkTables protobuf-encoded value entry.
Definition ProtobufTopic.h:278
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition ProtobufTopic.h:314
void Unpublish()
Stops publishing the entry if it's published.
Definition ProtobufTopic.h:328
TopicType GetTopic() const
Get the corresponding topic.
Definition ProtobufTopic.h:321
T ValueType
Definition ProtobufTopic.h:283
ProtobufEntry(NT_Entry handle, wpi::ProtobufMessage< T > msg, T defaultValue)
Construct from an entry handle; recommended to use ProtobufTopic::GetEntry() instead.
Definition ProtobufTopic.h:298
const T & ParamType
Definition ProtobufTopic.h:284
NetworkTables protobuf-encoded value publisher.
Definition ProtobufTopic.h:182
T ValueType
Definition ProtobufTopic.h:185
ProtobufPublisher(const ProtobufPublisher &)=delete
ProtobufPublisher()=default
void SetDefault(const T &value)
Publish a default value.
Definition ProtobufTopic.h:244
ProtobufPublisher(NT_Publisher handle, wpi::ProtobufMessage< T > msg)
Construct from a publisher handle; recommended to use ProtobufTopic::Publish() instead.
Definition ProtobufTopic.h:199
void Set(const T &value, int64_t time=0)
Publish a new value.
Definition ProtobufTopic.h:225
TopicType GetTopic() const
Get the corresponding topic.
Definition ProtobufTopic.h:261
ProtobufPublisher(ProtobufPublisher &&rhs)
Definition ProtobufTopic.h:205
const T & ParamType
Definition ProtobufTopic.h:186
ProtobufPublisher & operator=(ProtobufPublisher &&rhs)
Definition ProtobufTopic.h:210
ProtobufPublisher & operator=(const ProtobufPublisher &)=delete
NetworkTables protobuf-encoded value subscriber.
Definition ProtobufTopic.h:32
TimestampedValueType GetAtomic(const T &defaultValue) const
Get the last published value along with its timestamp.
Definition ProtobufTopic.h:127
bool GetInto(T *out)
Get the last published value, replacing the contents in place of an existing object.
Definition ProtobufTopic.h:99
ValueType Get(const T &defaultValue) const
Get the last published value.
Definition ProtobufTopic.h:87
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published or the value can...
Definition ProtobufTopic.h:117
std::vector< TimestampedValueType > ReadQueue()
Get an array of all valid value changes since the last call to ReadQueue.
Definition ProtobufTopic.h:150
ProtobufSubscriber & operator=(const ProtobufSubscriber &)=delete
TopicType GetTopic() const
Get the corresponding topic.
Definition ProtobufTopic.h:168
const T & ParamType
Definition ProtobufTopic.h:36
ProtobufSubscriber & operator=(ProtobufSubscriber &&rhs)
Definition ProtobufTopic.h:63
ProtobufSubscriber(const ProtobufSubscriber &)=delete
ProtobufSubscriber(ProtobufSubscriber &&rhs)
Definition ProtobufTopic.h:58
ProtobufSubscriber(NT_Subscriber handle, wpi::ProtobufMessage< T > msg, T defaultValue)
Construct from a subscriber handle; recommended to use ProtobufTopic::Subscribe() instead.
Definition ProtobufTopic.h:49
ProtobufSubscriber()=default
T ValueType
Definition ProtobufTopic.h:35
ValueType Get() const
Get the last published value.
Definition ProtobufTopic.h:77
NetworkTables protobuf-encoded value topic.
Definition NetworkTable.h:36
EntryType GetEntry(T defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition ProtobufTopic.h:458
ProtobufTopic(Topic topic)
Construct from a generic topic.
Definition ProtobufTopic.h:359
SubscriberType Subscribe(T defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition ProtobufTopic.h:377
PublisherType PublishEx(const wpi::json &properties, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic, with type string and initial properties.
Definition ProtobufTopic.h:427
ProtobufTopic(NT_Topic handle)
Construct from a topic handle; recommended to use NetworkTableInstance::GetProtobufTopic() instead.
Definition ProtobufTopic.h:352
T ValueType
Definition ProtobufTopic.h:340
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition ProtobufTopic.h:402
const T & ParamType
Definition ProtobufTopic.h:341
NetworkTables publisher.
Definition Topic.h:393
Publisher & operator=(const Publisher &)=delete
NT_Publisher m_pubHandle
NetworkTables handle.
Definition Topic.h:441
NetworkTables subscriber.
Definition Topic.h:321
Subscriber & operator=(const Subscriber &)=delete
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.
Ease of use wrapper to make nanopb streams more opaque to the user.
Definition Protobuf.h:307
std::string GetTypeString() const
Gets the type string for the message.
Definition Protobuf.h:370
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:55
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
Definition PointerIntPair.h:280
::std::mutex mutex
Definition mutex.h:17
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