Skip to content

Commit

Permalink
Use array instead of List for ROS message types (#55)
Browse files Browse the repository at this point in the history
* change logic(list -> array) in msg.java.em

* change logic(list -> array) in msg.cpp.em

* Support getting and setting as Lists in addition to arrays

Signed-off-by: Jacob Perron <jacob@openrobotics.org>

* Update rcljava to work with new methods for list types

Signed-off-by: Jacob Perron <jacob@openrobotics.org>

* Update tests to work with new methods for list types

Signed-off-by: Jacob Perron <jacob@openrobotics.org>

* Fixes to template files

* Avoid nullptr access when converting from Java arrays to C arrays
* Make sure Java arrays are initialized

Signed-off-by: Jacob Perron <jacob@openrobotics.org>

* Minor refactor

Signed-off-by: Jacob Perron <jacob@openrobotics.org>

* A note about performance to *AsList docblocks

Signed-off-by: Jacob Perron <jacob@openrobotics.org>

Co-authored-by: pluris <zighart8456@naver.com>
  • Loading branch information
jacobperron and pluris authored Jan 7, 2021
1 parent 2c00872 commit c012ba8
Show file tree
Hide file tree
Showing 11 changed files with 611 additions and 428 deletions.
12 changes: 8 additions & 4 deletions rcljava/src/main/java/org/ros2/rcljava/node/NodeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ public rcl_interfaces.msg.ListParametersResult listParameters(
rcl_interfaces.msg.ListParametersResult result =
new rcl_interfaces.msg.ListParametersResult();

List<String> resultNames = new ArrayList<String>();
List<String> resultPrefixes = new ArrayList<String>();
synchronized (parametersMutex) {
for (Map.Entry<String, ParameterAndDescriptor> entry : this.parameters.entrySet()) {
boolean getAll =
Expand All @@ -805,16 +807,18 @@ public rcl_interfaces.msg.ListParametersResult listParameters(
}

if (getAll || prefixMatches) {
result.getNames().add(entry.getKey());
resultNames.add(entry.getKey());
int lastSeparator = entry.getKey().lastIndexOf(separator);
if (-1 != lastSeparator) {
String prefix = entry.getKey().substring(0, lastSeparator);
if (!result.getPrefixes().contains(prefix)) {
result.getPrefixes().add(prefix);
if (!resultPrefixes.contains(prefix)) {
resultPrefixes.add(prefix);
}
}
}
}
result.setNames(resultNames);
result.setPrefixes(resultPrefixes);
return result;
}
}
Expand Down Expand Up @@ -853,7 +857,7 @@ public final Collection<EndpointInfo> getPublishersInfo(final String topicName)

private native static final void nativeGetPublishersInfo(
final long handle, final String topicName, ArrayList<EndpointInfo> endpointInfo);

public final Collection<EndpointInfo> getSubscriptionsInfo(final String topicName) {
ArrayList<EndpointInfo> returnValue = new ArrayList();
nativeGetSubscriptionsInfo(this.handle, topicName, returnValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,38 +100,38 @@ public ParameterVariant(final String name, final String stringValue) {
this.value.setType(ParameterType.PARAMETER_STRING.getValue());
}

public ParameterVariant(final String name, final Byte[] byteArrayValue) {
public ParameterVariant(final String name, final byte[] byteArrayValue) {
this.name = name;
this.value = new rcl_interfaces.msg.ParameterValue();
this.value.setByteArrayValue(Arrays.asList(byteArrayValue));
this.value.setByteArrayValue(byteArrayValue);
this.value.setType(ParameterType.PARAMETER_BYTE_ARRAY.getValue());
}

public ParameterVariant(final String name, final Boolean[] boolArrayValue) {
public ParameterVariant(final String name, final boolean[] boolArrayValue) {
this.name = name;
this.value = new rcl_interfaces.msg.ParameterValue();
this.value.setBoolArrayValue(Arrays.asList(boolArrayValue));
this.value.setBoolArrayValue(boolArrayValue);
this.value.setType(ParameterType.PARAMETER_BOOL_ARRAY.getValue());
}

public ParameterVariant(final String name, final Long[] integerArrayValue) {
public ParameterVariant(final String name, final long[] integerArrayValue) {
this.name = name;
this.value = new rcl_interfaces.msg.ParameterValue();
this.value.setIntegerArrayValue(Arrays.asList(integerArrayValue));
this.value.setIntegerArrayValue(integerArrayValue);
this.value.setType(ParameterType.PARAMETER_INTEGER_ARRAY.getValue());
}

public ParameterVariant(final String name, final Double[] doubleArrayValue) {
public ParameterVariant(final String name, final double[] doubleArrayValue) {
this.name = name;
this.value = new rcl_interfaces.msg.ParameterValue();
this.value.setDoubleArrayValue(Arrays.asList(doubleArrayValue));
this.value.setDoubleArrayValue(doubleArrayValue);
this.value.setType(ParameterType.PARAMETER_DOUBLE_ARRAY.getValue());
}

public ParameterVariant(final String name, final String[] stringArrayValue) {
this.name = name;
this.value = new rcl_interfaces.msg.ParameterValue();
this.value.setStringArrayValue(Arrays.asList(stringArrayValue));
this.value.setStringArrayValue(stringArrayValue);
this.value.setType(ParameterType.PARAMETER_STRING_ARRAY.getValue());
}

Expand Down Expand Up @@ -203,39 +203,39 @@ public final boolean asBool() {
return this.value.getBoolValue();
}

public final Byte[] asByteArray() {
public final byte[] asByteArray() {
if (getType() != ParameterType.PARAMETER_BYTE_ARRAY) {
throw new IllegalArgumentException("Invalid type");
}
return this.value.getByteArrayValue().toArray(new Byte[0]);
return this.value.getByteArrayValue();
}

public final Boolean[] asBooleanArray() {
public final boolean[] asBooleanArray() {
if (getType() != ParameterType.PARAMETER_BOOL_ARRAY) {
throw new IllegalArgumentException("Invalid type");
}
return this.value.getBoolArrayValue().toArray(new Boolean[0]);
return this.value.getBoolArrayValue();
}

public final Long[] asIntegerArray() {
public final long[] asIntegerArray() {
if (getType() != ParameterType.PARAMETER_INTEGER_ARRAY) {
throw new IllegalArgumentException("Invalid type");
}
return this.value.getIntegerArrayValue().toArray(new Long[0]);
return this.value.getIntegerArrayValue();
}

public final Double[] asDoubleArray() {
public final double[] asDoubleArray() {
if (getType() != ParameterType.PARAMETER_DOUBLE_ARRAY) {
throw new IllegalArgumentException("Invalid type");
}
return this.value.getDoubleArrayValue().toArray(new Double[0]);
return this.value.getDoubleArrayValue();
}

public final String[] asStringArray() {
if (getType() != ParameterType.PARAMETER_STRING_ARRAY) {
throw new IllegalArgumentException("Invalid type");
}
return this.value.getStringArrayValue().toArray(new String[0]);
return this.value.getStringArrayValue();
}

public final rcl_interfaces.msg.Parameter toParameter() {
Expand All @@ -256,15 +256,15 @@ public static ParameterVariant fromParameter(final rcl_interfaces.msg.Parameter
case rcl_interfaces.msg.ParameterType.PARAMETER_STRING:
return new ParameterVariant(parameter.getName(), parameter.getValue().getStringValue());
case rcl_interfaces.msg.ParameterType.PARAMETER_BYTE_ARRAY:
return new ParameterVariant(parameter.getName(), parameter.getValue().getByteArrayValue().toArray(new Byte[0]));
return new ParameterVariant(parameter.getName(), parameter.getValue().getByteArrayValue());
case rcl_interfaces.msg.ParameterType.PARAMETER_BOOL_ARRAY:
return new ParameterVariant(parameter.getName(), parameter.getValue().getBoolArrayValue().toArray(new Boolean[0]));
return new ParameterVariant(parameter.getName(), parameter.getValue().getBoolArrayValue());
case rcl_interfaces.msg.ParameterType.PARAMETER_INTEGER_ARRAY:
return new ParameterVariant(parameter.getName(), parameter.getValue().getIntegerArrayValue().toArray(new Long[0]));
return new ParameterVariant(parameter.getName(), parameter.getValue().getIntegerArrayValue());
case rcl_interfaces.msg.ParameterType.PARAMETER_DOUBLE_ARRAY:
return new ParameterVariant(parameter.getName(), parameter.getValue().getDoubleArrayValue().toArray(new Double[0]));
return new ParameterVariant(parameter.getName(), parameter.getValue().getDoubleArrayValue());
case rcl_interfaces.msg.ParameterType.PARAMETER_STRING_ARRAY:
return new ParameterVariant(parameter.getName(), parameter.getValue().getStringArrayValue().toArray(new String[0]));
return new ParameterVariant(parameter.getName(), parameter.getValue().getStringArrayValue());
case rcl_interfaces.msg.ParameterType.PARAMETER_NOT_SET:
throw new IllegalArgumentException("Type from ParameterValue is not set");
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ public Future<List<ParameterVariant>> getParameters(
request, new Consumer<Future<rcl_interfaces.srv.GetParameters_Response>>() {
public void accept(final Future<rcl_interfaces.srv.GetParameters_Response> future) {
List<ParameterVariant> parameterVariants = new ArrayList<ParameterVariant>();
List<rcl_interfaces.msg.ParameterValue> pvalues = null;
rcl_interfaces.msg.ParameterValue[] pvalues = new rcl_interfaces.msg.ParameterValue[0];
try {
pvalues = future.get().getValues();
} catch (Exception e) {
// TODO(esteve): do something
}
for (int i = 0; i < pvalues.size(); i++) {
for (int i = 0; i < pvalues.length; i++) {
rcl_interfaces.msg.Parameter parameter = new rcl_interfaces.msg.Parameter();
parameter.setName(request.getNames().get(i));
parameter.setValue(pvalues.get(i));
parameter.setName(request.getNames()[i]);
parameter.setValue(pvalues[i]);
parameterVariants.add(ParameterVariant.fromParameter(parameter));
}
futureResult.set(parameterVariants);
Expand Down Expand Up @@ -151,13 +151,13 @@ public Future<List<ParameterType>> getParameterTypes(
request, new Consumer<Future<rcl_interfaces.srv.GetParameterTypes_Response>>() {
public void accept(final Future<rcl_interfaces.srv.GetParameterTypes_Response> future) {
List<ParameterType> parameterTypes = new ArrayList<ParameterType>();
List<Byte> pts = null;
byte[] pts = new byte[0];
try {
pts = future.get().getTypes();
} catch (Exception e) {
// TODO(esteve): do something
}
for (Byte pt : pts) {
for (byte pt : pts) {
parameterTypes.add(ParameterType.fromByte(pt));
}
futureResult.set(parameterTypes);
Expand Down Expand Up @@ -194,7 +194,7 @@ public Future<List<rcl_interfaces.msg.SetParametersResult>> setParameters(
public void accept(final Future<rcl_interfaces.srv.SetParameters_Response> future) {
List<rcl_interfaces.msg.SetParametersResult> setParametersResult = null;
try {
setParametersResult = future.get().getResults();
setParametersResult = future.get().getResultsAsList();
} catch (Exception e) {
// TODO(esteve): do something
}
Expand Down Expand Up @@ -297,7 +297,7 @@ public Future<List<rcl_interfaces.msg.ParameterDescriptor>> describeParameters(
public void accept(final Future<rcl_interfaces.srv.DescribeParameters_Response> future) {
List<rcl_interfaces.msg.ParameterDescriptor> parameterDescriptors = null;
try {
parameterDescriptors = future.get().getDescriptors();
parameterDescriptors = future.get().getDescriptorsAsList();
} catch (Exception e) {
// TODO(esteve): do something
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ParameterServiceImpl(final Node node, final QoSProfile qosProfile)
public void accept(RMWRequestId rmwRequestId,
rcl_interfaces.srv.GetParameters_Request request,
rcl_interfaces.srv.GetParameters_Response response) {
List<ParameterVariant> values = node.getParameters(request.getNames());
List<ParameterVariant> values = node.getParameters(request.getNamesAsList());
List<rcl_interfaces.msg.ParameterValue> pvalues =
new ArrayList<rcl_interfaces.msg.ParameterValue>();
for (ParameterVariant pvariant : values) {
Expand All @@ -67,7 +67,7 @@ public void accept(RMWRequestId rmwRequestId,
public void accept(RMWRequestId rmwRequestId,
rcl_interfaces.srv.GetParameterTypes_Request request,
rcl_interfaces.srv.GetParameterTypes_Response response) {
List<ParameterType> types = node.getParameterTypes(request.getNames());
List<ParameterType> types = node.getParameterTypes(request.getNamesAsList());
List<Byte> ptypes = new ArrayList<Byte>();
for (ParameterType type : types) {
ptypes.add(type.getValue());
Expand Down Expand Up @@ -124,7 +124,7 @@ public void accept(RMWRequestId rmwRequestId,
rcl_interfaces.srv.DescribeParameters_Request request,
rcl_interfaces.srv.DescribeParameters_Response response) {
List<rcl_interfaces.msg.ParameterDescriptor> descriptors =
node.describeParameters(request.getNames());
node.describeParameters(request.getNamesAsList());
response.setDescriptors(descriptors);
}
},
Expand All @@ -139,7 +139,7 @@ public void accept(RMWRequestId rmwRequestId,
rcl_interfaces.srv.ListParameters_Request request,
rcl_interfaces.srv.ListParameters_Response response) {
rcl_interfaces.msg.ListParametersResult result =
node.listParameters(request.getPrefixes(), request.getDepth());
node.listParameters(request.getPrefixesAsList(), request.getDepth());
response.setResult(result);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public final void testListParameters() {

rcl_interfaces.msg.ListParametersResult result = node.listParameters(new ArrayList<String>(), rcl_interfaces.srv.ListParameters_Request.DEPTH_RECURSIVE);
// 2 we added above plus the implicit "use_sim_time"
assertEquals(3, result.getNames().size());
assertEquals(3, result.getNames().length);
}

@Test
Expand Down
Loading

0 comments on commit c012ba8

Please sign in to comment.