Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.event.types; | |
14 | ||
a4fa4e36 MK |
15 | import java.util.Collection; |
16 | import java.util.List; | |
17 | ||
18 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; | |
19 | import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; | |
2feb8414 AM |
20 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
21 | ||
a4fa4e36 MK |
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; | |
26 | ||
866e5b51 | 27 | /** |
d37aaa7f | 28 | * A CTF sequence declaration. |
d5efe032 | 29 | * |
9ac2eb62 MK |
30 | * An array where the size is fixed but declared in the trace, unlike array |
31 | * where it is declared with a literal | |
7b4f13e6 | 32 | * @deprecated use {@link org.eclipse.linuxtools.internal.ctf.core.event.types.SequenceDeclaration} |
d37aaa7f FC |
33 | * @version 1.0 |
34 | * @author Matthew Khouzam | |
35 | * @author Simon Marchi | |
866e5b51 | 36 | */ |
7b4f13e6 | 37 | @Deprecated |
9377d173 | 38 | public class SequenceDeclaration extends CompoundDeclaration { |
866e5b51 FC |
39 | |
40 | // ------------------------------------------------------------------------ | |
41 | // Attributes | |
42 | // ------------------------------------------------------------------------ | |
43 | ||
a4fa4e36 MK |
44 | private final IDeclaration fElemType; |
45 | private final String fLengthName; | |
46 | private final Multimap<String, String> fPaths = ArrayListMultimap.<String, String>create(); | |
866e5b51 FC |
47 | |
48 | // ------------------------------------------------------------------------ | |
49 | // Constructors | |
50 | // ------------------------------------------------------------------------ | |
51 | ||
9ac2eb62 | 52 | /** |
be6df2d8 | 53 | * Constructor |
9ac2eb62 MK |
54 | * |
55 | * @param lengthName | |
be6df2d8 | 56 | * the name of the field describing the length |
9ac2eb62 | 57 | * @param elemType |
be6df2d8 | 58 | * The element type |
9ac2eb62 | 59 | */ |
866e5b51 | 60 | public SequenceDeclaration(String lengthName, IDeclaration elemType) { |
a4fa4e36 MK |
61 | fElemType = elemType; |
62 | fLengthName = lengthName; | |
866e5b51 FC |
63 | } |
64 | ||
65 | // ------------------------------------------------------------------------ | |
be6df2d8 | 66 | // Getters/Setters/Predicates |
866e5b51 FC |
67 | // ------------------------------------------------------------------------ |
68 | ||
9377d173 | 69 | @Override |
866e5b51 | 70 | public IDeclaration getElementType() { |
a4fa4e36 | 71 | return fElemType; |
866e5b51 FC |
72 | } |
73 | ||
9ac2eb62 MK |
74 | /** |
75 | * Gets the name of the length field | |
a4fa4e36 | 76 | * |
9ac2eb62 MK |
77 | * @return the name of the length field |
78 | */ | |
866e5b51 | 79 | public String getLengthName() { |
a4fa4e36 | 80 | return fLengthName; |
866e5b51 FC |
81 | } |
82 | ||
83 | // ------------------------------------------------------------------------ | |
84 | // Operations | |
85 | // ------------------------------------------------------------------------ | |
86 | ||
a4fa4e36 MK |
87 | /** |
88 | * @since 3.0 | |
89 | */ | |
90 | @SuppressWarnings("null") // immutablelist | |
866e5b51 FC |
91 | @Override |
92 | public SequenceDefinition createDefinition( | |
a4fa4e36 | 93 | IDefinitionScope definitionScope, String fieldName, BitBuffer input) throws CTFReaderException { |
6c7592e1 | 94 | IDefinition lenDef = null; |
a4fa4e36 MK |
95 | |
96 | if (definitionScope != null) { | |
97 | lenDef = definitionScope.lookupDefinition(getLengthName()); | |
98 | } | |
99 | ||
100 | if (lenDef == null) { | |
101 | throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$ | |
2feb8414 | 102 | } |
a4fa4e36 MK |
103 | |
104 | if (!(lenDef instanceof IntegerDefinition)) { | |
105 | throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$ | |
106 | } | |
107 | ||
108 | IntegerDefinition lengthDefinition = (IntegerDefinition) lenDef; | |
109 | ||
110 | if (lengthDefinition.getDeclaration().isSigned()) { | |
111 | throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$ | |
112 | } | |
113 | ||
114 | long length = lengthDefinition.getValue(); | |
115 | if ((length > Integer.MAX_VALUE) || (!input.canRead((int) length * fElemType.getMaximumSize()))) { | |
116 | throw new CTFReaderException("Sequence length too long " + length); //$NON-NLS-1$ | |
117 | } | |
118 | ||
119 | Collection<String> collection = fPaths.get(fieldName); | |
120 | while (collection.size() < length) { | |
121 | fPaths.put(fieldName, fieldName + '[' + collection.size() + ']'); | |
122 | } | |
123 | List<String> paths = (List<String>) fPaths.get(fieldName); | |
124 | Builder<Definition> definitions = new ImmutableList.Builder<>(); | |
125 | for (int i = 0; i < length; i++) { | |
126 | definitions.add(fElemType.createDefinition(definitionScope, paths.get(i), input)); | |
127 | } | |
128 | return new SequenceDefinition(this, definitionScope, fieldName, definitions.build()); | |
866e5b51 FC |
129 | } |
130 | ||
131 | @Override | |
132 | public String toString() { | |
133 | /* Only used for debugging */ | |
134 | return "[declaration] sequence[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ | |
135 | } | |
136 | ||
a4fa4e36 MK |
137 | /** |
138 | * @since 3.0 | |
139 | */ | |
140 | @Override | |
141 | public int getMaximumSize() { | |
142 | return Integer.MAX_VALUE; | |
143 | } | |
144 | ||
866e5b51 | 145 | } |