[ntcore] Enhance Java raw value support

Add support for start, length, and ByteBuffers
This commit is contained in:
Peter Johnson
2023-05-18 00:44:45 -07:00
parent 3a6e40a44b
commit fb57d82e52
8 changed files with 731 additions and 5 deletions

View File

@@ -4,6 +4,7 @@
package edu.wpi.first.networktables;
import java.nio.ByteBuffer;
import java.util.function.Consumer;
/** NetworkTables generic publisher. */
@@ -54,7 +55,86 @@ public interface GenericPublisher extends Publisher, Consumer<NetworkTableValue>
default boolean set{{ t.TypeName }}({{ t.java.ValueType }} value) {
return set{{ t.TypeName }}(value, 0);
}
{% if t.TypeName == "Raw" %}
/**
* Publish a new value.
*
* @param value value to publish
* @return False if the topic already exists with a different type
*/
default boolean setRaw(ByteBuffer value) {
return setRaw(value, 0);
}
/**
* Publish a new value.
*
* @param value value to publish
* @param time timestamp; 0 indicates current NT time should be used
* @return False if the topic already exists with a different type
*/
default boolean setRaw(byte[] value, long time) {
return setRaw(value, 0, value.length, time);
}
/**
* Publish a new value.
*
* @param value value to publish; will send from value.position() to value.limit()
* @param time timestamp; 0 indicates current NT time should be used
* @return False if the topic already exists with a different type
*/
default boolean setRaw(ByteBuffer value, long time) {
int pos = value.position();
return setRaw(value, pos, value.limit() - pos, time);
}
/**
* Publish a new value.
*
* @param value value to publish
* @param start Start position of data (in buffer)
* @param len Length of data (must be less than or equal to value.length - start)
* @return False if the topic already exists with a different type
*/
default boolean setRaw(byte[] value, int start, int len) {
return setRaw(value, start, len, 0);
}
/**
* Publish a new value.
*
* @param value value to publish
* @param start Start position of data (in buffer)
* @param len Length of data (must be less than or equal to value.length - start)
* @param time timestamp; 0 indicates current NT time should be used
* @return False if the topic already exists with a different type
*/
boolean setRaw(byte[] value, int start, int len, long time);
/**
* Publish a new value.
*
* @param value value to publish
* @param start Start position of data (in buffer)
* @param len Length of data (must be less than or equal to value.capacity() - start)
* @return False if the topic already exists with a different type
*/
default boolean setRaw(ByteBuffer value, int start, int len) {
return setRaw(value, start, len, 0);
}
/**
* Publish a new value.
*
* @param value value to publish
* @param start Start position of data (in buffer)
* @param len Length of data (must be less than or equal to value.capacity() - start)
* @param time timestamp; 0 indicates current NT time should be used
* @return False if the topic already exists with a different type
*/
boolean setRaw(ByteBuffer value, int start, int len, long time);
{% else %}
/**
* Publish a new value.
*
@@ -63,6 +143,7 @@ public interface GenericPublisher extends Publisher, Consumer<NetworkTableValue>
* @return False if the topic already exists with a different type
*/
boolean set{{ t.TypeName }}({{ t.java.ValueType }} value, long time);
{% endif -%}
{% if t.java.WrapValueType %}
/**
* Publish a new value.
@@ -101,6 +182,49 @@ public interface GenericPublisher extends Publisher, Consumer<NetworkTableValue>
*/
boolean setDefaultValue(Object defaultValue);
{% for t in types %}
{% if t.TypeName == "Raw" %}
/**
* Sets the entry's value if it does not exist.
*
* @param defaultValue the default value to set
* @return False if the entry exists with a different type
*/
default boolean setDefaultRaw(byte[] defaultValue) {
return setDefaultRaw(defaultValue, 0, defaultValue.length);
}
/**
* Sets the entry's value if it does not exist.
*
* @param defaultValue the default value to set; will send from defaultValue.position() to
* defaultValue.limit()
* @return False if the entry exists with a different type
*/
default boolean setDefaultRaw(ByteBuffer defaultValue) {
int pos = defaultValue.position();
return setDefaultRaw(defaultValue, pos, defaultValue.limit() - pos);
}
/**
* Sets the entry's value if it does not exist.
*
* @param defaultValue the default value to set
* @param start Start position of data (in buffer)
* @param len Length of data (must be less than or equal to value.length - start)
* @return False if the entry exists with a different type
*/
boolean setDefaultRaw(byte[] defaultValue, int start, int len);
/**
* Sets the entry's value if it does not exist.
*
* @param defaultValue the default value to set
* @param start Start position of data (in buffer)
* @param len Length of data (must be less than or equal to value.capacity() - start)
* @return False if the entry exists with a different type
*/
boolean setDefaultRaw(ByteBuffer defaultValue, int start, int len);
{% else %}
/**
* Sets the entry's value if it does not exist.
*
@@ -108,6 +232,7 @@ public interface GenericPublisher extends Publisher, Consumer<NetworkTableValue>
* @return False if the entry exists with a different type
*/
boolean setDefault{{ t.TypeName }}({{ t.java.ValueType }} defaultValue);
{% endif -%}
{% if t.java.WrapValueType %}
boolean setDefault{{ t.TypeName }}({{ t.java.WrapValueType }} defaultValue);
{% endif -%}