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
.internal
.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 Definition retVal
= definitions
.get(lookupPath
);
91 retVal
= definitions
.get("_" + lookupPath
); //$NON-NLS-1$
96 public ArrayDefinition
lookupArray(String name
) {
97 Definition def
= lookupDefinition(name
);
98 return (ArrayDefinition
) ((def
instanceof ArrayDefinition
) ? def
: null);
101 public EnumDefinition
lookupEnum(String name
) {
102 Definition def
= lookupDefinition(name
);
103 return (EnumDefinition
) ((def
instanceof EnumDefinition
) ? def
: null);
106 public IntegerDefinition
lookupInteger(String name
) {
107 Definition def
= lookupDefinition(name
);
108 return (IntegerDefinition
) ((def
instanceof IntegerDefinition
) ? def
112 public SequenceDefinition
lookupSequence(String name
) {
113 Definition def
= lookupDefinition(name
);
114 return (SequenceDefinition
) ((def
instanceof SequenceDefinition
) ? def
118 public StringDefinition
lookupString(String name
) {
119 Definition def
= lookupDefinition(name
);
120 return (StringDefinition
) ((def
instanceof StringDefinition
) ? def
124 public StructDefinition
lookupStruct(String name
) {
125 Definition def
= lookupDefinition(name
);
126 return (StructDefinition
) ((def
instanceof StructDefinition
) ? def
130 public VariantDefinition
lookupVariant(String name
) {
131 Definition def
= lookupDefinition(name
);
132 return (VariantDefinition
) ((def
instanceof VariantDefinition
) ? def
137 public String
toString() {
138 StringBuilder builder
= new StringBuilder();
140 int size
= this.declaration
.getFieldsList().size();
144 builder
.append("{ "); //$NON-NLS-1$
147 ListIterator
<String
> listIterator
= this.declaration
.getFieldsList().listIterator();
149 while (listIterator
.hasNext()) {
150 String field
= listIterator
.next();
151 builder
.append(lookupDefinition(field
).toString());
154 builder
.append(", "); //$NON-NLS-1$
159 builder
.append(" }"); //$NON-NLS-1$
162 return builder
.toString();