Merge master in TmfTraceModel
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StructDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
3 *
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
8 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.ctf.core.event.types;
14
15 import java.util.HashMap;
16 import java.util.ListIterator;
17
18 import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
19
20 /**
21 * <b><u>StructDefinition</u></b>
22 */
23 public class StructDefinition extends Definition implements IDefinitionScope {
24
25 // ------------------------------------------------------------------------
26 // Attributes
27 // ------------------------------------------------------------------------
28
29 private final StructDeclaration declaration;
30 private final HashMap<String, Definition> definitions = new HashMap<String, Definition>();
31
32 // ------------------------------------------------------------------------
33 // Constructors
34 // ------------------------------------------------------------------------
35
36 public StructDefinition(StructDeclaration declaration,
37 IDefinitionScope definitionScope, String structFieldName) {
38 super(definitionScope, structFieldName);
39
40 this.declaration = declaration;
41
42 for (String fName : declaration.getFieldsList()) {
43 IDeclaration fieldDecl = declaration.getFields().get(fName);
44 assert (fieldDecl != null);
45
46 Definition def = fieldDecl.createDefinition(this, fName);
47 definitions.put(fName, def);
48 }
49 }
50
51 // ------------------------------------------------------------------------
52 // Getters/Setters/Predicates
53 // ------------------------------------------------------------------------
54
55 @Override
56 public String getPath() {
57 return path;
58 }
59
60 public HashMap<String, Definition> getDefinitions() {
61 return definitions;
62 }
63
64 public StructDeclaration getDeclaration() {
65 return declaration;
66 }
67
68 // ------------------------------------------------------------------------
69 // Operations
70 // ------------------------------------------------------------------------
71
72 @Override
73 public void read(BitBuffer input) {
74 for (String fName : declaration.getFieldsList()) {
75 Definition def = definitions.get(fName);
76 assert (def != null);
77
78 def.read(input);
79 }
80 }
81
82 @Override
83 public Definition lookupDefinition(String lookupPath) {
84 /*
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.
88 */
89 Definition retVal = definitions.get(lookupPath);
90 if (retVal == null) {
91 retVal = definitions.get("_" + lookupPath); //$NON-NLS-1$
92 }
93 return retVal;
94 }
95
96 public ArrayDefinition lookupArray(String name) {
97 Definition def = lookupDefinition(name);
98 return (ArrayDefinition) ((def instanceof ArrayDefinition) ? def : null);
99 }
100
101 public EnumDefinition lookupEnum(String name) {
102 Definition def = lookupDefinition(name);
103 return (EnumDefinition) ((def instanceof EnumDefinition) ? def : null);
104 }
105
106 public IntegerDefinition lookupInteger(String name) {
107 Definition def = lookupDefinition(name);
108 return (IntegerDefinition) ((def instanceof IntegerDefinition) ? def
109 : null);
110 }
111
112 public SequenceDefinition lookupSequence(String name) {
113 Definition def = lookupDefinition(name);
114 return (SequenceDefinition) ((def instanceof SequenceDefinition) ? def
115 : null);
116 }
117
118 public StringDefinition lookupString(String name) {
119 Definition def = lookupDefinition(name);
120 return (StringDefinition) ((def instanceof StringDefinition) ? def
121 : null);
122 }
123
124 public StructDefinition lookupStruct(String name) {
125 Definition def = lookupDefinition(name);
126 return (StructDefinition) ((def instanceof StructDefinition) ? def
127 : null);
128 }
129
130 public VariantDefinition lookupVariant(String name) {
131 Definition def = lookupDefinition(name);
132 return (VariantDefinition) ((def instanceof VariantDefinition) ? def
133 : null);
134 }
135
136 @Override
137 public String toString() {
138 StringBuilder builder = new StringBuilder();
139
140 int size = this.declaration.getFieldsList().size();
141 int n = 0;
142
143 if (size > 1) {
144 builder.append("{ "); //$NON-NLS-1$
145 }
146
147 ListIterator<String> listIterator = this.declaration.getFieldsList().listIterator();
148
149 while (listIterator.hasNext()) {
150 String field = listIterator.next();
151 builder.append(lookupDefinition(field).toString());
152 n++;
153 if (n != size) {
154 builder.append(", "); //$NON-NLS-1$
155 }
156 }
157
158 if (size > 1) {
159 builder.append(" }"); //$NON-NLS-1$
160 }
161
162 return builder.toString();
163 }
164 }
This page took 0.034937 seconds and 5 git commands to generate.