Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
4bd7f2db | 2 | * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
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; | |
2b7f6f09 | 16 | import java.util.List; |
866e5b51 FC |
17 | import java.util.ListIterator; |
18 | ||
486efb2e | 19 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
866e5b51 FC |
20 | |
21 | /** | |
d37aaa7f | 22 | * A CTF structure definition (similar to a C structure). |
486efb2e | 23 | * |
d37aaa7f FC |
24 | * A structure is similar to a C structure, it is a compound data type that |
25 | * contains other datatypes in fields. they are stored in an hashmap and indexed | |
26 | * by names which are strings. | |
27 | * | |
28 | * @version 1.0 | |
29 | * @author Matthew Khouzam | |
30 | * @author Simon Marchi | |
866e5b51 FC |
31 | */ |
32 | public class StructDefinition extends Definition implements IDefinitionScope { | |
33 | ||
34 | // ------------------------------------------------------------------------ | |
35 | // Attributes | |
36 | // ------------------------------------------------------------------------ | |
37 | ||
38 | private final StructDeclaration declaration; | |
39 | private final HashMap<String, Definition> definitions = new HashMap<String, Definition>(); | |
40 | ||
41 | // ------------------------------------------------------------------------ | |
42 | // Constructors | |
43 | // ------------------------------------------------------------------------ | |
44 | ||
9ac2eb62 MK |
45 | /** |
46 | * Constructor | |
47 | * | |
48 | * @param declaration | |
49 | * the parent declaration | |
50 | * @param definitionScope | |
51 | * the parent scope | |
be6df2d8 | 52 | * @param structFieldName |
9ac2eb62 MK |
53 | * the field name |
54 | */ | |
866e5b51 FC |
55 | public StructDefinition(StructDeclaration declaration, |
56 | IDefinitionScope definitionScope, String structFieldName) { | |
57 | super(definitionScope, structFieldName); | |
58 | ||
59 | this.declaration = declaration; | |
60 | ||
61 | for (String fName : declaration.getFieldsList()) { | |
62 | IDeclaration fieldDecl = declaration.getFields().get(fName); | |
63 | assert (fieldDecl != null); | |
64 | ||
65 | Definition def = fieldDecl.createDefinition(this, fName); | |
66 | definitions.put(fName, def); | |
67 | } | |
68 | } | |
69 | ||
70 | // ------------------------------------------------------------------------ | |
71 | // Getters/Setters/Predicates | |
72 | // ------------------------------------------------------------------------ | |
73 | ||
74 | @Override | |
75 | public String getPath() { | |
76 | return path; | |
77 | } | |
78 | ||
9ac2eb62 MK |
79 | /** |
80 | * @return The definitions of all the fields | |
81 | */ | |
866e5b51 FC |
82 | public HashMap<String, Definition> getDefinitions() { |
83 | return definitions; | |
84 | } | |
85 | ||
9ac2eb62 | 86 | @Override |
866e5b51 FC |
87 | public StructDeclaration getDeclaration() { |
88 | return declaration; | |
89 | } | |
90 | ||
91 | // ------------------------------------------------------------------------ | |
92 | // Operations | |
93 | // ------------------------------------------------------------------------ | |
94 | ||
95 | @Override | |
96 | public void read(BitBuffer input) { | |
2b7f6f09 | 97 | final int align = (int) declaration.getAlignment(); |
9ac2eb62 MK |
98 | int pos = input.position() |
99 | + ((align - (input.position() % align)) % align); | |
2b7f6f09 MK |
100 | input.position(pos); |
101 | final List<String> fieldList = declaration.getFieldsList(); | |
102 | for (String fName : fieldList) { | |
866e5b51 FC |
103 | Definition def = definitions.get(fName); |
104 | assert (def != null); | |
866e5b51 FC |
105 | def.read(input); |
106 | } | |
107 | } | |
108 | ||
109 | @Override | |
110 | public Definition lookupDefinition(String lookupPath) { | |
111 | /* | |
112 | * The fields are created in order of appearance, so if a variant or | |
113 | * sequence refers to a field that is after it, the field's definition | |
114 | * will not be there yet in the hashmap. | |
115 | */ | |
91847dfc FC |
116 | Definition retVal = definitions.get(lookupPath); |
117 | if (retVal == null) { | |
118 | retVal = definitions.get("_" + lookupPath); //$NON-NLS-1$ | |
119 | } | |
120 | return retVal; | |
866e5b51 FC |
121 | } |
122 | ||
9ac2eb62 MK |
123 | /** |
124 | * Lookup an array in a struct. if the name returns a non-array (like an | |
125 | * int) than the method returns null | |
126 | * | |
127 | * @param name | |
128 | * the name of the array | |
129 | * @return the array or null. | |
130 | */ | |
866e5b51 | 131 | public ArrayDefinition lookupArray(String name) { |
91847dfc | 132 | Definition def = lookupDefinition(name); |
866e5b51 FC |
133 | return (ArrayDefinition) ((def instanceof ArrayDefinition) ? def : null); |
134 | } | |
135 | ||
9ac2eb62 MK |
136 | /** |
137 | * Lookup an enum in a struct. if the name returns a non-enum (like an int) | |
138 | * than the method returns null | |
139 | * | |
140 | * @param name | |
141 | * the name of the enum | |
142 | * @return the enum or null. | |
143 | */ | |
866e5b51 | 144 | public EnumDefinition lookupEnum(String name) { |
91847dfc | 145 | Definition def = lookupDefinition(name); |
866e5b51 FC |
146 | return (EnumDefinition) ((def instanceof EnumDefinition) ? def : null); |
147 | } | |
148 | ||
9ac2eb62 MK |
149 | /** |
150 | * Lookup an integer in a struct. if the name returns a non-integer (like an | |
151 | * float) than the method returns null | |
152 | * | |
153 | * @param name | |
154 | * the name of the integer | |
155 | * @return the integer or null. | |
156 | */ | |
866e5b51 | 157 | public IntegerDefinition lookupInteger(String name) { |
91847dfc | 158 | Definition def = lookupDefinition(name); |
866e5b51 FC |
159 | return (IntegerDefinition) ((def instanceof IntegerDefinition) ? def |
160 | : null); | |
161 | } | |
162 | ||
9ac2eb62 MK |
163 | /** |
164 | * Lookup a sequence in a struct. if the name returns a non-sequence (like | |
165 | * an int) than the method returns null | |
166 | * | |
167 | * @param name | |
168 | * the name of the sequence | |
169 | * @return the sequence or null. | |
170 | */ | |
866e5b51 | 171 | public SequenceDefinition lookupSequence(String name) { |
91847dfc | 172 | Definition def = lookupDefinition(name); |
866e5b51 FC |
173 | return (SequenceDefinition) ((def instanceof SequenceDefinition) ? def |
174 | : null); | |
175 | } | |
176 | ||
9ac2eb62 MK |
177 | /** |
178 | * Lookup a string in a struct. if the name returns a non-string (like | |
179 | * an int) than the method returns null | |
180 | * | |
181 | * @param name | |
182 | * the name of the string | |
183 | * @return the string or null. | |
184 | */ | |
866e5b51 | 185 | public StringDefinition lookupString(String name) { |
91847dfc | 186 | Definition def = lookupDefinition(name); |
866e5b51 FC |
187 | return (StringDefinition) ((def instanceof StringDefinition) ? def |
188 | : null); | |
189 | } | |
190 | ||
9ac2eb62 MK |
191 | /** |
192 | * Lookup a struct in a struct. if the name returns a non-struct (like | |
193 | * an int) than the method returns null | |
194 | * | |
195 | * @param name | |
196 | * the name of the struct | |
197 | * @return the struct or null. | |
198 | */ | |
866e5b51 | 199 | public StructDefinition lookupStruct(String name) { |
91847dfc | 200 | Definition def = lookupDefinition(name); |
866e5b51 FC |
201 | return (StructDefinition) ((def instanceof StructDefinition) ? def |
202 | : null); | |
203 | } | |
204 | ||
9ac2eb62 MK |
205 | /** |
206 | * Lookup a variant in a struct. if the name returns a non-variant (like | |
207 | * an int) than the method returns null | |
208 | * | |
209 | * @param name | |
210 | * the name of the variant | |
211 | * @return the variant or null. | |
212 | */ | |
866e5b51 | 213 | public VariantDefinition lookupVariant(String name) { |
91847dfc | 214 | Definition def = lookupDefinition(name); |
866e5b51 FC |
215 | return (VariantDefinition) ((def instanceof VariantDefinition) ? def |
216 | : null); | |
217 | } | |
218 | ||
219 | @Override | |
220 | public String toString() { | |
221 | StringBuilder builder = new StringBuilder(); | |
222 | ||
419f09a8 | 223 | builder.append("{ "); //$NON-NLS-1$ |
866e5b51 | 224 | |
9ac2eb62 MK |
225 | ListIterator<String> listIterator = this.declaration.getFieldsList() |
226 | .listIterator(); | |
866e5b51 FC |
227 | |
228 | while (listIterator.hasNext()) { | |
229 | String field = listIterator.next(); | |
419f09a8 SM |
230 | |
231 | builder.append(field); | |
232 | builder.append(" = "); //$NON-NLS-1$ | |
91847dfc | 233 | builder.append(lookupDefinition(field).toString()); |
419f09a8 SM |
234 | |
235 | if (listIterator.hasNext()) { | |
866e5b51 FC |
236 | builder.append(", "); //$NON-NLS-1$ |
237 | } | |
238 | } | |
239 | ||
419f09a8 | 240 | builder.append(" }"); //$NON-NLS-1$ |
866e5b51 FC |
241 | |
242 | return builder.toString(); | |
243 | } | |
244 | } |