WPILibC++ 2023.4.3-108-ge5452e3
UnitTopic.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 <span>
10#include <string_view>
11#include <vector>
12
13#include "networktables/Topic.h"
14#include "ntcore_cpp.h"
15
16namespace wpi {
17class json;
18} // namespace wpi
19
20namespace nt {
21
22template <typename T>
23class UnitTopic;
24
25/**
26 * Timestamped unit.
27 *
28 * @tparam T unit type, e.g. units::meter_t
29 */
30template <typename T>
32 TimestampedUnit() = default;
35
36 /**
37 * Time in local time base.
38 */
40
41 /**
42 * Time in server time base. May be 0 or 1 for locally set values.
43 */
45
46 /**
47 * Value.
48 */
49 T value = {};
50};
51
52/**
53 * NetworkTables unit-typed subscriber.
54 *
55 * @tparam T unit type, e.g. units::meter_t
56 */
57template <typename T>
58class UnitSubscriber : public Subscriber {
59 public:
61 using ValueType = T;
62 using ParamType = T;
64
65 UnitSubscriber() = default;
66
67 /**
68 * Construct from a subscriber handle; recommended to use
69 * UnitTopic::Subscribe() instead.
70 *
71 * @param handle Native handle
72 * @param defaultValue Default value
73 */
74 UnitSubscriber(NT_Subscriber handle, ParamType defaultValue);
75
76 /**
77 * Get the last published value.
78 * If no value has been published, returns the stored default value.
79 *
80 * @return value
81 */
82 ValueType Get() const;
83
84 /**
85 * Get the last published value.
86 * If no value has been published, returns the passed defaultValue.
87 *
88 * @param defaultValue default value to return if no value has been published
89 * @return value
90 */
91 ValueType Get(ParamType defaultValue) const;
92
93 /**
94 * Get the last published value along with its timestamp
95 * If no value has been published, returns the stored default value and a
96 * timestamp of 0.
97 *
98 * @return timestamped value
99 */
101
102 /**
103 * Get the last published value along with its timestamp.
104 * If no value has been published, returns the passed defaultValue and a
105 * timestamp of 0.
106 *
107 * @param defaultValue default value to return if no value has been published
108 * @return timestamped value
109 */
110 TimestampedValueType GetAtomic(ParamType defaultValue) const;
111
112 /**
113 * Get an array of all value changes since the last call to ReadQueue.
114 * Also provides a timestamp for each value.
115 *
116 * @note The "poll storage" subscribe option can be used to set the queue
117 * depth.
118 *
119 * @return Array of timestamped values; empty array if no new changes have
120 * been published since the previous call.
121 */
122 std::vector<TimestampedValueType> ReadQueue();
123
124 /**
125 * Get the corresponding topic.
126 *
127 * @return Topic
128 */
129 TopicType GetTopic() const;
130
131 private:
132 ValueType m_defaultValue;
133};
134
135/**
136 * NetworkTables unit-typed publisher.
137 *
138 * @tparam T unit type, e.g. units::meter_t
139 */
140template <typename T>
141class UnitPublisher : public Publisher {
142 public:
144 using ValueType = T;
145 using ParamType = T;
146
148
149 UnitPublisher() = default;
150
151 /**
152 * Construct from a publisher handle; recommended to use
153 * UnitTopic::Publish() instead.
154 *
155 * @param handle Native handle
156 */
157 explicit UnitPublisher(NT_Publisher handle);
158
159 /**
160 * Publish a new value.
161 *
162 * @param value value to publish
163 * @param time timestamp; 0 indicates current NT time should be used
164 */
165 void Set(ParamType value, int64_t time = 0);
166
167 /**
168 * Publish a default value.
169 * On reconnect, a default value will never be used in preference to a
170 * published value.
171 *
172 * @param value value
173 */
175
176 /**
177 * Get the corresponding topic.
178 *
179 * @return Topic
180 */
181 TopicType GetTopic() const;
182};
183
184/**
185 * NetworkTables unit-typed entry.
186 *
187 * @note Unlike NetworkTableEntry, the entry goes away when this is destroyed.
188 *
189 * @tparam T unit type, e.g. units::meter_t
190 */
191template <typename T>
192class UnitEntry final : public UnitSubscriber<T>, public UnitPublisher<T> {
193 public:
197 using ValueType = T;
198 using ParamType = T;
199
201
202 UnitEntry() = default;
203
204 /**
205 * Construct from an entry handle; recommended to use
206 * UnitTopic::GetEntry() instead.
207 *
208 * @param handle Native handle
209 * @param defaultValue Default value
210 */
211 UnitEntry(NT_Entry handle, ParamType defaultValue);
212
213 /**
214 * Determines if the native handle is valid.
215 *
216 * @return True if the native handle is valid, false otherwise.
217 */
218 explicit operator bool() const { return this->m_subHandle != 0; }
219
220 /**
221 * Gets the native handle for the entry.
222 *
223 * @return Native handle
224 */
225 NT_Entry GetHandle() const { return this->m_subHandle; }
226
227 /**
228 * Get the corresponding topic.
229 *
230 * @return Topic
231 */
232 TopicType GetTopic() const;
233
234 /**
235 * Stops publishing the entry if it's published.
236 */
237 void Unpublish();
238};
239
240/**
241 * NetworkTables unit-typed topic. Publishers publish the type name (e.g.
242 * "meter") as the "unit" property. Type conversions are not performed--for
243 * correct behavior the publisher and subscriber must use the same unit type,
244 * but this can be checked at runtime using IsMatchingUnit().
245 *
246 * @tparam T unit type, e.g. units::meter_t
247 */
248template <typename T>
249class UnitTopic final : public Topic {
250 public:
254 using ValueType = T;
255 using ParamType = T;
257 /** The default type string for this topic type. */
258 static constexpr std::string_view kTypeString = "double";
259
260 UnitTopic() = default;
261
262 /**
263 * Construct from a topic handle.
264 *
265 * @param handle Native handle
266 */
267 explicit UnitTopic(NT_Topic handle) : Topic{handle} {}
268
269 /**
270 * Construct from a generic topic.
271 *
272 * @param topic Topic
273 */
274 explicit UnitTopic(Topic topic) : Topic{topic} {}
275
276 /**
277 * Verify the topic has a matching unit type (if the topic is published).
278 *
279 * @return True if unit matches, false if not matching or topic not published.
280 */
281 bool IsMatchingUnit() const;
282
283 /**
284 * Create a new subscriber to the topic.
285 *
286 * <p>The subscriber is only active as long as the returned object
287 * is not destroyed.
288 *
289 * @note Subscribers that do not match the published data type do not return
290 * any values. To determine if the data type matches, use the appropriate
291 * Topic functions.
292 *
293 * @param defaultValue default value used when a default is not provided to a
294 * getter function
295 * @param options subscribe options
296 * @return subscriber
297 */
298 [[nodiscard]]
300 ParamType defaultValue,
301 const PubSubOptions& options = kDefaultPubSubOptions);
302
303 /**
304 * Create a new subscriber to the topic, with specific type string.
305 *
306 * <p>The subscriber is only active as long as the returned object
307 * is not destroyed.
308 *
309 * @note Subscribers that do not match the published data type do not return
310 * any values. To determine if the data type matches, use the appropriate
311 * Topic functions.
312 *
313 * @param typeString type string
314 * @param defaultValue default value used when a default is not provided to a
315 * getter function
316 * @param options subscribe options
317 * @return subscriber
318 */
319 [[nodiscard]]
321 std::string_view typeString, ParamType defaultValue,
322 const PubSubOptions& options = kDefaultPubSubOptions);
323
324 /**
325 * Create a new publisher to the topic.
326 *
327 * The publisher is only active as long as the returned object
328 * is not destroyed.
329 *
330 * @note It is not possible to publish two different data types to the same
331 * topic. Conflicts between publishers are typically resolved by the
332 * server on a first-come, first-served basis. Any published values that
333 * do not match the topic's data type are dropped (ignored). To determine
334 * if the data type matches, use the appropriate Topic functions.
335 *
336 * @param options publish options
337 * @return publisher
338 */
339 [[nodiscard]]
341
342 /**
343 * Create a new publisher to the topic, with type string and initial
344 * properties.
345 *
346 * The publisher is only active as long as the returned object
347 * is not destroyed.
348 *
349 * @note It is not possible to publish two different data types to the same
350 * topic. Conflicts between publishers are typically resolved by the
351 * server on a first-come, first-served basis. Any published values that
352 * do not match the topic's data type are dropped (ignored). To determine
353 * if the data type matches, use the appropriate Topic functions.
354 *
355 * @param typeString type string
356 * @param properties JSON properties
357 * @param options publish options
358 * @return publisher
359 */
360 [[nodiscard]]
362 const wpi::json& properties,
363 const PubSubOptions& options = kDefaultPubSubOptions);
364
365 /**
366 * Create a new entry for the topic.
367 *
368 * Entries act as a combination of a subscriber and a weak publisher. The
369 * subscriber is active as long as the entry is not destroyed. The publisher
370 * is created when the entry is first written to, and remains active until
371 * either Unpublish() is called or the entry is destroyed.
372 *
373 * @note It is not possible to use two different data types with the same
374 * topic. Conflicts between publishers are typically resolved by the
375 * server on a first-come, first-served basis. Any published values that
376 * do not match the topic's data type are dropped (ignored), and the entry
377 * will show no new values if the data type does not match. To determine
378 * if the data type matches, use the appropriate Topic functions.
379 *
380 * @param defaultValue default value used when a default is not provided to a
381 * getter function
382 * @param options publish and/or subscribe options
383 * @return entry
384 */
385 [[nodiscard]]
386 EntryType GetEntry(ParamType defaultValue,
387 const PubSubOptions& options = kDefaultPubSubOptions);
388
389 /**
390 * Create a new entry for the topic, with specific type string.
391 *
392 * Entries act as a combination of a subscriber and a weak publisher. The
393 * subscriber is active as long as the entry is not destroyed. The publisher
394 * is created when the entry is first written to, and remains active until
395 * either Unpublish() is called or the entry is destroyed.
396 *
397 * @note It is not possible to use two different data types with the same
398 * topic. Conflicts between publishers are typically resolved by the
399 * server on a first-come, first-served basis. Any published values that
400 * do not match the topic's data type are dropped (ignored), and the entry
401 * will show no new values if the data type does not match. To determine
402 * if the data type matches, use the appropriate Topic functions.
403 *
404 * @param typeString type string
405 * @param defaultValue default value used when a default is not provided to a
406 * getter function
407 * @param options publish and/or subscribe options
408 * @return entry
409 */
410 [[nodiscard]]
411 EntryType GetEntryEx(std::string_view typeString, ParamType defaultValue,
412 const PubSubOptions& options = kDefaultPubSubOptions);
413};
414
415} // namespace nt
416
NetworkTables publisher.
Definition: Topic.h:351
NetworkTables subscriber.
Definition: Topic.h:296
NT_Subscriber m_subHandle
Definition: Topic.h:347
NetworkTables Topic.
Definition: Topic.h:30
NetworkTables unit-typed entry.
Definition: UnitTopic.h:192
UnitEntry()=default
void Unpublish()
Stops publishing the entry if it's published.
Definition: UnitTopic.inc:86
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition: UnitTopic.h:225
TopicType GetTopic() const
Get the corresponding topic.
Definition: UnitTopic.inc:81
UnitTopic< T > TopicType
Definition: UnitTopic.h:196
NetworkTables unit-typed publisher.
Definition: UnitTopic.h:141
UnitPublisher()=default
void Set(ParamType value, int64_t time=0)
Publish a new value.
Definition: UnitTopic.inc:62
void SetDefault(ParamType value)
Publish a default value.
Definition: UnitTopic.inc:67
TopicType GetTopic() const
Get the corresponding topic.
Definition: UnitTopic.inc:72
T ParamType
Definition: UnitTopic.h:145
T ValueType
Definition: UnitTopic.h:144
NetworkTables unit-typed subscriber.
Definition: UnitTopic.h:58
TopicType GetTopic() const
Get the corresponding topic.
Definition: UnitTopic.inc:53
TimestampedValueType GetAtomic() const
Get the last published value along with its timestamp If no value has been published,...
Definition: UnitTopic.inc:32
ValueType Get() const
Get the last published value.
Definition: UnitTopic.inc:22
std::vector< TimestampedValueType > ReadQueue()
Get an array of all value changes since the last call to ReadQueue.
Definition: UnitTopic.inc:43
UnitSubscriber()=default
T ValueType
Definition: UnitTopic.h:61
T ParamType
Definition: UnitTopic.h:62
NetworkTables unit-typed topic.
Definition: UnitTopic.h:249
UnitEntry< T > EntryType
Definition: UnitTopic.h:253
bool IsMatchingUnit() const
Verify the topic has a matching unit type (if the topic is published).
Definition: UnitTopic.inc:91
PublisherType Publish(const PubSubOptions &options=kDefaultPubSubOptions)
Create a new publisher to the topic.
Definition: UnitTopic.inc:110
SubscriberType Subscribe(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic.
Definition: UnitTopic.inc:96
static constexpr std::string_view kTypeString
The default type string for this topic type.
Definition: UnitTopic.h:258
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: UnitTopic.inc:116
T ValueType
Definition: UnitTopic.h:254
UnitTopic(NT_Topic handle)
Construct from a topic handle.
Definition: UnitTopic.h:267
SubscriberType SubscribeEx(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new subscriber to the topic, with specific type string.
Definition: UnitTopic.inc:103
UnitTopic()=default
UnitTopic(Topic topic)
Construct from a generic topic.
Definition: UnitTopic.h:274
UnitSubscriber< T > SubscriberType
Definition: UnitTopic.h:251
T ParamType
Definition: UnitTopic.h:255
EntryType GetEntryEx(std::string_view typeString, ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic, with specific type string.
Definition: UnitTopic.inc:133
EntryType GetEntry(ParamType defaultValue, const PubSubOptions &options=kDefaultPubSubOptions)
Create a new entry for the topic.
Definition: UnitTopic.inc:126
UnitPublisher< T > PublisherType
Definition: UnitTopic.h:252
Definition: core.h:1240
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
NetworkTables (ntcore) namespace.
Definition: Topic.inc:15
Definition: AprilTagFieldLayout.h:18
NetworkTables publish/subscribe options.
Definition: ntcore_cpp.h:304
Timestamped unit.
Definition: UnitTopic.h:31
int64_t time
Time in local time base.
Definition: UnitTopic.h:39
int64_t serverTime
Time in server time base.
Definition: UnitTopic.h:44
TimestampedUnit(int64_t time, int64_t serverTime, T value)
Definition: UnitTopic.h:33
TimestampedUnit()=default