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