WPILibC++ 2023.4.3
NetworkTable.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 <functional>
8#include <memory>
9#include <span>
10#include <string>
11#include <string_view>
12#include <utility>
13#include <vector>
14
15#include <wpi/StringMap.h>
16#include <wpi/mutex.h>
17
19#include "ntcore_c.h"
20
21namespace nt {
22
23class BooleanArrayTopic;
24class BooleanTopic;
25class DoubleArrayTopic;
26class DoubleTopic;
27class FloatArrayTopic;
28class FloatTopic;
29class IntegerArrayTopic;
30class IntegerTopic;
31class NetworkTableInstance;
32class RawTopic;
33class StringArrayTopic;
34class StringTopic;
35class Topic;
36
37/**
38 * @defgroup ntcore_cpp_api ntcore C++ object-oriented API
39 *
40 * Recommended interface for C++, identical to Java API.
41 */
42
43/**
44 * A network table that knows its subtable path.
45 * @ingroup ntcore_cpp_api
46 */
47class NetworkTable final {
48 private:
49 NT_Inst m_inst;
50 std::string m_path;
51 mutable wpi::mutex m_mutex;
52 mutable wpi::StringMap<NT_Entry> m_entries;
53
54 struct private_init {};
56
57 public:
58 /**
59 * Gets the "base name" of a key. For example, "/foo/bar" becomes "bar".
60 * If the key has a trailing slash, returns an empty string.
61 *
62 * @param key key
63 * @return base name
64 */
66
67 /**
68 * Normalizes an network table key to contain no consecutive slashes and
69 * optionally start with a leading slash. For example:
70 *
71 * <pre><code>
72 * normalizeKey("/foo/bar", true) == "/foo/bar"
73 * normalizeKey("foo/bar", true) == "/foo/bar"
74 * normalizeKey("/foo/bar", false) == "foo/bar"
75 * normalizeKey("foo//bar", false) == "foo/bar"
76 * </code></pre>
77 *
78 * @param key the key to normalize
79 * @param withLeadingSlash whether or not the normalized key should begin
80 * with a leading slash
81 * @return normalized key
82 */
83 static std::string NormalizeKey(std::string_view key,
84 bool withLeadingSlash = true);
85
88 bool withLeadingSlash = true);
89
90 /**
91 * Gets a list of the names of all the super tables of a given key. For
92 * example, the key "/foo/bar/baz" has a hierarchy of "/", "/foo",
93 * "/foo/bar", and "/foo/bar/baz".
94 *
95 * @param key the key
96 * @return List of super tables
97 */
98 static std::vector<std::string> GetHierarchy(std::string_view key);
99
100 /**
101 * Constructor. Use NetworkTableInstance::GetTable() or GetSubTable()
102 * instead.
103 */
104 NetworkTable(NT_Inst inst, std::string_view path, const private_init&);
106
107 /**
108 * Gets the instance for the table.
109 *
110 * @return Instance
111 */
113
114 /**
115 * The path separator for sub-tables and keys
116 */
117 static constexpr char PATH_SEPARATOR_CHAR = '/';
118
119 /**
120 * Gets the entry for a subkey.
121 *
122 * @param key the key name
123 * @return Network table entry.
124 */
126
127 /**
128 * Get (generic) topic.
129 *
130 * @param name topic name
131 * @return Topic
132 */
134
135 /**
136 * Get boolean topic.
137 *
138 * @param name topic name
139 * @return BooleanTopic
140 */
142
143 /**
144 * Get integer topic.
145 *
146 * @param name topic name
147 * @return IntegerTopic
148 */
150
151 /**
152 * Get float topic.
153 *
154 * @param name topic name
155 * @return FloatTopic
156 */
158
159 /**
160 * Get double topic.
161 *
162 * @param name topic name
163 * @return DoubleTopic
164 */
166
167 /**
168 * Get String topic.
169 *
170 * @param name topic name
171 * @return StringTopic
172 */
174
175 /**
176 * Get raw topic.
177 *
178 * @param name topic name
179 * @return BooleanArrayTopic
180 */
182
183 /**
184 * Get boolean[] topic.
185 *
186 * @param name topic name
187 * @return BooleanArrayTopic
188 */
190
191 /**
192 * Get integer[] topic.
193 *
194 * @param name topic name
195 * @return IntegerArrayTopic
196 */
198
199 /**
200 * Get float[] topic.
201 *
202 * @param name topic name
203 * @return FloatArrayTopic
204 */
206
207 /**
208 * Get double[] topic.
209 *
210 * @param name topic name
211 * @return DoubleArrayTopic
212 */
214
215 /**
216 * Get String[] topic.
217 *
218 * @param name topic name
219 * @return StringArrayTopic
220 */
222
223 /**
224 * Returns the table at the specified key. If there is no table at the
225 * specified key, it will create a new table
226 *
227 * @param key the key name
228 * @return the networktable to be returned
229 */
230 std::shared_ptr<NetworkTable> GetSubTable(std::string_view key) const;
231
232 /**
233 * Determines whether the given key is in this table.
234 *
235 * @param key the key to search for
236 * @return true if the table as a value assigned to the given key
237 */
239
240 /**
241 * Determines whether there exists a non-empty subtable for this key
242 * in this table.
243 *
244 * @param key the key to search for
245 * @return true if there is a subtable with the key which contains at least
246 * one key/subtable of its own
247 */
249
250 /**
251 * Gets topic information for all keys in the table (not including
252 * sub-tables).
253 *
254 * @param types bitmask of types; 0 is treated as a "don't care".
255 * @return topic information for keys currently in the table
256 */
257 std::vector<TopicInfo> GetTopicInfo(int types = 0) const;
258
259 /**
260 * Gets all topics in the table (not including sub-tables).
261 *
262 * @param types bitmask of types; 0 is treated as a "don't care".
263 * @return topic for keys currently in the table
264 */
265 std::vector<Topic> GetTopics(int types = 0) const;
266
267 /**
268 * Gets all keys in the table (not including sub-tables).
269 *
270 * @param types bitmask of types; 0 is treated as a "don't care".
271 * @return keys currently in the table
272 */
273 std::vector<std::string> GetKeys(int types = 0) const;
274
275 /**
276 * Gets the names of all subtables in the table.
277 *
278 * @return subtables currently in the table
279 */
280 std::vector<std::string> GetSubTables() const;
281
282 /**
283 * Makes a key's value persistent through program restarts.
284 *
285 * @param key the key to make persistent
286 */
288
289 /**
290 * Stop making a key's value persistent through program restarts.
291 * The key cannot be null.
292 *
293 * @param key the key name
294 */
296
297 /**
298 * Returns whether the value is persistent through program restarts.
299 * The key cannot be null.
300 *
301 * @param key the key name
302 */
304
305 /**
306 * Put a number in the table
307 *
308 * @param key the key to be assigned to
309 * @param value the value that will be assigned
310 * @return False if the table key already exists with a different type
311 */
313
314 /**
315 * Gets the current value in the table, setting it if it does not exist.
316 *
317 * @param key the key
318 * @param defaultValue the default value to set if key doesn't exist.
319 * @returns False if the table key exists with a different type
320 */
321 bool SetDefaultNumber(std::string_view key, double defaultValue);
322
323 /**
324 * Gets the number associated with the given name.
325 *
326 * @param key the key to look up
327 * @param defaultValue the value to be returned if no value is found
328 * @return the value associated with the given key or the given default value
329 * if there is no value associated with the key
330 */
331 double GetNumber(std::string_view key, double defaultValue) const;
332
333 /**
334 * Put a string in the table
335 *
336 * @param key the key to be assigned to
337 * @param value the value that will be assigned
338 * @return False if the table key already exists with a different type
339 */
341
342 /**
343 * Gets the current value in the table, setting it if it does not exist.
344 *
345 * @param key the key
346 * @param defaultValue the default value to set if key doesn't exist.
347 * @returns False if the table key exists with a different type
348 */
350
351 /**
352 * Gets the string associated with the given name. If the key does not
353 * exist or is of different type, it will return the default value.
354 *
355 * @param key the key to look up
356 * @param defaultValue the value to be returned if no value is found
357 * @return the value associated with the given key or the given default value
358 * if there is no value associated with the key
359 */
361 std::string_view defaultValue) const;
362
363 /**
364 * Put a boolean in the table
365 *
366 * @param key the key to be assigned to
367 * @param value the value that will be assigned
368 * @return False if the table key already exists with a different type
369 */
371
372 /**
373 * Gets the current value in the table, setting it if it does not exist.
374 *
375 * @param key the key
376 * @param defaultValue the default value to set if key doesn't exist.
377 * @returns False if the table key exists with a different type
378 */
379 bool SetDefaultBoolean(std::string_view key, bool defaultValue);
380
381 /**
382 * Gets the boolean associated with the given name. If the key does not
383 * exist or is of different type, it will return the default value.
384 *
385 * @param key the key to look up
386 * @param defaultValue the value to be returned if no value is found
387 * @return the value associated with the given key or the given default value
388 * if there is no value associated with the key
389 */
390 bool GetBoolean(std::string_view key, bool defaultValue) const;
391
392 /**
393 * Put a boolean array in the table
394 *
395 * @param key the key to be assigned to
396 * @param value the value that will be assigned
397 * @return False if the table key already exists with a different type
398 *
399 * @note The array must be of int's rather than of bool's because
400 * std::vector<bool> is special-cased in C++. 0 is false, any
401 * non-zero value is true.
402 */
403 bool PutBooleanArray(std::string_view key, std::span<const int> value);
404
405 /**
406 * Gets the current value in the table, setting it if it does not exist.
407 *
408 * @param key the key
409 * @param defaultValue the default value to set if key doesn't exist.
410 * @return False if the table key exists with a different type
411 */
413 std::span<const int> defaultValue);
414
415 /**
416 * Returns the boolean array the key maps to. If the key does not exist or is
417 * of different type, it will return the default value.
418 *
419 * @param key the key to look up
420 * @param defaultValue the value to be returned if no value is found
421 * @return the value associated with the given key or the given default value
422 * if there is no value associated with the key
423 *
424 * @note This makes a copy of the array. If the overhead of this is a
425 * concern, use GetValue() instead.
426 *
427 * @note The returned array is std::vector<int> instead of std::vector<bool>
428 * because std::vector<bool> is special-cased in C++. 0 is false, any
429 * non-zero value is true.
430 */
431 std::vector<int> GetBooleanArray(std::string_view key,
432 std::span<const int> defaultValue) const;
433
434 /**
435 * Put a number array in the table
436 *
437 * @param key the key to be assigned to
438 * @param value the value that will be assigned
439 * @return False if the table key already exists with a different type
440 */
441 bool PutNumberArray(std::string_view key, std::span<const double> value);
442
443 /**
444 * Gets the current value in the table, setting it if it does not exist.
445 *
446 * @param key the key
447 * @param defaultValue the default value to set if key doesn't exist.
448 * @returns False if the table key exists with a different type
449 */
451 std::span<const double> defaultValue);
452
453 /**
454 * Returns the number array the key maps to. If the key does not exist or is
455 * of different type, it will return the default value.
456 *
457 * @param key the key to look up
458 * @param defaultValue the value to be returned if no value is found
459 * @return the value associated with the given key or the given default value
460 * if there is no value associated with the key
461 *
462 * @note This makes a copy of the array. If the overhead of this is a
463 * concern, use GetValue() instead.
464 */
465 std::vector<double> GetNumberArray(
466 std::string_view key, std::span<const double> defaultValue) const;
467
468 /**
469 * Put a string array in the table
470 *
471 * @param key the key to be assigned to
472 * @param value the value that will be assigned
473 * @return False if the table key already exists with a different type
474 */
475 bool PutStringArray(std::string_view key, std::span<const std::string> value);
476
477 /**
478 * Gets the current value in the table, setting it if it does not exist.
479 *
480 * @param key the key
481 * @param defaultValue the default value to set if key doesn't exist.
482 * @returns False if the table key exists with a different type
483 */
485 std::span<const std::string> defaultValue);
486
487 /**
488 * Returns the string array the key maps to. If the key does not exist or is
489 * of different type, it will return the default value.
490 *
491 * @param key the key to look up
492 * @param defaultValue the value to be returned if no value is found
493 * @return the value associated with the given key or the given default value
494 * if there is no value associated with the key
495 *
496 * @note This makes a copy of the array. If the overhead of this is a
497 * concern, use GetValue() instead.
498 */
499 std::vector<std::string> GetStringArray(
500 std::string_view key, std::span<const std::string> defaultValue) const;
501
502 /**
503 * Put a raw value (byte array) in the table
504 *
505 * @param key the key to be assigned to
506 * @param value the value that will be assigned
507 * @return False if the table key already exists with a different type
508 */
509 bool PutRaw(std::string_view key, std::span<const uint8_t> value);
510
511 /**
512 * Gets the current value in the table, setting it if it does not exist.
513 *
514 * @param key the key
515 * @param defaultValue the default value to set if key doesn't exist.
516 * @return False if the table key exists with a different type
517 */
519 std::span<const uint8_t> defaultValue);
520
521 /**
522 * Returns the raw value (byte array) the key maps to. If the key does not
523 * exist or is of different type, it will return the default value.
524 *
525 * @param key the key to look up
526 * @param defaultValue the value to be returned if no value is found
527 * @return the value associated with the given key or the given default value
528 * if there is no value associated with the key
529 *
530 * @note This makes a copy of the raw contents. If the overhead of this is a
531 * concern, use GetValue() instead.
532 */
533 std::vector<uint8_t> GetRaw(std::string_view key,
534 std::span<const uint8_t> defaultValue) const;
535
536 /**
537 * Put a value in the table
538 *
539 * @param key the key to be assigned to
540 * @param value the value that will be assigned
541 * @return False if the table key already exists with a different type
542 */
544
545 /**
546 * Gets the current value in the table, setting it if it does not exist.
547 *
548 * @param key the key
549 * @param defaultValue the default value to set if key doesn't exist.
550 * @return False if the table key exists with a different type
551 */
552 bool SetDefaultValue(std::string_view key, const Value& defaultValue);
553
554 /**
555 * Gets the value associated with a key as an object
556 *
557 * @param key the key of the value to look up
558 * @return the value associated with the given key, or nullptr if the key
559 * does not exist
560 */
562
563 /**
564 * Gets the full path of this table. Does not include the trailing "/".
565 *
566 * @return The path (e.g "", "/foo").
567 */
569
570 /**
571 * Called when an event occurs on a topic in a {@link NetworkTable}.
572 *
573 * @param table the table the topic exists in
574 * @param key the key associated with the topic that changed
575 * @param event the event
576 */
577 using TableEventListener = std::function<void(
578 NetworkTable* table, std::string_view key, const Event& event)>;
579
580 /**
581 * Listen to topics only within this table.
582 *
583 * @param eventMask Bitmask of EventFlags values
584 * @param listener listener to add
585 * @return Listener handle
586 */
588
589 /**
590 * Listen to a single key.
591 *
592 * @param key the key name
593 * @param eventMask Bitmask of EventFlags values
594 * @param listener listener to add
595 * @return Listener handle
596 */
598 TableEventListener listener);
599
600 /**
601 * Called when a new table is created within a NetworkTable.
602 *
603 * @param parent the parent of the table
604 * @param name the name of the new table
605 * @param table the new table
606 */
608 std::function<void(NetworkTable* parent, std::string_view name,
609 std::shared_ptr<NetworkTable> table)>;
610
611 /**
612 * Listen for sub-table creation. This calls the listener once for each newly
613 * created sub-table. It immediately calls the listener for any existing
614 * sub-tables.
615 *
616 * @param listener listener to add
617 * @return Listener handle
618 */
620
621 /**
622 * Remove a listener.
623 *
624 * @param listener listener handle
625 */
627};
628
629} // namespace nt
This file defines the StringMap class.
NetworkTables BooleanArray topic.
Definition: BooleanArrayTopic.h:263
NetworkTables Boolean topic.
Definition: BooleanTopic.h:210
NetworkTables DoubleArray topic.
Definition: DoubleArrayTopic.h:263
NetworkTables Double topic.
Definition: DoubleTopic.h:210
NetworkTables event.
Definition: ntcore_cpp.h:216
NetworkTables FloatArray topic.
Definition: FloatArrayTopic.h:263
NetworkTables Float topic.
Definition: FloatTopic.h:210
NetworkTables IntegerArray topic.
Definition: IntegerArrayTopic.h:263
NetworkTables Integer topic.
Definition: IntegerTopic.h:210
NetworkTables Entry.
Definition: NetworkTableEntry.h:36
A network table that knows its subtable path.
Definition: NetworkTable.h:47
std::vector< Topic > GetTopics(int types=0) const
Gets all topics in the table (not including sub-tables).
bool PutNumber(std::string_view key, double value)
Put a number in the table.
std::vector< double > GetNumberArray(std::string_view key, std::span< const double > defaultValue) const
Returns the number array the key maps to.
Value GetValue(std::string_view key) const
Gets the value associated with a key as an object.
bool SetDefaultBooleanArray(std::string_view key, std::span< const int > defaultValue)
Gets the current value in the table, setting it if it does not exist.
bool SetDefaultValue(std::string_view key, const Value &defaultValue)
Gets the current value in the table, setting it if it does not exist.
FloatTopic GetFloatTopic(std::string_view name) const
Get float topic.
std::vector< uint8_t > GetRaw(std::string_view key, std::span< const uint8_t > defaultValue) const
Returns the raw value (byte array) the key maps to.
static std::vector< std::string > GetHierarchy(std::string_view key)
Gets a list of the names of all the super tables of a given key.
std::vector< int > GetBooleanArray(std::string_view key, std::span< const int > defaultValue) const
Returns the boolean array the key maps to.
bool PutNumberArray(std::string_view key, std::span< const double > value)
Put a number array in the table.
StringTopic GetStringTopic(std::string_view name) const
Get String topic.
std::string_view GetPath() const
Gets the full path of this table.
std::vector< std::string > GetStringArray(std::string_view key, std::span< const std::string > defaultValue) const
Returns the string array the key maps to.
bool SetDefaultBoolean(std::string_view key, bool defaultValue)
Gets the current value in the table, setting it if it does not exist.
static std::string NormalizeKey(std::string_view key, bool withLeadingSlash=true)
Normalizes an network table key to contain no consecutive slashes and optionally start with a leading...
std::vector< TopicInfo > GetTopicInfo(int types=0) const
Gets topic information for all keys in the table (not including sub-tables).
bool SetDefaultRaw(std::string_view key, std::span< const uint8_t > defaultValue)
Gets the current value in the table, setting it if it does not exist.
std::shared_ptr< NetworkTable > GetSubTable(std::string_view key) const
Returns the table at the specified key.
bool ContainsKey(std::string_view key) const
Determines whether the given key is in this table.
BooleanTopic GetBooleanTopic(std::string_view name) const
Get boolean topic.
std::function< void(NetworkTable *parent, std::string_view name, std::shared_ptr< NetworkTable > table)> SubTableListener
Called when a new table is created within a NetworkTable.
Definition: NetworkTable.h:609
bool PutBoolean(std::string_view key, bool value)
Put a boolean in the table.
bool ContainsSubTable(std::string_view key) const
Determines whether there exists a non-empty subtable for this key in this table.
std::function< void(NetworkTable *table, std::string_view key, const Event &event)> TableEventListener
Called when an event occurs on a topic in a NetworkTable.
Definition: NetworkTable.h:578
bool PutString(std::string_view key, std::string_view value)
Put a string in the table.
bool IsPersistent(std::string_view key) const
Returns whether the value is persistent through program restarts.
bool SetDefaultNumber(std::string_view key, double defaultValue)
Gets the current value in the table, setting it if it does not exist.
bool SetDefaultStringArray(std::string_view key, std::span< const std::string > defaultValue)
Gets the current value in the table, setting it if it does not exist.
bool PutRaw(std::string_view key, std::span< const uint8_t > value)
Put a raw value (byte array) in the table.
double GetNumber(std::string_view key, double defaultValue) const
Gets the number associated with the given name.
RawTopic GetRawTopic(std::string_view name) const
Get raw topic.
void RemoveListener(NT_Listener listener)
Remove a listener.
StringArrayTopic GetStringArrayTopic(std::string_view name) const
Get String[] topic.
bool PutStringArray(std::string_view key, std::span< const std::string > value)
Put a string array in the table.
bool PutValue(std::string_view key, const Value &value)
Put a value in the table.
IntegerArrayTopic GetIntegerArrayTopic(std::string_view name) const
Get integer[] topic.
bool PutBooleanArray(std::string_view key, std::span< const int > value)
Put a boolean array in the table.
NetworkTableEntry GetEntry(std::string_view key) const
Gets the entry for a subkey.
BooleanArrayTopic GetBooleanArrayTopic(std::string_view name) const
Get boolean[] topic.
static constexpr char PATH_SEPARATOR_CHAR
The path separator for sub-tables and keys.
Definition: NetworkTable.h:117
NetworkTableInstance GetInstance() const
Gets the instance for the table.
Topic GetTopic(std::string_view name) const
Get (generic) topic.
IntegerTopic GetIntegerTopic(std::string_view name) const
Get integer topic.
bool GetBoolean(std::string_view key, bool defaultValue) const
Gets the boolean associated with the given name.
std::string GetString(std::string_view key, std::string_view defaultValue) const
Gets the string associated with the given name.
DoubleArrayTopic GetDoubleArrayTopic(std::string_view name) const
Get double[] topic.
static std::string_view BasenameKey(std::string_view key)
Gets the "base name" of a key.
std::vector< std::string > GetKeys(int types=0) const
Gets all keys in the table (not including sub-tables).
DoubleTopic GetDoubleTopic(std::string_view name) const
Get double topic.
std::vector< std::string > GetSubTables() const
Gets the names of all subtables in the table.
NT_Listener AddListener(std::string_view key, int eventMask, TableEventListener listener)
Listen to a single key.
NetworkTable(NT_Inst inst, std::string_view path, const private_init &)
Constructor.
void SetPersistent(std::string_view key)
Makes a key's value persistent through program restarts.
NT_Listener AddListener(int eventMask, TableEventListener listener)
Listen to topics only within this table.
NT_Listener AddSubTableListener(SubTableListener listener)
Listen for sub-table creation.
bool SetDefaultString(std::string_view key, std::string_view defaultValue)
Gets the current value in the table, setting it if it does not exist.
void ClearPersistent(std::string_view key)
Stop making a key's value persistent through program restarts.
bool SetDefaultNumberArray(std::string_view key, std::span< const double > defaultValue)
Gets the current value in the table, setting it if it does not exist.
FloatArrayTopic GetFloatArrayTopic(std::string_view name) const
Get float[] topic.
static std::string_view NormalizeKey(std::string_view key, wpi::SmallVectorImpl< char > &buf, bool withLeadingSlash=true)
NetworkTables Instance.
Definition: NetworkTableInstance.h:59
NetworkTables Raw topic.
Definition: RawTopic.h:263
NetworkTables StringArray topic.
Definition: StringArrayTopic.h:210
NetworkTables String topic.
Definition: StringTopic.h:265
NetworkTables Topic.
Definition: Topic.h:30
A network table entry value.
Definition: NetworkTableValue.h:27
Definition: core.h:1240
basic_string_view< char > string_view
Definition: core.h:520
NT_Handle NT_Listener
Definition: ntcore_c.h:35
NT_Handle NT_Inst
Definition: ntcore_c.h:34
NetworkTables (ntcore) namespace.
Definition: ntcore_cpp.h:35
::std::mutex mutex
Definition: mutex.h:17