ctf: Fix API inconsistencies
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StructDefinition.java
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
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.ListIterator;
18
19 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
20
21 /**
22 * A CTF structure definition (similar to a C structure).
23 *
24 * A structure is similar to a C structure, it is a compound data type that
25 * contains other datatypes in fields. they are stored in an hashmap and indexed
26 * by names which are strings.
27 *
28 * @version 1.0
29 * @author Matthew Khouzam
30 * @author Simon Marchi
31 */
32 public class StructDefinition extends Definition implements IDefinitionScope {
33
34 // ------------------------------------------------------------------------
35 // Attributes
36 // ------------------------------------------------------------------------
37
38 private final StructDeclaration declaration;
39 private final HashMap<String, Definition> definitions = new HashMap<String, Definition>();
40
41 // ------------------------------------------------------------------------
42 // Constructors
43 // ------------------------------------------------------------------------
44
45 /**
46 * Constructor
47 *
48 * @param declaration
49 * the parent declaration
50 * @param definitionScope
51 * the parent scope
52 * @param structFieldName
53 * the field name
54 */
55 public StructDefinition(StructDeclaration declaration,
56 IDefinitionScope definitionScope, String structFieldName) {
57 super(definitionScope, structFieldName);
58
59 this.declaration = declaration;
60
61 for (String fName : declaration.getFieldsList()) {
62 IDeclaration fieldDecl = declaration.getFields().get(fName);
63 assert (fieldDecl != null);
64
65 Definition def = fieldDecl.createDefinition(this, fName);
66 definitions.put(fName, def);
67 }
68 }
69
70 // ------------------------------------------------------------------------
71 // Getters/Setters/Predicates
72 // ------------------------------------------------------------------------
73
74 @Override
75 public String getPath() {
76 return path;
77 }
78
79 /**
80 * @return The definitions of all the fields
81 */
82 public HashMap<String, Definition> getDefinitions() {
83 return definitions;
84 }
85
86 @Override
87 public StructDeclaration getDeclaration() {
88 return declaration;
89 }
90
91 // ------------------------------------------------------------------------
92 // Operations
93 // ------------------------------------------------------------------------
94
95 @Override
96 public void read(BitBuffer input) {
97 final int align = (int) declaration.getAlignment();
98 int pos = input.position()
99 + ((align - (input.position() % align)) % align);
100 input.position(pos);
101 final List<String> fieldList = declaration.getFieldsList();
102 for (String fName : fieldList) {
103 Definition def = definitions.get(fName);
104 assert (def != null);
105 def.read(input);
106 }
107 }
108
109 @Override
110 public Definition lookupDefinition(String lookupPath) {
111 /*
112 * The fields are created in order of appearance, so if a variant or
113 * sequence refers to a field that is after it, the field's definition
114 * will not be there yet in the hashmap.
115 */
116 Definition retVal = definitions.get(lookupPath);
117 if (retVal == null) {
118 retVal = definitions.get("_" + lookupPath); //$NON-NLS-1$
119 }
120 return retVal;
121 }
122
123 /**
124 * Lookup an array in a struct. if the name returns a non-array (like an
125 * int) than the method returns null
126 *
127 * @param name
128 * the name of the array
129 * @return the array or null.
130 */
131 public ArrayDefinition lookupArray(String name) {
132 Definition def = lookupDefinition(name);
133 return (ArrayDefinition) ((def instanceof ArrayDefinition) ? def : null);
134 }
135
136 /**
137 * Lookup an enum in a struct. if the name returns a non-enum (like an int)
138 * than the method returns null
139 *
140 * @param name
141 * the name of the enum
142 * @return the enum or null.
143 */
144 public EnumDefinition lookupEnum(String name) {
145 Definition def = lookupDefinition(name);
146 return (EnumDefinition) ((def instanceof EnumDefinition) ? def : null);
147 }
148
149 /**
150 * Lookup an integer in a struct. if the name returns a non-integer (like an
151 * float) than the method returns null
152 *
153 * @param name
154 * the name of the integer
155 * @return the integer or null.
156 */
157 public IntegerDefinition lookupInteger(String name) {
158 Definition def = lookupDefinition(name);
159 return (IntegerDefinition) ((def instanceof IntegerDefinition) ? def
160 : null);
161 }
162
163 /**
164 * Lookup a sequence in a struct. if the name returns a non-sequence (like
165 * an int) than the method returns null
166 *
167 * @param name
168 * the name of the sequence
169 * @return the sequence or null.
170 */
171 public SequenceDefinition lookupSequence(String name) {
172 Definition def = lookupDefinition(name);
173 return (SequenceDefinition) ((def instanceof SequenceDefinition) ? def
174 : null);
175 }
176
177 /**
178 * Lookup a string in a struct. if the name returns a non-string (like
179 * an int) than the method returns null
180 *
181 * @param name
182 * the name of the string
183 * @return the string or null.
184 */
185 public StringDefinition lookupString(String name) {
186 Definition def = lookupDefinition(name);
187 return (StringDefinition) ((def instanceof StringDefinition) ? def
188 : null);
189 }
190
191 /**
192 * Lookup a struct in a struct. if the name returns a non-struct (like
193 * an int) than the method returns null
194 *
195 * @param name
196 * the name of the struct
197 * @return the struct or null.
198 */
199 public StructDefinition lookupStruct(String name) {
200 Definition def = lookupDefinition(name);
201 return (StructDefinition) ((def instanceof StructDefinition) ? def
202 : null);
203 }
204
205 /**
206 * Lookup a variant in a struct. if the name returns a non-variant (like
207 * an int) than the method returns null
208 *
209 * @param name
210 * the name of the variant
211 * @return the variant or null.
212 */
213 public VariantDefinition lookupVariant(String name) {
214 Definition def = lookupDefinition(name);
215 return (VariantDefinition) ((def instanceof VariantDefinition) ? def
216 : null);
217 }
218
219 @Override
220 public String toString() {
221 StringBuilder builder = new StringBuilder();
222
223 int size = this.declaration.getFieldsList().size();
224 int n = 0;
225
226 if (size > 1) {
227 builder.append("{ "); //$NON-NLS-1$
228 }
229
230 ListIterator<String> listIterator = this.declaration.getFieldsList()
231 .listIterator();
232
233 while (listIterator.hasNext()) {
234 String field = listIterator.next();
235 builder.append(lookupDefinition(field).toString());
236 n++;
237 if (n != size) {
238 builder.append(", "); //$NON-NLS-1$
239 }
240 }
241
242 if (size > 1) {
243 builder.append(" }"); //$NON-NLS-1$
244 }
245
246 return builder.toString();
247 }
248 }
This page took 0.043729 seconds and 6 git commands to generate.