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