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
;
20 * A CTF array definition
22 * Arrays are fixed-length. Their length is declared in the type
23 * declaration within the meta-data. They contain an array of "inner type"
24 * elements, which can refer to any type not containing the type of the
25 * array being declared (no circular dependency). The length is the number
26 * of elements in an array.
29 * @author Matthew Khouzam
30 * @author Simon Marchi
32 public class ArrayDefinition
extends Definition
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 private final ArrayDeclaration declaration
;
39 private Definition definitions
[];
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
47 * @param declaration the parent declaration
48 * @param definitionScope the parent scope
49 * @param fieldName the field name
51 public ArrayDefinition(ArrayDeclaration declaration
,
52 IDefinitionScope definitionScope
, String fieldName
) {
53 super(definitionScope
, fieldName
);
55 this.declaration
= declaration
;
57 definitions
= new Definition
[declaration
.getLength()];
59 for (int i
= 0; i
< declaration
.getLength(); i
++) {
60 definitions
[i
] = declaration
.getElementType().createDefinition(
61 definitionScope
, fieldName
+ "[" + i
+ "]"); //$NON-NLS-1$ //$NON-NLS-2$
65 // ------------------------------------------------------------------------
66 // Getters/Setters/Predicates
67 // ------------------------------------------------------------------------
70 * @return the definitions
72 public Definition
[] getDefinitions() {
73 return Arrays
.copyOf(definitions
, definitions
.length
);
78 * the definitions to set
80 public void setDefinitions(Definition
[] definitions
) {
81 this.definitions
= Arrays
.copyOf(definitions
, definitions
.length
);
85 * Get the element at i
86 * @param i the index (cannot be negative)
87 * @return The element at I, if I > length, null, if I < 0, the method throws an out of bounds exception
89 public Definition
getElem(int i
) {
90 if (i
> definitions
.length
) {
94 return definitions
[i
];
98 public ArrayDeclaration
getDeclaration() {
103 * Sometimes, strings are encoded as an array of 1-byte integers (each one
104 * being an UTF-8 byte).
106 * @return true if this array is in fact an UTF-8 string. false if it's a
107 * "normal" array of generic Definition's.
109 public boolean isString() {
110 IntegerDeclaration elemInt
;
112 if (declaration
.getElementType() instanceof IntegerDeclaration
) {
114 * If the first byte is a "character", we'll consider the whole
115 * array a character string.
117 elemInt
= (IntegerDeclaration
) declaration
.getElementType();
118 if (elemInt
.isCharacter()) {
125 // ------------------------------------------------------------------------
127 // ------------------------------------------------------------------------
130 public void read(BitBuffer input
) {
131 for (Definition definition
: definitions
) {
132 definition
.read(input
);
137 public String
toString() {
138 StringBuilder b
= new StringBuilder();
140 if (this.isString()) {
141 for (Definition def
: definitions
) {
142 IntegerDefinition character
= (IntegerDefinition
) def
;
144 if (character
.getValue() == 0) {
148 b
.append(character
.toString());
150 } else if (definitions
== null) {
151 b
.append("[ ]"); //$NON-NLS-1$
154 for (int i
= 0; i
< (definitions
.length
- 1); i
++) {
156 b
.append(definitions
[i
].toString());
160 b
.append(definitions
[definitions
.length
- 1].toString());
161 b
.append(" ]"); //$NON-NLS-1$