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
.ListIterator
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
21 * <b><u>StructDefinition</u></b>
23 public class StructDefinition
extends Definition
implements IDefinitionScope
{
25 // ------------------------------------------------------------------------
27 // ------------------------------------------------------------------------
29 private final StructDeclaration declaration
;
30 private final HashMap
<String
, Definition
> definitions
= new HashMap
<String
, Definition
>();
32 // ------------------------------------------------------------------------
34 // ------------------------------------------------------------------------
36 public StructDefinition(StructDeclaration declaration
,
37 IDefinitionScope definitionScope
, String structFieldName
) {
38 super(definitionScope
, structFieldName
);
40 this.declaration
= declaration
;
42 for (String fName
: declaration
.getFieldsList()) {
43 IDeclaration fieldDecl
= declaration
.getFields().get(fName
);
44 assert (fieldDecl
!= null);
46 Definition def
= fieldDecl
.createDefinition(this, fName
);
47 definitions
.put(fName
, def
);
51 // ------------------------------------------------------------------------
52 // Getters/Setters/Predicates
53 // ------------------------------------------------------------------------
56 public String
getPath() {
60 public HashMap
<String
, Definition
> getDefinitions() {
64 public StructDeclaration
getDeclaration() {
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
73 public void read(BitBuffer input
) {
74 for (String fName
: declaration
.getFieldsList()) {
75 Definition def
= definitions
.get(fName
);
83 public Definition
lookupDefinition(String lookupPath
) {
85 * The fields are created in order of appearance, so if a variant or
86 * sequence refers to a field that is after it, the field's definition
87 * will not be there yet in the hashmap.
89 return definitions
.get(lookupPath
);
92 public ArrayDefinition
lookupArray(String name
) {
93 Definition def
= definitions
.get(name
);
94 return (ArrayDefinition
) ((def
instanceof ArrayDefinition
) ? def
: null);
97 public EnumDefinition
lookupEnum(String name
) {
98 Definition def
= definitions
.get(name
);
99 return (EnumDefinition
) ((def
instanceof EnumDefinition
) ? def
: null);
102 public IntegerDefinition
lookupInteger(String name
) {
103 Definition def
= definitions
.get(name
);
104 return (IntegerDefinition
) ((def
instanceof IntegerDefinition
) ? def
108 public SequenceDefinition
lookupSequence(String name
) {
109 Definition def
= definitions
.get(name
);
110 return (SequenceDefinition
) ((def
instanceof SequenceDefinition
) ? def
114 public StringDefinition
lookupString(String name
) {
115 Definition def
= definitions
.get(name
);
116 return (StringDefinition
) ((def
instanceof StringDefinition
) ? def
120 public StructDefinition
lookupStruct(String name
) {
121 Definition def
= definitions
.get(name
);
122 return (StructDefinition
) ((def
instanceof StructDefinition
) ? def
126 public VariantDefinition
lookupVariant(String name
) {
127 Definition def
= definitions
.get(name
);
128 return (VariantDefinition
) ((def
instanceof VariantDefinition
) ? def
133 public String
toString() {
134 StringBuilder builder
= new StringBuilder();
136 int size
= this.declaration
.getFieldsList().size();
140 builder
.append("{ "); //$NON-NLS-1$
143 ListIterator
<String
> listIterator
= this.declaration
.getFieldsList().listIterator();
145 while (listIterator
.hasNext()) {
146 String field
= listIterator
.next();
147 builder
.append(definitions
.get(field
).toString());
150 builder
.append(", "); //$NON-NLS-1$
155 builder
.append(" }"); //$NON-NLS-1$
158 return builder
.toString();