WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
NetworkTableInstance.hpp
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 <memory>
8#include <optional>
9#include <span>
10#include <string_view>
11#include <utility>
12#include <vector>
13
16#include "wpi/nt/ntcore_c.h"
17#include "wpi/nt/ntcore_cpp.hpp"
20
21namespace wpi::nt {
22
24class BooleanTopic;
26class DoubleTopic;
27class FloatArrayTopic;
28class FloatTopic;
30class IntegerTopic;
31class MultiSubscriber;
32template <wpi::util::ProtobufSerializable T>
33class ProtobufTopic;
34class RawTopic;
36class StringTopic;
37template <typename T, typename... I>
38 requires wpi::util::StructSerializable<T, I...>
40template <typename T, typename... I>
41 requires wpi::util::StructSerializable<T, I...>
42class StructTopic;
43class Subscriber;
44class Topic;
45
46/**
47 * NetworkTables Instance.
48 *
49 * Instances are completely independent from each other. Table operations on
50 * one instance will not be visible to other instances unless the instances are
51 * connected via the network. The main limitation on instances is that you
52 * cannot have two servers on the same network port. The main utility of
53 * instances is for unit testing, but they can also enable one program to
54 * connect to two different NetworkTables networks.
55 *
56 * The global "default" instance (as returned by GetDefault()) is
57 * always available, and is intended for the common case when there is only
58 * a single NetworkTables instance being used in the program. The
59 * default instance cannot be destroyed.
60 *
61 * Additional instances can be created with the Create() function.
62 * Instances are not reference counted or RAII. Instead, they must be
63 * explicitly destroyed (with Destroy()).
64 *
65 * @ingroup ntcore_cpp_api
66 */
68 public:
69 /**
70 * Client/server mode flag values (as returned by GetNetworkMode()).
71 * This is a bitmask.
72 */
79
80 /**
81 * Logging levels (as used by SetLogger()).
82 */
94
95 /**
96 * The default port that network tables operates on.
97 */
98 static constexpr unsigned int kDefaultPort = NT_DEFAULT_PORT;
99
100 /**
101 * Construct invalid instance.
102 */
103 NetworkTableInstance() noexcept = default;
104
105 /**
106 * Construct from native handle.
107 *
108 * @param handle Native handle
109 */
110 explicit NetworkTableInstance(NT_Inst handle) noexcept : m_handle{handle} {}
111
112 /**
113 * Determines if the native handle is valid.
114 *
115 * @return True if the native handle is valid, false otherwise.
116 */
117 explicit operator bool() const { return m_handle != 0; }
118
119 /**
120 * Get global default instance.
121 *
122 * @return Global default instance
123 */
127
128 /**
129 * Create an instance.
130 *
131 * @return Newly created instance
132 */
136
137 /**
138 * Destroys an instance (note: this has global effect).
139 *
140 * @param inst Instance
141 */
142 static void Destroy(NetworkTableInstance& inst) {
143 if (inst.m_handle != 0) {
144 DestroyInstance(inst.m_handle);
145 inst.m_handle = 0;
146 }
147 }
148
149 /**
150 * Gets the native handle for the entry.
151 *
152 * @return Native handle
153 */
154 NT_Inst GetHandle() const { return m_handle; }
155
156 /**
157 * Gets a "generic" (untyped) topic.
158 *
159 * @param name topic name
160 * @return Topic
161 */
162 Topic GetTopic(std::string_view name) const;
163
164 /**
165 * Gets a boolean topic.
166 *
167 * @param name topic name
168 * @return Topic
169 */
170 BooleanTopic GetBooleanTopic(std::string_view name) const;
171
172 /**
173 * Gets an integer topic.
174 *
175 * @param name topic name
176 * @return Topic
177 */
178 IntegerTopic GetIntegerTopic(std::string_view name) const;
179
180 /**
181 * Gets a float topic.
182 *
183 * @param name topic name
184 * @return Topic
185 */
186 FloatTopic GetFloatTopic(std::string_view name) const;
187
188 /**
189 * Gets a double topic.
190 *
191 * @param name topic name
192 * @return Topic
193 */
194 DoubleTopic GetDoubleTopic(std::string_view name) const;
195
196 /**
197 * Gets a string topic.
198 *
199 * @param name topic name
200 * @return Topic
201 */
202 StringTopic GetStringTopic(std::string_view name) const;
203
204 /**
205 * Gets a raw topic.
206 *
207 * @param name topic name
208 * @return Topic
209 */
210 RawTopic GetRawTopic(std::string_view name) const;
211
212 /**
213 * Gets a boolean array topic.
214 *
215 * @param name topic name
216 * @return Topic
217 */
219
220 /**
221 * Gets an integer array topic.
222 *
223 * @param name topic name
224 * @return Topic
225 */
227
228 /**
229 * Gets a float array topic.
230 *
231 * @param name topic name
232 * @return Topic
233 */
234 FloatArrayTopic GetFloatArrayTopic(std::string_view name) const;
235
236 /**
237 * Gets a double array topic.
238 *
239 * @param name topic name
240 * @return Topic
241 */
243
244 /**
245 * Gets a string array topic.
246 *
247 * @param name topic name
248 * @return Topic
249 */
251
252 /**
253 * Gets a protobuf serialized value topic.
254 *
255 * @param name topic name
256 * @return Topic
257 */
258 template <wpi::util::ProtobufSerializable T>
259 ProtobufTopic<T> GetProtobufTopic(std::string_view name) const {
261 }
262
263 /**
264 * Gets a raw struct serialized value topic.
265 *
266 * @param name topic name
267 * @param info optional struct type info
268 * @return Topic
269 */
270 template <typename T, typename... I>
271 requires wpi::util::StructSerializable<T, I...>
272 StructTopic<T, I...> GetStructTopic(std::string_view name, I... info) const {
273 return StructTopic<T, I...>{GetTopic(name), std::move(info)...};
274 }
275
276 /**
277 * Gets a raw struct serialized array topic.
278 *
279 * @param name topic name
280 * @param info optional struct type info
281 * @return Topic
282 */
283 template <typename T, typename... I>
284 requires wpi::util::StructSerializable<T, I...>
285 StructArrayTopic<T, I...> GetStructArrayTopic(std::string_view name,
286 I... info) const {
287 return StructArrayTopic<T, I...>{GetTopic(name), std::move(info)...};
288 }
289
290 /**
291 * Get Published Topics.
292 *
293 * Returns an array of topics.
294 *
295 * @return Array of topics.
296 */
297 std::vector<Topic> GetTopics() {
298 auto handles = ::wpi::nt::GetTopics(m_handle, "", 0);
299 return {handles.begin(), handles.end()};
300 }
301
302 /**
303 * Get Published Topics.
304 *
305 * Returns an array of topics. The results are filtered by
306 * string prefix to only return a subset of all topics.
307 *
308 * @param prefix name required prefix; only topics whose name
309 * starts with this string are returned
310 * @return Array of topics.
311 */
312 std::vector<Topic> GetTopics(std::string_view prefix) {
313 auto handles = ::wpi::nt::GetTopics(m_handle, prefix, 0);
314 return {handles.begin(), handles.end()};
315 }
316
317 /**
318 * Get Published Topics.
319 *
320 * Returns an array of topics. The results are filtered by
321 * string prefix and type to only return a subset of all topics.
322 *
323 * @param prefix name required prefix; only topics whose name
324 * starts with this string are returned
325 * @param types bitmask of NT_Type values; 0 is treated specially
326 * as a "don't care"
327 * @return Array of topics.
328 */
329 std::vector<Topic> GetTopics(std::string_view prefix, unsigned int types) {
330 auto handles = ::wpi::nt::GetTopics(m_handle, prefix, types);
331 return {handles.begin(), handles.end()};
332 }
333
334 /**
335 * Get Published Topics.
336 *
337 * Returns an array of topics. The results are filtered by
338 * string prefix and type to only return a subset of all topics.
339 *
340 * @param prefix name required prefix; only topics whose name
341 * starts with this string are returned
342 * @param types array of type strings
343 * @return Array of topic handles.
344 */
345 std::vector<Topic> GetTopics(std::string_view prefix,
346 std::span<std::string_view> types) {
347 auto handles = ::wpi::nt::GetTopics(m_handle, prefix, types);
348 return {handles.begin(), handles.end()};
349 }
350
351 /**
352 * Get Topic Information about multiple topics.
353 *
354 * Returns an array of topic information (handle, name, type, and properties).
355 *
356 * @return Array of topic information.
357 */
358 std::vector<TopicInfo> GetTopicInfo() {
359 return ::wpi::nt::GetTopicInfo(m_handle, "", 0);
360 }
361
362 /**
363 * Get Topic Information about multiple topics.
364 *
365 * Returns an array of topic information (handle, name, type, and properties).
366 * The results are filtered by string prefix to only
367 * return a subset of all topics.
368 *
369 * @param prefix name required prefix; only topics whose name
370 * starts with this string are returned
371 * @return Array of topic information.
372 */
373 std::vector<TopicInfo> GetTopicInfo(std::string_view prefix) {
374 return ::wpi::nt::GetTopicInfo(m_handle, prefix, 0);
375 }
376
377 /**
378 * Get Topic Information about multiple topics.
379 *
380 * Returns an array of topic information (handle, name, type, and properties).
381 * The results are filtered by string prefix and type to only
382 * return a subset of all topics.
383 *
384 * @param prefix name required prefix; only topics whose name
385 * starts with this string are returned
386 * @param types bitmask of NT_Type values; 0 is treated specially
387 * as a "don't care"
388 * @return Array of topic information.
389 */
390 std::vector<TopicInfo> GetTopicInfo(std::string_view prefix,
391 unsigned int types) {
392 return ::wpi::nt::GetTopicInfo(m_handle, prefix, types);
393 }
394
395 /**
396 * Get Topic Information about multiple topics.
397 *
398 * Returns an array of topic information (handle, name, type, and properties).
399 * The results are filtered by string prefix and type to only
400 * return a subset of all topics.
401 *
402 * @param prefix name required prefix; only topics whose name
403 * starts with this string are returned
404 * @param types array of type strings
405 * @return Array of topic information.
406 */
407 std::vector<TopicInfo> GetTopicInfo(std::string_view prefix,
408 std::span<std::string_view> types) {
409 return ::wpi::nt::GetTopicInfo(m_handle, prefix, types);
410 }
411
412 /**
413 * Gets the entry for a key.
414 *
415 * @param name Key
416 * @return Network table entry.
417 */
418 NetworkTableEntry GetEntry(std::string_view name) {
419 return NetworkTableEntry{::wpi::nt::GetEntry(m_handle, name)};
420 }
421
422 /**
423 * Gets the table with the specified key.
424 *
425 * @param key the key name
426 * @return The network table
427 */
428 std::shared_ptr<NetworkTable> GetTable(std::string_view key) const;
429
430 /**
431 * @{
432 * @name Listener Functions
433 */
434
435 /**
436 * Remove a listener.
437 *
438 * @param listener Listener handle to remove
439 */
440 static void RemoveListener(NT_Listener listener) {
442 }
443
444 /**
445 * Wait for the listener queue to be empty. This is primarily
446 * useful for deterministic testing. This blocks until either the
447 * listener queue is empty (e.g. there are no more events that need to be
448 * passed along to callbacks or poll queues) or the timeout expires.
449 *
450 * @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or
451 * a negative value to block indefinitely
452 * @return False if timed out, otherwise true.
453 */
454 bool WaitForListenerQueue(double timeout) {
455 return ::wpi::nt::WaitForListenerQueue(m_handle, timeout);
456 }
457
458 /**
459 * Add a connection listener. The callback function is called asynchronously
460 * on a separate thread, so it's important to use synchronization or atomics
461 * when accessing any shared state from the callback function.
462 *
463 * @param immediate_notify notify listener of all existing connections
464 * @param callback listener to add
465 * @return Listener handle
466 */
467 NT_Listener AddConnectionListener(bool immediate_notify,
468 ListenerCallback callback) const {
469 return ::wpi::nt::AddListener(
470 m_handle,
471 NT_EVENT_CONNECTION | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
472 std::move(callback));
473 }
474
475 /**
476 * Add a time synchronization listener. The callback function is called
477 * asynchronously on a separate thread, so it's important to use
478 * synchronization or atomics when accessing any shared state from the
479 * callback function.
480 *
481 * @param immediate_notify notify listener of current time synchronization
482 * value
483 * @param callback listener to add
484 * @return Listener handle
485 */
486 NT_Listener AddTimeSyncListener(bool immediate_notify,
487 ListenerCallback callback) const {
488 return ::wpi::nt::AddListener(
489 m_handle,
490 NT_EVENT_TIMESYNC | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
491 std::move(callback));
492 }
493
494 /**
495 * Add a listener for changes on a particular topic. The callback
496 * function is called asynchronously on a separate thread, so it's important
497 * to use synchronization or atomics when accessing any shared state from the
498 * callback function.
499 *
500 * This creates a corresponding internal subscriber with the lifetime of the
501 * listener.
502 *
503 * @param topic Topic
504 * @param eventMask Bitmask of EventFlags values
505 * @param listener Listener function
506 * @return Listener handle
507 */
508 NT_Listener AddListener(Topic topic, unsigned int eventMask,
509 ListenerCallback listener);
510
511 /**
512 * Add a listener for changes on a subscriber. The callback
513 * function is called asynchronously on a separate thread, so it's important
514 * to use synchronization or atomics when accessing any shared state from the
515 * callback function. This does NOT keep the subscriber active.
516 *
517 * @param subscriber Subscriber
518 * @param eventMask Bitmask of EventFlags values
519 * @param listener Listener function
520 * @return Listener handle
521 */
522 NT_Listener AddListener(Subscriber& subscriber, unsigned int eventMask,
523 ListenerCallback listener);
524
525 /**
526 * Add a listener for changes on a subscriber. The callback
527 * function is called asynchronously on a separate thread, so it's important
528 * to use synchronization or atomics when accessing any shared state from the
529 * callback function. This does NOT keep the subscriber active.
530 *
531 * @param subscriber Subscriber
532 * @param eventMask Bitmask of EventFlags values
533 * @param listener Listener function
534 * @return Listener handle
535 */
536 NT_Listener AddListener(MultiSubscriber& subscriber, int eventMask,
537 ListenerCallback listener);
538
539 /**
540 * Add a listener for changes on an entry. The callback function
541 * is called asynchronously on a separate thread, so it's important to use
542 * synchronization or atomics when accessing any shared state from the
543 * callback function.
544 *
545 * @param entry Entry
546 * @param eventMask Bitmask of EventFlags values
547 * @param listener Listener function
548 * @return Listener handle
549 */
550 NT_Listener AddListener(const NetworkTableEntry& entry, int eventMask,
551 ListenerCallback listener);
552
553 /**
554 * Add a listener for changes to topics with names that start with any
555 * of the given prefixes. The callback function is called asynchronously on a
556 * separate thread, so it's important to use synchronization or atomics when
557 * accessing any shared state from the callback function.
558 *
559 * This creates a corresponding internal subscriber with the lifetime of the
560 * listener.
561 *
562 * @param prefixes Topic name string prefixes
563 * @param eventMask Bitmask of EventFlags values
564 * @param listener Listener function
565 * @return Listener handle
566 */
567 NT_Listener AddListener(std::span<const std::string_view> prefixes,
568 int eventMask, ListenerCallback listener) {
569 return ::wpi::nt::AddListener(m_handle, prefixes, eventMask,
570 std::move(listener));
571 }
572
573 /** @} */
574
575 /**
576 * @{
577 * @name Client/Server Functions
578 */
579
580 /**
581 * Get the current network mode.
582 *
583 * @return Bitmask of NetworkMode.
584 */
585 unsigned int GetNetworkMode() const {
586 return ::wpi::nt::GetNetworkMode(m_handle);
587 }
588
589 /**
590 * Starts local-only operation. Prevents calls to StartServer or StartClient
591 * from taking effect. Has no effect if StartServer or StartClient
592 * has already been called.
593 */
594 void StartLocal() { ::wpi::nt::StartLocal(m_handle); }
595
596 /**
597 * Stops local-only operation. StartServer or StartClient can be called after
598 * this call to start a server or client.
599 */
600 void StopLocal() { ::wpi::nt::StopLocal(m_handle); }
601
602 /**
603 * Starts a server using the specified filename, listening address, and port.
604 *
605 * @param persist_filename the name of the persist file to use (UTF-8 string,
606 * null terminated)
607 * @param listen_address the address to listen on, or an empty string to
608 * listen on any address. (UTF-8 string, null
609 * terminated)
610 * @param mdns_service the mDNS service name to announce, or an empty
611 * string to not announce via mDNS (UTF-8 string,
612 * null terminated)
613 * @param port port to communicate over
614 */
615 void StartServer(std::string_view persist_filename = "networktables.json",
616 std::string_view listen_address = "",
617 std::string_view mdns_service = "",
618 unsigned int port = kDefaultPort) {
619 ::wpi::nt::StartServer(m_handle, persist_filename, listen_address,
620 mdns_service, port);
621 }
622
623 /**
624 * Stops the server if it is running.
625 */
626 void StopServer() { ::wpi::nt::StopServer(m_handle); }
627
628 /**
629 * Starts a client. Use SetServer or SetServerTeam to set the server name
630 * and port.
631 *
632 * @param identity network identity to advertise (cannot be empty string)
633 */
634 void StartClient(std::string_view identity) {
635 ::wpi::nt::StartClient(m_handle, identity);
636 }
637
638 /**
639 * Stops the client if it is running.
640 */
641 void StopClient() { ::wpi::nt::StopClient(m_handle); }
642
643 /**
644 * Sets server address and port for client (without restarting client).
645 *
646 * @param server_name server name (UTF-8 string)
647 * @param port port to communicate over (0 = default)
648 */
649 void SetServer(std::string_view server_name, unsigned int port = 0) {
650 ::wpi::nt::SetServer(m_handle, server_name, port);
651 }
652
653 /**
654 * Sets server addresses and ports for client (without restarting client).
655 * The client will attempt to connect to each server in round robin fashion.
656 *
657 * @param servers array of server address and port pairs
658 */
660 std::span<const std::pair<std::string_view, unsigned int>> servers) {
661 ::wpi::nt::SetServer(m_handle, servers);
662 }
663
664 /**
665 * Sets server addresses and port for client (without restarting client).
666 * The client will attempt to connect to each server in round robin fashion.
667 *
668 * @param servers array of server names
669 * @param port port to communicate over (0 = default)
670 */
671 void SetServer(std::span<const std::string_view> servers,
672 unsigned int port = 0);
673
674 /**
675 * Sets server addresses and port for client (without restarting client).
676 * Connects using commonly known robot addresses for the specified team.
677 *
678 * @param team team number
679 * @param port port to communicate over (0 = default)
680 */
681 void SetServerTeam(unsigned int team, unsigned int port = 0) {
682 ::wpi::nt::SetServerTeam(m_handle, team, port);
683 }
684
685 /**
686 * Disconnects the client if it's running and connected. This will
687 * automatically start reconnection attempts to the current server list.
688 */
689 void Disconnect() { ::wpi::nt::Disconnect(m_handle); }
690
691 /**
692 * Starts requesting server address from Driver Station.
693 * This connects to the Driver Station running on localhost to obtain the
694 * server IP address.
695 *
696 * @param port server port to use in combination with IP from DS (0 = default)
697 */
698 void StartDSClient(unsigned int port = 0) {
699 ::wpi::nt::StartDSClient(m_handle, port);
700 }
701
702 /**
703 * Stops requesting server address from Driver Station.
704 */
706
707 /**
708 * Flushes all updated values immediately to the local client/server. This
709 * does not flush to the network.
710 */
711 void FlushLocal() const { ::wpi::nt::FlushLocal(m_handle); }
712
713 /**
714 * Flushes all updated values immediately to the network.
715 * @note This is rate-limited to protect the network from flooding.
716 * This is primarily useful for synchronizing network updates with
717 * user code.
718 */
719 void Flush() const { ::wpi::nt::Flush(m_handle); }
720
721 /**
722 * Get information on the currently established network connections.
723 * If operating as a client, this will return either zero or one values.
724 *
725 * @return array of connection information
726 */
727 std::vector<ConnectionInfo> GetConnections() const {
728 return ::wpi::nt::GetConnections(m_handle);
729 }
730
731 /**
732 * Return whether or not the instance is connected to another node.
733 *
734 * @return True if connected.
735 */
736 bool IsConnected() const { return ::wpi::nt::IsConnected(m_handle); }
737
738 /**
739 * Get the time offset between server time and local time. Add this value to
740 * local time to get the estimated equivalent server time. In server mode,
741 * this always returns 0. In client mode, this returns the time offset only if
742 * the client and server are connected and have exchanged synchronization
743 * messages. Note the time offset may change over time as it is periodically
744 * updated; to receive updates as events, add a listener to the "time sync"
745 * event.
746 *
747 * @return Time offset in microseconds (optional)
748 */
749 std::optional<int64_t> GetServerTimeOffset() const {
750 return ::wpi::nt::GetServerTimeOffset(m_handle);
751 }
752
753 /** @} */
754
755 /**
756 * @{
757 * @name Data wpi::util::Logger Functions
758 */
759
760 /**
761 * Starts logging entry changes to a DataLog.
762 *
763 * @param log data log object; lifetime must extend until StopEntryDataLog is
764 * called or the instance is destroyed
765 * @param prefix only store entries with names that start with this prefix;
766 * the prefix is not included in the data log entry name
767 * @param logPrefix prefix to add to data log entry names
768 * @return Data logger handle
769 */
771 std::string_view prefix,
772 std::string_view logPrefix) {
773 return ::wpi::nt::StartEntryDataLog(m_handle, log, prefix, logPrefix);
774 }
775
776 /**
777 * Stops logging entry changes to a DataLog.
778 *
779 * @param logger data logger handle
780 */
781 static void StopEntryDataLog(NT_DataLogger logger) {
783 }
784
785 /**
786 * Starts logging connection changes to a DataLog.
787 *
788 * @param log data log object; lifetime must extend until
789 * StopConnectionDataLog is called or the instance is destroyed
790 * @param name data log entry name
791 * @return Data logger handle
792 */
794 std::string_view name) {
795 return ::wpi::nt::StartConnectionDataLog(m_handle, log, name);
796 }
797
798 /**
799 * Stops logging connection changes to a DataLog.
800 *
801 * @param logger data logger handle
802 */
806
807 /** @} */
808
809 /**
810 * @{
811 * @name wpi::util::Logger Functions
812 */
813
814 /**
815 * Add logger callback function. By default, log messages are sent to stderr;
816 * this function sends log messages with the specified levels to the provided
817 * callback function instead. The callback function will only be called for
818 * log messages with level greater than or equal to minLevel and less than or
819 * equal to maxLevel; messages outside this range will be silently ignored.
820 *
821 * @param minLevel minimum log level
822 * @param maxLevel maximum log level
823 * @param func callback function
824 * @return Listener handle
825 */
826 NT_Listener AddLogger(unsigned int minLevel, unsigned int maxLevel,
827 ListenerCallback func) {
828 return ::wpi::nt::AddLogger(m_handle, minLevel, maxLevel, std::move(func));
829 }
830
831 /** @} */
832
833 /**
834 * @{
835 * @name Schema Functions
836 */
837
838 /**
839 * Returns whether there is a data schema already registered with the given
840 * name. This does NOT perform a check as to whether the schema has already
841 * been published by another node on the network.
842 *
843 * @param name Name (the string passed as the data type for topics using this
844 * schema)
845 * @return True if schema already registered
846 */
847 bool HasSchema(std::string_view name) const {
848 return ::wpi::nt::HasSchema(m_handle, name);
849 }
850
851 /**
852 * Registers a data schema. Data schemas provide information for how a
853 * certain data type string can be decoded. The type string of a data schema
854 * indicates the type of the schema itself (e.g. "protobuf" for protobuf
855 * schemas, "struct" for struct schemas, etc). In NetworkTables, schemas are
856 * published just like normal topics, with the name being generated from the
857 * provided name: "/.schema/<name>". Duplicate calls to this function with
858 * the same name are silently ignored.
859 *
860 * @param name Name (the string passed as the data type for topics using this
861 * schema)
862 * @param type Type of schema (e.g. "protobuf", "struct", etc)
863 * @param schema Schema data
864 */
865 void AddSchema(std::string_view name, std::string_view type,
866 std::span<const uint8_t> schema) {
867 ::wpi::nt::AddSchema(m_handle, name, type, schema);
868 }
869
870 /**
871 * Registers a data schema. Data schemas provide information for how a
872 * certain data type string can be decoded. The type string of a data schema
873 * indicates the type of the schema itself (e.g. "protobuf" for protobuf
874 * schemas, "struct" for struct schemas, etc). In NetworkTables, schemas are
875 * published just like normal topics, with the name being generated from the
876 * provided name: "/.schema/<name>". Duplicate calls to this function with
877 * the same name are silently ignored.
878 *
879 * @param name Name (the string passed as the data type for topics using this
880 * schema)
881 * @param type Type of schema (e.g. "protobuf", "struct", etc)
882 * @param schema Schema data
883 */
884 void AddSchema(std::string_view name, std::string_view type,
885 std::string_view schema) {
886 ::wpi::nt::AddSchema(m_handle, name, type, schema);
887 }
888
889// Suppress unused-lambda-capture warning on AddSchema() call
890#ifdef __clang__
891#pragma clang diagnostic push
892#pragma clang diagnostic ignored "-Wunused-lambda-capture"
893#endif
894
895 /**
896 * Registers a protobuf schema. Duplicate calls to this function with the same
897 * name are silently ignored.
898 *
899 * @tparam T protobuf serializable type
900 * @param msg protobuf message
901 */
902 template <wpi::util::ProtobufSerializable T>
905 [this](auto typeString) { return HasSchema(typeString); },
906 [this](auto typeString, auto schema) {
907 AddSchema(typeString, "proto:FileDescriptorProto", schema);
908 });
909 }
910
911 /**
912 * Registers a struct schema. Duplicate calls to this function with the same
913 * name are silently ignored.
914 *
915 * @tparam T struct serializable type
916 * @param info optional struct type info
917 */
918 template <typename T, typename... I>
919 requires wpi::util::StructSerializable<T, I...>
920 void AddStructSchema(const I&... info) {
922 [this](auto typeString, auto schema) {
923 AddSchema(typeString, "structschema", schema);
924 },
925 info...);
926 }
927
928#ifdef __clang__
929#pragma clang diagnostic pop
930#endif
931
932 /**
933 * Equality operator. Returns true if both instances refer to the same
934 * native handle.
935 */
936 bool operator==(const NetworkTableInstance&) const = default;
937
938 private:
939 /* Native handle */
940 NT_Inst m_handle{0};
941};
942
943} // namespace wpi::nt
@ name
Definition base.h:690
A data log for high-speed writing of data values.
Definition DataLog.hpp:69
NetworkTables BooleanArray topic.
Definition BooleanArrayTopic.hpp:296
NetworkTables Boolean topic.
Definition BooleanTopic.hpp:235
NetworkTables DoubleArray topic.
Definition DoubleArrayTopic.hpp:296
NetworkTables Double topic.
Definition DoubleTopic.hpp:235
NetworkTables FloatArray topic.
Definition FloatArrayTopic.hpp:296
NetworkTables Float topic.
Definition FloatTopic.hpp:235
NetworkTables IntegerArray topic.
Definition IntegerArrayTopic.hpp:296
NetworkTables Integer topic.
Definition IntegerTopic.hpp:235
Subscribe to multiple topics based on one or more topic name prefixes.
Definition MultiSubscriber.hpp:20
NetworkTables Entry.
Definition NetworkTableEntry.hpp:31
void StartServer(std::string_view persist_filename="networktables.json", std::string_view listen_address="", std::string_view mdns_service="", unsigned int port=kDefaultPort)
Starts a server using the specified filename, listening address, and port.
Definition NetworkTableInstance.hpp:615
bool WaitForListenerQueue(double timeout)
Wait for the listener queue to be empty.
Definition NetworkTableInstance.hpp:454
static void StopConnectionDataLog(NT_ConnectionDataLogger logger)
Stops logging connection changes to a DataLog.
Definition NetworkTableInstance.hpp:803
void SetServer(std::span< const std::string_view > servers, unsigned int port=0)
Sets server addresses and port for client (without restarting client).
void AddSchema(std::string_view name, std::string_view type, std::span< const uint8_t > schema)
Registers a data schema.
Definition NetworkTableInstance.hpp:865
Topic GetTopic(std::string_view name) const
Gets a "generic" (untyped) topic.
NT_Listener AddListener(Subscriber &subscriber, unsigned int eventMask, ListenerCallback listener)
Add a listener for changes on a subscriber.
void SetServerTeam(unsigned int team, unsigned int port=0)
Sets server addresses and port for client (without restarting client).
Definition NetworkTableInstance.hpp:681
static constexpr unsigned int kDefaultPort
The default port that network tables operates on.
Definition NetworkTableInstance.hpp:98
std::vector< Topic > GetTopics()
Get Published Topics.
Definition NetworkTableInstance.hpp:297
StringArrayTopic GetStringArrayTopic(std::string_view name) const
Gets a string array topic.
BooleanTopic GetBooleanTopic(std::string_view name) const
Gets a boolean topic.
void Disconnect()
Disconnects the client if it's running and connected.
Definition NetworkTableInstance.hpp:689
bool operator==(const NetworkTableInstance &) const =default
Equality operator.
NT_Listener AddListener(const NetworkTableEntry &entry, int eventMask, ListenerCallback listener)
Add a listener for changes on an entry.
void AddStructSchema(const I &... info)
Registers a struct schema.
Definition NetworkTableInstance.hpp:920
void SetServer(std::span< const std::pair< std::string_view, unsigned int > > servers)
Sets server addresses and ports for client (without restarting client).
Definition NetworkTableInstance.hpp:659
NT_Listener AddConnectionListener(bool immediate_notify, ListenerCallback callback) const
Add a connection listener.
Definition NetworkTableInstance.hpp:467
void StartClient(std::string_view identity)
Starts a client.
Definition NetworkTableInstance.hpp:634
NetworkMode
Client/server mode flag values (as returned by GetNetworkMode()).
Definition NetworkTableInstance.hpp:73
@ kNetModeClient
Definition NetworkTableInstance.hpp:76
@ kNetModeLocal
Definition NetworkTableInstance.hpp:77
@ kNetModeNone
Definition NetworkTableInstance.hpp:74
@ kNetModeServer
Definition NetworkTableInstance.hpp:75
void StopServer()
Stops the server if it is running.
Definition NetworkTableInstance.hpp:626
DoubleTopic GetDoubleTopic(std::string_view name) const
Gets a double topic.
void StopClient()
Stops the client if it is running.
Definition NetworkTableInstance.hpp:641
FloatArrayTopic GetFloatArrayTopic(std::string_view name) const
Gets a float array topic.
FloatTopic GetFloatTopic(std::string_view name) const
Gets a float topic.
std::vector< Topic > GetTopics(std::string_view prefix)
Get Published Topics.
Definition NetworkTableInstance.hpp:312
NT_Inst GetHandle() const
Gets the native handle for the entry.
Definition NetworkTableInstance.hpp:154
std::vector< TopicInfo > GetTopicInfo()
Get Topic Information about multiple topics.
Definition NetworkTableInstance.hpp:358
void Flush() const
Flushes all updated values immediately to the network.
Definition NetworkTableInstance.hpp:719
static void RemoveListener(NT_Listener listener)
Remove a listener.
Definition NetworkTableInstance.hpp:440
NT_Listener AddLogger(unsigned int minLevel, unsigned int maxLevel, ListenerCallback func)
Add logger callback function.
Definition NetworkTableInstance.hpp:826
DoubleArrayTopic GetDoubleArrayTopic(std::string_view name) const
Gets a double array topic.
std::shared_ptr< NetworkTable > GetTable(std::string_view key) const
Gets the table with the specified key.
std::vector< TopicInfo > GetTopicInfo(std::string_view prefix, std::span< std::string_view > types)
Get Topic Information about multiple topics.
Definition NetworkTableInstance.hpp:407
static void StopEntryDataLog(NT_DataLogger logger)
Stops logging entry changes to a DataLog.
Definition NetworkTableInstance.hpp:781
NetworkTableInstance() noexcept=default
Construct invalid instance.
IntegerTopic GetIntegerTopic(std::string_view name) const
Gets an integer topic.
ProtobufTopic< T > GetProtobufTopic(std::string_view name) const
Gets a protobuf serialized value topic.
Definition NetworkTableInstance.hpp:259
StructTopic< T, I... > GetStructTopic(std::string_view name, I... info) const
Gets a raw struct serialized value topic.
Definition NetworkTableInstance.hpp:272
unsigned int GetNetworkMode() const
Get the current network mode.
Definition NetworkTableInstance.hpp:585
StringTopic GetStringTopic(std::string_view name) const
Gets a string topic.
NT_DataLogger StartEntryDataLog(wpi::log::DataLog &log, std::string_view prefix, std::string_view logPrefix)
Starts logging entry changes to a DataLog.
Definition NetworkTableInstance.hpp:770
std::vector< ConnectionInfo > GetConnections() const
Get information on the currently established network connections.
Definition NetworkTableInstance.hpp:727
std::vector< TopicInfo > GetTopicInfo(std::string_view prefix)
Get Topic Information about multiple topics.
Definition NetworkTableInstance.hpp:373
NT_Listener AddListener(std::span< const std::string_view > prefixes, int eventMask, ListenerCallback listener)
Add a listener for changes to topics with names that start with any of the given prefixes.
Definition NetworkTableInstance.hpp:567
std::vector< Topic > GetTopics(std::string_view prefix, unsigned int types)
Get Published Topics.
Definition NetworkTableInstance.hpp:329
void StopLocal()
Stops local-only operation.
Definition NetworkTableInstance.hpp:600
void StartLocal()
Starts local-only operation.
Definition NetworkTableInstance.hpp:594
bool HasSchema(std::string_view name) const
Returns whether there is a data schema already registered with the given name.
Definition NetworkTableInstance.hpp:847
static void Destroy(NetworkTableInstance &inst)
Destroys an instance (note: this has global effect).
Definition NetworkTableInstance.hpp:142
NetworkTableEntry GetEntry(std::string_view name)
Gets the entry for a key.
Definition NetworkTableInstance.hpp:418
NT_Listener AddTimeSyncListener(bool immediate_notify, ListenerCallback callback) const
Add a time synchronization listener.
Definition NetworkTableInstance.hpp:486
RawTopic GetRawTopic(std::string_view name) const
Gets a raw topic.
void AddSchema(std::string_view name, std::string_view type, std::string_view schema)
Registers a data schema.
Definition NetworkTableInstance.hpp:884
NT_ConnectionDataLogger StartConnectionDataLog(wpi::log::DataLog &log, std::string_view name)
Starts logging connection changes to a DataLog.
Definition NetworkTableInstance.hpp:793
static NetworkTableInstance GetDefault()
Get global default instance.
Definition NetworkTableInstance.hpp:124
bool IsConnected() const
Return whether or not the instance is connected to another node.
Definition NetworkTableInstance.hpp:736
void StartDSClient(unsigned int port=0)
Starts requesting server address from Driver Station.
Definition NetworkTableInstance.hpp:698
NT_Listener AddListener(MultiSubscriber &subscriber, int eventMask, ListenerCallback listener)
Add a listener for changes on a subscriber.
BooleanArrayTopic GetBooleanArrayTopic(std::string_view name) const
Gets a boolean array topic.
static NetworkTableInstance Create()
Create an instance.
Definition NetworkTableInstance.hpp:133
void SetServer(std::string_view server_name, unsigned int port=0)
Sets server address and port for client (without restarting client).
Definition NetworkTableInstance.hpp:649
NT_Listener AddListener(Topic topic, unsigned int eventMask, ListenerCallback listener)
Add a listener for changes on a particular topic.
IntegerArrayTopic GetIntegerArrayTopic(std::string_view name) const
Gets an integer array topic.
LogLevel
Logging levels (as used by SetLogger()).
Definition NetworkTableInstance.hpp:83
@ kLogCritical
Definition NetworkTableInstance.hpp:84
@ kLogDebug3
Definition NetworkTableInstance.hpp:91
@ kLogDebug2
Definition NetworkTableInstance.hpp:90
@ kLogError
Definition NetworkTableInstance.hpp:85
@ kLogDebug
Definition NetworkTableInstance.hpp:88
@ kLogInfo
Definition NetworkTableInstance.hpp:87
@ kLogWarning
Definition NetworkTableInstance.hpp:86
@ kLogDebug4
Definition NetworkTableInstance.hpp:92
@ kLogDebug1
Definition NetworkTableInstance.hpp:89
StructArrayTopic< T, I... > GetStructArrayTopic(std::string_view name, I... info) const
Gets a raw struct serialized array topic.
Definition NetworkTableInstance.hpp:285
std::vector< TopicInfo > GetTopicInfo(std::string_view prefix, unsigned int types)
Get Topic Information about multiple topics.
Definition NetworkTableInstance.hpp:390
void StopDSClient()
Stops requesting server address from Driver Station.
Definition NetworkTableInstance.hpp:705
std::optional< int64_t > GetServerTimeOffset() const
Get the time offset between server time and local time.
Definition NetworkTableInstance.hpp:749
void AddProtobufSchema(wpi::util::ProtobufMessage< T > &msg)
Registers a protobuf schema.
Definition NetworkTableInstance.hpp:903
std::vector< Topic > GetTopics(std::string_view prefix, std::span< std::string_view > types)
Get Published Topics.
Definition NetworkTableInstance.hpp:345
void FlushLocal() const
Flushes all updated values immediately to the local client/server.
Definition NetworkTableInstance.hpp:711
NetworkTables protobuf-encoded value topic.
Definition ProtobufTopic.hpp:336
NetworkTables Raw topic.
Definition RawTopic.hpp:296
NetworkTables StringArray topic.
Definition StringArrayTopic.hpp:235
NetworkTables String topic.
Definition StringTopic.hpp:298
NetworkTables struct-encoded value array topic.
Definition StructArrayTopic.hpp:466
NetworkTables struct-encoded value topic.
Definition StructTopic.hpp:372
NetworkTables subscriber.
Definition Topic.hpp:320
NetworkTables Topic.
Definition Topic.hpp:27
Ease of use wrapper to make nanopb streams more opaque to the user.
Definition Protobuf.hpp:308
void ForEachProtobufDescriptor(function_ref< bool(std::string_view filename)> exists, function_ref< void(std::string_view filename, std::span< const uint8_t > descriptor)> fn)
Loops over all protobuf descriptors including nested/referenced descriptors.
Definition Protobuf.hpp:384
Specifies that a type is capable of raw struct serialization and deserialization.
Definition Struct.hpp:69
NT_Handle NT_ConnectionDataLogger
Definition ntcore_c.h:35
NT_Handle NT_Listener
Definition ntcore_c.h:39
NT_Handle NT_Inst
Definition ntcore_c.h:38
#define NT_DEFAULT_PORT
Default network tables port number.
Definition ntcore_c.h:47
NT_Handle NT_DataLogger
Definition ntcore_c.h:36
@ NT_LOG_DEBUG4
Definition ntcore_c.h:83
@ NT_LOG_WARNING
Definition ntcore_c.h:77
@ NT_LOG_INFO
Definition ntcore_c.h:78
@ NT_LOG_DEBUG2
Definition ntcore_c.h:81
@ NT_LOG_DEBUG
Definition ntcore_c.h:79
@ NT_LOG_CRITICAL
Definition ntcore_c.h:75
@ NT_LOG_ERROR
Definition ntcore_c.h:76
@ NT_LOG_DEBUG3
Definition ntcore_c.h:82
@ NT_LOG_DEBUG1
Definition ntcore_c.h:80
@ NT_NET_MODE_LOCAL
Definition ntcore_c.h:92
@ NT_NET_MODE_CLIENT
Definition ntcore_c.h:90
@ NT_NET_MODE_SERVER
Definition ntcore_c.h:89
@ NT_NET_MODE_NONE
Definition ntcore_c.h:88
@ NT_EVENT_TIMESYNC
Time synchronized with server.
Definition ntcore_c.h:124
@ NT_EVENT_CONNECTION
Any connection event (connect or disconnect).
Definition ntcore_c.h:106
@ NT_EVENT_IMMEDIATE
Initial listener addition.
Definition ntcore_c.h:100
void StopConnectionDataLog(NT_ConnectionDataLogger logger)
Stops logging connection changes to a DataLog.
void StopEntryDataLog(NT_DataLogger logger)
Stops logging entry changes to a DataLog.
void DestroyInstance(NT_Inst inst)
Destroy an instance.
NT_Inst CreateInstance()
Create an instance.
NT_Inst GetDefaultInstance()
Get default instance.
void RemoveListener(NT_Listener listener)
Removes a listener.
std::function< void(const Event &)> ListenerCallback
Definition ntcore_cpp.hpp:897
void StartLocal(NT_Inst inst)
Starts local-only operation.
void SetServer(NT_Inst inst, std::string_view server_name, unsigned int port)
Sets server address and port for client (without restarting client).
void StopLocal(NT_Inst inst)
Stops local-only operation.
void StartClient(NT_Inst inst, std::string_view identity)
Starts a client.
void StartDSClient(NT_Inst inst, unsigned int port)
Starts requesting server address from Driver Station.
void FlushLocal(NT_Inst inst)
Flush local updates.
void StopClient(NT_Inst inst)
Stops the client if it is running.
void Flush(NT_Inst inst)
Flush to network.
void SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port)
Sets server addresses and port for client (without restarting client).
void StopServer(NT_Inst inst)
Stops the server if it is running.
void StartServer(NT_Inst inst, std::string_view persist_filename, std::string_view listen_address, std::string_view mdns_service, unsigned int port)
Starts a server using the specified filename, listening address, and port.
void StopDSClient(NT_Inst inst)
Stops requesting server address from Driver Station.
void Disconnect(NT_Inst inst)
Disconnects the client if it's running and connected.
void AddSchema(NT_Inst inst, std::string_view name, std::string_view type, std::span< const uint8_t > schema)
Registers a data schema.
NT_Entry GetEntry(NT_Inst inst, std::string_view name)
Get Entry Handle.
std::vector< NT_Topic > GetTopics(NT_Inst inst, std::string_view prefix, unsigned int types)
Get Published Topics.
Definition DataLogReader.hpp:17
NetworkTables (ntcore) namespace.
Definition NTSendable.hpp:9
void ForEachStructSchema(std::invocable< std::string_view, std::string_view > auto fn, const I &... info)
Definition Struct.hpp:424