WPILibC++ 2024.3.2
GenericEntry.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>
11#include <string_view>
12#include <utility>
13#include <vector>
14
15#include "networktables/Topic.h"
16
17namespace nt {
18
19class Value;
20
21/**
22 * NetworkTables generic subscriber.
23 */
25 public:
28 using ParamType = const Value&;
30
31 GenericSubscriber() = default;
32
33 /**
34 * Construct from a subscriber handle; recommended to use
35 * Topic::GenericSubscribe() instead.
36 *
37 * @param handle Native handle
38 */
39 explicit GenericSubscriber(NT_Subscriber handle);
40
41 /**
42 * Get the last published value.
43 * If no value has been published, returns a value with unassigned type.
44 *
45 * @return value
46 */
47 ValueType Get() const;
48
49 /**
50 * Gets the entry's value as a boolean. If the entry does not exist or is of
51 * different type, it will return the default value.
52 *
53 * @param defaultValue the value to be returned if no value is found
54 * @return the entry's value or the given default value
55 */
56 bool GetBoolean(bool defaultValue) const;
57
58 /**
59 * Gets the entry's value as a integer. If the entry does not exist or is of
60 * different type, it will return the default value.
61 *
62 * @param defaultValue the value to be returned if no value is found
63 * @return the entry's value or the given default value
64 */
65 int64_t GetInteger(int64_t defaultValue) const;
66
67 /**
68 * Gets the entry's value as a float. If the entry does not exist or is of
69 * different type, it will return the default value.
70 *
71 * @param defaultValue the value to be returned if no value is found
72 * @return the entry's value or the given default value
73 */
74 float GetFloat(float defaultValue) const;
75
76 /**
77 * Gets the entry's value as a double. If the entry does not exist or is of
78 * different type, it will return the default value.
79 *
80 * @param defaultValue the value to be returned if no value is found
81 * @return the entry's value or the given default value
82 */
83 double GetDouble(double defaultValue) const;
84
85 /**
86 * Gets the entry's value as a string. If the entry does not exist or is of
87 * different type, it will return the default value.
88 *
89 * @param defaultValue the value to be returned if no value is found
90 * @return the entry's value or the given default value
91 */
92 std::string GetString(std::string_view defaultValue) const;
93
94 /**
95 * Gets the entry's value as a raw. If the entry does not exist or is of
96 * different type, it will return the default value.
97 *
98 * @param defaultValue the value to be returned if no value is found
99 * @return the entry's value or the given default value
100 */
101 std::vector<uint8_t> GetRaw(std::span<const uint8_t> defaultValue) const;
102
103 /**
104 * Gets the entry's value as a boolean array. If the entry does not exist
105 * or is of different type, it will return the default value.
106 *
107 * @param defaultValue the value to be returned if no value is found
108 * @return the entry's value or the given default value
109 *
110 * @note This makes a copy of the array. If the overhead of this is a
111 * concern, use GetValue() instead.
112 *
113 * @note The returned array is std::vector<int> instead of std::vector<bool>
114 * because std::vector<bool> is special-cased in C++. 0 is false, any
115 * non-zero value is true.
116 */
117 std::vector<int> GetBooleanArray(std::span<const int> defaultValue) const;
118
119 /**
120 * Gets the entry's value as a integer array. If the entry does not exist
121 * or is of different type, it will return the default value.
122 *
123 * @param defaultValue the value to be returned if no value is found
124 * @return the entry's value or the given default value
125 *
126 * @note This makes a copy of the array. If the overhead of this is a
127 * concern, use GetValue() instead.
128 */
129 std::vector<int64_t> GetIntegerArray(
130 std::span<const int64_t> defaultValue) const;
131
132 /**
133 * Gets the entry's value as a float array. If the entry does not exist
134 * or is of different type, it will return the default value.
135 *
136 * @param defaultValue the value to be returned if no value is found
137 * @return the entry's value or the given default value
138 *
139 * @note This makes a copy of the array. If the overhead of this is a
140 * concern, use GetValue() instead.
141 */
142 std::vector<float> GetFloatArray(std::span<const float> defaultValue) const;
143
144 /**
145 * Gets the entry's value as a double array. If the entry does not exist
146 * or is of different type, it will return the default value.
147 *
148 * @param defaultValue the value to be returned if no value is found
149 * @return the entry's value or the given default value
150 *
151 * @note This makes a copy of the array. If the overhead of this is a
152 * concern, use GetValue() instead.
153 */
154 std::vector<double> GetDoubleArray(
155 std::span<const double> defaultValue) const;
156
157 /**
158 * Gets the entry's value as a string array. If the entry does not exist
159 * or is of different type, it will return the default value.
160 *
161 * @param defaultValue the value to be returned if no value is found
162 * @return the entry's value or the given default value
163 *
164 * @note This makes a copy of the array. If the overhead of this is a
165 * concern, use GetValue() instead.
166 */
167 std::vector<std::string> GetStringArray(
168 std::span<const std::string> defaultValue) const;
169
170 /**
171 * Get an array of all value changes since the last call to ReadQueue.
172 * Also provides a timestamp for each value.
173 *
174 * @note The "poll storage" subscribe option can be used to set the queue
175 * depth.
176 *
177 * @return Array of timestamped values; empty array if no new changes have
178 * been published since the previous call.
179 */
180 std::vector<TimestampedValueType> ReadQueue();
181
182 /**
183 * Get the corresponding topic.
184 *
185 * @return Topic
186 */
187 TopicType GetTopic() const;
188};
189
190/**
191 * NetworkTables generic publisher.
192 */
194 public:
197 using ParamType = const Value&;
199
200 GenericPublisher() = default;
201
202 /**
203 * Construct from a publisher handle; recommended to use
204 * Topic::GenericPublish() instead.
205 *
206 * @param handle Native handle
207 */
208 explicit GenericPublisher(NT_Publisher handle);
209
210 /**
211 * Publish a new value.
212 *
213 * @param value value to publish
214 */
215 void Set(ParamType value);
216
217 /**
218 * Sets the entry's value.
219 *
220 * @param value the value to set
221 * @param time the timestamp to set (0 = nt::Now())
222 * @return False if the entry exists with a different type
223 */
224 bool SetBoolean(bool value, int64_t time = 0);
225
226 /**
227 * Sets the entry's value.
228 *
229 * @param value the value to set
230 * @param time the timestamp to set (0 = nt::Now())
231 * @return False if the entry exists with a different type
232 */
233 bool SetInteger(int64_t value, int64_t time = 0);
234
235 /**
236 * Sets the entry's value.
237 *
238 * @param value the value to set
239 * @param time the timestamp to set (0 = nt::Now())
240 * @return False if the entry exists with a different type
241 */
242 bool SetFloat(float value, int64_t time = 0);
243
244 /**
245 * Sets the entry's value.
246 *
247 * @param value the value to set
248 * @param time the timestamp to set (0 = nt::Now())
249 * @return False if the entry exists with a different type
250 */
251 bool SetDouble(double value, int64_t time = 0);
252
253 /**
254 * Sets the entry's value.
255 *
256 * @param value the value to set
257 * @param time the timestamp to set (0 = nt::Now())
258 * @return False if the entry exists with a different type
259 */
260 bool SetString(std::string_view value, int64_t time = 0);
261
262 /**
263 * Sets the entry's value.
264 *
265 * @param value the value to set
266 * @param time the timestamp to set (0 = nt::Now())
267 * @return False if the entry exists with a different type
268 */
269 bool SetRaw(std::span<const uint8_t> value, int64_t time = 0);
270
271 /**
272 * Sets the entry's value.
273 *
274 * @param value the value to set
275 * @param time the timestamp to set (0 = nt::Now())
276 * @return False if the entry exists with a different type
277 */
278 bool SetBooleanArray(std::span<const bool> value, int64_t time = 0);
279
280 /**
281 * Sets the entry's value.
282 *
283 * @param value the value to set
284 * @param time the timestamp to set (0 = nt::Now())
285 * @return False if the entry exists with a different type
286 */
287 bool SetBooleanArray(std::span<const int> value, int64_t time = 0);
288
289 /**
290 * Sets the entry's value.
291 *
292 * @param value the value to set
293 * @param time the timestamp to set (0 = nt::Now())
294 * @return False if the entry exists with a different type
295 */
296 bool SetIntegerArray(std::span<const int64_t> value, int64_t time = 0);
297
298 /**
299 * Sets the entry's value.
300 *
301 * @param value the value to set
302 * @param time the timestamp to set (0 = nt::Now())
303 * @return False if the entry exists with a different type
304 */
305 bool SetFloatArray(std::span<const float> value, int64_t time = 0);
306
307 /**
308 * Sets the entry's value.
309 *
310 * @param value the value to set
311 * @param time the timestamp to set (0 = nt::Now())
312 * @return False if the entry exists with a different type
313 */
314 bool SetDoubleArray(std::span<const double> value, int64_t time = 0);
315
316 /**
317 * Sets the entry's value.
318 *
319 * @param value the value to set
320 * @param time the timestamp to set (0 = nt::Now())
321 * @return False if the entry exists with a different type
322 */
323 bool SetStringArray(std::span<const std::string> value, int64_t time = 0);
324
325 /**
326 * Publish a default value.
327 * On reconnect, a default value will never be used in preference to a
328 * published value.
329 *
330 * @param value value
331 */
332 void SetDefault(ParamType value);
333
334 /**
335 * Sets the entry's value if it does not exist.
336 *
337 * @param defaultValue the default value to set
338 * @return False if the entry exists with a different type
339 */
340 bool SetDefaultBoolean(bool defaultValue);
341
342 /**
343 * Sets the entry's value if it does not exist.
344 *
345 * @param defaultValue the default value to set
346 * @return False if the entry exists with a different type
347 */
348 bool SetDefaultInteger(int64_t defaultValue);
349
350 /**
351 * Sets the entry's value if it does not exist.
352 *
353 * @param defaultValue the default value to set
354 * @return False if the entry exists with a different type
355 */
356 bool SetDefaultFloat(float defaultValue);
357
358 /**
359 * Sets the entry's value if it does not exist.
360 *
361 * @param defaultValue the default value to set
362 * @return False if the entry exists with a different type
363 */
364 bool SetDefaultDouble(double defaultValue);
365
366 /**
367 * Sets the entry's value if it does not exist.
368 *
369 * @param defaultValue the default value to set
370 * @return False if the entry exists with a different type
371 */
372 bool SetDefaultString(std::string_view defaultValue);
373
374 /**
375 * Sets the entry's value if it does not exist.
376 *
377 * @param defaultValue the default value to set
378 * @return False if the entry exists with a different type
379 */
380 bool SetDefaultRaw(std::span<const uint8_t> defaultValue);
381
382 /**
383 * Sets the entry's value if it does not exist.
384 *
385 * @param defaultValue the default value to set
386 * @return False if the entry exists with a different type
387 */
388 bool SetDefaultBooleanArray(std::span<const int> defaultValue);
389
390 /**
391 * Sets the entry's value if it does not exist.
392 *
393 * @param defaultValue the default value to set
394 * @return False if the entry exists with a different type
395 */
396 bool SetDefaultIntegerArray(std::span<const int64_t> defaultValue);
397
398 /**
399 * Sets the entry's value if it does not exist.
400 *
401 * @param defaultValue the default value to set
402 * @return False if the entry exists with a different type
403 */
404 bool SetDefaultFloatArray(std::span<const float> defaultValue);
405
406 /**
407 * Sets the entry's value if it does not exist.
408 *
409 * @param defaultValue the default value to set
410 * @return False if the entry exists with a different type
411 */
412 bool SetDefaultDoubleArray(std::span<const double> defaultValue);
413
414 /**
415 * Sets the entry's value if it does not exist.
416 *
417 * @param defaultValue the default value to set
418 * @return False if the entry exists with a different type
419 */
420 bool SetDefaultStringArray(std::span<const std::string> defaultValue);
421
422 /**
423 * Get the corresponding topic.
424 *
425 * @return Topic
426 */
427 TopicType GetTopic() const;
428};
429
430/**
431 * NetworkTables generic entry.
432 *
433 * @note Unlike NetworkTableEntry, the entry goes away when this is destroyed.
434 */
435class GenericEntry final : public GenericSubscriber, public GenericPublisher {
436 public:
441 using ParamType = const Value&;
443
444 GenericEntry() = default;
445
446 /**
447 * Construct from an entry handle; recommended to use
448 * RawTopic::GetEntry() instead.
449 *
450 * @param handle Native handle
451 */
452 explicit GenericEntry(NT_Entry handle);
453
454 /**
455 * Determines if the native handle is valid.
456 *
457 * @return True if the native handle is valid, false otherwise.
458 */
459 explicit operator bool() const { return m_subHandle != 0; }
460
461 /**
462 * Gets the native handle for the entry.
463 *
464 * @return Native handle
465 */
466 NT_Entry GetHandle() const { return m_subHandle; }
467
468 /**
469 * Get the corresponding topic.
470 *
471 * @return Topic
472 */
473 TopicType GetTopic() const;
474
475 /**
476 * Stops publishing the entry if it's published.
477 */
478 void Unpublish();
479};
480
481} // namespace nt
482
NetworkTables generic entry.
Definition: GenericEntry.h:435
void Unpublish()
Stops publishing the entry if it's published.
Definition: GenericEntry.inc:211
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition: GenericEntry.h:466
Topic TopicType
Definition: GenericEntry.h:439
GenericEntry()=default
TopicType GetTopic() const
Get the corresponding topic.
Definition: GenericEntry.inc:207
NetworkTables generic publisher.
Definition: GenericEntry.h:193
bool SetDefaultRaw(std::span< const uint8_t > defaultValue)
Sets the entry's value if it does not exist.
Definition: GenericEntry.inc:170
bool SetDefaultDoubleArray(std::span< const double > defaultValue)
Sets the entry's value if it does not exist.
Definition: GenericEntry.inc:190
bool SetDefaultString(std::string_view defaultValue)
Sets the entry's value if it does not exist.
Definition: GenericEntry.inc:166
bool SetString(std::string_view value, int64_t time=0)
Sets the entry's value.
Definition: GenericEntry.inc:107
bool SetDouble(double value, int64_t time=0)
Sets the entry's value.
Definition: GenericEntry.inc:103
TopicType GetTopic() const
Get the corresponding topic.
Definition: GenericEntry.inc:200
bool SetDoubleArray(std::span< const double > value, int64_t time=0)
Sets the entry's value.
Definition: GenericEntry.inc:136
bool SetDefaultDouble(double defaultValue)
Sets the entry's value if it does not exist.
Definition: GenericEntry.inc:162
bool SetStringArray(std::span< const std::string > value, int64_t time=0)
Sets the entry's value.
Definition: GenericEntry.inc:141
bool SetBoolean(bool value, int64_t time=0)
Sets the entry's value.
Definition: GenericEntry.inc:91
void SetDefault(ParamType value)
Publish a default value.
Definition: GenericEntry.inc:146
bool SetDefaultFloatArray(std::span< const float > defaultValue)
Sets the entry's value if it does not exist.
Definition: GenericEntry.inc:185
bool SetInteger(int64_t value, int64_t time=0)
Sets the entry's value.
Definition: GenericEntry.inc:95
bool SetDefaultBooleanArray(std::span< const int > defaultValue)
Sets the entry's value if it does not exist.
Definition: GenericEntry.inc:175
bool SetDefaultFloat(float defaultValue)
Sets the entry's value if it does not exist.
Definition: GenericEntry.inc:158
bool SetFloat(float value, int64_t time=0)
Sets the entry's value.
Definition: GenericEntry.inc:99
void Set(ParamType value)
Publish a new value.
Definition: GenericEntry.inc:87
bool SetFloatArray(std::span< const float > value, int64_t time=0)
Sets the entry's value.
Definition: GenericEntry.inc:131
bool SetDefaultInteger(int64_t defaultValue)
Sets the entry's value if it does not exist.
Definition: GenericEntry.inc:154
bool SetIntegerArray(std::span< const int64_t > value, int64_t time=0)
Sets the entry's value.
Definition: GenericEntry.inc:126
bool SetDefaultBoolean(bool defaultValue)
Sets the entry's value if it does not exist.
Definition: GenericEntry.inc:150
bool SetBooleanArray(std::span< const bool > value, int64_t time=0)
Sets the entry's value.
Definition: GenericEntry.inc:116
bool SetDefaultIntegerArray(std::span< const int64_t > defaultValue)
Sets the entry's value if it does not exist.
Definition: GenericEntry.inc:180
bool SetDefaultStringArray(std::span< const std::string > defaultValue)
Sets the entry's value if it does not exist.
Definition: GenericEntry.inc:195
GenericPublisher()=default
bool SetRaw(std::span< const uint8_t > value, int64_t time=0)
Sets the entry's value.
Definition: GenericEntry.inc:111
NetworkTables generic subscriber.
Definition: GenericEntry.h:24
std::vector< TimestampedValueType > ReadQueue()
Get an array of all value changes since the last call to ReadQueue.
Definition: GenericEntry.inc:76
double GetDouble(double defaultValue) const
Gets the entry's value as a double.
Definition: GenericEntry.inc:37
std::vector< int > GetBooleanArray(std::span< const int > defaultValue) const
Gets the entry's value as a boolean array.
Definition: GenericEntry.inc:51
int64_t GetInteger(int64_t defaultValue) const
Gets the entry's value as a integer.
Definition: GenericEntry.inc:29
TopicType GetTopic() const
Get the corresponding topic.
Definition: GenericEntry.inc:80
float GetFloat(float defaultValue) const
Gets the entry's value as a float.
Definition: GenericEntry.inc:33
std::vector< float > GetFloatArray(std::span< const float > defaultValue) const
Gets the entry's value as a float array.
Definition: GenericEntry.inc:61
ValueType Get() const
Get the last published value.
Definition: GenericEntry.inc:21
std::vector< uint8_t > GetRaw(std::span< const uint8_t > defaultValue) const
Gets the entry's value as a raw.
Definition: GenericEntry.inc:46
GenericSubscriber()=default
std::vector< double > GetDoubleArray(std::span< const double > defaultValue) const
Gets the entry's value as a double array.
Definition: GenericEntry.inc:66
std::string GetString(std::string_view defaultValue) const
Gets the entry's value as a string.
Definition: GenericEntry.inc:41
std::vector< std::string > GetStringArray(std::span< const std::string > defaultValue) const
Gets the entry's value as a string array.
Definition: GenericEntry.inc:71
std::vector< int64_t > GetIntegerArray(std::span< const int64_t > defaultValue) const
Gets the entry's value as a integer array.
Definition: GenericEntry.inc:56
bool GetBoolean(bool defaultValue) const
Gets the entry's value as a boolean.
Definition: GenericEntry.inc:25
NetworkTables publisher.
Definition: Topic.h:364
NetworkTables subscriber.
Definition: Topic.h:309
NT_Subscriber m_subHandle
Definition: Topic.h:360
NetworkTables Topic.
Definition: Topic.h:28
A network table entry value.
Definition: NetworkTableValue.h:32
basic_string_view< char > string_view
Definition: core.h:501
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
NetworkTables (ntcore) namespace.
Definition: ntcore_cpp.h:36