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
.Collection
;
16 import java
.util
.List
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.IDefinitionScope
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
22 import com
.google
.common
.collect
.ArrayListMultimap
;
23 import com
.google
.common
.collect
.ImmutableList
;
24 import com
.google
.common
.collect
.ImmutableList
.Builder
;
25 import com
.google
.common
.collect
.Multimap
;
28 * A CTF sequence declaration.
30 * An array where the size is fixed but declared in the trace, unlike array
31 * where it is declared with a literal
34 * @author Matthew Khouzam
35 * @author Simon Marchi
37 public class SequenceDeclaration
extends Declaration
{
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
43 private final IDeclaration fElemType
;
44 private final String fLengthName
;
45 private final Multimap
<String
, String
> fPaths
= ArrayListMultimap
.<String
, String
>create();
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
55 * the name of the field describing the length
59 public SequenceDeclaration(String lengthName
, IDeclaration elemType
) {
61 fLengthName
= lengthName
;
64 // ------------------------------------------------------------------------
65 // Getters/Setters/Predicates
66 // ------------------------------------------------------------------------
69 * Gets the element type
71 * @return the element type
73 public IDeclaration
getElementType() {
78 * Gets the name of the length field
80 * @return the name of the length field
82 public String
getLengthName() {
87 public long getAlignment() {
88 return getElementType().getAlignment();
92 // ------------------------------------------------------------------------
94 // ------------------------------------------------------------------------
97 * Is the Sequence a string?
98 * @return true, if the elements are chars, false otherwise
101 public boolean isString(){
102 IntegerDeclaration elemInt
;
103 IDeclaration elementType
= getElementType();
104 if (elementType
instanceof IntegerDeclaration
) {
105 elemInt
= (IntegerDeclaration
) elementType
;
106 if (elemInt
.isCharacter()) {
116 @SuppressWarnings("null") // immutablelist
118 public SequenceDefinition
createDefinition(
119 IDefinitionScope definitionScope
, String fieldName
, BitBuffer input
) throws CTFReaderException
{
120 Definition lenDef
= null;
122 if (definitionScope
!= null) {
123 lenDef
= definitionScope
.lookupDefinition(getLengthName());
126 if (lenDef
== null) {
127 throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$
130 if (!(lenDef
instanceof IntegerDefinition
)) {
131 throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$
134 IntegerDefinition lengthDefinition
= (IntegerDefinition
) lenDef
;
136 if (lengthDefinition
.getDeclaration().isSigned()) {
137 throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$
140 long length
= lengthDefinition
.getValue();
141 if ((length
> Integer
.MAX_VALUE
) || (!input
.canRead((int) length
* fElemType
.getMaximumSize()))) {
142 throw new CTFReaderException("Sequence length too long " + length
); //$NON-NLS-1$
145 Collection
<String
> collection
= fPaths
.get(fieldName
);
146 while (collection
.size() < length
) {
147 fPaths
.put(fieldName
, fieldName
+ '[' + collection
.size() + ']');
149 List
<String
> paths
= (List
<String
>) fPaths
.get(fieldName
);
150 Builder
<Definition
> definitions
= new ImmutableList
.Builder
<>();
151 for (int i
= 0; i
< length
; i
++) {
152 definitions
.add(fElemType
.createDefinition(definitionScope
, paths
.get(i
), input
));
154 return new SequenceDefinition(this, definitionScope
, fieldName
, definitions
.build());
158 public String
toString() {
159 /* Only used for debugging */
160 return "[declaration] sequence[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$
167 public int getMaximumSize() {
168 return Integer
.MAX_VALUE
;