1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.event
.types
;
15 import java
.util
.HashMap
;
18 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.io
.BitBuffer
;
21 * <b><u>VariantDefinition</u></b>
23 public class VariantDefinition
extends Definition
implements IDefinitionScope
{
25 // ------------------------------------------------------------------------
27 // ------------------------------------------------------------------------
29 private VariantDeclaration declaration
;
31 private EnumDefinition tagDefinition
;
32 private HashMap
<String
, Definition
> definitions
= new HashMap
<String
, Definition
>();
33 private String currentField
;
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
41 * @param declaration the parent declaration
42 * @param definitionScope the parent scope
43 * @param fieldName the field name
45 public VariantDefinition(VariantDeclaration declaration
,
46 IDefinitionScope definitionScope
, String fieldName
) {
47 super(definitionScope
, fieldName
);
49 this.declaration
= declaration
;
51 Definition tagDef
= definitionScope
.lookupDefinition(declaration
.getTag());
53 * if (tagDef == null) { throw new
54 * Exception("Variant tag field not found"); }
56 * if (!(tagDef instanceof EnumDefinition)) { throw new
57 * Exception("Variant tag field not enum"); }
59 this.tagDefinition
= (EnumDefinition
) tagDef
;
61 for (Map
.Entry
<String
, IDeclaration
> field
: declaration
.getFields().entrySet()) {
62 Definition fieldDef
= field
.getValue().createDefinition(this,
64 definitions
.put(field
.getKey(), fieldDef
);
68 // ------------------------------------------------------------------------
69 // Getters/Setters/Predicates
70 // ------------------------------------------------------------------------
73 public VariantDeclaration
getDeclaration() {
78 * Sets the variant declaration
79 * @param declaration the variant declaration
81 public void setDeclaration(VariantDeclaration declaration
) {
82 this.declaration
= declaration
;
87 * @return the tag definition
89 public EnumDefinition
getTagDefinition() {
95 * @param tagDefinition the tag
97 public void setTagDefinition(EnumDefinition tagDefinition
) {
98 this.tagDefinition
= tagDefinition
;
102 * Get the definitions in the variant
103 * @return the definitions
105 public HashMap
<String
, Definition
> getDefinitions() {
110 * Set the definitions in a variant
111 * @param definitions the definitions
113 public void setDefinitions(HashMap
<String
, Definition
> definitions
) {
114 this.definitions
= definitions
;
118 * Set the current field
119 * @param currentField the current field
121 public void setCurrentField(String currentField
) {
122 this.currentField
= currentField
;
126 public String
getPath() {
131 * Get the current field name
132 * @return the current field name
134 public String
getCurrentFieldName() {
139 * Get the current field
140 * @return the current field
142 public Definition
getCurrentField() {
143 return definitions
.get(currentField
);
147 // ------------------------------------------------------------------------
149 // ------------------------------------------------------------------------
152 public void read(BitBuffer input
) {
153 currentField
= tagDefinition
.getValue();
155 Definition field
= definitions
.get(currentField
);
161 public Definition
lookupDefinition(String lookupPath
) {
162 return definitions
.get(lookupPath
);
167 * Lookup an array in a struct. if the name returns a non-array (like an
168 * int) than the method returns null
171 * the name of the array
172 * @return the array or null.
174 public ArrayDefinition
lookupArray(String name
) {
175 Definition def
= lookupDefinition(name
);
176 return (ArrayDefinition
) ((def
instanceof ArrayDefinition
) ? def
: null);
180 * Lookup an enum in a struct. if the name returns a non-enum (like an int)
181 * than the method returns null
184 * the name of the enum
185 * @return the enum or null.
187 public EnumDefinition
lookupEnum(String name
) {
188 Definition def
= lookupDefinition(name
);
189 return (EnumDefinition
) ((def
instanceof EnumDefinition
) ? def
: null);
193 * Lookup an integer in a struct. if the name returns a non-integer (like an
194 * float) than the method returns null
197 * the name of the integer
198 * @return the integer or null.
200 public IntegerDefinition
lookupInteger(String name
) {
201 Definition def
= lookupDefinition(name
);
202 return (IntegerDefinition
) ((def
instanceof IntegerDefinition
) ? def
207 * Lookup a sequence in a struct. if the name returns a non-sequence (like
208 * an int) than the method returns null
211 * the name of the sequence
212 * @return the sequence or null.
214 public SequenceDefinition
lookupSequence(String name
) {
215 Definition def
= lookupDefinition(name
);
216 return (SequenceDefinition
) ((def
instanceof SequenceDefinition
) ? def
221 * Lookup a string in a struct. if the name returns a non-string (like
222 * an int) than the method returns null
225 * the name of the string
226 * @return the string or null.
228 public StringDefinition
lookupString(String name
) {
229 Definition def
= lookupDefinition(name
);
230 return (StringDefinition
) ((def
instanceof StringDefinition
) ? def
235 * Lookup a struct in a struct. if the name returns a non-struct (like
236 * an int) than the method returns null
239 * the name of the struct
240 * @return the struct or null.
242 public StructDefinition
lookupStruct(String name
) {
243 Definition def
= lookupDefinition(name
);
244 return (StructDefinition
) ((def
instanceof StructDefinition
) ? def
249 * Lookup a variant in a struct. if the name returns a non-variant (like
250 * an int) than the method returns null
253 * the name of the variant
254 * @return the variant or null.
256 public VariantDefinition
lookupVariant(String name
) {
257 Definition def
= lookupDefinition(name
);
258 return (VariantDefinition
) ((def
instanceof VariantDefinition
) ? def