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
.internal
.ctf
.core
.event
.io
.BitBuffer
;
22 * <b><u>StructDefinition</u></b>
24 public class StructDefinition
extends Definition
implements IDefinitionScope
{
26 // ------------------------------------------------------------------------
28 // ------------------------------------------------------------------------
30 private final StructDeclaration declaration
;
31 private final HashMap
<String
, Definition
> definitions
= new HashMap
<String
, Definition
>();
33 // ------------------------------------------------------------------------
35 // ------------------------------------------------------------------------
41 * the parent declaration
42 * @param definitionScope
47 public StructDefinition(StructDeclaration declaration
,
48 IDefinitionScope definitionScope
, String structFieldName
) {
49 super(definitionScope
, structFieldName
);
51 this.declaration
= declaration
;
53 for (String fName
: declaration
.getFieldsList()) {
54 IDeclaration fieldDecl
= declaration
.getFields().get(fName
);
55 assert (fieldDecl
!= null);
57 Definition def
= fieldDecl
.createDefinition(this, fName
);
58 definitions
.put(fName
, def
);
62 // ------------------------------------------------------------------------
63 // Getters/Setters/Predicates
64 // ------------------------------------------------------------------------
67 public String
getPath() {
72 * @return The definitions of all the fields
74 public HashMap
<String
, Definition
> getDefinitions() {
79 public StructDeclaration
getDeclaration() {
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
88 public void read(BitBuffer input
) {
89 final int align
= (int) declaration
.getAlignment();
90 int pos
= input
.position()
91 + ((align
- (input
.position() % align
)) % align
);
93 final List
<String
> fieldList
= declaration
.getFieldsList();
94 for (String fName
: fieldList
) {
95 Definition def
= definitions
.get(fName
);
102 public Definition
lookupDefinition(String lookupPath
) {
104 * The fields are created in order of appearance, so if a variant or
105 * sequence refers to a field that is after it, the field's definition
106 * will not be there yet in the hashmap.
108 Definition retVal
= definitions
.get(lookupPath
);
109 if (retVal
== null) {
110 retVal
= definitions
.get("_" + lookupPath
); //$NON-NLS-1$
116 * Lookup an array in a struct. if the name returns a non-array (like an
117 * int) than the method returns null
120 * the name of the array
121 * @return the array or null.
123 public ArrayDefinition
lookupArray(String name
) {
124 Definition def
= lookupDefinition(name
);
125 return (ArrayDefinition
) ((def
instanceof ArrayDefinition
) ? def
: null);
129 * Lookup an enum in a struct. if the name returns a non-enum (like an int)
130 * than the method returns null
133 * the name of the enum
134 * @return the enum or null.
136 public EnumDefinition
lookupEnum(String name
) {
137 Definition def
= lookupDefinition(name
);
138 return (EnumDefinition
) ((def
instanceof EnumDefinition
) ? def
: null);
142 * Lookup an integer in a struct. if the name returns a non-integer (like an
143 * float) than the method returns null
146 * the name of the integer
147 * @return the integer or null.
149 public IntegerDefinition
lookupInteger(String name
) {
150 Definition def
= lookupDefinition(name
);
151 return (IntegerDefinition
) ((def
instanceof IntegerDefinition
) ? def
156 * Lookup a sequence in a struct. if the name returns a non-sequence (like
157 * an int) than the method returns null
160 * the name of the sequence
161 * @return the sequence or null.
163 public SequenceDefinition
lookupSequence(String name
) {
164 Definition def
= lookupDefinition(name
);
165 return (SequenceDefinition
) ((def
instanceof SequenceDefinition
) ? def
170 * Lookup a string in a struct. if the name returns a non-string (like
171 * an int) than the method returns null
174 * the name of the string
175 * @return the string or null.
177 public StringDefinition
lookupString(String name
) {
178 Definition def
= lookupDefinition(name
);
179 return (StringDefinition
) ((def
instanceof StringDefinition
) ? def
184 * Lookup a struct in a struct. if the name returns a non-struct (like
185 * an int) than the method returns null
188 * the name of the struct
189 * @return the struct or null.
191 public StructDefinition
lookupStruct(String name
) {
192 Definition def
= lookupDefinition(name
);
193 return (StructDefinition
) ((def
instanceof StructDefinition
) ? def
198 * Lookup a variant in a struct. if the name returns a non-variant (like
199 * an int) than the method returns null
202 * the name of the variant
203 * @return the variant or null.
205 public VariantDefinition
lookupVariant(String name
) {
206 Definition def
= lookupDefinition(name
);
207 return (VariantDefinition
) ((def
instanceof VariantDefinition
) ? def
212 public String
toString() {
213 StringBuilder builder
= new StringBuilder();
215 int size
= this.declaration
.getFieldsList().size();
219 builder
.append("{ "); //$NON-NLS-1$
222 ListIterator
<String
> listIterator
= this.declaration
.getFieldsList()
225 while (listIterator
.hasNext()) {
226 String field
= listIterator
.next();
227 builder
.append(lookupDefinition(field
).toString());
230 builder
.append(", "); //$NON-NLS-1$
235 builder
.append(" }"); //$NON-NLS-1$
238 return builder
.toString();