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
.HashMap
;
16 import java
.util
.List
;
17 import java
.util
.ListIterator
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
22 * A CTF structure definition (similar to a C structure).
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.
29 * @author Matthew Khouzam
30 * @author Simon Marchi
32 public class StructDefinition
extends Definition
implements IDefinitionScope
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 private final StructDeclaration declaration
;
39 private final HashMap
<String
, Definition
> definitions
= new HashMap
<String
, Definition
>();
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
49 * the parent declaration
50 * @param definitionScope
52 * @param structFieldName
55 public StructDefinition(StructDeclaration declaration
,
56 IDefinitionScope definitionScope
, String structFieldName
) {
57 super(definitionScope
, structFieldName
);
59 this.declaration
= declaration
;
61 for (String fName
: declaration
.getFieldsList()) {
62 IDeclaration fieldDecl
= declaration
.getFields().get(fName
);
63 assert (fieldDecl
!= null);
65 Definition def
= fieldDecl
.createDefinition(this, fName
);
66 definitions
.put(fName
, def
);
70 // ------------------------------------------------------------------------
71 // Getters/Setters/Predicates
72 // ------------------------------------------------------------------------
75 public String
getPath() {
80 * @return The definitions of all the fields
82 public HashMap
<String
, Definition
> getDefinitions() {
87 public StructDeclaration
getDeclaration() {
91 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
96 public void read(BitBuffer input
) {
97 final int align
= (int) declaration
.getAlignment();
98 int pos
= input
.position()
99 + ((align
- (input
.position() % align
)) % align
);
101 final List
<String
> fieldList
= declaration
.getFieldsList();
102 for (String fName
: fieldList
) {
103 Definition def
= definitions
.get(fName
);
104 assert (def
!= null);
110 public Definition
lookupDefinition(String lookupPath
) {
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.
116 Definition retVal
= definitions
.get(lookupPath
);
117 if (retVal
== null) {
118 retVal
= definitions
.get("_" + lookupPath
); //$NON-NLS-1$
124 * Lookup an array in a struct. if the name returns a non-array (like an
125 * int) than the method returns null
128 * the name of the array
129 * @return the array or null.
131 public ArrayDefinition
lookupArray(String name
) {
132 Definition def
= lookupDefinition(name
);
133 return (ArrayDefinition
) ((def
instanceof ArrayDefinition
) ? def
: null);
137 * Lookup an enum in a struct. if the name returns a non-enum (like an int)
138 * than the method returns null
141 * the name of the enum
142 * @return the enum or null.
144 public EnumDefinition
lookupEnum(String name
) {
145 Definition def
= lookupDefinition(name
);
146 return (EnumDefinition
) ((def
instanceof EnumDefinition
) ? def
: null);
150 * Lookup an integer in a struct. if the name returns a non-integer (like an
151 * float) than the method returns null
154 * the name of the integer
155 * @return the integer or null.
157 public IntegerDefinition
lookupInteger(String name
) {
158 Definition def
= lookupDefinition(name
);
159 return (IntegerDefinition
) ((def
instanceof IntegerDefinition
) ? def
164 * Lookup a sequence in a struct. if the name returns a non-sequence (like
165 * an int) than the method returns null
168 * the name of the sequence
169 * @return the sequence or null.
171 public SequenceDefinition
lookupSequence(String name
) {
172 Definition def
= lookupDefinition(name
);
173 return (SequenceDefinition
) ((def
instanceof SequenceDefinition
) ? def
178 * Lookup a string in a struct. if the name returns a non-string (like
179 * an int) than the method returns null
182 * the name of the string
183 * @return the string or null.
185 public StringDefinition
lookupString(String name
) {
186 Definition def
= lookupDefinition(name
);
187 return (StringDefinition
) ((def
instanceof StringDefinition
) ? def
192 * Lookup a struct in a struct. if the name returns a non-struct (like
193 * an int) than the method returns null
196 * the name of the struct
197 * @return the struct or null.
199 public StructDefinition
lookupStruct(String name
) {
200 Definition def
= lookupDefinition(name
);
201 return (StructDefinition
) ((def
instanceof StructDefinition
) ? def
206 * Lookup a variant in a struct. if the name returns a non-variant (like
207 * an int) than the method returns null
210 * the name of the variant
211 * @return the variant or null.
213 public VariantDefinition
lookupVariant(String name
) {
214 Definition def
= lookupDefinition(name
);
215 return (VariantDefinition
) ((def
instanceof VariantDefinition
) ? def
220 public String
toString() {
221 StringBuilder builder
= new StringBuilder();
223 int size
= this.declaration
.getFieldsList().size();
227 builder
.append("{ "); //$NON-NLS-1$
230 ListIterator
<String
> listIterator
= this.declaration
.getFieldsList()
233 while (listIterator
.hasNext()) {
234 String field
= listIterator
.next();
235 builder
.append(lookupDefinition(field
).toString());
238 builder
.append(", "); //$NON-NLS-1$
243 builder
.append(" }"); //$NON-NLS-1$
246 return builder
.toString();