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
.util
.Arrays
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
21 * A CTF array definition
23 * Arrays are fixed-length. Their length is declared in the type
24 * declaration within the meta-data. They contain an array of "inner type"
25 * elements, which can refer to any type not containing the type of the
26 * array being declared (no circular dependency). The length is the number
27 * of elements in an array.
30 * @author Matthew Khouzam
31 * @author Simon Marchi
33 public class ArrayDefinition
extends Definition
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
39 private final ArrayDeclaration declaration
;
40 private Definition definitions
[];
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
48 * @param declaration the parent declaration
49 * @param definitionScope the parent scope
50 * @param fieldName the field name
52 public ArrayDefinition(ArrayDeclaration declaration
,
53 IDefinitionScope definitionScope
, String fieldName
) {
54 super(definitionScope
, fieldName
);
56 this.declaration
= declaration
;
58 definitions
= new Definition
[declaration
.getLength()];
60 for (int i
= 0; i
< declaration
.getLength(); i
++) {
61 definitions
[i
] = declaration
.getElementType().createDefinition(
62 definitionScope
, fieldName
+ "[" + i
+ "]"); //$NON-NLS-1$ //$NON-NLS-2$
66 // ------------------------------------------------------------------------
67 // Getters/Setters/Predicates
68 // ------------------------------------------------------------------------
71 * @return the definitions
73 public Definition
[] getDefinitions() {
74 return Arrays
.copyOf(definitions
, definitions
.length
);
79 * the definitions to set
81 public void setDefinitions(Definition
[] definitions
) {
82 this.definitions
= Arrays
.copyOf(definitions
, definitions
.length
);
86 * Get the element at i
87 * @param i the index (cannot be negative)
88 * @return The element at I, if I > length, null, if I < 0, the method throws an out of bounds exception
90 public Definition
getElem(int i
) {
91 if (i
> definitions
.length
) {
95 return definitions
[i
];
99 public ArrayDeclaration
getDeclaration() {
104 * Sometimes, strings are encoded as an array of 1-byte integers (each one
105 * being an UTF-8 byte).
107 * @return true if this array is in fact an UTF-8 string. false if it's a
108 * "normal" array of generic Definition's.
110 public boolean isString() {
111 IntegerDeclaration elemInt
;
113 if (declaration
.getElementType() instanceof IntegerDeclaration
) {
115 * If the first byte is a "character", we'll consider the whole
116 * array a character string.
118 elemInt
= (IntegerDeclaration
) declaration
.getElementType();
119 if (elemInt
.isCharacter()) {
126 // ------------------------------------------------------------------------
128 // ------------------------------------------------------------------------
131 public void read(BitBuffer input
) throws CTFReaderException
{
132 for (Definition definition
: definitions
) {
133 definition
.read(input
);
138 public String
toString() {
139 StringBuilder b
= new StringBuilder();
141 if (this.isString()) {
142 for (Definition def
: definitions
) {
143 IntegerDefinition character
= (IntegerDefinition
) def
;
145 if (character
.getValue() == 0) {
149 b
.append(character
.toString());
151 } else if (definitions
== null) {
152 b
.append("[ ]"); //$NON-NLS-1$
155 for (int i
= 0; i
< (definitions
.length
- 1); i
++) {
157 b
.append(definitions
[i
].toString());
161 b
.append(definitions
[definitions
.length
- 1].toString());
162 b
.append(" ]"); //$NON-NLS-1$