WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
RawTopic.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5// THIS FILE WAS AUTO-GENERATED BY ./ntcore/generate_topics.py. DO NOT MODIFY
6
7#pragma once
8
9#include <stdint.h>
10
11#include <utility>
12#include <span>
13#include <string_view>
14#include <vector>
15
16#include <wpi/json_fwd.h>
17
19#include "networktables/Topic.h"
20#include "ntcore_cpp.h"
21
22namespace wpi {
23template <typename T>
24class SmallVectorImpl;
25} // namespace wpi
26
27namespace nt {
28
29class RawTopic;
30
31/**
32 * NetworkTables Raw subscriber.
33 */
34class RawSubscriber : public Subscriber {
35 public:
37 using ValueType = std::vector<uint8_t>;
38 using ParamType = std::span<const uint8_t>;
40
41 using SmallRetType = std::span<uint8_t>;
42 using SmallElemType = uint8_t;
44
45
46 RawSubscriber() = default;
47
48 /**
49 * Construct from a subscriber handle; recommended to use
50 * RawTopic::Subscribe() instead.
51 *
52 * @param handle Native handle
53 * @param defaultValue Default value
54 */
56 : Subscriber{handle},
57 m_defaultValue{defaultValue.begin(), defaultValue.end()} {}
58
59 /**
60 * Get the last published value.
61 * If no value has been published, returns the stored default value.
62 *
63 * @return value
64 */
65 ValueType Get() const {
66 return Get(m_defaultValue);
67 }
68
69 /**
70 * Get the last published value.
71 * If no value has been published, returns the passed defaultValue.
72 *
73 * @param defaultValue default value to return if no value has been published
74 * @return value
75 */
76 ValueType Get(ParamType defaultValue) const {
77 return ::nt::GetRaw(m_subHandle, defaultValue);
78 }
79
80 /**
81 * Get the last published value.
82 * If no value has been published, returns the stored default value.
83 *
84 * @param buf storage for returned value
85 * @return value
86 */
88 return Get(buf, m_defaultValue);
89 }
90
91 /**
92 * Get the last published value.
93 * If no value has been published, returns the passed defaultValue.
94 *
95 * @param buf storage for returned value
96 * @param defaultValue default value to return if no value has been published
97 * @return value
98 */
100 return nt::GetRaw(m_subHandle, buf, defaultValue);
101 }
102
103 /**
104 * Get the last published value along with its timestamp
105 * If no value has been published, returns the stored default value and a
106 * timestamp of 0.
107 *
108 * @return timestamped value
109 */
111 return GetAtomic(m_defaultValue);
112 }
113
114 /**
115 * Get the last published value along with its timestamp.
116 * If no value has been published, returns the passed defaultValue and a
117 * timestamp of 0.
118 *
119 * @param defaultValue default value to return if no value has been published
120 * @return timestamped value
121 */
123 return ::nt::GetAtomicRaw(m_subHandle, defaultValue);
124 }
125
126 /**
127 * Get the last published value along with its timestamp.
128 * If no value has been published, returns the stored default value and a
129 * timestamp of 0.
130 *
131 * @param buf storage for returned value
132 * @return timestamped value
133 */
136 return GetAtomic(buf, m_defaultValue);
137 }
138
139 /**
140 * Get the last published value along with its timestamp.
141 * If no value has been published, returns the passed defaultValue and a
142 * timestamp of 0.
143 *
144 * @param buf storage for returned value
145 * @param defaultValue default value to return if no value has been published
146 * @return timestamped value
147 */
150 ParamType defaultValue) const {
151 return nt::GetAtomicRaw(m_subHandle, buf, defaultValue);
152 }
153
154 /**
155 * Get an array of all value changes since the last call to ReadQueue.
156 * Also provides a timestamp for each value.
157 *
158 * @note The "poll storage" subscribe option can be used to set the queue
159 * depth.
160 *
161 * @return Array of timestamped values; empty array if no new changes have
162 * been published since the previous call.
163 */
164 std::vector<TimestampedValueType> ReadQueue() {
165 return ::nt::ReadQueueRaw(m_subHandle);
166 }
167
168 /**
169 * Get the corresponding topic.
170 *
171 * @return Topic
172 */
173 TopicType GetTopic() const;
174
175 private:
176 ValueType m_defaultValue;
177};
178
179/**
180 * NetworkTables Raw publisher.
181 */
182class RawPublisher : public Publisher {
183 public:
185 using ValueType = std::vector<uint8_t>;
186 using ParamType = std::span<const uint8_t>;
187
188 using SmallRetType = std::span<uint8_t>;
189 using SmallElemType = uint8_t;
190
192
193 RawPublisher() = default;
194
195 /**
196 * Construct from a publisher handle; recommended to use
197 * RawTopic::Publish() instead.
198 *
199 * @param handle Native handle
200 */
201 explicit RawPublisher(NT_Publisher handle) : Publisher{handle} {}
202
203 /**
204 * Publish a new value.
205 *
206 * @param value value to publish
207 * @param time timestamp; 0 indicates current NT time should be used
208 */
209 void Set(ParamType value, int64_t time = 0) {
210 ::nt::SetRaw(m_pubHandle, value, time);
211 }
212
213 /**
214 * Publish a default value.
215 * On reconnect, a default value will never be used in preference to a
216 * published value.
217 *
218 * @param value value
219 */
220 void SetDefault(ParamType value) {
222 }
223
224 /**
225 * Get the corresponding topic.
226 *
227 * @return Topic
228 */
229 TopicType GetTopic() const;
230};
231
232/**
233 * NetworkTables Raw entry.
234 *
235 * @note Unlike NetworkTableEntry, the entry goes away when this is destroyed.
236 */
237class RawEntry final : public RawSubscriber,
238 public RawPublisher {
239 public:
243 using ValueType = std::vector<uint8_t>;
244 using ParamType = std::span<const uint8_t>;
245
246 using SmallRetType = std::span<uint8_t>;
247 using SmallElemType = uint8_t;
248
250
251 RawEntry() = default;
252
253 /**
254 * Construct from an entry handle; recommended to use
255 * RawTopic::GetEntry() instead.
256 *
257 * @param handle Native handle
258 * @param defaultValue Default value
259 */
260 RawEntry(NT_Entry handle, ParamType defaultValue)
261 : RawSubscriber{handle, defaultValue},
262 RawPublisher{handle} {}
263
264 /**
265 * Determines if the native handle is valid.
266 *
267 * @return True if the native handle is valid, false otherwise.
268 */
269 explicit operator bool() const { return m_subHandle != 0; }
270
271 /**
272 * Gets the native handle for the entry.
273 *
274 * @return Native handle
275 */
276 NT_Entry GetHandle() const { return m_subHandle; }
277
278 /**
279 * Get the corresponding topic.
280 *
281 * @return Topic
282 */
283 TopicType GetTopic() const;
284
285 /**
286 * Stops publishing the entry if it's published.
287 */
291};
292
293/**
294 * NetworkTables Raw topic.
295 */
296class RawTopic final : public Topic {
297 public:
301 using ValueType = std::vector<uint8_t>;
302 using ParamType = std::span<const uint8_t>;
304
305 RawTopic() = default;
306
307 /**
308 * Construct from a topic handle; recommended to use
309 * NetworkTableInstance::GetRawTopic() instead.
310 *
311 * @param handle Native handle
312 */
313 explicit RawTopic(NT_Topic handle) : Topic{handle} {}
314
315 /**
316 * Construct from a generic topic.
317 *
318 * @param topic Topic
319 */
320 explicit RawTopic(Topic topic) : Topic{topic} {}
321
322 /**
323 * Create a new subscriber to the topic.
324 *
325 * <p>The subscriber is only active as long as the returned object
326 * is not destroyed.
327 *
328 * @note Subscribers that do not match the published data type do not return
329 * any values. To determine if the data type matches, use the appropriate
330 * Topic functions.
331 *
332 * @param typeString type string
333
334 * @param defaultValue default value used when a default is not provided to a
335 * getter function
336 * @param options subscribe options
337 * @return subscriber
338 */
339 [[nodiscard]]
341 std::string_view typeString, ParamType defaultValue,
342 const PubSubOptions& options = kDefaultPubSubOptions) {
343 return RawSubscriber{
344 ::nt::Subscribe(m_handle, NT_RAW, typeString, options),
345 defaultValue};
346 }
347 /**
348 * Create a new publisher to the topic.
349 *
350 * The publisher is only active as long as the returned object
351 * is not destroyed.
352 *
353 * @note It is not possible to publish two different data types to the same
354 * topic. Conflicts between publishers are typically resolved by the
355 * server on a first-come, first-served basis. Any published values that
356 * do not match the topic's data type are dropped (ignored). To determine
357 * if the data type matches, use the appropriate Topic functions.
358 *
359 * @param typeString type string
360
361 * @param options publish options
362 * @return publisher
363 */
364 [[nodiscard]]
365 PublisherType Publish(std::string_view typeString, const PubSubOptions& options = kDefaultPubSubOptions) {
366 return RawPublisher{
367 ::nt::Publish(m_handle, NT_RAW, typeString, options)};
368 }
369
370 /**
371 * Create a new publisher to the topic, with type string and initial
372 * properties.
373 *
374 * The publisher is only active as long as the returned object
375 * is not destroyed.
376 *
377 * @note It is not possible to publish two different data types to the same
378 * topic. Conflicts between publishers are typically resolved by the
379 * server on a first-come, first-served basis. Any published values that
380 * do not match the topic's data type are dropped (ignored). To determine
381 * if the data type matches, use the appropriate Topic functions.
382 *
383 * @param typeString type string
384 * @param properties JSON properties
385 * @param options publish options
386 * @return publisher
387 */
388 [[nodiscard]]
389 PublisherType PublishEx(std::string_view typeString,
390 const wpi::json& properties, const PubSubOptions& options = kDefaultPubSubOptions) {
391 return RawPublisher{
392 ::nt::PublishEx(m_handle, NT_RAW, typeString, properties, options)};
393 }
394
395 /**
396 * Create a new entry for the topic.
397 *
398 * Entries act as a combination of a subscriber and a weak publisher. The
399 * subscriber is active as long as the entry is not destroyed. The publisher
400 * is created when the entry is first written to, and remains active until
401 * either Unpublish() is called or the entry is destroyed.
402 *
403 * @note It is not possible to use two different data types with the same
404 * topic. Conflicts between publishers are typically resolved by the
405 * server on a first-come, first-served basis. Any published values that
406 * do not match the topic's data type are dropped (ignored), and the entry
407 * will show no new values if the data type does not match. To determine
408 * if the data type matches, use the appropriate Topic functions.
409 *
410 * @param typeString type string
411
412 * @param defaultValue default value used when a default is not provided to a
413 * getter function
414 * @param options publish and/or subscribe options
415 * @return entry
416 */
417 [[nodiscard]]
418 EntryType GetEntry(std::string_view typeString, ParamType defaultValue,
419 const PubSubOptions& options = kDefaultPubSubOptions) {
420 return RawEntry{
421 ::nt::GetEntry(m_handle, NT_RAW, typeString, options),
422 defaultValue};
423 }
424};
425
429
433
437
438} // namespace nt
NetworkTables publisher.
Definition Topic.h:393
NT_Publisher m_pubHandle
NetworkTables handle.
Definition Topic.h:441
NetworkTables Raw entry.
Definition RawTopic.h:238
std::vector< uint8_t > ValueType
Definition RawTopic.h:243
RawEntry(NT_Entry handle, ParamType defaultValue)
Construct from an entry handle; recommended to use RawTopic::GetEntry() instead.
Definition RawTopic.h:260
TopicType GetTopic() const
Get the corresponding topic.
Definition RawTopic.h:434
std::span< uint8_t > SmallRetType
Definition RawTopic.h:246
void Unpublish()
Stops publishing the entry if it's published.
Definition RawTopic.h:288
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition RawTopic.h:276
RawEntry()=default
uint8_t SmallElemType
Definition RawTopic.h:247
RawTopic TopicType
Definition RawTopic.h:242
std::span< const uint8_t > ParamType
Definition RawTopic.h:244
NetworkTables Raw publisher.
Definition RawTopic.h:182
void Set(ParamType value, int64_t time=0)
Publish a new value.
Definition RawTopic.h:209
std::vector< uint8_t > ValueType
Definition RawTopic.h:185
TopicType GetTopic() const
Get the corresponding topic.
Definition RawTopic.h:430
std::span< const uint8_t > ParamType
Definition RawTopic.h:186
uint8_t SmallElemType
Definition RawTopic.h:189
void SetDefault(ParamType value)
Publish a default value.
Definition RawTopic.h:220
std::span< uint8_t > SmallRetType
Definition RawTopic.h:188
RawPublisher(NT_Publisher handle)
Construct from a publisher handle; recommended to use RawTopic::Publish() instead.
Definition RawTopic.h:201
RawTopic TopicType
Definition RawTopic.h:184
RawPublisher()=default
NetworkTables Raw subscriber.
Definition RawTopic.h:34
std::vector< uint8_t > ValueType
Definition RawTopic.h:37
TimestampedValueViewType GetAtomic(wpi::SmallVectorImpl< SmallElemType > &buf, ParamType defaultValue) const
Get the last published value along with its timestamp.
Definition RawTopic.h:148
TimestampedValueViewType GetAtomic(wpi::SmallVectorImpl< SmallElemType > &buf) const
Get the last published value along with its timestamp.
Definition RawTopic.h:134
RawSubscriber()=default
SmallRetType Get(wpi::SmallVectorImpl< SmallElemType > &buf, ParamType defaultValue) const
Get the last published value.
Definition RawTopic.h:99
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published,...
Definition RawTopic.h:110
ValueType Get() const
Get the last published value.
Definition RawTopic.h:65
std::span< uint8_t > SmallRetType
Definition RawTopic.h:41
RawSubscriber(NT_Subscriber handle, ParamType defaultValue)
Construct from a subscriber handle; recommended to use RawTopic::Subscribe() instead.
Definition RawTopic.h:55
std::vector< TimestampedValueType > ReadQueue()
Get an array of all value changes since the last call to ReadQueue.
Definition RawTopic.h:164
TopicType GetTopic() const
Get the corresponding topic.
Definition RawTopic.h:426
std::span< const uint8_t > ParamType
Definition RawTopic.h:38
ValueType Get(ParamType defaultValue) const
Get the last published value.
Definition RawTopic.h:76
SmallRetType Get(wpi::SmallVectorImpl< SmallElemType > &buf) const
Get the last published value.
Definition RawTopic.h:87
TimestampedValueType GetAtomic(ParamType defaultValue) const
Get the last published value along with its timestamp.
Definition RawTopic.h:122
uint8_t SmallElemType
Definition RawTopic.h:42
RawTopic TopicType
Definition RawTopic.h:36
NetworkTables Raw topic.
Definition RawTopic.h:296
std::vector< uint8_t > ValueType
Definition RawTopic.h:301
SubscriberType Subscribe(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition RawTopic.h:340
PublisherType Publish(std::string_view typeString, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition RawTopic.h:365
EntryType GetEntry(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition RawTopic.h:418
RawTopic(NT_Topic handle)
Construct from a topic handle; recommended to use NetworkTableInstance::GetRawTopic() instead.
Definition RawTopic.h:313
std::span< const uint8_t > ParamType
Definition RawTopic.h:302
RawTopic()=default
RawTopic(Topic topic)
Construct from a generic topic.
Definition RawTopic.h:320
PublisherType PublishEx(std::string_view typeString, const wpi::json &properties, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic, with type string and initial properties.
Definition RawTopic.h:389
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
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition sha1.h:30
bool SetDefaultRaw(NT_Handle pubentry, std::span< const uint8_t > defaultValue)
Publish a default value.
std::vector< uint8_t > GetRaw(NT_Handle subentry, std::span< const uint8_t > defaultValue)
Get the last published value.
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
Timestamped< std::vector< uint8_t > > TimestampedRaw
Timestamped Raw.
Definition ntcore_cpp_types.h:463
Timestamped< std::span< uint8_t > > TimestampedRawView
Timestamped Raw view (for SmallVector-taking functions).
Definition ntcore_cpp_types.h:469
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
Foonathan namespace.
Definition ntcore_cpp.h:26
NetworkTables publish/subscribe options.
Definition ntcore_cpp.h:305
Timestamped value.
Definition ntcore_cpp_types.h:30