WPILibC++ 2023.4.3
NetworkTableEntry.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 <initializer_list>
10#include <memory>
11#include <span>
12#include <string>
13#include <string_view>
14#include <vector>
15
16#include <wpi/deprecated.h>
17
20#include "ntcore_c.h"
21#include "ntcore_cpp.h"
22
23namespace nt {
24
25class NetworkTableInstance;
26class Topic;
27
28/**
29 * NetworkTables Entry
30 *
31 * @note For backwards compatibility, the NetworkTableEntry destructor does not
32 * release the entry.
33 *
34 * @ingroup ntcore_cpp_api
35 */
36class NetworkTableEntry final {
37 public:
38 /**
39 * Flag values (as returned by GetFlags()).
40 * @deprecated Use IsPersistent() instead.
41 */
43
44 /**
45 * Construct invalid instance.
46 */
48
49 /**
50 * Construct from native handle.
51 *
52 * @param handle Native handle
53 */
54 explicit NetworkTableEntry(NT_Entry handle);
55
56 /**
57 * Determines if the native handle is valid.
58 *
59 * @return True if the native handle is valid, false otherwise.
60 */
61 explicit operator bool() const { return m_handle != 0; }
62
63 /**
64 * Gets the native handle for the entry.
65 *
66 * @return Native handle
67 */
68 NT_Entry GetHandle() const;
69
70 /**
71 * Gets the instance for the entry.
72 *
73 * @return Instance
74 */
76
77 /**
78 * Determines if the entry currently exists.
79 *
80 * @return True if the entry exists, false otherwise.
81 */
82 bool Exists() const;
83
84 /**
85 * Gets the name of the entry (the key).
86 *
87 * @return the entry's name
88 */
89 std::string GetName() const;
90
91 /**
92 * Gets the type of the entry.
93 *
94 * @return the entry's type
95 */
97
98 /**
99 * Returns the flags.
100 *
101 * @return the flags (bitmask)
102 * @deprecated Use IsPersistent() or topic properties instead
103 */
104 WPI_DEPRECATED("Use IsPersistent() or topic properties instead")
105 unsigned int GetFlags() const;
106
107 /**
108 * Gets the last time the entry's value was changed.
109 *
110 * @return Entry last change time
111 */
112 int64_t GetLastChange() const;
113
114 /**
115 * Gets the entry's value. If the entry does not exist, returns an empty
116 * value.
117 *
118 * @return the entry's value or an empty value if it does not exist.
119 */
120 Value GetValue() const;
121
122 /**
123 * Gets the entry's value as a boolean. If the entry does not exist or is of
124 * different type, it will return the default value.
125 *
126 * @param defaultValue the value to be returned if no value is found
127 * @return the entry's value or the given default value
128 */
129 bool GetBoolean(bool defaultValue) const;
130
131 /**
132 * Gets the entry's value as a integer. If the entry does not exist or is of
133 * different type, it will return the default value.
134 *
135 * @param defaultValue the value to be returned if no value is found
136 * @return the entry's value or the given default value
137 */
138 int64_t GetInteger(int64_t defaultValue) const;
139
140 /**
141 * Gets the entry's value as a float. If the entry does not exist or is of
142 * different type, it will return the default value.
143 *
144 * @param defaultValue the value to be returned if no value is found
145 * @return the entry's value or the given default value
146 */
147 float GetFloat(float defaultValue) const;
148
149 /**
150 * Gets the entry's value as a double. If the entry does not exist or is of
151 * different type, it will return the default value.
152 *
153 * @param defaultValue the value to be returned if no value is found
154 * @return the entry's value or the given default value
155 */
156 double GetDouble(double defaultValue) const;
157
158 /**
159 * Gets the entry's value as a string. If the entry does not exist or is of
160 * different type, it will return the default value.
161 *
162 * @param defaultValue the value to be returned if no value is found
163 * @return the entry's value or the given default value
164 */
165 std::string GetString(std::string_view defaultValue) const;
166
167 /**
168 * Gets the entry's value as a raw. If the entry does not exist or is of
169 * different type, it will return the default value.
170 *
171 * @param defaultValue the value to be returned if no value is found
172 * @return the entry's value or the given default value
173 */
174 std::vector<uint8_t> GetRaw(std::span<const uint8_t> defaultValue) const;
175
176 /**
177 * Gets the entry's value as a boolean array. If the entry does not exist
178 * or is of different type, it will return the default value.
179 *
180 * @param defaultValue the value to be returned if no value is found
181 * @return the entry's value or the given default value
182 *
183 * @note This makes a copy of the array. If the overhead of this is a
184 * concern, use GetValue() instead.
185 *
186 * @note The returned array is std::vector<int> instead of std::vector<bool>
187 * because std::vector<bool> is special-cased in C++. 0 is false, any
188 * non-zero value is true.
189 */
190 std::vector<int> GetBooleanArray(std::span<const int> defaultValue) const;
191
192 /**
193 * Gets the entry's value as a integer array. If the entry does not exist
194 * or is of different type, it will return the default value.
195 *
196 * @param defaultValue the value to be returned if no value is found
197 * @return the entry's value or the given default value
198 *
199 * @note This makes a copy of the array. If the overhead of this is a
200 * concern, use GetValue() instead.
201 */
202 std::vector<int64_t> GetIntegerArray(
203 std::span<const int64_t> defaultValue) const;
204
205 /**
206 * Gets the entry's value as a float array. If the entry does not exist
207 * or is of different type, it will return the default value.
208 *
209 * @param defaultValue the value to be returned if no value is found
210 * @return the entry's value or the given default value
211 *
212 * @note This makes a copy of the array. If the overhead of this is a
213 * concern, use GetValue() instead.
214 */
215 std::vector<float> GetFloatArray(std::span<const float> defaultValue) const;
216
217 /**
218 * Gets the entry's value as a double array. If the entry does not exist
219 * or is of different type, it will return the default value.
220 *
221 * @param defaultValue the value to be returned if no value is found
222 * @return the entry's value or the given default value
223 *
224 * @note This makes a copy of the array. If the overhead of this is a
225 * concern, use GetValue() instead.
226 */
227 std::vector<double> GetDoubleArray(
228 std::span<const double> defaultValue) const;
229
230 /**
231 * Gets the entry's value as a string array. If the entry does not exist
232 * or is of different type, it will return the default value.
233 *
234 * @param defaultValue the value to be returned if no value is found
235 * @return the entry's value or the given default value
236 *
237 * @note This makes a copy of the array. If the overhead of this is a
238 * concern, use GetValue() instead.
239 */
240 std::vector<std::string> GetStringArray(
241 std::span<const std::string> defaultValue) const;
242
243 /**
244 * Get an array of all value changes since the last call to ReadQueue.
245 *
246 * The "poll storage" subscribe option can be used to set the queue depth.
247 *
248 * @return Array of values; empty array if no new changes have been
249 * published since the previous call.
250 */
252
253 /**
254 * Sets the entry's value if it does not exist.
255 *
256 * @param defaultValue the default value to set
257 * @return False if the entry exists with a different type
258 */
259 bool SetDefaultValue(const Value& defaultValue);
260
261 /**
262 * Sets the entry's value if it does not exist.
263 *
264 * @param defaultValue the default value to set
265 * @return False if the entry exists with a different type
266 */
267 bool SetDefaultBoolean(bool defaultValue);
268
269 /**
270 * Sets the entry's value if it does not exist.
271 *
272 * @param defaultValue the default value to set
273 * @return False if the entry exists with a different type
274 */
275 bool SetDefaultInteger(int64_t defaultValue);
276
277 /**
278 * Sets the entry's value if it does not exist.
279 *
280 * @param defaultValue the default value to set
281 * @return False if the entry exists with a different type
282 */
283 bool SetDefaultFloat(float defaultValue);
284
285 /**
286 * Sets the entry's value if it does not exist.
287 *
288 * @param defaultValue the default value to set
289 * @return False if the entry exists with a different type
290 */
291 bool SetDefaultDouble(double defaultValue);
292
293 /**
294 * Sets the entry's value if it does not exist.
295 *
296 * @param defaultValue the default value to set
297 * @return False if the entry exists with a different type
298 */
299 bool SetDefaultString(std::string_view defaultValue);
300
301 /**
302 * Sets the entry's value if it does not exist.
303 *
304 * @param defaultValue the default value to set
305 * @return False if the entry exists with a different type
306 */
307 bool SetDefaultRaw(std::span<const uint8_t> defaultValue);
308
309 /**
310 * Sets the entry's value if it does not exist.
311 *
312 * @param defaultValue the default value to set
313 * @return False if the entry exists with a different type
314 */
315 bool SetDefaultBooleanArray(std::span<const int> defaultValue);
316
317 /**
318 * Sets the entry's value if it does not exist.
319 *
320 * @param defaultValue the default value to set
321 * @return False if the entry exists with a different type
322 */
323 bool SetDefaultIntegerArray(std::span<const int64_t> defaultValue);
324
325 /**
326 * Sets the entry's value if it does not exist.
327 *
328 * @param defaultValue the default value to set
329 * @return False if the entry exists with a different type
330 */
331 bool SetDefaultFloatArray(std::span<const float> defaultValue);
332
333 /**
334 * Sets the entry's value if it does not exist.
335 *
336 * @param defaultValue the default value to set
337 * @return False if the entry exists with a different type
338 */
339 bool SetDefaultDoubleArray(std::span<const double> defaultValue);
340
341 /**
342 * Sets the entry's value if it does not exist.
343 *
344 * @param defaultValue the default value to set
345 * @return False if the entry exists with a different type
346 */
347 bool SetDefaultStringArray(std::span<const std::string> defaultValue);
348
349 /**
350 * Sets the entry's value.
351 *
352 * @param value the value to set
353 * @return False if the entry exists with a different type
354 */
355 bool SetValue(const Value& value);
356
357 /**
358 * Sets the entry's value.
359 *
360 * @param value the value to set
361 * @param time the timestamp to set (0 = nt::Now())
362 * @return False if the entry exists with a different type
363 */
364 bool SetBoolean(bool value, int64_t time = 0);
365
366 /**
367 * Sets the entry's value.
368 *
369 * @param value the value to set
370 * @param time the timestamp to set (0 = nt::Now())
371 * @return False if the entry exists with a different type
372 */
373 bool SetInteger(int64_t value, int64_t time = 0);
374
375 /**
376 * Sets the entry's value.
377 *
378 * @param value the value to set
379 * @param time the timestamp to set (0 = nt::Now())
380 * @return False if the entry exists with a different type
381 */
382 bool SetFloat(float value, int64_t time = 0);
383
384 /**
385 * Sets the entry's value.
386 *
387 * @param value the value to set
388 * @param time the timestamp to set (0 = nt::Now())
389 * @return False if the entry exists with a different type
390 */
391 bool SetDouble(double value, int64_t time = 0);
392
393 /**
394 * Sets the entry's value.
395 *
396 * @param value the value to set
397 * @param time the timestamp to set (0 = nt::Now())
398 * @return False if the entry exists with a different type
399 */
400 bool SetString(std::string_view value, int64_t time = 0);
401
402 /**
403 * Sets the entry's value.
404 *
405 * @param value the value to set
406 * @param time the timestamp to set (0 = nt::Now())
407 * @return False if the entry exists with a different type
408 */
409 bool SetRaw(std::span<const uint8_t> value, int64_t time = 0);
410
411 /**
412 * Sets the entry's value.
413 *
414 * @param value the value to set
415 * @param time the timestamp to set (0 = nt::Now())
416 * @return False if the entry exists with a different type
417 */
418 bool SetBooleanArray(std::span<const bool> value, int64_t time = 0);
419
420 /**
421 * Sets the entry's value.
422 *
423 * @param value the value to set
424 * @param time the timestamp to set (0 = nt::Now())
425 * @return False if the entry exists with a different type
426 */
427 bool SetBooleanArray(std::span<const int> value, int64_t time = 0);
428
429 /**
430 * Sets the entry's value.
431 *
432 * @param value the value to set
433 * @param time the timestamp to set (0 = nt::Now())
434 * @return False if the entry exists with a different type
435 */
436 bool SetIntegerArray(std::span<const int64_t> value, int64_t time = 0);
437
438 /**
439 * Sets the entry's value.
440 *
441 * @param value the value to set
442 * @param time the timestamp to set (0 = nt::Now())
443 * @return False if the entry exists with a different type
444 */
445 bool SetFloatArray(std::span<const float> value, int64_t time = 0);
446
447 /**
448 * Sets the entry's value.
449 *
450 * @param value the value to set
451 * @param time the timestamp to set (0 = nt::Now())
452 * @return False if the entry exists with a different type
453 */
454 bool SetDoubleArray(std::span<const double> value, int64_t time = 0);
455
456 /**
457 * Sets the entry's value.
458 *
459 * @param value the value to set
460 * @param time the timestamp to set (0 = nt::Now())
461 * @return False if the entry exists with a different type
462 */
463 bool SetStringArray(std::span<const std::string> value, int64_t time = 0);
464
465 /**
466 * Sets flags.
467 *
468 * @param flags the flags to set (bitmask)
469 * @deprecated Use SetPersistent() or topic properties instead
470 */
471 WPI_DEPRECATED("Use SetPersistent() or topic properties instead")
472 void SetFlags(unsigned int flags);
473
474 /**
475 * Clears flags.
476 *
477 * @param flags the flags to clear (bitmask)
478 * @deprecated Use SetPersistent() or topic properties instead
479 */
480 WPI_DEPRECATED("Use SetPersistent() or topic properties instead")
481 void ClearFlags(unsigned int flags);
482
483 /**
484 * Make value persistent through program restarts.
485 */
486 void SetPersistent();
487
488 /**
489 * Stop making value persistent through program restarts.
490 */
491 void ClearPersistent();
492
493 /**
494 * Returns whether the value is persistent through program restarts.
495 *
496 * @return True if the value is persistent.
497 */
498 bool IsPersistent() const;
499
500 /**
501 * Stops publishing the entry if it's been published.
502 */
503 void Unpublish();
504
505 /**
506 * Deletes the entry.
507 * @deprecated Use Unpublish() instead.
508 */
509 WPI_DEPRECATED("Use Unpublish() instead")
510 void Delete();
511
512 /**
513 * Gets the entry's topic.
514 *
515 * @return Topic
516 */
518
519 /**
520 * Equality operator. Returns true if both instances refer to the same
521 * native handle.
522 */
523 bool operator==(const NetworkTableEntry&) const = default;
524
525 protected:
526 /* Native handle */
528};
529
530} // namespace nt
531
or
Definition: ThirdPartyNotices.txt:199
NetworkTables Entry.
Definition: NetworkTableEntry.h:36
NetworkTableType GetType() const
Gets the type of the entry.
Definition: NetworkTableEntry.inc:35
std::vector< int > GetBooleanArray(std::span< const int > defaultValue) const
Gets the entry's value as a boolean array.
Definition: NetworkTableEntry.inc:77
std::string GetName() const
Gets the name of the entry (the key).
Definition: NetworkTableEntry.inc:31
bool SetDouble(double value, int64_t time=0)
Sets the entry's value.
Definition: NetworkTableEntry.inc:176
unsigned int GetFlags() const
Returns the flags.
Definition: NetworkTableEntry.inc:39
void Unpublish()
Stops publishing the entry if it's been published.
Definition: NetworkTableEntry.inc:239
std::vector< float > GetFloatArray(std::span< const float > defaultValue) const
Gets the entry's value as a float array.
Definition: NetworkTableEntry.inc:87
void ClearPersistent()
Stop making value persistent through program restarts.
Definition: NetworkTableEntry.inc:231
std::vector< std::string > GetStringArray(std::span< const std::string > defaultValue) const
Gets the entry's value as a string array.
Definition: NetworkTableEntry.inc:97
void SetPersistent()
Make value persistent through program restarts.
Definition: NetworkTableEntry.inc:227
bool SetString(std::string_view value, int64_t time=0)
Sets the entry's value.
Definition: NetworkTableEntry.inc:180
void SetFlags(unsigned int flags)
Sets flags.
Definition: NetworkTableEntry.inc:219
float GetFloat(float defaultValue) const
Gets the entry's value as a float.
Definition: NetworkTableEntry.inc:59
bool SetDefaultValue(const Value &defaultValue)
Sets the entry's value if it does not exist.
Definition: NetworkTableEntry.inc:106
Topic GetTopic() const
Gets the entry's topic.
bool SetDefaultFloat(float defaultValue)
Sets the entry's value if it does not exist.
Definition: NetworkTableEntry.inc:118
bool SetBoolean(bool value, int64_t time=0)
Sets the entry's value.
Definition: NetworkTableEntry.inc:164
int64_t GetLastChange() const
Gets the last time the entry's value was changed.
Definition: NetworkTableEntry.inc:43
bool SetDefaultFloatArray(std::span< const float > defaultValue)
Sets the entry's value if it does not exist.
Definition: NetworkTableEntry.inc:145
std::vector< double > GetDoubleArray(std::span< const double > defaultValue) const
Gets the entry's value as a double array.
Definition: NetworkTableEntry.inc:92
bool SetDefaultRaw(std::span< const uint8_t > defaultValue)
Sets the entry's value if it does not exist.
Definition: NetworkTableEntry.inc:130
bool IsPersistent() const
Returns whether the value is persistent through program restarts.
Definition: NetworkTableEntry.inc:235
void ClearFlags(unsigned int flags)
Clears flags.
Definition: NetworkTableEntry.inc:223
bool SetValue(const Value &value)
Sets the entry's value.
Definition: NetworkTableEntry.inc:160
bool SetDoubleArray(std::span< const double > value, int64_t time=0)
Sets the entry's value.
Definition: NetworkTableEntry.inc:209
double GetDouble(double defaultValue) const
Gets the entry's value as a double.
Definition: NetworkTableEntry.inc:63
std::vector< uint8_t > GetRaw(std::span< const uint8_t > defaultValue) const
Gets the entry's value as a raw.
Definition: NetworkTableEntry.inc:72
bool SetDefaultString(std::string_view defaultValue)
Sets the entry's value if it does not exist.
Definition: NetworkTableEntry.inc:126
bool SetFloatArray(std::span< const float > value, int64_t time=0)
Sets the entry's value.
Definition: NetworkTableEntry.inc:204
bool SetInteger(int64_t value, int64_t time=0)
Sets the entry's value.
Definition: NetworkTableEntry.inc:168
bool SetFloat(float value, int64_t time=0)
Sets the entry's value.
Definition: NetworkTableEntry.inc:172
int64_t GetInteger(int64_t defaultValue) const
Gets the entry's value as a integer.
Definition: NetworkTableEntry.inc:55
bool Exists() const
Determines if the entry currently exists.
Definition: NetworkTableEntry.inc:27
void Delete()
Deletes the entry.
Definition: NetworkTableEntry.inc:243
bool SetDefaultDouble(double defaultValue)
Sets the entry's value if it does not exist.
Definition: NetworkTableEntry.inc:122
bool SetDefaultDoubleArray(std::span< const double > defaultValue)
Sets the entry's value if it does not exist.
Definition: NetworkTableEntry.inc:150
Value GetValue() const
Gets the entry's value.
Definition: NetworkTableEntry.inc:47
NetworkTableInstance GetInstance() const
Gets the instance for the entry.
bool SetDefaultBooleanArray(std::span< const int > defaultValue)
Sets the entry's value if it does not exist.
Definition: NetworkTableEntry.inc:135
std::vector< NetworkTableValue > ReadQueue()
Get an array of all value changes since the last call to ReadQueue.
Definition: NetworkTableEntry.inc:102
NT_Entry m_handle
Definition: NetworkTableEntry.h:527
bool SetRaw(std::span< const uint8_t > value, int64_t time=0)
Sets the entry's value.
Definition: NetworkTableEntry.inc:184
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition: NetworkTableEntry.inc:23
bool GetBoolean(bool defaultValue) const
Gets the entry's value as a boolean.
Definition: NetworkTableEntry.inc:51
std::vector< int64_t > GetIntegerArray(std::span< const int64_t > defaultValue) const
Gets the entry's value as a integer array.
Definition: NetworkTableEntry.inc:82
bool SetDefaultBoolean(bool defaultValue)
Sets the entry's value if it does not exist.
Definition: NetworkTableEntry.inc:110
Flags
Flag values (as returned by GetFlags()).
Definition: NetworkTableEntry.h:42
@ kPersistent
Definition: NetworkTableEntry.h:42
bool SetBooleanArray(std::span< const bool > value, int64_t time=0)
Sets the entry's value.
Definition: NetworkTableEntry.inc:189
NetworkTableEntry()
Construct invalid instance.
Definition: NetworkTableEntry.inc:18
bool SetStringArray(std::span< const std::string > value, int64_t time=0)
Sets the entry's value.
Definition: NetworkTableEntry.inc:214
bool SetDefaultStringArray(std::span< const std::string > defaultValue)
Sets the entry's value if it does not exist.
Definition: NetworkTableEntry.inc:155
std::string GetString(std::string_view defaultValue) const
Gets the entry's value as a string.
Definition: NetworkTableEntry.inc:67
bool SetDefaultIntegerArray(std::span< const int64_t > defaultValue)
Sets the entry's value if it does not exist.
Definition: NetworkTableEntry.inc:140
bool SetDefaultInteger(int64_t defaultValue)
Sets the entry's value if it does not exist.
Definition: NetworkTableEntry.inc:114
bool SetIntegerArray(std::span< const int64_t > value, int64_t time=0)
Sets the entry's value.
Definition: NetworkTableEntry.inc:199
NetworkTables Instance.
Definition: NetworkTableInstance.h:59
NetworkTables Topic.
Definition: Topic.h:30
A network table entry value.
Definition: NetworkTableValue.h:27
Definition: core.h:1240
NT_Handle NT_Entry
Definition: ntcore_c.h:33
@ NT_PERSISTENT
Definition: ntcore_c.h:66
NetworkTableType
NetworkTable entry type.
Definition: NetworkTableType.h:15
::int64_t int64_t
Definition: Meta.h:59
::uint8_t uint8_t
Definition: Meta.h:52
NetworkTables (ntcore) namespace.
Definition: ntcore_cpp.h:35
Definition: StdDeque.h:50
flags
Definition: http_parser.h:206