ctf: Move CTF plugins to Java 7 and fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / VariantDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
3 *
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
8 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.ctf.core.event.types;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
19 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
20
21 /**
22 * A CTF variant definition (similar to a C union).
23 *
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.
27 *
28 * @version 1.0
29 * @author Matthew Khouzam
30 * @author Simon Marchi
31 */
32 public class VariantDefinition extends Definition implements IDefinitionScope {
33
34 // ------------------------------------------------------------------------
35 // Attributes
36 // ------------------------------------------------------------------------
37
38 private VariantDeclaration declaration;
39
40 private EnumDefinition tagDefinition;
41 private Map<String, Definition> definitions = new HashMap<>();
42 private String currentField;
43
44 // ------------------------------------------------------------------------
45 // Constructors
46 // ------------------------------------------------------------------------
47
48 /**
49 * Constructor
50 * @param declaration the parent declaration
51 * @param definitionScope the parent scope
52 * @param fieldName the field name
53 */
54 public VariantDefinition(VariantDeclaration declaration,
55 IDefinitionScope definitionScope, String fieldName) {
56 super(definitionScope, fieldName);
57
58 this.declaration = declaration;
59
60 Definition tagDef = definitionScope.lookupDefinition(declaration.getTag());
61 this.tagDefinition = (EnumDefinition) tagDef;
62
63 for (Map.Entry<String, IDeclaration> field : declaration.getFields().entrySet()) {
64 Definition fieldDef = field.getValue().createDefinition(this,
65 field.getKey());
66 definitions.put(field.getKey(), fieldDef);
67 }
68 }
69
70 // ------------------------------------------------------------------------
71 // Getters/Setters/Predicates
72 // ------------------------------------------------------------------------
73
74 @Override
75 public VariantDeclaration getDeclaration() {
76 return declaration;
77 }
78
79 /**
80 * Sets the variant declaration
81 * @param declaration the variant declaration
82 */
83 public void setDeclaration(VariantDeclaration declaration) {
84 this.declaration = declaration;
85 }
86
87 /**
88 * Gets the tag
89 * @return the tag definition
90 */
91 public EnumDefinition getTagDefinition() {
92 return tagDefinition;
93 }
94
95 /**
96 * Sets the tag
97 * @param tagDefinition the tag
98 */
99 public void setTagDefinition(EnumDefinition tagDefinition) {
100 this.tagDefinition = tagDefinition;
101 }
102
103 /**
104 * Get the definitions in the variant
105 * @return the definitions
106 * @since 2.0
107 */
108 public Map<String, Definition> getDefinitions() {
109 return definitions;
110 }
111
112 /**
113 * Set the definitions in a variant
114 * @param definitions the definitions
115 * @since 2.0
116 */
117 public void setDefinitions(Map<String, Definition> definitions) {
118 this.definitions = definitions;
119 }
120
121 /**
122 * Set the current field
123 * @param currentField the current field
124 */
125 public void setCurrentField(String currentField) {
126 this.currentField = currentField;
127 }
128
129 /**
130 * Get the current field name
131 * @return the current field name
132 */
133 public String getCurrentFieldName() {
134 return currentField;
135 }
136
137 /**
138 * Get the current field
139 * @return the current field
140 */
141 public Definition getCurrentField() {
142 return definitions.get(currentField);
143 }
144
145
146 // ------------------------------------------------------------------------
147 // Operations
148 // ------------------------------------------------------------------------
149
150 @Override
151 public void read(BitBuffer input) throws CTFReaderException {
152 currentField = tagDefinition.getValue();
153
154 Definition field = definitions.get(currentField);
155 if (field == null) {
156 throw new CTFReaderException("Variant was not defined for: "+ currentField); //$NON-NLS-1$
157 }
158 field.read(input);
159 }
160
161 @Override
162 public Definition lookupDefinition(String lookupPath) {
163 return definitions.get(lookupPath);
164 }
165
166
167 /**
168 * Lookup an array in a struct. if the name returns a non-array (like an
169 * int) than the method returns null
170 *
171 * @param name
172 * the name of the array
173 * @return the array or null.
174 */
175 public ArrayDefinition lookupArray(String name) {
176 Definition def = lookupDefinition(name);
177 return (ArrayDefinition) ((def instanceof ArrayDefinition) ? def : null);
178 }
179
180 /**
181 * Lookup an enum in a struct. if the name returns a non-enum (like an int)
182 * than the method returns null
183 *
184 * @param name
185 * the name of the enum
186 * @return the enum or null.
187 */
188 public EnumDefinition lookupEnum(String name) {
189 Definition def = lookupDefinition(name);
190 return (EnumDefinition) ((def instanceof EnumDefinition) ? def : null);
191 }
192
193 /**
194 * Lookup an integer in a struct. if the name returns a non-integer (like an
195 * float) than the method returns null
196 *
197 * @param name
198 * the name of the integer
199 * @return the integer or null.
200 */
201 public IntegerDefinition lookupInteger(String name) {
202 Definition def = lookupDefinition(name);
203 return (IntegerDefinition) ((def instanceof IntegerDefinition) ? def
204 : null);
205 }
206
207 /**
208 * Lookup a sequence in a struct. if the name returns a non-sequence (like
209 * an int) than the method returns null
210 *
211 * @param name
212 * the name of the sequence
213 * @return the sequence or null.
214 */
215 public SequenceDefinition lookupSequence(String name) {
216 Definition def = lookupDefinition(name);
217 return (SequenceDefinition) ((def instanceof SequenceDefinition) ? def
218 : null);
219 }
220
221 /**
222 * Lookup a string in a struct. if the name returns a non-string (like
223 * an int) than the method returns null
224 *
225 * @param name
226 * the name of the string
227 * @return the string or null.
228 */
229 public StringDefinition lookupString(String name) {
230 Definition def = lookupDefinition(name);
231 return (StringDefinition) ((def instanceof StringDefinition) ? def
232 : null);
233 }
234
235 /**
236 * Lookup a struct in a struct. if the name returns a non-struct (like
237 * an int) than the method returns null
238 *
239 * @param name
240 * the name of the struct
241 * @return the struct or null.
242 */
243 public StructDefinition lookupStruct(String name) {
244 Definition def = lookupDefinition(name);
245 return (StructDefinition) ((def instanceof StructDefinition) ? def
246 : null);
247 }
248
249 /**
250 * Lookup a variant in a struct. if the name returns a non-variant (like
251 * an int) than the method returns null
252 *
253 * @param name
254 * the name of the variant
255 * @return the variant or null.
256 */
257 public VariantDefinition lookupVariant(String name) {
258 Definition def = lookupDefinition(name);
259 return (VariantDefinition) ((def instanceof VariantDefinition) ? def
260 : null);
261 }
262
263 @Override
264 public String toString() {
265 return "{ " + getCurrentFieldName() + //$NON-NLS-1$
266 " = " + getCurrentField() + //$NON-NLS-1$
267 " }"; //$NON-NLS-1$
268 }
269 }
This page took 0.043423 seconds and 6 git commands to generate.