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