1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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
.LinkedHashMap
;
16 import java
.util
.List
;
17 import java
.util
.ListIterator
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
23 * A CTF structure definition (similar to a C structure).
25 * A structure is similar to a C structure, it is a compound data type that
26 * contains other datatypes in fields. they are stored in an hashmap and indexed
27 * by names which are strings.
30 * @author Matthew Khouzam
31 * @author Simon Marchi
33 public class StructDefinition
extends Definition
implements IDefinitionScope
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
39 private final StructDeclaration declaration
;
40 private final Map
<String
, Definition
> definitions
= new LinkedHashMap
<String
, Definition
>();
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
50 * the parent declaration
51 * @param definitionScope
53 * @param structFieldName
56 public StructDefinition(StructDeclaration declaration
,
57 IDefinitionScope definitionScope
, String structFieldName
) {
58 super(definitionScope
, structFieldName
);
60 this.declaration
= declaration
;
62 for (String fName
: declaration
.getFieldsList()) {
63 IDeclaration fieldDecl
= declaration
.getFields().get(fName
);
64 assert (fieldDecl
!= null);
66 Definition def
= fieldDecl
.createDefinition(this, fName
);
67 definitions
.put(fName
, def
);
71 // ------------------------------------------------------------------------
72 // Getters/Setters/Predicates
73 // ------------------------------------------------------------------------
76 * @return The definitions of all the fields
79 public Map
<String
, Definition
> getDefinitions() {
84 public StructDeclaration
getDeclaration() {
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
93 public void read(BitBuffer input
) {
94 final int align
= (int) declaration
.getAlignment();
95 int pos
= input
.position()
96 + ((align
- (input
.position() % align
)) % align
);
98 final List
<String
> fieldList
= declaration
.getFieldsList();
99 for (String fName
: fieldList
) {
100 Definition def
= definitions
.get(fName
);
101 assert (def
!= null);
107 public Definition
lookupDefinition(String lookupPath
) {
109 * The fields are created in order of appearance, so if a variant or
110 * sequence refers to a field that is after it, the field's definition
111 * will not be there yet in the hashmap.
113 Definition retVal
= definitions
.get(lookupPath
);
114 if (retVal
== null) {
115 retVal
= definitions
.get("_" + lookupPath
); //$NON-NLS-1$
121 * Lookup an array in a struct. if the name returns a non-array (like an
122 * int) than the method returns null
125 * the name of the array
126 * @return the array or null.
128 public ArrayDefinition
lookupArray(String name
) {
129 Definition def
= lookupDefinition(name
);
130 return (ArrayDefinition
) ((def
instanceof ArrayDefinition
) ? def
: null);
134 * Lookup an enum in a struct. if the name returns a non-enum (like an int)
135 * than the method returns null
138 * the name of the enum
139 * @return the enum or null.
141 public EnumDefinition
lookupEnum(String name
) {
142 Definition def
= lookupDefinition(name
);
143 return (EnumDefinition
) ((def
instanceof EnumDefinition
) ? def
: null);
147 * Lookup an integer in a struct. if the name returns a non-integer (like an
148 * float) than the method returns null
151 * the name of the integer
152 * @return the integer or null.
154 public IntegerDefinition
lookupInteger(String name
) {
155 Definition def
= lookupDefinition(name
);
156 return (IntegerDefinition
) ((def
instanceof IntegerDefinition
) ? def
161 * Lookup a sequence in a struct. if the name returns a non-sequence (like
162 * an int) than the method returns null
165 * the name of the sequence
166 * @return the sequence or null.
168 public SequenceDefinition
lookupSequence(String name
) {
169 Definition def
= lookupDefinition(name
);
170 return (SequenceDefinition
) ((def
instanceof SequenceDefinition
) ? def
175 * Lookup a string in a struct. if the name returns a non-string (like
176 * an int) than the method returns null
179 * the name of the string
180 * @return the string or null.
182 public StringDefinition
lookupString(String name
) {
183 Definition def
= lookupDefinition(name
);
184 return (StringDefinition
) ((def
instanceof StringDefinition
) ? def
189 * Lookup a struct in a struct. if the name returns a non-struct (like
190 * an int) than the method returns null
193 * the name of the struct
194 * @return the struct or null.
196 public StructDefinition
lookupStruct(String name
) {
197 Definition def
= lookupDefinition(name
);
198 return (StructDefinition
) ((def
instanceof StructDefinition
) ? def
203 * Lookup a variant in a struct. if the name returns a non-variant (like
204 * an int) than the method returns null
207 * the name of the variant
208 * @return the variant or null.
210 public VariantDefinition
lookupVariant(String name
) {
211 Definition def
= lookupDefinition(name
);
212 return (VariantDefinition
) ((def
instanceof VariantDefinition
) ? def
217 public String
toString() {
218 StringBuilder builder
= new StringBuilder();
220 builder
.append("{ "); //$NON-NLS-1$
222 ListIterator
<String
> listIterator
= this.declaration
.getFieldsList()
225 while (listIterator
.hasNext()) {
226 String field
= listIterator
.next();
228 builder
.append(field
);
229 builder
.append(" = "); //$NON-NLS-1$
230 builder
.append(lookupDefinition(field
).toString());
232 if (listIterator
.hasNext()) {
233 builder
.append(", "); //$NON-NLS-1$
237 builder
.append(" }"); //$NON-NLS-1$
239 return builder
.toString();