ctf: Remove missing builder from the parser plugin
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StructDeclaration.java
CommitLineData
866e5b51
FC
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
13package org.eclipse.linuxtools.ctf.core.event.types;
14
15import java.util.HashMap;
16import java.util.LinkedList;
17import java.util.List;
18
19/**
d37aaa7f
FC
20 * A CTF structure declaration.
21 *
22 * A structure is similar to a C structure, it is a compound data type that
23 * contains other datatypes in fields. they are stored in an hashmap and indexed
24 * by names which are strings.
25 *
26 * @version 1.0
27 * @author Matthew Khouzam
28 * @author Simon Marchi
866e5b51
FC
29 */
30public class StructDeclaration implements IDeclaration {
31
32 // ------------------------------------------------------------------------
33 // Attributes
34 // ------------------------------------------------------------------------
35
36 private final HashMap<String, IDeclaration> fields = new HashMap<String, IDeclaration>();
37 private final List<String> fieldsList = new LinkedList<String>();
2b7f6f09 38 private long maxAlign;
866e5b51
FC
39
40 // ------------------------------------------------------------------------
41 // Constructors
42 // ------------------------------------------------------------------------
43
9ac2eb62
MK
44 /**
45 * The struct declaration, add fields later
46 *
47 * @param align
48 * the minimum alignment of the struct. (if a struct is 8bit
49 * aligned and has a 32 bit aligned field, the struct becomes 32
50 * bit aligned.
51 */
2b7f6f09
MK
52 public StructDeclaration(long align) {
53 this.maxAlign = Math.max(align, 1);
866e5b51
FC
54 }
55
56 // ------------------------------------------------------------------------
57 // Getters/Setters/Predicates
58 // ------------------------------------------------------------------------
59
9ac2eb62
MK
60 /**
61 * Get current alignment
62 * @return the alignment of the struct and all its fields
63 */
2b7f6f09
MK
64 public long getMaxAlign() {
65 return maxAlign;
866e5b51
FC
66 }
67
9ac2eb62
MK
68 /**
69 * Query if the struct has a given field
70 * @param name the name of the field, scopeless please
71 * @return does the field exist?
72 */
866e5b51
FC
73 public boolean hasField(String name) {
74 return this.fields.containsKey(name);
75 }
76
9ac2eb62
MK
77 /**
78 * get the fields of the struct in a map. Faster access time than a list.
79 * @return a HashMap of the fields (key is the name)
80 */
866e5b51
FC
81 public HashMap<String, IDeclaration> getFields() {
82 return this.fields;
83 }
84
9ac2eb62
MK
85 /**
86 * Gets the field list. Very important since the map of fields does not retain the order of the fields.
87 * @return the field list.
88 */
866e5b51
FC
89 public List<String> getFieldsList() {
90 return this.fieldsList;
91 }
92
fd74e6c1
MK
93 @Override
94 public long getAlignment() {
2b7f6f09 95 return this.maxAlign;
fd74e6c1 96 }
9ac2eb62 97
866e5b51
FC
98 // ------------------------------------------------------------------------
99 // Operations
100 // ------------------------------------------------------------------------
101
102 @Override
103 public StructDefinition createDefinition(IDefinitionScope definitionScope,
104 String fieldName) {
105 return new StructDefinition(this, definitionScope, fieldName);
106 }
107
9ac2eb62
MK
108 /**
109 * Add a field to the struct
110 * @param name the name of the field, scopeless
111 * @param declaration the declaration of the field
112 */
866e5b51 113 public void addField(String name, IDeclaration declaration) {
866e5b51
FC
114 this.fields.put(name, declaration);
115 this.fieldsList.add(name);
2b7f6f09 116 maxAlign = Math.max(maxAlign, declaration.getAlignment());
9ac2eb62
MK
117 if (maxAlign == 1) {
118 maxAlign = 1;
2b7f6f09 119 }
866e5b51
FC
120 }
121
122 @Override
123 public String toString() {
124 /* Only used for debugging */
125 return "[declaration] struct[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
126 }
127
9ac2eb62
MK
128 /*
129 * (non-Javadoc)
130 *
4dd0eaed
MK
131 * @see java.lang.Object#hashCode()
132 */
133 @Override
134 public int hashCode() {
135 final int prime = 31;
136 int result = 1;
137 result = (prime * result)
138 + ((fieldsList == null) ? 0 : fieldsList.hashCode());
139 result = (prime * result) + (int) (maxAlign ^ (maxAlign >>> 32));
140 return result;
141 }
142
9ac2eb62
MK
143 /*
144 * (non-Javadoc)
145 *
4dd0eaed
MK
146 * @see java.lang.Object#equals(java.lang.Object)
147 */
148 @Override
149 public boolean equals(Object obj) {
150 if (this == obj) {
151 return true;
152 }
153 if (obj == null) {
154 return false;
155 }
156 if (!(obj instanceof StructDeclaration)) {
157 return false;
158 }
159 StructDeclaration other = (StructDeclaration) obj;
160 if (fieldsList == null) {
161 if (other.fieldsList != null) {
162 return false;
163 }
164 } else if (!fieldsList.equals(other.fieldsList)) {
165 return false;
166 }
167 if (maxAlign != other.maxAlign) {
168 return false;
169 }
170 return true;
171 }
172
866e5b51 173}
This page took 0.050303 seconds and 5 git commands to generate.