1 /*******************************************************************************
2 * Copyright (c) 2011-2012 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 * @see org.eclipse.linuxtools.ctf.core.event.types.SimpleDatatypeDefinition#getLongValue()
89 public Long
getIntegerValue() {
94 * @see org.eclipse.linuxtools.ctf.core.event.types.SimpleDatatypeDefinition#getStringValue()
97 public String
getStringValue() {
98 return this.toString();
102 public void read(BitBuffer input
) {
103 final long longNegBit
= 0x0000000080000000L
;
104 int align
= (int) declaration
.getAlignment();
105 int pos
= input
.position() + ((align
- (input
.position() % align
)) % align
);
107 boolean signed
= declaration
.isSigned();
108 int length
= declaration
.getLength();
112 * Is the endianness of this field the same as the endianness of the
113 * input buffer? If not, then temporarily set the buffer's endianness to
114 * this field's just to read the data
116 ByteOrder byteOrder
= input
.getByteOrder();
117 if ((this.declaration
.getByteOrder() != null) &&
118 (this.declaration
.getByteOrder() != input
.getByteOrder())) {
119 input
.setByteOrder(this.declaration
.getByteOrder());
122 // TODO: use the eventual getLong from BitBuffer
124 long low
= input
.getInt(32, false);
125 low
= low
& 0x00000000FFFFFFFFL
;
126 long high
= input
.getInt(32, false);
127 high
= high
& 0x00000000FFFFFFFFL
;
128 if (this.declaration
.getByteOrder() != ByteOrder
.BIG_ENDIAN
) {
129 bits
= (high
<< 32) | low
;
131 bits
= (low
<< 32) | high
;
134 bits
= input
.getInt(length
, signed
);
135 bits
= bits
& 0x00000000FFFFFFFFL
;
137 * The previous line loses sign information but is necessary, this
138 * fixes the sign for 32 bit numbers. Sorry, in java all 64 bit ints
141 if ((longNegBit
== (bits
& longNegBit
)) && signed
) {
142 bits
|= 0xffffffff00000000L
;
146 * Put the input buffer's endianness back to original if it was changed
148 if (byteOrder
!= input
.getByteOrder()) {
149 input
.setByteOrder(byteOrder
);
156 public String
toString() {
157 if (declaration
.isCharacter()) {
158 char c
= (char) value
;
159 return Character
.toString(c
);
161 return String
.valueOf(value
);