1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.event
.types
;
15 import java
.nio
.ByteOrder
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
20 * A CTF integer definition.
22 * The definition of a integer basic data type. It will take the data
23 * from a trace and store it (and make it fit) as a long.
26 * @author Matthew Khouzam
27 * @author Simon Marchi
29 public class IntegerDefinition
extends SimpleDatatypeDefinition
{
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
35 private final IntegerDeclaration declaration
;
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
44 * @param declaration the parent declaration
45 * @param definitionScope the parent scope
46 * @param fieldName the field name
48 public IntegerDefinition(IntegerDeclaration declaration
,
49 IDefinitionScope definitionScope
, String fieldName
) {
50 super(definitionScope
, fieldName
);
51 this.declaration
= declaration
;
54 // ------------------------------------------------------------------------
55 // Gettters/Setters/Predicates
56 // ------------------------------------------------------------------------
59 * Gets the value of the integer
60 * @return the value of the integer (in long)
62 public long getValue() {
67 * Sets the value of an integer
68 * @param val the value
70 public void setValue(long val
) {
75 public IntegerDeclaration
getDeclaration() {
81 // ------------------------------------------------------------------------
83 // ------------------------------------------------------------------------
86 public Long
getIntegerValue() {
91 public String
getStringValue() {
92 return this.toString();
96 public void read(BitBuffer input
) {
97 final long longNegBit
= 0x0000000080000000L
;
98 int align
= (int) declaration
.getAlignment();
99 int pos
= input
.position() + ((align
- (input
.position() % align
)) % align
);
101 boolean signed
= declaration
.isSigned();
102 int length
= declaration
.getLength();
106 * Is the endianness of this field the same as the endianness of the
107 * input buffer? If not, then temporarily set the buffer's endianness to
108 * this field's just to read the data
110 ByteOrder byteOrder
= input
.getByteOrder();
111 if ((this.declaration
.getByteOrder() != null) &&
112 (this.declaration
.getByteOrder() != input
.getByteOrder())) {
113 input
.setByteOrder(this.declaration
.getByteOrder());
116 // TODO: use the eventual getLong from BitBuffer
118 long low
= input
.getInt(32, false);
119 low
= low
& 0x00000000FFFFFFFFL
;
120 long high
= input
.getInt(32, false);
121 high
= high
& 0x00000000FFFFFFFFL
;
122 if (this.declaration
.getByteOrder() != ByteOrder
.BIG_ENDIAN
) {
123 bits
= (high
<< 32) | low
;
125 bits
= (low
<< 32) | high
;
128 bits
= input
.getInt(length
, signed
);
129 bits
= bits
& 0x00000000FFFFFFFFL
;
131 * The previous line loses sign information but is necessary, this
132 * fixes the sign for 32 bit numbers. Sorry, in java all 64 bit ints
135 if ((longNegBit
== (bits
& longNegBit
)) && signed
) {
136 bits
|= 0xffffffff00000000L
;
140 * Put the input buffer's endianness back to original if it was changed
142 if (byteOrder
!= input
.getByteOrder()) {
143 input
.setByteOrder(byteOrder
);
150 public String
toString() {
151 if (declaration
.isCharacter()) {
152 char c
= (char) value
;
153 return Character
.toString(c
);
155 return String
.valueOf(value
);