WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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 <span>
8#include <string_view>
9#include <utility>
10#include <vector>
11
15#include "networktables/Topic.h"
16#include "ntcore_cpp.h"
17
18namespace nt {
19
20class MultiSubscriber;
21class NetworkTableEntry;
22class NetworkTableInstance;
23class Subscriber;
24class Topic;
25
26/**
27 * Event listener. This calls back to a callback function when an event
28 * matching the specified mask occurs. The callback function is called
29 * asynchronously on a separate thread, so it's important to use synchronization
30 * or atomics when accessing any shared state from the callback function.
31 */
33 public:
35
36 /**
37 * Create a listener for changes to topics with names that start with any of
38 * the given prefixes. This creates a corresponding internal subscriber with
39 * the lifetime of the listener.
40 *
41 * @param inst Instance
42 * @param prefixes Topic name string prefixes
43 * @param mask Bitmask of EventFlags values
44 * @param listener Listener function
45 * @return Listener
46 */
48 NetworkTableInstance inst, std::span<const std::string_view> prefixes,
49 unsigned int mask, ListenerCallback listener) {
50 return NetworkTableListener{::nt::AddListener(inst.GetHandle(), prefixes,
51 mask, std::move(listener))};
52 }
53
54 /**
55 * Create a listener for changes on a particular topic. This creates a
56 * corresponding internal subscriber with the lifetime of the listener.
57 *
58 * @param topic Topic
59 * @param mask Bitmask of EventFlags values
60 * @param listener Listener function
61 * @return Listener
62 */
63 static NetworkTableListener CreateListener(Topic topic, unsigned int mask,
64 ListenerCallback listener) {
66 nt::AddListener(topic.GetHandle(), mask, std::move(listener))};
67 }
68
69 /**
70 * Create a listener for topic changes on a subscriber. This does NOT keep the
71 * subscriber active.
72 *
73 * @param subscriber Subscriber
74 * @param mask Bitmask of EventFlags values
75 * @param listener Listener function
76 * @return Listener
77 */
79 unsigned int mask,
80 ListenerCallback listener) {
82 ::nt::AddListener(subscriber.GetHandle(), mask, std::move(listener))};
83 }
84
85 /**
86 * Create a listener for topic changes on a subscriber. This does NOT keep the
87 * subscriber active.
88 *
89 * @param subscriber Subscriber
90 * @param mask Bitmask of EventFlags values
91 * @param listener Listener function
92 * @return Listener
93 */
95 unsigned int mask,
96 ListenerCallback listener) {
98 ::nt::AddListener(subscriber.GetHandle(), mask, std::move(listener))};
99 }
100
101 /**
102 * Create a listener for topic changes on an entry.
103 *
104 * @param entry Entry
105 * @param mask Bitmask of EventFlags values
106 * @param listener Listener function
107 * @return Listener
108 */
110 unsigned int mask,
111 ListenerCallback listener) {
113 ::nt::AddListener(entry.GetHandle(), mask, std::move(listener))};
114 }
115
116 /**
117 * Create a connection listener.
118 *
119 * @param inst instance
120 * @param immediate_notify notify listener of all existing connections
121 * @param listener listener function
122 * @return Listener
123 */
125 NetworkTableInstance inst, bool immediate_notify,
126 ListenerCallback listener) {
128 inst.GetHandle(),
129 NT_EVENT_CONNECTION | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
130 std::move(listener))};
131 }
132
133 /**
134 * Create a time synchronization listener.
135 *
136 * @param inst instance
137 * @param immediate_notify notify listener of current time synchronization
138 * value
139 * @param listener listener function
140 * @return Listener
141 */
143 NetworkTableInstance inst, bool immediate_notify,
144 ListenerCallback listener) {
146 inst.GetHandle(),
147 NT_EVENT_TIMESYNC | (immediate_notify ? NT_EVENT_IMMEDIATE : 0),
148 std::move(listener))};
149 }
150
151 /**
152 * Create a listener for log messages. By default, log messages are sent to
153 * stderr; this function sends log messages with the specified levels to the
154 * provided callback function instead. The callback function will only be
155 * called for log messages with level greater than or equal to minLevel and
156 * less than or equal to maxLevel; messages outside this range will be
157 * silently ignored.
158 *
159 * @param inst instance
160 * @param minLevel minimum log level
161 * @param maxLevel maximum log level
162 * @param listener listener function
163 * @return Listener
164 */
166 unsigned int minLevel,
167 unsigned int maxLevel,
168 ListenerCallback listener) {
169 return NetworkTableListener{::nt::AddLogger(inst.GetHandle(), minLevel,
170 maxLevel, std::move(listener))};
171 }
172
175
176 NetworkTableListener(NetworkTableListener&& rhs) : m_handle(rhs.m_handle) {
177 rhs.m_handle = 0;
178 }
179
181 if (m_handle != 0) {
182 nt::RemoveListener(m_handle);
183 }
184 m_handle = rhs.m_handle;
185 rhs.m_handle = 0;
186 return *this;
187 }
188
190 if (m_handle != 0) {
191 nt::RemoveListener(m_handle);
192 }
193 }
194
195 explicit operator bool() const { return m_handle != 0; }
196
197 /**
198 * Gets the native handle.
199 *
200 * @return Handle
201 */
202 NT_Listener GetHandle() const { return m_handle; }
203
204 /**
205 * Wait for the listener queue to be empty. This is primarily useful for
206 * deterministic testing. This blocks until either the listener queue is
207 * empty (e.g. there are no more events that need to be passed along to
208 * callbacks or poll queues) or the timeout expires.
209 *
210 * @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or
211 * a negative value to block indefinitely
212 * @return False if timed out, otherwise true.
213 */
214 bool WaitForQueue(double timeout) {
215 if (m_handle != 0) {
216 return nt::WaitForListenerQueue(m_handle, timeout);
217 } else {
218 return false;
219 }
220 }
221
222 private:
223 explicit NetworkTableListener(NT_Listener handle) : m_handle{handle} {}
224
225 NT_Listener m_handle{0};
226};
227
228/**
229 * Event polled listener. This queues events matching the specified mask. Code
230 * using the listener must periodically call ReadQueue() to read the
231 * events.
232 */
234 public:
236
237 /**
238 * Construct a listener poller.
239 *
240 * @param inst Instance
241 */
244
247 delete;
248
250 : m_handle(rhs.m_handle) {
251 rhs.m_handle = 0;
252 }
253
255 if (m_handle != 0) {
257 }
258 m_handle = rhs.m_handle;
259 rhs.m_handle = 0;
260 return *this;
261 }
262
264 if (m_handle != 0) {
266 }
267 }
268
269 explicit operator bool() const { return m_handle != 0; }
270
271 /**
272 * Gets the native handle.
273 *
274 * @return Handle
275 */
276 NT_ListenerPoller GetHandle() const { return m_handle; }
277
278 /**
279 * Start listening to topic changes for topics with names that start with any
280 * of the given prefixes. This creates a corresponding internal subscriber
281 * with the lifetime of the listener.
282 *
283 * @param prefixes Topic name string prefixes
284 * @param mask Bitmask of EventFlags values
285 * @return Listener handle
286 */
287 NT_Listener AddListener(std::span<const std::string_view> prefixes,
288 unsigned int mask) {
289 return nt::AddPolledListener(m_handle, prefixes, mask);
290 }
291
292 /**
293 * Start listening to changes to a particular topic. This creates a
294 * corresponding internal subscriber with the lifetime of the listener.
295 *
296 * @param topic Topic
297 * @param mask Bitmask of EventFlags values
298 * @return Listener handle
299 */
300 NT_Listener AddListener(Topic topic, unsigned int mask) {
301 return ::nt::AddPolledListener(m_handle, topic.GetHandle(), mask);
302 }
303
304 /**
305 * Start listening to topic changes on a subscriber. This does NOT keep the
306 * subscriber active.
307 *
308 * @param subscriber Subscriber
309 * @param mask Bitmask of EventFlags values
310 * @return Listener handle
311 */
312 NT_Listener AddListener(Subscriber& subscriber, unsigned int mask) {
313 return ::nt::AddPolledListener(m_handle, subscriber.GetHandle(), mask);
314 }
315
316 /**
317 * Start listening to topic changes on a subscriber. This does NOT keep the
318 * subscriber active.
319 *
320 * @param subscriber Subscriber
321 * @param mask Bitmask of EventFlags values
322 * @return Listener handle
323 */
324 NT_Listener AddListener(MultiSubscriber& subscriber, unsigned int mask) {
325 return ::nt::AddPolledListener(m_handle, subscriber.GetHandle(), mask);
326 }
327
328 /**
329 * Start listening to topic changes on an entry.
330 *
331 * @param entry Entry
332 * @param mask Bitmask of EventFlags values
333 * @return Listener handle
334 */
335 NT_Listener AddListener(NetworkTableEntry& entry, unsigned int mask) {
336 return ::nt::AddPolledListener(m_handle, entry.GetHandle(), mask);
337 }
338
339 /**
340 * Add a connection listener. The callback function is called asynchronously
341 * on a separate thread, so it's important to use synchronization or atomics
342 * when accessing any shared state from the callback function.
343 *
344 * @param immediate_notify notify listener of all existing connections
345 * @return Listener handle
346 */
347 NT_Listener AddConnectionListener(bool immediate_notify) {
348 return ::nt::AddPolledListener(
349 m_handle, ::nt::GetInstanceFromHandle(m_handle),
350 NT_EVENT_CONNECTION | (immediate_notify ? NT_EVENT_IMMEDIATE : 0));
351 }
352
353 /**
354 * Add a time synchronization listener. The callback function is called
355 * asynchronously on a separate thread, so it's important to use
356 * synchronization or atomics when accessing any shared state from the
357 * callback function.
358 *
359 * @param immediate_notify notify listener of current time synchronization
360 * value
361 * @return Listener handle
362 */
363 NT_Listener AddTimeSyncListener(bool immediate_notify) {
364 return ::nt::AddPolledListener(
365 m_handle, ::nt::GetInstanceFromHandle(m_handle),
366 NT_EVENT_TIMESYNC | (immediate_notify ? NT_EVENT_IMMEDIATE : 0));
367 }
368
369 /**
370 * Add logger callback function. By default, log messages are sent to stderr;
371 * this function sends log messages with the specified levels to the provided
372 * callback function instead. The callback function will only be called for
373 * log messages with level greater than or equal to minLevel and less than or
374 * equal to maxLevel; messages outside this range will be silently ignored.
375 *
376 * @param minLevel minimum log level
377 * @param maxLevel maximum log level
378 * @return Listener handle
379 */
380 NT_Listener AddLogger(unsigned int minLevel, unsigned int maxLevel) {
381 return ::nt::AddPolledLogger(m_handle, minLevel, maxLevel);
382 }
383
384 /**
385 * Remove a listener.
386 *
387 * @param listener Listener handle
388 */
389 void RemoveListener(NT_Listener listener) { ::nt::RemoveListener(listener); }
390
391 /**
392 * Read events.
393 *
394 * @return Events since the previous call to ReadQueue()
395 */
396 std::vector<Event> ReadQueue() { return ::nt::ReadListenerQueue(m_handle); }
397
398 private:
399 NT_ListenerPoller m_handle{0};
400};
401
402} // namespace nt
Subscribe to multiple topics based on one or more topic name prefixes.
Definition MultiSubscriber.h:20
NT_MultiSubscriber GetHandle() const
Gets the native handle.
Definition MultiSubscriber.h:71
NetworkTables Entry.
Definition NetworkTableEntry.h:31
NT_Entry GetHandle() const
Gets the native handle for the entry.
Definition NetworkTableEntry.h:57
NetworkTables Instance.
Definition NetworkTableInstance.h:68
NT_Inst GetHandle() const
Gets the native handle for the entry.
Definition NetworkTableInstance.h:161
Event listener.
Definition NetworkTableListener.h:32
static NetworkTableListener CreateTimeSyncListener(NetworkTableInstance inst, bool immediate_notify, ListenerCallback listener)
Create a time synchronization listener.
Definition NetworkTableListener.h:142
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.h:47
NetworkTableListener & operator=(NetworkTableListener &&rhs)
Definition NetworkTableListener.h:180
NetworkTableListener & operator=(const NetworkTableListener &)=delete
bool WaitForQueue(double timeout)
Wait for the listener queue to be empty.
Definition NetworkTableListener.h:214
static NetworkTableListener CreateListener(NetworkTableEntry &entry, unsigned int mask, ListenerCallback listener)
Create a listener for topic changes on an entry.
Definition NetworkTableListener.h:109
static NetworkTableListener CreateLogger(NetworkTableInstance inst, unsigned int minLevel, unsigned int maxLevel, ListenerCallback listener)
Create a listener for log messages.
Definition NetworkTableListener.h:165
NT_Listener GetHandle() const
Gets the native handle.
Definition NetworkTableListener.h:202
NetworkTableListener(const NetworkTableListener &)=delete
static NetworkTableListener CreateListener(Subscriber &subscriber, unsigned int mask, ListenerCallback listener)
Create a listener for topic changes on a subscriber.
Definition NetworkTableListener.h:78
~NetworkTableListener()
Definition NetworkTableListener.h:189
static NetworkTableListener CreateListener(Topic topic, unsigned int mask, ListenerCallback listener)
Create a listener for changes on a particular topic.
Definition NetworkTableListener.h:63
static NetworkTableListener CreateListener(MultiSubscriber &subscriber, unsigned int mask, ListenerCallback listener)
Create a listener for topic changes on a subscriber.
Definition NetworkTableListener.h:94
static NetworkTableListener CreateConnectionListener(NetworkTableInstance inst, bool immediate_notify, ListenerCallback listener)
Create a connection listener.
Definition NetworkTableListener.h:124
NetworkTableListener(NetworkTableListener &&rhs)
Definition NetworkTableListener.h:176
Event polled listener.
Definition NetworkTableListener.h:233
void RemoveListener(NT_Listener listener)
Remove a listener.
Definition NetworkTableListener.h:389
NT_Listener AddListener(MultiSubscriber &subscriber, unsigned int mask)
Start listening to topic changes on a subscriber.
Definition NetworkTableListener.h:324
NT_ListenerPoller GetHandle() const
Gets the native handle.
Definition NetworkTableListener.h:276
NT_Listener AddListener(Topic topic, unsigned int mask)
Start listening to changes to a particular topic.
Definition NetworkTableListener.h:300
NT_Listener AddTimeSyncListener(bool immediate_notify)
Add a time synchronization listener.
Definition NetworkTableListener.h:363
NetworkTableListenerPoller(NetworkTableListenerPoller &&rhs)
Definition NetworkTableListener.h:249
NetworkTableListenerPoller(const NetworkTableListenerPoller &)=delete
NT_Listener AddLogger(unsigned int minLevel, unsigned int maxLevel)
Add logger callback function.
Definition NetworkTableListener.h:380
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.h:287
NetworkTableListenerPoller(NetworkTableInstance inst)
Construct a listener poller.
Definition NetworkTableListener.h:242
std::vector< Event > ReadQueue()
Read events.
Definition NetworkTableListener.h:396
NetworkTableListenerPoller & operator=(const NetworkTableListenerPoller &)=delete
NT_Listener AddConnectionListener(bool immediate_notify)
Add a connection listener.
Definition NetworkTableListener.h:347
NetworkTableListenerPoller & operator=(NetworkTableListenerPoller &&rhs)
Definition NetworkTableListener.h:254
NT_Listener AddListener(Subscriber &subscriber, unsigned int mask)
Start listening to topic changes on a subscriber.
Definition NetworkTableListener.h:312
NT_Listener AddListener(NetworkTableEntry &entry, unsigned int mask)
Start listening to topic changes on an entry.
Definition NetworkTableListener.h:335
~NetworkTableListenerPoller()
Definition NetworkTableListener.h:263
NetworkTables subscriber.
Definition Topic.h:321
NT_Subscriber GetHandle() const
Gets the native handle for the subscriber.
Definition Topic.h:353
NetworkTables Topic.
Definition Topic.h:28
NT_Topic GetHandle() const
Gets the native handle for the topic.
Definition Topic.h:45
NT_Handle NT_Listener
Definition ntcore_c.h:39
NT_Handle NT_ListenerPoller
Definition ntcore_c.h:40
@ NT_EVENT_TIMESYNC
Time synchronized with server.
Definition ntcore_c.h:127
@ NT_EVENT_CONNECTION
Any connection event (connect or disconnect).
Definition ntcore_c.h:109
@ NT_EVENT_IMMEDIATE
Initial listener addition.
Definition ntcore_c.h:103
NT_Inst GetInstanceFromHandle(NT_Handle handle)
Get instance handle from another handle.
NT_Listener AddPolledListener(NT_ListenerPoller poller, std::span< const std::string_view > prefixes, unsigned int mask)
Creates a polled listener.
NT_ListenerPoller CreateListenerPoller(NT_Inst inst)
Creates a listener poller.
bool WaitForListenerQueue(NT_Handle handle, double timeout)
Wait for the listener queue to be empty.
NT_Listener AddListener(NT_Inst inst, std::span< const std::string_view > prefixes, unsigned int mask, ListenerCallback callback)
Create a listener for changes to topics with names that start with any of the given prefixes.
void DestroyListenerPoller(NT_ListenerPoller poller)
Destroys a listener poller.
std::function< void(const Event &)> ListenerCallback
Definition ntcore_cpp.h:899
void RemoveListener(NT_Listener listener)
Removes a listener.
NT_Listener AddLogger(NT_Inst inst, unsigned int min_level, unsigned int max_level, ListenerCallback func)
Add logger callback function.
NetworkTables (ntcore) namespace.
Definition ntcore_cpp.h:36