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