Commit | Line | Data |
---|---|---|
a3fc8213 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal |
a3fc8213 AM |
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 | * | |
d4a8d935 BH |
9 | * Contributors: |
10 | * Matthew Khouzam - Initial API and implementation | |
404b264a | 11 | * Alexandre Montplaisir - Initial API and implementation, extend TmfEventField |
d4a8d935 | 12 | * Bernd Hufmann - Add Enum field handling |
404b264a | 13 | * Geneviève Bastien - Add Struct and Variant field handling |
459f705b | 14 | * Jean-Christian Kouame - Correct handling of unsigned integer fields |
4591bed9 | 15 | * François Doray - Add generic array field type |
a3fc8213 AM |
16 | *******************************************************************************/ |
17 | ||
91e7f946 | 18 | package org.eclipse.linuxtools.tmf.ctf.core; |
a3fc8213 | 19 | |
a6223d74 | 20 | import java.util.ArrayList; |
7a6cee1a | 21 | import java.util.Arrays; |
7b4f13e6 | 22 | import java.util.Collection; |
a6223d74 MK |
23 | import java.util.List; |
24 | ||
7b4f13e6 | 25 | import org.eclipse.linuxtools.ctf.core.event.types.CompoundDeclaration; |
a3fc8213 | 26 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
21fb02fa | 27 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; |
a04464b1 | 28 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition; |
009883d7 | 29 | import org.eclipse.linuxtools.ctf.core.event.types.ICompositeDefinition; |
7b4f13e6 | 30 | import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration; |
cc98c947 | 31 | import org.eclipse.linuxtools.ctf.core.event.types.IDefinition; |
a3fc8213 AM |
32 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
33 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; | |
a3fc8213 | 34 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; |
404b264a | 35 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition; |
7b4f13e6 MK |
36 | import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDefinition; |
37 | import org.eclipse.linuxtools.internal.ctf.core.event.types.ByteArrayDefinition; | |
7a6cee1a | 38 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
68b18f2f | 39 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
a3fc8213 AM |
40 | |
41 | /** | |
7558a1e1 AM |
42 | * The CTF implementation of the TMF event field model |
43 | * | |
d4a8d935 | 44 | * @version 2.0 |
7558a1e1 | 45 | * @author Matthew Khouzam |
d09f973b | 46 | * @author Alexandre Montplaisir |
a3fc8213 | 47 | */ |
68b18f2f | 48 | public abstract class CtfTmfEventField extends TmfEventField { |
a3fc8213 | 49 | |
a3fc8213 | 50 | // ------------------------------------------------------------------------ |
7558a1e1 | 51 | // Constructor |
a3fc8213 AM |
52 | // ------------------------------------------------------------------------ |
53 | ||
b1baa808 | 54 | /** |
7558a1e1 AM |
55 | * Standard constructor. Only to be used internally, call parseField() to |
56 | * generate a new field object. | |
57 | * | |
58 | * @param name | |
59 | * The name of this field | |
68b18f2f AM |
60 | * @param value |
61 | * The value of this field. Its type should match the field type. | |
81ed27a8 MK |
62 | * @param fields |
63 | * The children fields. Useful for composite fields | |
68b18f2f | 64 | * @since 2.0 |
b1baa808 | 65 | */ |
81ed27a8 | 66 | protected CtfTmfEventField(String name, Object value, ITmfEventField[] fields) { |
68b18f2f AM |
67 | super(/* Strip the underscore from the field name if there is one */ |
68 | name.startsWith("_") ? name.substring(1) : name, //$NON-NLS-1$ | |
69 | value, | |
81ed27a8 | 70 | fields); |
a3fc8213 AM |
71 | } |
72 | ||
73 | // ------------------------------------------------------------------------ | |
74 | // Operations | |
75 | // ------------------------------------------------------------------------ | |
76 | ||
b1baa808 | 77 | /** |
7558a1e1 AM |
78 | * Factory method to instantiate CtfTmfEventField objects. |
79 | * | |
80 | * @param fieldDef | |
81 | * The CTF Definition of this event field | |
82 | * @param fieldName | |
83 | * String The name to assign to this field | |
84 | * @return The resulting CtfTmfEventField object | |
cc98c947 | 85 | * @deprecated use {@link CtfTmfEventField#parseField(IDefinition, String)} |
b1baa808 | 86 | */ |
cc98c947 | 87 | @Deprecated |
a3fc8213 AM |
88 | public static CtfTmfEventField parseField(Definition fieldDef, |
89 | String fieldName) { | |
cc98c947 MK |
90 | return parseField((IDefinition) fieldDef, fieldName); |
91 | } | |
92 | ||
93 | /** | |
94 | * Factory method to instantiate CtfTmfEventField objects. | |
95 | * | |
96 | * @param fieldDef | |
97 | * The CTF Definition of this event field | |
98 | * @param fieldName | |
99 | * String The name to assign to this field | |
100 | * @return The resulting CtfTmfEventField object | |
101 | * @since 3.1 | |
102 | */ | |
103 | public static CtfTmfEventField parseField(IDefinition fieldDef, | |
104 | String fieldName) { | |
a3fc8213 AM |
105 | CtfTmfEventField field = null; |
106 | ||
107 | /* Determine the Definition type */ | |
108 | if (fieldDef instanceof IntegerDefinition) { | |
367bcd2b AM |
109 | IntegerDefinition intDef = (IntegerDefinition) fieldDef; |
110 | int base = intDef.getDeclaration().getBase(); | |
459f705b | 111 | field = new CTFIntegerField(fieldName, intDef.getValue(), base, intDef.getDeclaration().isSigned()); |
a3fc8213 | 112 | |
21fb02fa MK |
113 | } else if (fieldDef instanceof EnumDefinition) { |
114 | EnumDefinition enumDef = (EnumDefinition) fieldDef; | |
68b18f2f | 115 | field = new CTFEnumField(fieldName, new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue())); |
21fb02fa | 116 | |
a3fc8213 | 117 | } else if (fieldDef instanceof StringDefinition) { |
68b18f2f AM |
118 | field = new CTFStringField(fieldName, ((StringDefinition) fieldDef).getValue()); |
119 | ||
120 | } else if (fieldDef instanceof FloatDefinition) { | |
121 | FloatDefinition floatDef = (FloatDefinition) fieldDef; | |
122 | field = new CTFFloatField(fieldName, floatDef.getValue()); | |
a3fc8213 AM |
123 | |
124 | } else if (fieldDef instanceof ArrayDefinition) { | |
125 | ArrayDefinition arrayDef = (ArrayDefinition) fieldDef; | |
7b4f13e6 MK |
126 | IDeclaration decl = arrayDef.getDeclaration(); |
127 | if (!(decl instanceof CompoundDeclaration)) { | |
128 | throw new IllegalArgumentException("Array definitions should only come from sequence or array declarations"); //$NON-NLS-1$ | |
129 | } | |
130 | CompoundDeclaration arrDecl = (CompoundDeclaration) decl; | |
131 | IDeclaration elemType = null; | |
132 | Collection<Definition> definitions = arrayDef.getDefinitions(); | |
133 | elemType = arrDecl.getElementType(); | |
134 | if (elemType instanceof IntegerDeclaration) { | |
135 | /* Array of integers => CTFIntegerArrayField */ | |
136 | IntegerDeclaration elemIntType = (IntegerDeclaration) elemType; | |
137 | long[] values = new long[arrayDef.getLength()]; | |
138 | for (int i = 0; i < arrayDef.getLength(); i++) { | |
cc98c947 | 139 | IDefinition elem = arrayDef.getDefinitions().get(i); |
7b4f13e6 MK |
140 | if (elem == null) { |
141 | break; | |
142 | } | |
143 | values[i] = ((IntegerDefinition) elem).getValue(); | |
144 | } | |
145 | field = new CTFIntegerArrayField(fieldName, values, | |
146 | elemIntType.getBase(), | |
147 | elemIntType.isSigned()); | |
a3fc8213 | 148 | |
4591bed9 FD |
149 | } else { |
150 | /* Arrays of elements of any other type */ | |
7b4f13e6 | 151 | CtfTmfEventField[] elements = new CtfTmfEventField[arrayDef.getLength()]; |
4591bed9 | 152 | /* Parse the elements of the array. */ |
7b4f13e6 | 153 | int i = 0; |
cc98c947 | 154 | for (IDefinition definition : definitions) { |
4591bed9 | 155 | CtfTmfEventField curField = CtfTmfEventField.parseField( |
7b4f13e6 | 156 | definition, fieldName + '[' + i + ']'); |
4591bed9 | 157 | elements[i] = curField; |
7b4f13e6 | 158 | i++; |
a3fc8213 | 159 | } |
a3fc8213 | 160 | |
4591bed9 FD |
161 | field = new CTFArrayField(fieldName, elements); |
162 | } | |
7b4f13e6 MK |
163 | } else if (fieldDef instanceof ByteArrayDefinition) { |
164 | /* This is an array of ascii bytes, a.k.a. a String! */ | |
165 | field = new CTFStringField(fieldName, fieldDef.toString()); | |
367bcd2b | 166 | |
009883d7 MK |
167 | } else if (fieldDef instanceof ICompositeDefinition) { |
168 | ICompositeDefinition strDef = (ICompositeDefinition) fieldDef; | |
7a6cee1a | 169 | |
a4524c1b | 170 | List<ITmfEventField> list = new ArrayList<>(); |
7a6cee1a | 171 | /* Recursively parse the fields */ |
a4fa4e36 MK |
172 | for (String curFieldName : strDef.getFieldNames()) { |
173 | list.add(CtfTmfEventField.parseField(strDef.getDefinition(curFieldName), curFieldName)); | |
7a6cee1a GB |
174 | } |
175 | field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()])); | |
a0e9eac8 | 176 | |
404b264a GB |
177 | } else if (fieldDef instanceof VariantDefinition) { |
178 | VariantDefinition varDef = (VariantDefinition) fieldDef; | |
179 | ||
180 | String curFieldName = varDef.getCurrentFieldName(); | |
cc98c947 | 181 | IDefinition curFieldDef = varDef.getCurrentField(); |
404b264a | 182 | if (curFieldDef != null) { |
51cc7ef4 GB |
183 | CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName); |
184 | field = new CTFVariantField(fieldName, subField); | |
404b264a GB |
185 | } else { |
186 | /* A safe-guard, but curFieldDef should never be null */ | |
187 | field = new CTFStringField(curFieldName, ""); //$NON-NLS-1$ | |
188 | } | |
189 | ||
190 | } else { | |
459f705b JCK |
191 | /* |
192 | * Safe-guard, to avoid null exceptions later, field is expected not | |
193 | * to be null | |
194 | */ | |
91e7f946 | 195 | field = new CTFStringField(fieldName, Messages.CtfTmfEventField_UnsupportedType + fieldDef.getClass().toString()); |
a3fc8213 | 196 | } |
a3fc8213 AM |
197 | return field; |
198 | } | |
199 | ||
a3fc8213 | 200 | @Override |
68b18f2f | 201 | public String toString() { |
51cc7ef4 | 202 | return getName() + '=' + getFormattedValue(); |
a3fc8213 AM |
203 | } |
204 | ||
a3fc8213 AM |
205 | } |
206 | ||
207 | /** | |
7558a1e1 AM |
208 | * The CTF field implementation for integer fields. |
209 | * | |
210 | * @author alexmont | |
a3fc8213 AM |
211 | */ |
212 | final class CTFIntegerField extends CtfTmfEventField { | |
213 | ||
6fc4ce56 MK |
214 | private final int fBase; |
215 | private final boolean fSigned; | |
a3fc8213 AM |
216 | |
217 | /** | |
218 | * A CTF "IntegerDefinition" can be an integer of any byte size, so in the | |
219 | * Java parser this is interpreted as a long. | |
7558a1e1 | 220 | * |
7558a1e1 AM |
221 | * @param name |
222 | * The name of this field | |
459f705b JCK |
223 | * @param longValue |
224 | * The integer value of this field | |
225 | * @param signed | |
226 | * Is the value signed or not | |
a3fc8213 | 227 | */ |
459f705b | 228 | CTFIntegerField(String name, long longValue, int base, boolean signed) { |
81ed27a8 | 229 | super(name, longValue, null); |
6fc4ce56 MK |
230 | fSigned = signed; |
231 | fBase = base; | |
367bcd2b AM |
232 | } |
233 | ||
a3fc8213 AM |
234 | @Override |
235 | public Long getValue() { | |
68b18f2f | 236 | return (Long) super.getValue(); |
a3fc8213 AM |
237 | } |
238 | ||
8f86c552 GB |
239 | @Override |
240 | public String getFormattedValue() { | |
6fc4ce56 | 241 | return IntegerDefinition.formatNumber(getValue(), fBase, fSigned); |
8f86c552 GB |
242 | } |
243 | ||
a3fc8213 AM |
244 | } |
245 | ||
246 | /** | |
7558a1e1 AM |
247 | * The CTF field implementation for string fields |
248 | * | |
249 | * @author alexmont | |
a3fc8213 AM |
250 | */ |
251 | final class CTFStringField extends CtfTmfEventField { | |
252 | ||
b1baa808 MK |
253 | /** |
254 | * Constructor for CTFStringField. | |
7558a1e1 AM |
255 | * |
256 | * @param strValue | |
257 | * The string value of this field | |
258 | * @param name | |
259 | * The name of this field | |
b1baa808 | 260 | */ |
68b18f2f | 261 | CTFStringField(String name, String strValue) { |
81ed27a8 | 262 | super(name, strValue, null); |
a3fc8213 AM |
263 | } |
264 | ||
a3fc8213 AM |
265 | @Override |
266 | public String getValue() { | |
68b18f2f | 267 | return (String) super.getValue(); |
a3fc8213 AM |
268 | } |
269 | } | |
270 | ||
271 | /** | |
7558a1e1 AM |
272 | * CTF field implementation for arrays of integers. |
273 | * | |
274 | * @author alexmont | |
a3fc8213 AM |
275 | */ |
276 | final class CTFIntegerArrayField extends CtfTmfEventField { | |
277 | ||
6fc4ce56 MK |
278 | private final int fBase; |
279 | private final boolean fSigned; | |
280 | private String fFormattedValue = null; | |
404b264a | 281 | |
b1baa808 MK |
282 | /** |
283 | * Constructor for CTFIntegerArrayField. | |
7558a1e1 | 284 | * |
459f705b JCK |
285 | * @param name |
286 | * The name of this field | |
7558a1e1 AM |
287 | * @param longValues |
288 | * The array of integers (as longs) that compose this field's | |
289 | * value | |
459f705b JCK |
290 | * @param signed |
291 | * Are the values in the array signed or not | |
b1baa808 | 292 | */ |
cefe3edf | 293 | CTFIntegerArrayField(String name, long[] longValues, int base, boolean signed) { |
81ed27a8 | 294 | super(name, longValues, null); |
6fc4ce56 MK |
295 | fBase = base; |
296 | fSigned = signed; | |
a3fc8213 AM |
297 | } |
298 | ||
a6223d74 | 299 | @Override |
cefe3edf AM |
300 | public long[] getValue() { |
301 | return (long[]) super.getValue(); | |
a3fc8213 | 302 | } |
404b264a | 303 | |
8f86c552 | 304 | @Override |
4591bed9 | 305 | public synchronized String getFormattedValue() { |
6fc4ce56 | 306 | if (fFormattedValue == null) { |
a4524c1b | 307 | List<String> strings = new ArrayList<>(); |
cefe3edf | 308 | for (long value : getValue()) { |
6fc4ce56 | 309 | strings.add(IntegerDefinition.formatNumber(value, fBase, fSigned)); |
8f86c552 | 310 | } |
6fc4ce56 | 311 | fFormattedValue = strings.toString(); |
8f86c552 | 312 | } |
6fc4ce56 | 313 | return fFormattedValue; |
8f86c552 GB |
314 | } |
315 | ||
a3fc8213 AM |
316 | } |
317 | ||
4591bed9 FD |
318 | /** |
319 | * CTF field implementation for arrays of arbitrary types. | |
320 | * | |
321 | * @author fdoray | |
322 | */ | |
323 | final class CTFArrayField extends CtfTmfEventField { | |
324 | ||
6fc4ce56 | 325 | private String fFormattedValue = null; |
4591bed9 FD |
326 | |
327 | /** | |
328 | * Constructor for CTFArrayField. | |
329 | * | |
330 | * @param name | |
331 | * The name of this field | |
332 | * @param elements | |
333 | * The array elements of this field | |
334 | */ | |
335 | CTFArrayField(String name, CtfTmfEventField[] elements) { | |
336 | super(name, elements, elements); | |
337 | } | |
338 | ||
339 | @Override | |
340 | public CtfTmfEventField[] getValue() { | |
341 | return (CtfTmfEventField[]) super.getValue(); | |
342 | } | |
343 | ||
344 | @Override | |
345 | public synchronized String getFormattedValue() { | |
6fc4ce56 | 346 | if (fFormattedValue == null) { |
a4524c1b | 347 | List<String> strings = new ArrayList<>(); |
4591bed9 FD |
348 | for (CtfTmfEventField element : getValue()) { |
349 | strings.add(element.getFormattedValue()); | |
350 | } | |
6fc4ce56 | 351 | fFormattedValue = strings.toString(); |
4591bed9 | 352 | } |
6fc4ce56 | 353 | return fFormattedValue; |
4591bed9 FD |
354 | } |
355 | } | |
356 | ||
b1baa808 | 357 | /** |
7558a1e1 AM |
358 | * CTF field implementation for floats. |
359 | * | |
360 | * @author emathko | |
b1baa808 | 361 | */ |
a04464b1 MK |
362 | final class CTFFloatField extends CtfTmfEventField { |
363 | ||
b1baa808 MK |
364 | /** |
365 | * Constructor for CTFFloatField. | |
7558a1e1 AM |
366 | * |
367 | * @param value | |
368 | * The float value (actually a double) of this field | |
369 | * @param name | |
370 | * The name of this field | |
b1baa808 | 371 | */ |
68b18f2f | 372 | protected CTFFloatField(String name, double value) { |
81ed27a8 | 373 | super(name, value, null); |
a04464b1 MK |
374 | } |
375 | ||
a04464b1 | 376 | @Override |
81c8e6f7 | 377 | public Double getValue() { |
68b18f2f | 378 | return (Double) super.getValue(); |
a04464b1 | 379 | } |
a04464b1 | 380 | } |
7558a1e1 | 381 | |
d4a8d935 BH |
382 | /** |
383 | * The CTF field implementation for Enum fields | |
384 | * | |
385 | * @author Bernd Hufmann | |
386 | */ | |
387 | final class CTFEnumField extends CtfTmfEventField { | |
388 | ||
d4a8d935 BH |
389 | /** |
390 | * Constructor for CTFEnumField. | |
391 | * | |
392 | * @param enumValue | |
459f705b JCK |
393 | * The Enum value consisting of a pair of Enum value name and its |
394 | * long value | |
d4a8d935 BH |
395 | * @param name |
396 | * The name of this field | |
397 | */ | |
68b18f2f AM |
398 | CTFEnumField(String name, CtfEnumPair enumValue) { |
399 | super(name, new CtfEnumPair(enumValue.getFirst(), | |
0126a8ca | 400 | enumValue.getSecond()), null); |
d4a8d935 BH |
401 | } |
402 | ||
d4a8d935 | 403 | @Override |
68b18f2f AM |
404 | public CtfEnumPair getValue() { |
405 | return (CtfEnumPair) super.getValue(); | |
d4a8d935 BH |
406 | } |
407 | } | |
408 | ||
7a6cee1a | 409 | /** |
51cc7ef4 | 410 | * The CTF field implementation for struct fields with sub-fields |
7a6cee1a GB |
411 | * |
412 | * @author gbastien | |
413 | */ | |
414 | final class CTFStructField extends CtfTmfEventField { | |
415 | ||
416 | /** | |
51cc7ef4 | 417 | * Constructor for CTFStructField. |
7a6cee1a | 418 | * |
51cc7ef4 GB |
419 | * @param fields |
420 | * The children of this field | |
7a6cee1a GB |
421 | * @param name |
422 | * The name of this field | |
423 | */ | |
424 | CTFStructField(String name, CtfTmfEventField[] fields) { | |
81ed27a8 | 425 | super(name, fields, fields); |
7a6cee1a GB |
426 | } |
427 | ||
7a6cee1a GB |
428 | @Override |
429 | public CtfTmfEventField[] getValue() { | |
430 | return (CtfTmfEventField[]) super.getValue(); | |
431 | } | |
432 | ||
433 | @Override | |
51cc7ef4 GB |
434 | public String getFormattedValue() { |
435 | return Arrays.toString(getValue()); | |
7a6cee1a | 436 | } |
51cc7ef4 GB |
437 | |
438 | } | |
439 | ||
440 | /** | |
441 | * The CTF field implementation for variant fields its child | |
442 | * | |
443 | * @author gbastien | |
444 | */ | |
445 | final class CTFVariantField extends CtfTmfEventField { | |
446 | ||
447 | /** | |
448 | * Constructor for CTFVariantField. | |
449 | * | |
450 | * @param field | |
451 | * The field selected for this variant | |
452 | * @param name | |
453 | * The name of this field | |
454 | */ | |
455 | CTFVariantField(String name, CtfTmfEventField field) { | |
459f705b | 456 | super(name, field, new CtfTmfEventField[] { field }); |
51cc7ef4 GB |
457 | } |
458 | ||
459 | @Override | |
460 | public CtfTmfEventField getValue() { | |
461 | return (CtfTmfEventField) super.getValue(); | |
462 | } | |
463 | ||
7a6cee1a GB |
464 | } |
465 | ||
a3fc8213 | 466 | /* Implement other possible fields types here... */ |