| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011-2012 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 org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer; |
| 16 | |
| 17 | /** |
| 18 | * <b><u>IntegerDefinition</u></b> |
| 19 | * <p> |
| 20 | * TODO: Reading integers with an endianness different from the trace endianness |
| 21 | * is not supported |
| 22 | */ |
| 23 | public class IntegerDefinition extends Definition { |
| 24 | |
| 25 | // ------------------------------------------------------------------------ |
| 26 | // Attributes |
| 27 | // ------------------------------------------------------------------------ |
| 28 | |
| 29 | private final IntegerDeclaration declaration; |
| 30 | private long value; |
| 31 | |
| 32 | // ------------------------------------------------------------------------ |
| 33 | // Contructors |
| 34 | // ------------------------------------------------------------------------ |
| 35 | |
| 36 | public IntegerDefinition(IntegerDeclaration declaration, |
| 37 | IDefinitionScope definitionScope, String fieldName) { |
| 38 | super(definitionScope, fieldName); |
| 39 | this.declaration = declaration; |
| 40 | } |
| 41 | |
| 42 | // ------------------------------------------------------------------------ |
| 43 | // Gettters/Setters/Predicates |
| 44 | // ------------------------------------------------------------------------ |
| 45 | |
| 46 | public long getValue() { |
| 47 | return value; |
| 48 | } |
| 49 | |
| 50 | public void setValue(long val) { |
| 51 | value = val; |
| 52 | } |
| 53 | |
| 54 | public IntegerDeclaration getDeclaration() { |
| 55 | return declaration; |
| 56 | } |
| 57 | |
| 58 | // ------------------------------------------------------------------------ |
| 59 | // Operations |
| 60 | // ------------------------------------------------------------------------ |
| 61 | |
| 62 | @Override |
| 63 | public void read(BitBuffer input) { |
| 64 | boolean signed = declaration.isSigned(); |
| 65 | int length = declaration.getLength(); |
| 66 | long bits = 0; |
| 67 | |
| 68 | // TODO: use the eventual getLong from BitBuffer |
| 69 | |
| 70 | if (length == 64) { |
| 71 | long low = input.getInt(32, false); |
| 72 | low = low & 0x00000000FFFFFFFFL; |
| 73 | long high = input.getInt(32, false); |
| 74 | high = high & 0x00000000FFFFFFFFL; |
| 75 | |
| 76 | bits = (high << 32) | low; |
| 77 | } else { |
| 78 | bits = input.getInt(length, signed); |
| 79 | bits = bits & 0x00000000FFFFFFFFL; |
| 80 | } |
| 81 | |
| 82 | value = bits; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public String toString() { |
| 87 | if (declaration.isCharacter()) { |
| 88 | char c = (char) value; |
| 89 | return Character.toString(c); |
| 90 | } |
| 91 | return String.valueOf(value); |
| 92 | } |
| 93 | } |