Skip to content

Commit d37ffd3

Browse files
ctf: support int tags in variants
Add variant definition constructor for CTF2 Change-Id: I21f56a284c6fcee147d3714445ed30d81b706c1a Signed-off-by: Matthew Khouzam <[email protected]>
1 parent c3db7a0 commit d37ffd3

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

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

17+
import java.util.Arrays;
1718
import java.util.Collection;
1819
import java.util.Collections;
1920
import java.util.HashMap;
@@ -45,6 +46,7 @@ public class VariantDeclaration extends Declaration {
4546
private String fTag = null;
4647
private static final long ALIGNMENT = 1;
4748
private final Map<String, IDeclaration> fFields = Collections.synchronizedMap(new HashMap<String, IDeclaration>());
49+
private IDeclaration[] fFieldArray = new IDeclaration[0];
4850
private IDeclaration fDeclarationToPopulate;
4951

5052
// ------------------------------------------------------------------------
@@ -122,16 +124,32 @@ public VariantDefinition createDefinition(IDefinitionScope definitionScope,
122124
String fieldName, BitBuffer input) throws CTFException {
123125
alignRead(input);
124126
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());
127+
String varFieldName = fTag;
128+
SimpleDatatypeDefinition tagDef = null;
129+
if (def instanceof IntegerDefinition) {
130+
tagDef = (SimpleDatatypeDefinition) def;
131+
Long tagValue = ((IntegerDefinition) tagDef).getIntegerValue();
132+
String mappings = ((IntegerDefinition) tagDef).getMappings();
133+
if (mappings != null) {
134+
fDeclarationToPopulate = fFields.get(mappings);
135+
} else if (tagValue != null) {
136+
if (tagValue < 0 || tagValue >= fFieldArray.length) {
137+
throw new CTFException("Unknown value of " + tagValue + " for the variant " + toString()); //$NON-NLS-1$ //$NON-NLS-2$
138+
}
139+
fDeclarationToPopulate = fFieldArray[(tagValue.intValue())];
140+
}
141+
} else {
142+
tagDef = (EnumDefinition) ((def instanceof EnumDefinition) ? def : null);
143+
if (tagDef == null) {
144+
throw new CTFException("Tag is not defined " + fTag); //$NON-NLS-1$
145+
}
146+
varFieldName = tagDef.getStringValue();
147+
if (varFieldName == null) {
148+
throw new CTFException("Undefined enum selector for variant " + //$NON-NLS-1$
149+
definitionScope.getScopePath().getPath());
150+
}
151+
fDeclarationToPopulate = fFields.get(varFieldName);
133152
}
134-
fDeclarationToPopulate = fFields.get(varFieldName);
135153
if (fDeclarationToPopulate == null) {
136154
throw new CTFException("Unknown enum selector for variant " + //$NON-NLS-1$
137155
definitionScope.getScopePath().getPath());
@@ -150,6 +168,9 @@ public VariantDefinition createDefinition(IDefinitionScope definitionScope,
150168
*/
151169
public void addField(String fieldTag, IDeclaration declaration) {
152170
fFields.put(fieldTag, declaration);
171+
int size = fFields.size();
172+
fFieldArray = Arrays.copyOf(fFieldArray, size);
173+
fFieldArray[size-1]=declaration;
153174
}
154175

155176
@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)