WPILibC++ 2025.0.0-alpha-1-14-g3b6f38d
NetworkTableListener.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 <span>
9#include <string_view>
10#include <vector>
11
12#include "ntcore_cpp.h"
13
14namespace nt {
15
16class MultiSubscriber;
17class NetworkTableEntry;
18class NetworkTableInstance;
19class Subscriber;
20class Topic;
21
22/**
23 * Event listener. This calls back to a callback function when an event
24 * matching the specified mask occurs. The callback function is called
25 * asynchronously on a separate thread, so it's important to use synchronization
26 * or atomics when accessing any shared state from the callback function.
27 */
29 public:
31
32 /**
33 * Create a listener for changes to topics with names that start with any of
34 * the given prefixes. This creates a corresponding internal subscriber with
35 * the lifetime of the listener.
36 *
37 * @param inst Instance
38 * @param prefixes Topic name string prefixes
39 * @param mask Bitmask of EventFlags values
40 * @param listener Listener function
41 * @return Listener
42 */
44 NetworkTableInstance inst, std::span<const std::string_view> prefixes,
45 unsigned int mask, ListenerCallback listener);
46
47 /**
48 * Create a listener for changes on a particular topic. This creates a
49 * corresponding internal subscriber with the lifetime of the listener.
50 *
51 * @param topic Topic
52 * @param mask Bitmask of EventFlags values
53 * @param listener Listener function
54 * @return Listener
55 */
56 static NetworkTableListener CreateListener(Topic topic, unsigned int mask,
57 ListenerCallback listener);
58
59 /**
60 * Create a listener for topic changes on a subscriber. This does NOT keep the
61 * subscriber active.
62 *
63 * @param subscriber Subscriber
64 * @param mask Bitmask of EventFlags values
65 * @param listener Listener function
66 * @return Listener
67 */
69 unsigned int mask,
70 ListenerCallback listener);
71
72 /**
73 * Create a listener for topic changes on a subscriber. This does NOT keep the
74 * subscriber active.
75 *
76 * @param subscriber Subscriber
77 * @param mask Bitmask of EventFlags values
78 * @param listener Listener function
79 * @return Listener
80 */
82 unsigned int mask,
83 ListenerCallback listener);
84
85 /**
86 * Create a listener for topic changes on an entry.
87 *
88 * @param entry Entry
89 * @param mask Bitmask of EventFlags values
90 * @param listener Listener function
91 * @return Listener
92 */
94 unsigned int mask,
95 ListenerCallback listener);
96
97 /**
98 * Create a connection listener.
99 *
100 * @param inst instance
101 * @param immediate_notify notify listener of all existing connections
102 * @param listener listener function
103 * @return Listener
104 */
106 NetworkTableInstance inst, bool immediate_notify,
107 ListenerCallback listener);
108
109 /**
110 * Create a time synchronization listener.
111 *
112 * @param inst instance
113 * @param immediate_notify notify listener of current time synchronization
114 * value
115 * @param listener listener function
116 * @return Listener
117 */
119 bool immediate_notify,
120 ListenerCallback listener);
121
122 /**
123 * Create a listener for log messages. By default, log messages are sent to
124 * stderr; this function sends log messages with the specified levels to the
125 * provided callback function instead. The callback function will only be
126 * called for log messages with level greater than or equal to minLevel and
127 * less than or equal to maxLevel; messages outside this range will be
128 * silently ignored.
129 *
130 * @param inst instance
131 * @param minLevel minimum log level
132 * @param maxLevel maximum log level
133 * @param listener listener function
134 * @return Listener
135 */
137 unsigned int minLevel,
138 unsigned int maxLevel,
139 ListenerCallback listener);
140
146
147 explicit operator bool() const { return m_handle != 0; }
148
149 /**
150 * Gets the native handle.
151 *
152 * @return Handle
153 */
154 NT_Listener GetHandle() const { return m_handle; }
155
156 /**
157 * Wait for the listener queue to be empty. This is primarily useful for
158 * deterministic testing. This blocks until either the listener queue is
159 * empty (e.g. there are no more events that need to be passed along to
160 * callbacks or poll queues) or the timeout expires.
161 *
162 * @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or
163 * a negative value to block indefinitely
164 * @return False if timed out, otherwise true.
165 */
166 bool WaitForQueue(double timeout);
167
168 private:
169 explicit NetworkTableListener(NT_Listener handle) : m_handle{handle} {}
170
171 NT_Listener m_handle{0};
172};
173
174/**
175 * Event polled listener. This queues events matching the specified mask. Code
176 * using the listener must periodically call ReadQueue() to read the
177 * events.
178 */
180 public:
182
183 /**
184 * Construct a listener poller.
185 *
186 * @param inst Instance
187 */
189
192 delete;
196
197 explicit operator bool() const { return m_handle != 0; }
198
199 /**
200 * Gets the native handle.
201 *
202 * @return Handle
203 */
204 NT_ListenerPoller GetHandle() const { return m_handle; }
205
206 /**
207 * Start listening to topic changes for topics with names that start with any
208 * of the given prefixes. This creates a corresponding internal subscriber
209 * with the lifetime of the listener.
210 *
211 * @param prefixes Topic name string prefixes
212 * @param mask Bitmask of EventFlags values
213 * @return Listener handle
214 */
215 NT_Listener AddListener(std::span<const std::string_view> prefixes,
216 unsigned int mask);
217
218 /**
219 * Start listening to changes to a particular topic. This creates a
220 * corresponding internal subscriber with the lifetime of the listener.
221 *
222 * @param topic Topic
223 * @param mask Bitmask of EventFlags values
224 * @return Listener handle
225 */
226 NT_Listener AddListener(Topic topic, unsigned int mask);
227
228 /**
229 * Start listening to topic changes on a subscriber. This does NOT keep the
230 * subscriber active.
231 *
232 * @param subscriber Subscriber
233 * @param mask Bitmask of EventFlags values
234 * @return Listener handle
235 */
236 NT_Listener AddListener(Subscriber& subscriber, unsigned int mask);
237
238 /**
239 * Start listening to topic changes on a subscriber. This does NOT keep the
240 * subscriber active.
241 *
242 * @param subscriber Subscriber
243 * @param mask Bitmask of EventFlags values
244 * @return Listener handle
245 */
246 NT_Listener AddListener(MultiSubscriber& subscriber, unsigned int mask);
247
248 /**
249 * Start listening to topic changes on an entry.
250 *
251 * @param entry Entry
252 * @param mask Bitmask of EventFlags values
253 * @return Listener handle
254 */
255 NT_Listener AddListener(NetworkTableEntry& entry, unsigned int mask);
256
257 /**
258 * Add a connection listener. The callback function is called asynchronously
259 * on a separate thread, so it's important to use synchronization or atomics
260 * when accessing any shared state from the callback function.
261 *
262 * @param immediate_notify notify listener of all existing connections
263 * @return Listener handle
264 */
265 NT_Listener AddConnectionListener(bool immediate_notify);
266
267 /**
268 * Add a time synchronization listener. The callback function is called
269 * asynchronously on a separate thread, so it's important to use
270 * synchronization or atomics when accessing any shared state from the
271 * callback function.
272 *
273 * @param immediate_notify notify listener of current time synchronization
274 * value
275 * @return Listener handle
276 */
277 NT_Listener AddTimeSyncListener(bool immediate_notify);
278
279 /**
280 * Add logger callback function. By default, log messages are sent to stderr;
281 * this function sends log messages with the specified levels to the provided
282 * callback function instead. The callback function will only be called for
283 * log messages with level greater than or equal to minLevel and less than or
284 * equal to maxLevel; messages outside this range will be silently ignored.
285 *
286 * @param minLevel minimum log level
287 * @param maxLevel maximum log level
288 * @return Listener handle
289 */
290 NT_Listener AddLogger(unsigned int minLevel, unsigned int maxLevel);
291
292 /**
293 * Remove a listener.
294 *
295 * @param listener Listener handle
296 */
297 void RemoveListener(NT_Listener listener);
298
299 /**
300 * Read events.
301 *
302 * @return Events since the previous call to ReadQueue()
303 */
304 std::vector<Event> ReadQueue();
305
306 private:
307 NT_ListenerPoller m_handle{0};
308};
309
310} // namespace nt
311
Subscribe to multiple topics based on one or more topic name prefixes.
Definition: MultiSubscriber.h:20
NetworkTables Entry.
Definition: NetworkTableEntry.h:34
NetworkTables Instance.
Definition: NetworkTableInstance.h:70
Event listener.
Definition: NetworkTableListener.h:28
NetworkTableListener & operator=(const NetworkTableListener &)=delete
static NetworkTableListener CreateTimeSyncListener(NetworkTableInstance inst, bool immediate_notify, ListenerCallback listener)
Create a time synchronization listener.
Definition: NetworkTableListener.inc:61
bool WaitForQueue(double timeout)
Wait for the listener queue to be empty.
Definition: NetworkTableListener.inc:98
static NetworkTableListener CreateConnectionListener(NetworkTableInstance inst, bool immediate_notify, ListenerCallback listener)
Create a connection listener.
Definition: NetworkTableListener.inc:52
NT_Listener GetHandle() const
Gets the native handle.
Definition: NetworkTableListener.h:154
NetworkTableListener(const NetworkTableListener &)=delete
~NetworkTableListener()
Definition: NetworkTableListener.inc:92
static NetworkTableListener CreateLogger(NetworkTableInstance inst, unsigned int minLevel, unsigned int maxLevel, ListenerCallback listener)
Create a listener for log messages.
Definition: NetworkTableListener.inc:70
static NetworkTableListener CreateListener(NetworkTableInstance inst, std::span< const std::string_view > prefixes, unsigned int mask, ListenerCallback listener)
Create a listener for changes to topics with names that start with any of the given prefixes.
Definition: NetworkTableListener.inc:21
Event polled listener.
Definition: NetworkTableListener.h:179
void RemoveListener(NT_Listener listener)
Remove a listener.
Definition: NetworkTableListener.inc:176
NT_ListenerPoller GetHandle() const
Gets the native handle.
Definition: NetworkTableListener.h:204
NT_Listener AddTimeSyncListener(bool immediate_notify)
Add a time synchronization listener.
Definition: NetworkTableListener.inc:164
NetworkTableListenerPoller(const NetworkTableListenerPoller &)=delete
NT_Listener AddLogger(unsigned int minLevel, unsigned int maxLevel)
Add logger callback function.
Definition: NetworkTableListener.inc:171
NT_Listener AddListener(std::span< const std::string_view > prefixes, unsigned int mask)
Start listening to topic changes for topics with names that start with any of the given prefixes.
Definition: NetworkTableListener.inc:132
std::vector< Event > ReadQueue()
Read events.
Definition: NetworkTableListener.inc:180
NetworkTableListenerPoller & operator=(const NetworkTableListenerPoller &)=delete
NT_Listener AddConnectionListener(bool immediate_notify)
Add a connection listener.
Definition: NetworkTableListener.inc:157
~NetworkTableListenerPoller()
Definition: NetworkTableListener.inc:126
NetworkTables subscriber.
Definition: Topic.h:309
NetworkTables Topic.
Definition: Topic.h:28
NT_Handle NT_Listener
Definition: ntcore_c.h:39
NT_Handle NT_ListenerPoller
Definition: ntcore_c.h:40
std::function< void(const Event &)> ListenerCallback
Definition: ntcore_cpp.h:899
NetworkTables (ntcore) namespace.
Definition: NetworkTableListener.inc:19