1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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
.ctf
.core
.event
.io
.BitBuffer
;
21 * A CTF variant definition (similar to a C union).
23 * A variant is similar to a C union, only taking the minimum size of the types,
24 * it is a compound data type that contains other datatypes in fields. they are
25 * stored in an hashmap and indexed by names which are strings.
28 * @author Matthew Khouzam
29 * @author Simon Marchi
31 public class VariantDefinition
extends Definition
implements IDefinitionScope
{
33 // ------------------------------------------------------------------------
35 // ------------------------------------------------------------------------
37 private VariantDeclaration declaration
;
39 private EnumDefinition tagDefinition
;
40 private HashMap
<String
, Definition
> definitions
= new HashMap
<String
, Definition
>();
41 private String currentField
;
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
49 * @param declaration the parent declaration
50 * @param definitionScope the parent scope
51 * @param fieldName the field name
53 public VariantDefinition(VariantDeclaration declaration
,
54 IDefinitionScope definitionScope
, String fieldName
) {
55 super(definitionScope
, fieldName
);
57 this.declaration
= declaration
;
59 Definition tagDef
= definitionScope
.lookupDefinition(declaration
.getTag());
61 * if (tagDef == null) { throw new
62 * Exception("Variant tag field not found"); }
64 * if (!(tagDef instanceof EnumDefinition)) { throw new
65 * Exception("Variant tag field not enum"); }
67 this.tagDefinition
= (EnumDefinition
) tagDef
;
69 for (Map
.Entry
<String
, IDeclaration
> field
: declaration
.getFields().entrySet()) {
70 Definition fieldDef
= field
.getValue().createDefinition(this,
72 definitions
.put(field
.getKey(), fieldDef
);
76 // ------------------------------------------------------------------------
77 // Getters/Setters/Predicates
78 // ------------------------------------------------------------------------
81 public VariantDeclaration
getDeclaration() {
86 * Sets the variant declaration
87 * @param declaration the variant declaration
89 public void setDeclaration(VariantDeclaration declaration
) {
90 this.declaration
= declaration
;
95 * @return the tag definition
97 public EnumDefinition
getTagDefinition() {
103 * @param tagDefinition the tag
105 public void setTagDefinition(EnumDefinition tagDefinition
) {
106 this.tagDefinition
= tagDefinition
;
110 * Get the definitions in the variant
111 * @return the definitions
113 public HashMap
<String
, Definition
> getDefinitions() {
118 * Set the definitions in a variant
119 * @param definitions the definitions
121 public void setDefinitions(HashMap
<String
, Definition
> definitions
) {
122 this.definitions
= definitions
;
126 * Set the current field
127 * @param currentField the current field
129 public void setCurrentField(String currentField
) {
130 this.currentField
= currentField
;
134 public String
getPath() {
139 * Get the current field name
140 * @return the current field name
142 public String
getCurrentFieldName() {
147 * Get the current field
148 * @return the current field
150 public Definition
getCurrentField() {
151 return definitions
.get(currentField
);
155 // ------------------------------------------------------------------------
157 // ------------------------------------------------------------------------
160 public void read(BitBuffer input
) {
161 currentField
= tagDefinition
.getValue();
163 Definition field
= definitions
.get(currentField
);
169 public Definition
lookupDefinition(String lookupPath
) {
170 return definitions
.get(lookupPath
);
175 * Lookup an array in a struct. if the name returns a non-array (like an
176 * int) than the method returns null
179 * the name of the array
180 * @return the array or null.
182 public ArrayDefinition
lookupArray(String name
) {
183 Definition def
= lookupDefinition(name
);
184 return (ArrayDefinition
) ((def
instanceof ArrayDefinition
) ? def
: null);
188 * Lookup an enum in a struct. if the name returns a non-enum (like an int)
189 * than the method returns null
192 * the name of the enum
193 * @return the enum or null.
195 public EnumDefinition
lookupEnum(String name
) {
196 Definition def
= lookupDefinition(name
);
197 return (EnumDefinition
) ((def
instanceof EnumDefinition
) ? def
: null);
201 * Lookup an integer in a struct. if the name returns a non-integer (like an
202 * float) than the method returns null
205 * the name of the integer
206 * @return the integer or null.
208 public IntegerDefinition
lookupInteger(String name
) {
209 Definition def
= lookupDefinition(name
);
210 return (IntegerDefinition
) ((def
instanceof IntegerDefinition
) ? def
215 * Lookup a sequence in a struct. if the name returns a non-sequence (like
216 * an int) than the method returns null
219 * the name of the sequence
220 * @return the sequence or null.
222 public SequenceDefinition
lookupSequence(String name
) {
223 Definition def
= lookupDefinition(name
);
224 return (SequenceDefinition
) ((def
instanceof SequenceDefinition
) ? def
229 * Lookup a string in a struct. if the name returns a non-string (like
230 * an int) than the method returns null
233 * the name of the string
234 * @return the string or null.
236 public StringDefinition
lookupString(String name
) {
237 Definition def
= lookupDefinition(name
);
238 return (StringDefinition
) ((def
instanceof StringDefinition
) ? def
243 * Lookup a struct in a struct. if the name returns a non-struct (like
244 * an int) than the method returns null
247 * the name of the struct
248 * @return the struct or null.
250 public StructDefinition
lookupStruct(String name
) {
251 Definition def
= lookupDefinition(name
);
252 return (StructDefinition
) ((def
instanceof StructDefinition
) ? def
257 * Lookup a variant in a struct. if the name returns a non-variant (like
258 * an int) than the method returns null
261 * the name of the variant
262 * @return the variant or null.
264 public VariantDefinition
lookupVariant(String name
) {
265 Definition def
= lookupDefinition(name
);
266 return (VariantDefinition
) ((def
instanceof VariantDefinition
) ? def
271 public String
toString() {
272 return "{ " + getCurrentFieldName() + //$NON-NLS-1$
273 " = " + getCurrentField() + //$NON-NLS-1$