1414
1515package org .eclipse .tracecompass .ctf .core .event .types ;
1616
17+ import java .util .Arrays ;
1718import java .util .Collection ;
1819import java .util .Collections ;
1920import 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
0 commit comments