mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-02 02:51:42 +00:00
[ntcore] Change Java event mask to EnumSet (#4564)
Also convert NetworkTableInstance.getNetworkMode() to return EnumSet.
This commit is contained in:
@@ -7,52 +7,61 @@ package edu.wpi.first.networktables;
|
||||
/**
|
||||
* NetworkTables event.
|
||||
*
|
||||
* <p>Events have flags. The flags are a bitmask and must be OR'ed together when listening to an
|
||||
* event to indicate the combination of events desired to be received.
|
||||
* <p>There are different kinds of events. When creating a listener, a combination of event kinds
|
||||
* can be listened to by building an EnumSet of NetworkTableEvent.Kind.
|
||||
*/
|
||||
@SuppressWarnings("MemberName")
|
||||
public final class NetworkTableEvent {
|
||||
/** No flags. */
|
||||
public static final int kNone = 0;
|
||||
public enum Kind {
|
||||
/**
|
||||
* Initial listener addition. Set this to receive immediate notification of matches to other
|
||||
* criteria.
|
||||
*/
|
||||
kImmediate(0x0001),
|
||||
|
||||
/**
|
||||
* Initial listener addition. Set this flag to receive immediate notification of matches to the
|
||||
* flag criteria.
|
||||
*/
|
||||
public static final int kImmediate = 0x01;
|
||||
/** Client connected (on server, any client connected). */
|
||||
kConnected(0x0002),
|
||||
|
||||
/** Client connected (on server, any client connected). */
|
||||
public static final int kConnected = 0x02;
|
||||
/** Client disconnected (on server, any client disconnected). */
|
||||
kDisconnected(0x0004),
|
||||
|
||||
/** Client disconnected (on server, any client disconnected). */
|
||||
public static final int kDisconnected = 0x04;
|
||||
/** Any connection event (connect or disconnect). */
|
||||
kConnection(0x0004 | 0x0002),
|
||||
|
||||
/** Any connection event (connect or disconnect). */
|
||||
public static final int kConnection = kConnected | kDisconnected;
|
||||
/** New topic published. */
|
||||
kPublish(0x0008),
|
||||
|
||||
/** New topic published. */
|
||||
public static final int kPublish = 0x08;
|
||||
/** Topic unpublished. */
|
||||
kUnpublish(0x0010),
|
||||
|
||||
/** Topic unpublished. */
|
||||
public static final int kUnpublish = 0x10;
|
||||
/** Topic properties changed. */
|
||||
kProperties(0x0020),
|
||||
|
||||
/** Topic properties changed. */
|
||||
public static final int kProperties = 0x20;
|
||||
/** Any topic event (publish, unpublish, or properties changed). */
|
||||
kTopic(0x0020 | 0x0010 | 0x0008),
|
||||
|
||||
/** Any topic event (publish, unpublish, or properties changed). */
|
||||
public static final int kTopic = kPublish | kUnpublish | kProperties;
|
||||
/** Topic value updated (via network). */
|
||||
kValueRemote(0x0040),
|
||||
|
||||
/** Topic value updated (via network). */
|
||||
public static final int kValueRemote = 0x40;
|
||||
/** Topic value updated (local). */
|
||||
kValueLocal(0x0080),
|
||||
|
||||
/** Topic value updated (local). */
|
||||
public static final int kValueLocal = 0x80;
|
||||
/** Topic value updated (network or local). */
|
||||
kValueAll(0x0080 | 0x0040),
|
||||
|
||||
/** Topic value updated (network or local). */
|
||||
public static final int kValueAll = kValueRemote | kValueLocal;
|
||||
/** Log message. */
|
||||
kLogMessage(0x0100);
|
||||
|
||||
/** Log message. */
|
||||
public static final int kLogMessage = 0x100;
|
||||
private final int value;
|
||||
|
||||
Kind(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle of listener that was triggered. The value returned when adding the listener can be used
|
||||
@@ -61,8 +70,8 @@ public final class NetworkTableEvent {
|
||||
public final int listener;
|
||||
|
||||
/**
|
||||
* Event flags. For example, kPublish if the topic was not previously published. Also indicates
|
||||
* the data included with the event:
|
||||
* Determine if event is of a particular kind. For example, kPublish if the topic was not
|
||||
* previously published. Also indicates the data included with the event:
|
||||
*
|
||||
* <ul>
|
||||
* <li>kConnected or kDisconnected: connInfo
|
||||
@@ -70,8 +79,15 @@ public final class NetworkTableEvent {
|
||||
* <li>kValueRemote, kValueLocal: valueData
|
||||
* <li>kLogMessage: logMessage
|
||||
* </ul>
|
||||
*
|
||||
* @param kind Kind
|
||||
* @return True if event matches kind
|
||||
*/
|
||||
public final int flags;
|
||||
public boolean is(Kind kind) {
|
||||
return (m_flags & kind.getValue()) != 0;
|
||||
}
|
||||
|
||||
private final int m_flags;
|
||||
|
||||
/** Connection information (for connection events). */
|
||||
public final ConnectionInfo connInfo;
|
||||
@@ -106,7 +122,7 @@ public final class NetworkTableEvent {
|
||||
LogMessage logMessage) {
|
||||
this.m_inst = inst;
|
||||
this.listener = listener;
|
||||
this.flags = flags;
|
||||
this.m_flags = flags;
|
||||
this.connInfo = connInfo;
|
||||
this.topicInfo = topicInfo;
|
||||
this.valueData = valueData;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
@@ -18,16 +19,16 @@ public final class NetworkTableListener implements AutoCloseable {
|
||||
*
|
||||
* @param inst Instance
|
||||
* @param prefixes Topic name string prefixes
|
||||
* @param eventMask Bitmask of NetworkTableEvent flags values
|
||||
* @param eventKinds set of event kinds to listen to
|
||||
* @param listener Listener function
|
||||
* @return Listener
|
||||
*/
|
||||
public static NetworkTableListener createListener(
|
||||
NetworkTableInstance inst,
|
||||
String[] prefixes,
|
||||
int eventMask,
|
||||
EnumSet<NetworkTableEvent.Kind> eventKinds,
|
||||
Consumer<NetworkTableEvent> listener) {
|
||||
return new NetworkTableListener(inst, inst.addListener(prefixes, eventMask, listener));
|
||||
return new NetworkTableListener(inst, inst.addListener(prefixes, eventKinds, listener));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,56 +36,64 @@ public final class NetworkTableListener implements AutoCloseable {
|
||||
* subscriber with the lifetime of the listener.
|
||||
*
|
||||
* @param topic Topic
|
||||
* @param eventMask Bitmask of NetworkTableEvent flags values
|
||||
* @param eventKinds set of event kinds to listen to
|
||||
* @param listener Listener function
|
||||
* @return Listener
|
||||
*/
|
||||
public static NetworkTableListener createListener(
|
||||
Topic topic, int eventMask, Consumer<NetworkTableEvent> listener) {
|
||||
Topic topic,
|
||||
EnumSet<NetworkTableEvent.Kind> eventKinds,
|
||||
Consumer<NetworkTableEvent> listener) {
|
||||
NetworkTableInstance inst = topic.getInstance();
|
||||
return new NetworkTableListener(inst, inst.addListener(topic, eventMask, listener));
|
||||
return new NetworkTableListener(inst, inst.addListener(topic, eventKinds, listener));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a listener for topic changes on a subscriber. This does NOT keep the subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param eventMask Bitmask of NetworkTableEvent flags values
|
||||
* @param eventKinds set of event kinds to listen to
|
||||
* @param listener Listener function
|
||||
* @return Listener
|
||||
*/
|
||||
public static NetworkTableListener createListener(
|
||||
Subscriber subscriber, int eventMask, Consumer<NetworkTableEvent> listener) {
|
||||
Subscriber subscriber,
|
||||
EnumSet<NetworkTableEvent.Kind> eventKinds,
|
||||
Consumer<NetworkTableEvent> listener) {
|
||||
NetworkTableInstance inst = subscriber.getTopic().getInstance();
|
||||
return new NetworkTableListener(inst, inst.addListener(subscriber, eventMask, listener));
|
||||
return new NetworkTableListener(inst, inst.addListener(subscriber, eventKinds, listener));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a listener for topic changes on a subscriber. This does NOT keep the subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param eventMask Bitmask of NetworkTableEvent flags values
|
||||
* @param eventKinds set of event kinds to listen to
|
||||
* @param listener Listener function
|
||||
* @return Listener
|
||||
*/
|
||||
public static NetworkTableListener createListener(
|
||||
MultiSubscriber subscriber, int eventMask, Consumer<NetworkTableEvent> listener) {
|
||||
MultiSubscriber subscriber,
|
||||
EnumSet<NetworkTableEvent.Kind> eventKinds,
|
||||
Consumer<NetworkTableEvent> listener) {
|
||||
NetworkTableInstance inst = subscriber.getInstance();
|
||||
return new NetworkTableListener(inst, inst.addListener(subscriber, eventMask, listener));
|
||||
return new NetworkTableListener(inst, inst.addListener(subscriber, eventKinds, listener));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a listener for topic changes on an entry.
|
||||
*
|
||||
* @param entry Entry
|
||||
* @param eventMask Bitmask of NetworkTableEvent flags values
|
||||
* @param eventKinds set of event kinds to listen to
|
||||
* @param listener Listener function
|
||||
* @return Listener
|
||||
*/
|
||||
public static NetworkTableListener createListener(
|
||||
NetworkTableEntry entry, int eventMask, Consumer<NetworkTableEvent> listener) {
|
||||
NetworkTableEntry entry,
|
||||
EnumSet<NetworkTableEvent.Kind> eventKinds,
|
||||
Consumer<NetworkTableEvent> listener) {
|
||||
NetworkTableInstance inst = entry.getInstance();
|
||||
return new NetworkTableListener(inst, inst.addListener(entry, eventMask, listener));
|
||||
return new NetworkTableListener(inst, inst.addListener(entry, eventKinds, listener));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
package edu.wpi.first.networktables;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
/**
|
||||
* Topic change listener. This queues topic change events matching the specified mask. Code using
|
||||
* the listener must periodically call readQueue() to read the events.
|
||||
@@ -24,11 +26,11 @@ public final class NetworkTableListenerPoller implements AutoCloseable {
|
||||
* prefixes. This creates a corresponding internal subscriber with the lifetime of the listener.
|
||||
*
|
||||
* @param prefixes Topic name string prefixes
|
||||
* @param eventMask Bitmask of TopicListenerFlags values
|
||||
* @param eventKinds set of event kinds to listen to
|
||||
* @return Listener handle
|
||||
*/
|
||||
public int addListener(String[] prefixes, int eventMask) {
|
||||
return NetworkTablesJNI.addListener(m_handle, prefixes, eventMask);
|
||||
public int addListener(String[] prefixes, EnumSet<NetworkTableEvent.Kind> eventKinds) {
|
||||
return NetworkTablesJNI.addListener(m_handle, prefixes, eventKinds);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,44 +38,44 @@ public final class NetworkTableListenerPoller implements AutoCloseable {
|
||||
* subscriber with the lifetime of the listener.
|
||||
*
|
||||
* @param topic Topic
|
||||
* @param eventMask Bitmask of TopicListenerFlags values
|
||||
* @param eventKinds set of event kinds to listen to
|
||||
* @return Listener handle
|
||||
*/
|
||||
public int addListener(Topic topic, int eventMask) {
|
||||
return NetworkTablesJNI.addListener(m_handle, topic.getHandle(), eventMask);
|
||||
public int addListener(Topic topic, EnumSet<NetworkTableEvent.Kind> eventKinds) {
|
||||
return NetworkTablesJNI.addListener(m_handle, topic.getHandle(), eventKinds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start listening to topic changes on a subscriber. This does NOT keep the subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param eventMask Bitmask of TopicListenerFlags values
|
||||
* @param eventKinds set of event kinds to listen to
|
||||
* @return Listener handle
|
||||
*/
|
||||
public int addListener(Subscriber subscriber, int eventMask) {
|
||||
return NetworkTablesJNI.addListener(m_handle, subscriber.getHandle(), eventMask);
|
||||
public int addListener(Subscriber subscriber, EnumSet<NetworkTableEvent.Kind> eventKinds) {
|
||||
return NetworkTablesJNI.addListener(m_handle, subscriber.getHandle(), eventKinds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start listening to topic changes on a subscriber. This does NOT keep the subscriber active.
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
* @param eventMask Bitmask of TopicListenerFlags values
|
||||
* @param eventKinds set of event kinds to listen to
|
||||
* @return Listener handle
|
||||
*/
|
||||
public int addListener(MultiSubscriber subscriber, int eventMask) {
|
||||
return NetworkTablesJNI.addListener(m_handle, subscriber.getHandle(), eventMask);
|
||||
public int addListener(MultiSubscriber subscriber, EnumSet<NetworkTableEvent.Kind> eventKinds) {
|
||||
return NetworkTablesJNI.addListener(m_handle, subscriber.getHandle(), eventKinds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start listening to topic changes on an entry.
|
||||
*
|
||||
* @param entry Entry
|
||||
* @param eventMask Bitmask of TopicListenerFlags values
|
||||
* @param eventKinds set of event kinds to listen to
|
||||
* @return Listener handle
|
||||
*/
|
||||
public int addListener(NetworkTableEntry entry, int eventMask) {
|
||||
return NetworkTablesJNI.addListener(m_handle, entry.getHandle(), eventMask);
|
||||
public int addListener(NetworkTableEntry entry, EnumSet<NetworkTableEvent.Kind> eventKinds) {
|
||||
return NetworkTablesJNI.addListener(m_handle, entry.getHandle(), eventKinds);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,10 +87,11 @@ public final class NetworkTableListenerPoller implements AutoCloseable {
|
||||
* @return Listener handle
|
||||
*/
|
||||
public int addConnectionListener(boolean immediateNotify) {
|
||||
return NetworkTablesJNI.addListener(
|
||||
m_handle,
|
||||
m_inst.getHandle(),
|
||||
NetworkTableEvent.kConnection | (immediateNotify ? NetworkTableEvent.kImmediate : 0));
|
||||
EnumSet<NetworkTableEvent.Kind> eventKinds = EnumSet.of(NetworkTableEvent.Kind.kConnection);
|
||||
if (immediateNotify) {
|
||||
eventKinds.add(NetworkTableEvent.Kind.kImmediate);
|
||||
}
|
||||
return NetworkTablesJNI.addListener(m_handle, m_inst.getHandle(), eventKinds);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -223,6 +223,14 @@ class Event {
|
||||
*/
|
||||
unsigned int flags{0};
|
||||
|
||||
/**
|
||||
* Test event flags.
|
||||
*
|
||||
* @param kind event flag(s) to test
|
||||
* @return True if flags matches kind
|
||||
*/
|
||||
bool Is(unsigned int kind) const { return (flags & kind) != 0; }
|
||||
|
||||
/** Event data; content depends on flags. */
|
||||
std::variant<ConnectionInfo, TopicInfo, ValueEventData, LogMessage> data;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user