Skip to content

Commit 7aaa6a0

Browse files
ctf: support int tags in variants
Add variant definition constructor for CTF2 Change-Id: I21f56a284c6fcee147d3714445ed30d81b706c1a Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
1 parent ff5af01 commit 7aaa6a0

2 files changed

Lines changed: 60 additions & 12 deletions

File tree

ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/VariantDeclaration.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
package org.eclipse.tracecompass.ctf.core.event.types;
1616

17+
import java.util.Arrays;
1718
import java.util.Collection;
18-
import java.util.Collections;
1919
import java.util.HashMap;
2020
import java.util.Map;
2121
import java.util.Map.Entry;
@@ -44,7 +44,8 @@ public class VariantDeclaration extends Declaration {
4444

4545
private String fTag = null;
4646
private static final long ALIGNMENT = 1;
47-
private final Map<String, IDeclaration> fFields = Collections.synchronizedMap(new HashMap<String, IDeclaration>());
47+
private final Map<String, IDeclaration> fFields = new HashMap<>();
48+
private IDeclaration[] fFieldArray = new IDeclaration[0];
4849
private IDeclaration fDeclarationToPopulate;
4950

5051
// ------------------------------------------------------------------------
@@ -122,16 +123,32 @@ public VariantDefinition createDefinition(IDefinitionScope definitionScope,
122123
String fieldName, BitBuffer input) throws CTFException {
123124
alignRead(input);
124125
IDefinition def = definitionScope.lookupDefinition(fTag);
125-
EnumDefinition tagDef = (EnumDefinition) ((def instanceof EnumDefinition) ? def : null);
126-
if (tagDef == null) {
127-
throw new CTFException("Tag is not defined " + fTag); //$NON-NLS-1$
128-
}
129-
String varFieldName = tagDef.getStringValue();
130-
if (varFieldName == null) {
131-
throw new CTFException("Undefined enum selector for variant " + //$NON-NLS-1$
132-
definitionScope.getScopePath().getPath());
126+
String varFieldName = fTag;
127+
SimpleDatatypeDefinition tagDef = null;
128+
if (def instanceof IntegerDefinition) {
129+
tagDef = (SimpleDatatypeDefinition) def;
130+
Long tagValue = ((IntegerDefinition) tagDef).getIntegerValue();
131+
String mappings = ((IntegerDefinition) tagDef).getMappings();
132+
if (mappings != null) {
133+
fDeclarationToPopulate = fFields.get(mappings);
134+
} else if (tagValue != null) {
135+
if (tagValue < 0 || tagValue >= fFieldArray.length) {
136+
throw new CTFException("Unknown value of " + tagValue + " for the variant " + toString()); //$NON-NLS-1$ //$NON-NLS-2$
137+
}
138+
fDeclarationToPopulate = fFieldArray[(tagValue.intValue())];
139+
}
140+
} else {
141+
tagDef = (EnumDefinition) ((def instanceof EnumDefinition) ? def : null);
142+
if (tagDef == null) {
143+
throw new CTFException("Tag is not defined " + fTag); //$NON-NLS-1$
144+
}
145+
varFieldName = tagDef.getStringValue();
146+
if (varFieldName == null) {
147+
throw new CTFException("Undefined enum selector for variant " + //$NON-NLS-1$
148+
definitionScope.getScopePath().getPath());
149+
}
150+
fDeclarationToPopulate = fFields.get(varFieldName);
133151
}
134-
fDeclarationToPopulate = fFields.get(varFieldName);
135152
if (fDeclarationToPopulate == null) {
136153
throw new CTFException("Unknown enum selector for variant " + //$NON-NLS-1$
137154
definitionScope.getScopePath().getPath());
@@ -150,6 +167,9 @@ public VariantDefinition createDefinition(IDefinitionScope definitionScope,
150167
*/
151168
public void addField(String fieldTag, IDeclaration declaration) {
152169
fFields.put(fieldTag, declaration);
170+
int size = fFields.size();
171+
fFieldArray = Arrays.copyOf(fFieldArray, size);
172+
fFieldArray[size - 1] = declaration;
153173
}
154174

155175
@Override

ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/VariantDefinition.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,39 @@ public final class VariantDefinition extends ScopedDefinition {
3636
private final Definition fDefinition;
3737
private final String fCurrentField;
3838
private final String fFieldName;
39-
private final EnumDefinition fTagDef;
39+
private final SimpleDatatypeDefinition fTagDef;
4040

4141
// ------------------------------------------------------------------------
4242
// Constructors
4343
// ------------------------------------------------------------------------
4444

45+
/**
46+
* Constructor
47+
*
48+
* @param declaration
49+
* the parent declaration
50+
* @param definitionScope
51+
* the parent scope
52+
* @param tagDef
53+
* the tagging definition
54+
* @param selectedField
55+
* the selected field
56+
* @param fieldName
57+
* the field name
58+
* @param fieldValue
59+
* the field value
60+
* @since 5.1
61+
*/
62+
public VariantDefinition(@NonNull VariantDeclaration declaration, IDefinitionScope definitionScope, SimpleDatatypeDefinition tagDef, String selectedField, @NonNull String fieldName, Definition fieldValue) {
63+
super(declaration, definitionScope, fieldName);
64+
65+
fTagDef = tagDef;
66+
fFieldName = fieldName;
67+
fCurrentField = selectedField;
68+
fDefinition = fieldValue;
69+
}
70+
71+
4572
/**
4673
* Constructor
4774
*
@@ -68,6 +95,7 @@ public VariantDefinition(@NonNull VariantDeclaration declaration, IDefinitionSco
6895
fDefinition = fieldValue;
6996
}
7097

98+
7199
// ------------------------------------------------------------------------
72100
// Getters/Setters/Predicates
73101
// ------------------------------------------------------------------------

0 commit comments

Comments
 (0)