ctf: remove redundant code in structDeclaration
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StructDeclaration.java
CommitLineData
866e5b51 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
866e5b51
FC
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
13package org.eclipse.linuxtools.ctf.core.event.types;
14
2db699c2 15import java.util.Iterator;
a4fa4e36 16import java.util.LinkedHashMap;
0594c61c 17import java.util.Map;
866e5b51 18
a4fa4e36 19import org.eclipse.jdt.annotation.NonNull;
2db699c2 20import org.eclipse.jdt.annotation.Nullable;
a4fa4e36
MK
21import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
22import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
70f60307 23import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
a4fa4e36
MK
24import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
25
866e5b51 26/**
d37aaa7f 27 * A CTF structure declaration.
77fdc5df 28 *
d37aaa7f
FC
29 * A structure is similar to a C structure, it is a compound data type that
30 * contains other datatypes in fields. they are stored in an hashmap and indexed
31 * by names which are strings.
32 *
33 * @version 1.0
34 * @author Matthew Khouzam
35 * @author Simon Marchi
866e5b51 36 */
a4fa4e36 37public class StructDeclaration extends Declaration {
866e5b51
FC
38
39 // ------------------------------------------------------------------------
40 // Attributes
41 // ------------------------------------------------------------------------
42
a4fa4e36 43 /** linked list of field names. So fieldName->fieldValue */
2db699c2 44 private final @NonNull Map<String, IDeclaration> fFieldMap = new LinkedHashMap<>();
a4fa4e36
MK
45
46 /** maximum bit alignment */
47 private long fMaxAlign;
866e5b51
FC
48
49 // ------------------------------------------------------------------------
50 // Constructors
51 // ------------------------------------------------------------------------
52
9ac2eb62
MK
53 /**
54 * The struct declaration, add fields later
55 *
56 * @param align
57 * the minimum alignment of the struct. (if a struct is 8bit
58 * aligned and has a 32 bit aligned field, the struct becomes 32
59 * bit aligned.
60 */
2b7f6f09 61 public StructDeclaration(long align) {
a4fa4e36 62 fMaxAlign = Math.max(align, 1);
a4fa4e36
MK
63 }
64
65 /**
66 * Struct declaration constructor
67 *
68 * @param names
69 * the names of all the fields
70 * @param declarations
71 * all the fields
72 * @since 3.0
73 */
a4fa4e36
MK
74 public StructDeclaration(String[] names, Declaration[] declarations) {
75 fMaxAlign = 1;
2db699c2 76
a4fa4e36
MK
77 for (int i = 0; i < names.length; i++) {
78 addField(names[i], declarations[i]);
79 }
866e5b51
FC
80 }
81
82 // ------------------------------------------------------------------------
83 // Getters/Setters/Predicates
84 // ------------------------------------------------------------------------
85
9ac2eb62
MK
86 /**
87 * Get current alignment
a4fa4e36 88 *
9ac2eb62
MK
89 * @return the alignment of the struct and all its fields
90 */
2b7f6f09 91 public long getMaxAlign() {
a4fa4e36 92 return fMaxAlign;
866e5b51
FC
93 }
94
9ac2eb62
MK
95 /**
96 * Query if the struct has a given field
a4fa4e36
MK
97 *
98 * @param name
99 * the name of the field, scopeless please
9ac2eb62
MK
100 * @return does the field exist?
101 */
866e5b51 102 public boolean hasField(String name) {
a4fa4e36 103 return fFieldMap.containsKey(name);
866e5b51
FC
104 }
105
9ac2eb62 106 /**
2db699c2 107 * Get the fields of the struct as a map.
a4fa4e36 108 *
2db699c2 109 * @return a Map of the fields (key is the name)
0594c61c 110 * @since 2.0
9ac2eb62 111 */
0594c61c 112 public Map<String, IDeclaration> getFields() {
a4fa4e36 113 return fFieldMap;
866e5b51
FC
114 }
115
2db699c2
AM
116 /**
117 * Get the field declaration corresponding to a field name.
118 *
119 * @param fieldName
120 * The field name
121 * @return The declaration of the field, or null if there is no such field.
122 * @since 3.1
123 */
124 @Nullable
125 public IDeclaration getField(String fieldName) {
126 return fFieldMap.get(fieldName);
127 }
128
9ac2eb62 129 /**
a4fa4e36
MK
130 * Gets the field list. Very important since the map of fields does not
131 * retain the order of the fields.
132 *
9ac2eb62 133 * @return the field list.
a4fa4e36 134 * @since 3.0
9ac2eb62 135 */
a4fa4e36
MK
136 public Iterable<String> getFieldsList() {
137 return fFieldMap.keySet();
866e5b51
FC
138 }
139
fd74e6c1
MK
140 @Override
141 public long getAlignment() {
a4fa4e36
MK
142 return this.fMaxAlign;
143 }
144
145 /**
146 * @since 3.0
147 */
148 @Override
149 public int getMaximumSize() {
150 int maxSize = 0;
2db699c2
AM
151 for (IDeclaration field : fFieldMap.values()) {
152 maxSize += field.getMaximumSize();
a4fa4e36
MK
153 }
154 return Math.min(maxSize, Integer.MAX_VALUE);
fd74e6c1 155 }
9ac2eb62 156
866e5b51
FC
157 // ------------------------------------------------------------------------
158 // Operations
159 // ------------------------------------------------------------------------
160
a4fa4e36
MK
161 /**
162 * @since 3.0
163 */
866e5b51
FC
164 @Override
165 public StructDefinition createDefinition(IDefinitionScope definitionScope,
a4fa4e36
MK
166 String fieldName, BitBuffer input) throws CTFReaderException {
167 alignRead(input);
2db699c2
AM
168 final Definition[] myFields = new Definition[fFieldMap.size()];
169 StructDefinition structDefinition = new StructDefinition(this, definitionScope, fieldName, fFieldMap.keySet(), myFields);
cc575f45 170 fillStruct(input, myFields, structDefinition);
a4fa4e36 171 return structDefinition;
866e5b51
FC
172 }
173
70f60307
MK
174 /**
175 * Accelerated create definition
176 *
177 * @param definitionScope
178 * the definition scope
179 * @param fieldScope
180 * the lexical scope of this element
181 * @param input
182 * the {@Link BitBuffer} to read
183 * @return the Struct definition
184 * @throws CTFReaderException
185 * read error and such
186 * @since 3.1
187 */
188 public StructDefinition createDefinition(IDefinitionScope definitionScope,
189 LexicalScope fieldScope, @NonNull BitBuffer input) throws CTFReaderException {
190 alignRead(input);
191 final Definition[] myFields = new Definition[fFieldMap.size()];
bbe3a6a4
MK
192 /*
193 * Key set is NOT null
194 */
195 @SuppressWarnings("null")
196 StructDefinition structDefinition = new StructDefinition(this, definitionScope, fieldScope, fieldScope.getName(), fFieldMap.keySet(), myFields);
cc575f45 197 fillStruct(input, myFields, structDefinition);
70f60307
MK
198 return structDefinition;
199 }
200
9ac2eb62
MK
201 /**
202 * Add a field to the struct
a4fa4e36
MK
203 *
204 * @param name
205 * the name of the field, scopeless
206 * @param declaration
207 * the declaration of the field
9ac2eb62 208 */
866e5b51 209 public void addField(String name, IDeclaration declaration) {
a4fa4e36
MK
210 fFieldMap.put(name, declaration);
211 fMaxAlign = Math.max(fMaxAlign, declaration.getAlignment());
866e5b51
FC
212 }
213
cc575f45
MK
214 @SuppressWarnings("null")
215 private void fillStruct(@NonNull BitBuffer input, final Definition[] myFields, StructDefinition structDefinition) throws CTFReaderException {
216 Iterator<Map.Entry<String, IDeclaration>> iter = fFieldMap.entrySet().iterator();
217 for (int i = 0; i < fFieldMap.size(); i++) {
218 Map.Entry<String, IDeclaration> entry = iter.next();
219 myFields[i] = entry.getValue().createDefinition(structDefinition, entry.getKey(), input);
220 }
221 }
222
866e5b51
FC
223 @Override
224 public String toString() {
225 /* Only used for debugging */
226 return "[declaration] struct[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
227 }
228
4dd0eaed
MK
229 @Override
230 public int hashCode() {
231 final int prime = 31;
232 int result = 1;
a4fa4e36
MK
233 result = (prime * result) + fFieldMap.entrySet().hashCode();
234 result = (prime * result) + (int) (fMaxAlign ^ (fMaxAlign >>> 32));
4dd0eaed
MK
235 return result;
236 }
237
4dd0eaed
MK
238 @Override
239 public boolean equals(Object obj) {
240 if (this == obj) {
241 return true;
242 }
243 if (obj == null) {
244 return false;
245 }
246 if (!(obj instanceof StructDeclaration)) {
247 return false;
248 }
249 StructDeclaration other = (StructDeclaration) obj;
a4fa4e36 250 if (!fFieldMap.entrySet().equals(other.fFieldMap.entrySet())) {
4dd0eaed
MK
251 return false;
252 }
a4fa4e36 253 if (fMaxAlign != other.fMaxAlign) {
4dd0eaed
MK
254 return false;
255 }
256 return true;
257 }
258
866e5b51 259}
This page took 0.072601 seconds and 5 git commands to generate.