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
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
22 * A CTF variant definition (similar to a C union).
24 * A variant is similar to a C union, only taking the minimum size of the types,
25 * it is a compound data type that contains other datatypes in fields. they are
26 * stored in an hashmap and indexed by names which are strings.
29 * @author Matthew Khouzam
30 * @author Simon Marchi
32 public class VariantDefinition
extends Definition
implements IDefinitionScope
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 private VariantDeclaration declaration
;
40 private EnumDefinition tagDefinition
;
41 private Map
<String
, Definition
> definitions
= new HashMap
<>();
42 private String currentField
;
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
50 * @param declaration the parent declaration
51 * @param definitionScope the parent scope
52 * @param fieldName the field name
54 public VariantDefinition(VariantDeclaration declaration
,
55 IDefinitionScope definitionScope
, String fieldName
) {
56 super(definitionScope
, fieldName
);
58 this.declaration
= declaration
;
60 Definition tagDef
= definitionScope
.lookupDefinition(declaration
.getTag());
61 this.tagDefinition
= (EnumDefinition
) tagDef
;
63 for (Map
.Entry
<String
, IDeclaration
> field
: declaration
.getFields().entrySet()) {
64 Definition fieldDef
= field
.getValue().createDefinition(this,
66 definitions
.put(field
.getKey(), fieldDef
);
70 // ------------------------------------------------------------------------
71 // Getters/Setters/Predicates
72 // ------------------------------------------------------------------------
75 public VariantDeclaration
getDeclaration() {
80 * Sets the variant declaration
81 * @param declaration the variant declaration
83 public void setDeclaration(VariantDeclaration declaration
) {
84 this.declaration
= declaration
;
89 * @return the tag definition
91 public EnumDefinition
getTagDefinition() {
97 * @param tagDefinition the tag
99 public void setTagDefinition(EnumDefinition tagDefinition
) {
100 this.tagDefinition
= tagDefinition
;
104 * Get the definitions in the variant
105 * @return the definitions
108 public Map
<String
, Definition
> getDefinitions() {
113 * Set the definitions in a variant
114 * @param definitions the definitions
117 public void setDefinitions(Map
<String
, Definition
> definitions
) {
118 this.definitions
= definitions
;
122 * Set the current field
123 * @param currentField the current field
125 public void setCurrentField(String currentField
) {
126 this.currentField
= currentField
;
130 * Get the current field name
131 * @return the current field name
133 public String
getCurrentFieldName() {
138 * Get the current field
139 * @return the current field
141 public Definition
getCurrentField() {
142 return definitions
.get(currentField
);
146 // ------------------------------------------------------------------------
148 // ------------------------------------------------------------------------
151 public void read(BitBuffer input
) throws CTFReaderException
{
152 currentField
= tagDefinition
.getValue();
154 Definition field
= definitions
.get(currentField
);
156 throw new CTFReaderException("Variant was not defined for: "+ currentField
); //$NON-NLS-1$
162 public Definition
lookupDefinition(String lookupPath
) {
163 return definitions
.get(lookupPath
);
168 * Lookup an array in a struct. if the name returns a non-array (like an
169 * int) than the method returns null
172 * the name of the array
173 * @return the array or null.
175 public ArrayDefinition
lookupArray(String name
) {
176 Definition def
= lookupDefinition(name
);
177 return (ArrayDefinition
) ((def
instanceof ArrayDefinition
) ? def
: null);
181 * Lookup an enum in a struct. if the name returns a non-enum (like an int)
182 * than the method returns null
185 * the name of the enum
186 * @return the enum or null.
188 public EnumDefinition
lookupEnum(String name
) {
189 Definition def
= lookupDefinition(name
);
190 return (EnumDefinition
) ((def
instanceof EnumDefinition
) ? def
: null);
194 * Lookup an integer in a struct. if the name returns a non-integer (like an
195 * float) than the method returns null
198 * the name of the integer
199 * @return the integer or null.
201 public IntegerDefinition
lookupInteger(String name
) {
202 Definition def
= lookupDefinition(name
);
203 return (IntegerDefinition
) ((def
instanceof IntegerDefinition
) ? def
208 * Lookup a sequence in a struct. if the name returns a non-sequence (like
209 * an int) than the method returns null
212 * the name of the sequence
213 * @return the sequence or null.
215 public SequenceDefinition
lookupSequence(String name
) {
216 Definition def
= lookupDefinition(name
);
217 return (SequenceDefinition
) ((def
instanceof SequenceDefinition
) ? def
222 * Lookup a string in a struct. if the name returns a non-string (like
223 * an int) than the method returns null
226 * the name of the string
227 * @return the string or null.
229 public StringDefinition
lookupString(String name
) {
230 Definition def
= lookupDefinition(name
);
231 return (StringDefinition
) ((def
instanceof StringDefinition
) ? def
236 * Lookup a struct in a struct. if the name returns a non-struct (like
237 * an int) than the method returns null
240 * the name of the struct
241 * @return the struct or null.
243 public StructDefinition
lookupStruct(String name
) {
244 Definition def
= lookupDefinition(name
);
245 return (StructDefinition
) ((def
instanceof StructDefinition
) ? def
250 * Lookup a variant in a struct. if the name returns a non-variant (like
251 * an int) than the method returns null
254 * the name of the variant
255 * @return the variant or null.
257 public VariantDefinition
lookupVariant(String name
) {
258 Definition def
= lookupDefinition(name
);
259 return (VariantDefinition
) ((def
instanceof VariantDefinition
) ? def
264 public String
toString() {
265 return "{ " + getCurrentFieldName() + //$NON-NLS-1$
266 " = " + getCurrentField() + //$NON-NLS-1$