WPILibC++ 2023.4.3
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#pragma once
6
7#include <stdint.h>
8
9#include <utility>
10#include <span>
11#include <string_view>
12#include <vector>
13
14#include "networktables/Topic.h"
15
16namespace wpi {
17template <typename T>
18class SmallVectorImpl;
19class json;
20} // namespace wpi
21
22namespace nt {
23
24class RawTopic;
25
26/**
27 * NetworkTables Raw subscriber.
28 */
29class RawSubscriber : public Subscriber {
30 public:
32 using ValueType = std::vector<uint8_t>;
33 using ParamType = std::span<const uint8_t>;
35
36 using SmallRetType = std::span<uint8_t>;
39
40
41 RawSubscriber() = default;
42
43 /**
44 * Construct from a subscriber handle; recommended to use
45 * RawTopic::Subscribe() instead.
46 *
47 * @param handle Native handle
48 * @param defaultValue Default value
49 */
50 RawSubscriber(NT_Subscriber handle, ParamType defaultValue);
51
52 /**
53 * Get the last published value.
54 * If no value has been published, returns the stored default value.
55 *
56 * @return value
57 */
58 ValueType Get() const;
59
60 /**
61 * Get the last published value.
62 * If no value has been published, returns the passed defaultValue.
63 *
64 * @param defaultValue default value to return if no value has been published
65 * @return value
66 */
67 ValueType Get(ParamType defaultValue) const;
68
69 /**
70 * Get the last published value.
71 * If no value has been published, returns the stored default value.
72 *
73 * @param buf storage for returned value
74 * @return value
75 */
77
78 /**
79 * Get the last published value.
80 * If no value has been published, returns the passed defaultValue.
81 *
82 * @param buf storage for returned value
83 * @param defaultValue default value to return if no value has been published
84 * @return value
85 */
87
88 /**
89 * Get the last published value along with its timestamp
90 * If no value has been published, returns the stored default value and a
91 * timestamp of 0.
92 *
93 * @return timestamped value
94 */
96
97 /**
98 * Get the last published value along with its timestamp.
99 * If no value has been published, returns the passed defaultValue and a
100 * timestamp of 0.
101 *
102 * @param defaultValue default value to return if no value has been published
103 * @return timestamped value
104 */
106
107 /**
108 * Get the last published value along with its timestamp.
109 * If no value has been published, returns the stored default value and a
110 * timestamp of 0.
111 *
112 * @param buf storage for returned value
113 * @return timestamped value
114 */
117
118 /**
119 * Get the last published value along with its timestamp.
120 * If no value has been published, returns the passed defaultValue and a
121 * timestamp of 0.
122 *
123 * @param buf storage for returned value
124 * @param defaultValue default value to return if no value has been published
125 * @return timestamped value
126 */
129 ParamType defaultValue) const;
130
131 /**
132 * Get an array of all value changes since the last call to ReadQueue.
133 * Also provides a timestamp for each value.
134 *
135 * @note The "poll storage" subscribe option can be used to set the queue
136 * depth.
137 *
138 * @return Array of timestamped values; empty array if no new changes have
139 * been published since the previous call.
140 */
141 std::vector<TimestampedValueType> ReadQueue();
142
143 /**
144 * Get the corresponding topic.
145 *
146 * @return Topic
147 */
148 TopicType GetTopic() const;
149
150 private:
151 ValueType m_defaultValue;
152};
153
154/**
155 * NetworkTables Raw publisher.
156 */
157class RawPublisher : public Publisher {
158 public:
160 using ValueType = std::vector<uint8_t>;
161 using ParamType = std::span<const uint8_t>;
162
163 using SmallRetType = std::span<uint8_t>;
165
167
168 RawPublisher() = default;
169
170 /**
171 * Construct from a publisher handle; recommended to use
172 * RawTopic::Publish() instead.
173 *
174 * @param handle Native handle
175 */
176 explicit RawPublisher(NT_Publisher handle);
177
178 /**
179 * Publish a new value.
180 *
181 * @param value value to publish
182 * @param time timestamp; 0 indicates current NT time should be used
183 */
184 void Set(ParamType value, int64_t time = 0);
185
186 /**
187 * Publish a default value.
188 * On reconnect, a default value will never be used in preference to a
189 * published value.
190 *
191 * @param value value
192 */
194
195 /**
196 * Get the corresponding topic.
197 *
198 * @return Topic
199 */
200 TopicType GetTopic() const;
201};
202
203/**
204 * NetworkTables Raw entry.
205 *
206 * @note Unlike NetworkTableEntry, the entry goes away when this is destroyed.
207 */
208class RawEntry final : public RawSubscriber,
209 public RawPublisher {
210 public:
214 using ValueType = std::vector<uint8_t>;
215 using ParamType = std::span<const uint8_t>;
216
217 using SmallRetType = std::span<uint8_t>;
219
221
222 RawEntry() = default;
223
224 /**
225 * Construct from an entry handle; recommended to use
226 * RawTopic::GetEntry() instead.
227 *
228 * @param handle Native handle
229 * @param defaultValue Default value
230 */
231 RawEntry(NT_Entry handle, ParamType defaultValue);
232
233 /**
234 * Determines if the native handle is valid.
235 *
236 * @return True if the native handle is valid, false otherwise.
237 */
238 explicit operator bool() const { return m_subHandle != 0; }
239
240 /**
241 * Gets the native handle for the entry.
242 *
243 * @return Native handle
244 */
245 NT_Entry GetHandle() const { return m_subHandle; }
246
247 /**
248 * Get the corresponding topic.
249 *
250 * @return Topic
251 */
252 TopicType GetTopic() const;
253
254 /**
255 * Stops publishing the entry if it's published.
256 */
257 void Unpublish();
258};
259
260/**
261 * NetworkTables Raw topic.
262 */
263class RawTopic final : public Topic {
264 public:
268 using ValueType = std::vector<uint8_t>;
269 using ParamType = std::span<const uint8_t>;
271
272 RawTopic() = default;
273
274 /**
275 * Construct from a topic handle; recommended to use
276 * NetworkTableInstance::GetRawTopic() instead.
277 *
278 * @param handle Native handle
279 */
280 explicit RawTopic(NT_Topic handle) : Topic{handle} {}
281
282 /**
283 * Construct from a generic topic.
284 *
285 * @param topic Topic
286 */
287 explicit RawTopic(Topic topic) : Topic{topic} {}
288
289 /**
290 * Create a new subscriber to the topic.
291 *
292 * <p>The subscriber is only active as long as the returned object
293 * is not destroyed.
294 *
295 * @note Subscribers that do not match the published data type do not return
296 * any values. To determine if the data type matches, use the appropriate
297 * Topic functions.
298 *
299 * @param typeString type string
300
301 * @param defaultValue default value used when a default is not provided to a
302 * getter function
303 * @param options subscribe options
304 * @return subscriber
305 */
306 [[nodiscard]]
308 std::string_view typeString, ParamType defaultValue,
309 const PubSubOptions& options = kDefaultPubSubOptions);
310 /**
311 * Create a new publisher to the topic.
312 *
313 * The publisher is only active as long as the returned object
314 * is not destroyed.
315 *
316 * @note It is not possible to publish two different data types to the same
317 * topic. Conflicts between publishers are typically resolved by the
318 * server on a first-come, first-served basis. Any published values that
319 * do not match the topic's data type are dropped (ignored). To determine
320 * if the data type matches, use the appropriate Topic functions.
321 *
322 * @param typeString type string
323
324 * @param options publish options
325 * @return publisher
326 */
327 [[nodiscard]]
329
330 /**
331 * Create a new publisher to the topic, with type string and initial
332 * properties.
333 *
334 * The publisher is only active as long as the returned object
335 * is not destroyed.
336 *
337 * @note It is not possible to publish two different data types to the same
338 * topic. Conflicts between publishers are typically resolved by the
339 * server on a first-come, first-served basis. Any published values that
340 * do not match the topic's data type are dropped (ignored). To determine
341 * if the data type matches, use the appropriate Topic functions.
342 *
343 * @param typeString type string
344 * @param properties JSON properties
345 * @param options publish options
346 * @return publisher
347 */
348 [[nodiscard]]
350 const wpi::json& properties, const PubSubOptions& options = kDefaultPubSubOptions);
351
352 /**
353 * Create a new entry for the topic.
354 *
355 * Entries act as a combination of a subscriber and a weak publisher. The
356 * subscriber is active as long as the entry is not destroyed. The publisher
357 * is created when the entry is first written to, and remains active until
358 * either Unpublish() is called or the entry is destroyed.
359 *
360 * @note It is not possible to use two different data types with the same
361 * topic. Conflicts between publishers are typically resolved by the
362 * server on a first-come, first-served basis. Any published values that
363 * do not match the topic's data type are dropped (ignored), and the entry
364 * will show no new values if the data type does not match. To determine
365 * if the data type matches, use the appropriate Topic functions.
366 *
367 * @param typeString type string
368
369 * @param defaultValue default value used when a default is not provided to a
370 * getter function
371 * @param options publish and/or subscribe options
372 * @return entry
373 */
374 [[nodiscard]]
375 EntryType GetEntry(std::string_view typeString, ParamType defaultValue,
376 const PubSubOptions& options = kDefaultPubSubOptions);
377};
378
379} // namespace nt
380
NetworkTables publisher.
Definition: Topic.h:345
NetworkTables Raw entry.
Definition: RawTopic.h:209
RawEntry(NT_Entry handle, ParamType defaultValue)
Construct from an entry handle; recommended to use RawTopic::GetEntry() instead.
TopicType GetTopic() const
Get the corresponding topic.
Definition: RawTopic.inc:82
void Unpublish()
Stops publishing the entry if it's published.
Definition: RawTopic.inc:86
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition: RawTopic.h:245
RawEntry()=default
RawTopic TopicType
Definition: RawTopic.h:213
NetworkTables Raw publisher.
Definition: RawTopic.h:157
void Set(ParamType value, int64_t time=0)
Publish a new value.
Definition: RawTopic.inc:64
std::vector< uint8_t > ValueType
Definition: RawTopic.h:160
TopicType GetTopic() const
Get the corresponding topic.
Definition: RawTopic.inc:73
std::span< const uint8_t > ParamType
Definition: RawTopic.h:161
uint8_t SmallElemType
Definition: RawTopic.h:164
void SetDefault(ParamType value)
Publish a default value.
Definition: RawTopic.inc:69
std::span< uint8_t > SmallRetType
Definition: RawTopic.h:163
RawPublisher()=default
NetworkTables Raw subscriber.
Definition: RawTopic.h:29
std::vector< uint8_t > ValueType
Definition: RawTopic.h:32
ValueType Get() const
Get the last published value.
Definition: RawTopic.inc:18
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published,...
Definition: RawTopic.inc:35
TimestampedValueViewType GetAtomic(wpi::SmallVectorImpl< SmallElemType > &buf, ParamType defaultValue) const
Get the last published value along with its timestamp.
RawSubscriber()=default
SmallRetType Get(wpi::SmallVectorImpl< SmallElemType > &buf, ParamType defaultValue) const
Get the last published value.
std::vector< TimestampedValueType > ReadQueue()
Get an array of all value changes since the last call to ReadQueue.
Definition: RawTopic.inc:53
std::span< uint8_t > SmallRetType
Definition: RawTopic.h:36
RawSubscriber(NT_Subscriber handle, ParamType defaultValue)
Construct from a subscriber handle; recommended to use RawTopic::Subscribe() instead.
TopicType GetTopic() const
Get the corresponding topic.
Definition: RawTopic.inc:57
std::span< const uint8_t > ParamType
Definition: RawTopic.h:33
ValueType Get(ParamType defaultValue) const
Get the last published value.
TimestampedValueType GetAtomic(ParamType defaultValue) const
Get the last published value along with its timestamp.
uint8_t SmallElemType
Definition: RawTopic.h:37
NetworkTables Raw topic.
Definition: RawTopic.h:263
std::vector< uint8_t > ValueType
Definition: RawTopic.h:268
RawPublisher PublisherType
Definition: RawTopic.h:266
RawTopic(NT_Topic handle)
Construct from a topic handle; recommended to use NetworkTableInstance::GetRawTopic() instead.
Definition: RawTopic.h:280
std::span< const uint8_t > ParamType
Definition: RawTopic.h:269
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.inc:103
RawSubscriber SubscriberType
Definition: RawTopic.h:265
RawTopic()=default
RawEntry EntryType
Definition: RawTopic.h:267
EntryType GetEntry(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition: RawTopic.inc:110
RawTopic(Topic topic)
Construct from a generic topic.
Definition: RawTopic.h:287
PublisherType Publish(std::string_view typeString, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition: RawTopic.inc:97
SubscriberType Subscribe(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition: RawTopic.inc:90
NetworkTables subscriber.
Definition: Topic.h:290
NT_Subscriber m_subHandle
Definition: Topic.h:341
NetworkTables Topic.
Definition: Topic.h:30
Definition: core.h:1240
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:557
a class to store JSON values
Definition: json.h:2655
basic_string_view< char > string_view
Definition: core.h:520
NT_Handle NT_Topic
Definition: ntcore_c.h:38
NT_Handle NT_Subscriber
Definition: ntcore_c.h:39
NT_Handle NT_Publisher
Definition: ntcore_c.h:40
NT_Handle NT_Entry
Definition: ntcore_c.h:33
constexpr PubSubOptions kDefaultPubSubOptions
Default publish/subscribe options.
Definition: ntcore_cpp.h:381
::int64_t int64_t
Definition: Meta.h:59
::uint8_t uint8_t
Definition: Meta.h:52
NetworkTables (ntcore) namespace.
Definition: ntcore_cpp.h:35
/file This file defines the SmallVector class.
Definition: AprilTagFieldLayout.h:18
NetworkTables publish/subscribe options.
Definition: ntcore_cpp.h:304
Timestamped Raw.
Definition: ntcore_cpp_types.h:550
Timestamped Raw view (for SmallVector-taking functions).
Definition: ntcore_cpp_types.h:575