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
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
24 * A CTF structure definition (similar to a C structure).
26 * A structure is similar to a C structure, it is a compound data type that
27 * contains other datatypes in fields. they are stored in an hashmap and indexed
28 * by names which are strings.
31 * @author Matthew Khouzam
32 * @author Simon Marchi
34 public class StructDefinition
extends Definition
implements IDefinitionScope
{
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
40 private final StructDeclaration declaration
;
41 private final Map
<String
, Definition
> definitions
= new LinkedHashMap
<>();
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
51 * the parent declaration
52 * @param definitionScope
54 * @param structFieldName
57 public StructDefinition(StructDeclaration declaration
,
58 IDefinitionScope definitionScope
, String structFieldName
) {
59 super(definitionScope
, structFieldName
);
61 this.declaration
= declaration
;
63 for (String fName
: declaration
.getFieldsList()) {
64 IDeclaration fieldDecl
= declaration
.getFields().get(fName
);
65 assert (fieldDecl
!= null);
67 Definition def
= fieldDecl
.createDefinition(this, fName
);
68 definitions
.put(fName
, def
);
72 // ------------------------------------------------------------------------
73 // Getters/Setters/Predicates
74 // ------------------------------------------------------------------------
77 * @return The definitions of all the fields
80 public Map
<String
, Definition
> getDefinitions() {
85 public StructDeclaration
getDeclaration() {
89 // ------------------------------------------------------------------------
91 // ------------------------------------------------------------------------
94 public void read(BitBuffer input
) throws CTFReaderException
{
95 alignRead(input
, this.declaration
);
96 final List
<String
> fieldList
= declaration
.getFieldsList();
97 for (String fName
: fieldList
) {
98 Definition def
= definitions
.get(fName
);
105 public Definition
lookupDefinition(String lookupPath
) {
107 * The fields are created in order of appearance, so if a variant or
108 * sequence refers to a field that is after it, the field's definition
109 * will not be there yet in the hashmap.
111 Definition retVal
= definitions
.get(lookupPath
);
112 if (retVal
== null) {
113 retVal
= definitions
.get("_" + lookupPath
); //$NON-NLS-1$
119 * Lookup an array in a struct. If the name returns a non-array (like an
120 * int) than the method returns null
123 * the name of the array
124 * @return the array or null.
126 public ArrayDefinition
lookupArray(String name
) {
127 Definition def
= lookupDefinition(name
);
128 return (ArrayDefinition
) ((def
instanceof ArrayDefinition
) ? def
: null);
132 * Lookup an enum in a struct. If the name returns a non-enum (like an int)
133 * than the method returns null
136 * the name of the enum
137 * @return the enum or null.
139 public EnumDefinition
lookupEnum(String name
) {
140 Definition def
= lookupDefinition(name
);
141 return (EnumDefinition
) ((def
instanceof EnumDefinition
) ? def
: null);
145 * Lookup an integer in a struct. If the name returns a non-integer (like an
146 * float) than the method returns null
149 * the name of the integer
150 * @return the integer or null.
152 public IntegerDefinition
lookupInteger(String name
) {
153 Definition def
= lookupDefinition(name
);
154 return (IntegerDefinition
) ((def
instanceof IntegerDefinition
) ? def
159 * Lookup a sequence in a struct. If the name returns a non-sequence (like
160 * an int) than the method returns null
163 * the name of the sequence
164 * @return the sequence or null.
166 public SequenceDefinition
lookupSequence(String name
) {
167 Definition def
= lookupDefinition(name
);
168 return (SequenceDefinition
) ((def
instanceof SequenceDefinition
) ? def
173 * Lookup a string in a struct. If the name returns a non-string (like
174 * an int) than the method returns null
177 * the name of the string
178 * @return the string or null.
180 public StringDefinition
lookupString(String name
) {
181 Definition def
= lookupDefinition(name
);
182 return (StringDefinition
) ((def
instanceof StringDefinition
) ? def
187 * Lookup a struct in a struct. If the name returns a non-struct (like
188 * an int) than the method returns null
191 * the name of the struct
192 * @return the struct or null.
194 public StructDefinition
lookupStruct(String name
) {
195 Definition def
= lookupDefinition(name
);
196 return (StructDefinition
) ((def
instanceof StructDefinition
) ? def
201 * Lookup a variant in a struct. If the name returns a non-variant (like
202 * an int) than the method returns null
205 * the name of the variant
206 * @return the variant or null.
208 public VariantDefinition
lookupVariant(String name
) {
209 Definition def
= lookupDefinition(name
);
210 return (VariantDefinition
) ((def
instanceof VariantDefinition
) ? def
215 public String
toString() {
216 StringBuilder builder
= new StringBuilder();
218 builder
.append("{ "); //$NON-NLS-1$
220 ListIterator
<String
> listIterator
= this.declaration
.getFieldsList()
223 while (listIterator
.hasNext()) {
224 String field
= listIterator
.next();
226 builder
.append(field
);
227 builder
.append(" = "); //$NON-NLS-1$
228 builder
.append(lookupDefinition(field
).toString());
230 if (listIterator
.hasNext()) {
231 builder
.append(", "); //$NON-NLS-1$
235 builder
.append(" }"); //$NON-NLS-1$
237 return builder
.toString();