gdbtrace: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / CtfTmfEventField.java
CommitLineData
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
2bdf0193 18package org.eclipse.tracecompass.tmf.ctf.core;
a3fc8213 19
a6223d74 20import java.util.ArrayList;
7a6cee1a 21import java.util.Arrays;
7b4f13e6 22import java.util.Collection;
a6223d74
MK
23import java.util.List;
24
7b4f13e6 25import org.eclipse.linuxtools.ctf.core.event.types.CompoundDeclaration;
a3fc8213 26import org.eclipse.linuxtools.ctf.core.event.types.Definition;
21fb02fa 27import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
a04464b1 28import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition;
009883d7 29import org.eclipse.linuxtools.ctf.core.event.types.ICompositeDefinition;
7b4f13e6 30import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration;
cc98c947 31import org.eclipse.linuxtools.ctf.core.event.types.IDefinition;
a3fc8213
AM
32import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
33import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
a3fc8213 34import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
404b264a 35import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
7b4f13e6
MK
36import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDefinition;
37import org.eclipse.linuxtools.internal.ctf.core.event.types.ByteArrayDefinition;
2bdf0193
AM
38import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
39import org.eclipse.tracecompass.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 48public 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) {
373af311 135 /* Array of integers => CTFIntegerArrayField, unless it's a CTFStringField */
7b4f13e6 136 IntegerDeclaration elemIntType = (IntegerDeclaration) elemType;
373af311
MK
137 /* Are the integers characters and encoded? */
138 if (elemIntType.isCharacter()) {
139 /* it's a CTFStringField */
140 field = new CTFStringField(fieldName, arrayDef.toString());
141 } else {
142 /* it's a CTFIntegerArrayField */
143 long[] values = new long[arrayDef.getLength()];
144 for (int i = 0; i < arrayDef.getLength(); i++) {
145 IDefinition elem = arrayDef.getDefinitions().get(i);
146 if (elem == null) {
147 break;
148 }
149 values[i] = ((IntegerDefinition) elem).getValue();
7b4f13e6 150 }
373af311
MK
151 field = new CTFIntegerArrayField(fieldName, values,
152 elemIntType.getBase(),
153 elemIntType.isSigned());
7b4f13e6 154 }
4591bed9
FD
155 } else {
156 /* Arrays of elements of any other type */
7b4f13e6 157 CtfTmfEventField[] elements = new CtfTmfEventField[arrayDef.getLength()];
4591bed9 158 /* Parse the elements of the array. */
7b4f13e6 159 int i = 0;
cc98c947 160 for (IDefinition definition : definitions) {
4591bed9 161 CtfTmfEventField curField = CtfTmfEventField.parseField(
7b4f13e6 162 definition, fieldName + '[' + i + ']');
4591bed9 163 elements[i] = curField;
7b4f13e6 164 i++;
a3fc8213 165 }
a3fc8213 166
4591bed9
FD
167 field = new CTFArrayField(fieldName, elements);
168 }
7b4f13e6
MK
169 } else if (fieldDef instanceof ByteArrayDefinition) {
170 /* This is an array of ascii bytes, a.k.a. a String! */
171 field = new CTFStringField(fieldName, fieldDef.toString());
367bcd2b 172
009883d7
MK
173 } else if (fieldDef instanceof ICompositeDefinition) {
174 ICompositeDefinition strDef = (ICompositeDefinition) fieldDef;
7a6cee1a 175
a4524c1b 176 List<ITmfEventField> list = new ArrayList<>();
7a6cee1a 177 /* Recursively parse the fields */
a4fa4e36
MK
178 for (String curFieldName : strDef.getFieldNames()) {
179 list.add(CtfTmfEventField.parseField(strDef.getDefinition(curFieldName), curFieldName));
7a6cee1a
GB
180 }
181 field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()]));
a0e9eac8 182
404b264a
GB
183 } else if (fieldDef instanceof VariantDefinition) {
184 VariantDefinition varDef = (VariantDefinition) fieldDef;
185
186 String curFieldName = varDef.getCurrentFieldName();
cc98c947 187 IDefinition curFieldDef = varDef.getCurrentField();
404b264a 188 if (curFieldDef != null) {
51cc7ef4
GB
189 CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
190 field = new CTFVariantField(fieldName, subField);
404b264a
GB
191 } else {
192 /* A safe-guard, but curFieldDef should never be null */
193 field = new CTFStringField(curFieldName, ""); //$NON-NLS-1$
194 }
195
196 } else {
459f705b
JCK
197 /*
198 * Safe-guard, to avoid null exceptions later, field is expected not
199 * to be null
200 */
91e7f946 201 field = new CTFStringField(fieldName, Messages.CtfTmfEventField_UnsupportedType + fieldDef.getClass().toString());
a3fc8213 202 }
a3fc8213
AM
203 return field;
204 }
205
a3fc8213 206 @Override
68b18f2f 207 public String toString() {
51cc7ef4 208 return getName() + '=' + getFormattedValue();
a3fc8213
AM
209 }
210
a3fc8213
AM
211}
212
213/**
7558a1e1
AM
214 * The CTF field implementation for integer fields.
215 *
216 * @author alexmont
a3fc8213
AM
217 */
218final class CTFIntegerField extends CtfTmfEventField {
219
6fc4ce56
MK
220 private final int fBase;
221 private final boolean fSigned;
a3fc8213
AM
222
223 /**
224 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
225 * Java parser this is interpreted as a long.
7558a1e1 226 *
7558a1e1
AM
227 * @param name
228 * The name of this field
459f705b
JCK
229 * @param longValue
230 * The integer value of this field
231 * @param signed
232 * Is the value signed or not
a3fc8213 233 */
459f705b 234 CTFIntegerField(String name, long longValue, int base, boolean signed) {
81ed27a8 235 super(name, longValue, null);
6fc4ce56
MK
236 fSigned = signed;
237 fBase = base;
367bcd2b
AM
238 }
239
a3fc8213
AM
240 @Override
241 public Long getValue() {
68b18f2f 242 return (Long) super.getValue();
a3fc8213
AM
243 }
244
8f86c552
GB
245 @Override
246 public String getFormattedValue() {
6fc4ce56 247 return IntegerDefinition.formatNumber(getValue(), fBase, fSigned);
8f86c552
GB
248 }
249
a3fc8213
AM
250}
251
252/**
7558a1e1
AM
253 * The CTF field implementation for string fields
254 *
255 * @author alexmont
a3fc8213
AM
256 */
257final class CTFStringField extends CtfTmfEventField {
258
b1baa808
MK
259 /**
260 * Constructor for CTFStringField.
7558a1e1
AM
261 *
262 * @param strValue
263 * The string value of this field
264 * @param name
265 * The name of this field
b1baa808 266 */
68b18f2f 267 CTFStringField(String name, String strValue) {
81ed27a8 268 super(name, strValue, null);
a3fc8213
AM
269 }
270
a3fc8213
AM
271 @Override
272 public String getValue() {
68b18f2f 273 return (String) super.getValue();
a3fc8213
AM
274 }
275}
276
277/**
7558a1e1
AM
278 * CTF field implementation for arrays of integers.
279 *
280 * @author alexmont
a3fc8213
AM
281 */
282final class CTFIntegerArrayField extends CtfTmfEventField {
283
6fc4ce56
MK
284 private final int fBase;
285 private final boolean fSigned;
286 private String fFormattedValue = null;
404b264a 287
b1baa808
MK
288 /**
289 * Constructor for CTFIntegerArrayField.
7558a1e1 290 *
459f705b
JCK
291 * @param name
292 * The name of this field
7558a1e1
AM
293 * @param longValues
294 * The array of integers (as longs) that compose this field's
295 * value
459f705b
JCK
296 * @param signed
297 * Are the values in the array signed or not
b1baa808 298 */
cefe3edf 299 CTFIntegerArrayField(String name, long[] longValues, int base, boolean signed) {
81ed27a8 300 super(name, longValues, null);
6fc4ce56
MK
301 fBase = base;
302 fSigned = signed;
a3fc8213
AM
303 }
304
a6223d74 305 @Override
cefe3edf
AM
306 public long[] getValue() {
307 return (long[]) super.getValue();
a3fc8213 308 }
404b264a 309
8f86c552 310 @Override
4591bed9 311 public synchronized String getFormattedValue() {
6fc4ce56 312 if (fFormattedValue == null) {
a4524c1b 313 List<String> strings = new ArrayList<>();
cefe3edf 314 for (long value : getValue()) {
6fc4ce56 315 strings.add(IntegerDefinition.formatNumber(value, fBase, fSigned));
8f86c552 316 }
6fc4ce56 317 fFormattedValue = strings.toString();
8f86c552 318 }
6fc4ce56 319 return fFormattedValue;
8f86c552
GB
320 }
321
a3fc8213
AM
322}
323
4591bed9
FD
324/**
325 * CTF field implementation for arrays of arbitrary types.
326 *
327 * @author fdoray
328 */
329final class CTFArrayField extends CtfTmfEventField {
330
6fc4ce56 331 private String fFormattedValue = null;
4591bed9
FD
332
333 /**
334 * Constructor for CTFArrayField.
335 *
336 * @param name
337 * The name of this field
338 * @param elements
339 * The array elements of this field
340 */
341 CTFArrayField(String name, CtfTmfEventField[] elements) {
342 super(name, elements, elements);
343 }
344
345 @Override
346 public CtfTmfEventField[] getValue() {
347 return (CtfTmfEventField[]) super.getValue();
348 }
349
350 @Override
351 public synchronized String getFormattedValue() {
6fc4ce56 352 if (fFormattedValue == null) {
a4524c1b 353 List<String> strings = new ArrayList<>();
4591bed9
FD
354 for (CtfTmfEventField element : getValue()) {
355 strings.add(element.getFormattedValue());
356 }
6fc4ce56 357 fFormattedValue = strings.toString();
4591bed9 358 }
6fc4ce56 359 return fFormattedValue;
4591bed9
FD
360 }
361}
362
b1baa808 363/**
7558a1e1
AM
364 * CTF field implementation for floats.
365 *
366 * @author emathko
b1baa808 367 */
a04464b1
MK
368final class CTFFloatField extends CtfTmfEventField {
369
b1baa808
MK
370 /**
371 * Constructor for CTFFloatField.
7558a1e1
AM
372 *
373 * @param value
374 * The float value (actually a double) of this field
375 * @param name
376 * The name of this field
b1baa808 377 */
68b18f2f 378 protected CTFFloatField(String name, double value) {
81ed27a8 379 super(name, value, null);
a04464b1
MK
380 }
381
a04464b1 382 @Override
81c8e6f7 383 public Double getValue() {
68b18f2f 384 return (Double) super.getValue();
a04464b1 385 }
a04464b1 386}
7558a1e1 387
d4a8d935
BH
388/**
389 * The CTF field implementation for Enum fields
390 *
391 * @author Bernd Hufmann
392 */
393final class CTFEnumField extends CtfTmfEventField {
394
d4a8d935
BH
395 /**
396 * Constructor for CTFEnumField.
397 *
398 * @param enumValue
459f705b
JCK
399 * The Enum value consisting of a pair of Enum value name and its
400 * long value
d4a8d935
BH
401 * @param name
402 * The name of this field
403 */
68b18f2f
AM
404 CTFEnumField(String name, CtfEnumPair enumValue) {
405 super(name, new CtfEnumPair(enumValue.getFirst(),
0126a8ca 406 enumValue.getSecond()), null);
d4a8d935
BH
407 }
408
d4a8d935 409 @Override
68b18f2f
AM
410 public CtfEnumPair getValue() {
411 return (CtfEnumPair) super.getValue();
d4a8d935
BH
412 }
413}
414
7a6cee1a 415/**
51cc7ef4 416 * The CTF field implementation for struct fields with sub-fields
7a6cee1a
GB
417 *
418 * @author gbastien
419 */
420final class CTFStructField extends CtfTmfEventField {
421
422 /**
51cc7ef4 423 * Constructor for CTFStructField.
7a6cee1a 424 *
51cc7ef4
GB
425 * @param fields
426 * The children of this field
7a6cee1a
GB
427 * @param name
428 * The name of this field
429 */
430 CTFStructField(String name, CtfTmfEventField[] fields) {
81ed27a8 431 super(name, fields, fields);
7a6cee1a
GB
432 }
433
7a6cee1a
GB
434 @Override
435 public CtfTmfEventField[] getValue() {
436 return (CtfTmfEventField[]) super.getValue();
437 }
438
439 @Override
51cc7ef4
GB
440 public String getFormattedValue() {
441 return Arrays.toString(getValue());
7a6cee1a 442 }
51cc7ef4
GB
443
444}
445
446/**
447 * The CTF field implementation for variant fields its child
448 *
449 * @author gbastien
450 */
451final class CTFVariantField extends CtfTmfEventField {
452
453 /**
454 * Constructor for CTFVariantField.
455 *
456 * @param field
457 * The field selected for this variant
458 * @param name
459 * The name of this field
460 */
461 CTFVariantField(String name, CtfTmfEventField field) {
459f705b 462 super(name, field, new CtfTmfEventField[] { field });
51cc7ef4
GB
463 }
464
465 @Override
466 public CtfTmfEventField getValue() {
467 return (CtfTmfEventField) super.getValue();
468 }
469
7a6cee1a
GB
470}
471
a3fc8213 472/* Implement other possible fields types here... */
This page took 0.098936 seconds and 5 git commands to generate.