9c5fa0a9992e28303e26c5387072e5adaefddcb4
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StructDeclaration.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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.LinkedList;
17 import java.util.List;
18
19 /**
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
29 */
30 public 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>();
38 private long maxAlign;
39
40 // ------------------------------------------------------------------------
41 // Constructors
42 // ------------------------------------------------------------------------
43
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 */
52 public StructDeclaration(long align) {
53 this.maxAlign = Math.max(align, 1);
54 }
55
56 // ------------------------------------------------------------------------
57 // Getters/Setters/Predicates
58 // ------------------------------------------------------------------------
59
60 /**
61 * Get current alignment
62 * @return the alignment of the struct and all its fields
63 */
64 public long getMaxAlign() {
65 return maxAlign;
66 }
67
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 */
73 public boolean hasField(String name) {
74 return this.fields.containsKey(name);
75 }
76
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 */
81 public HashMap<String, IDeclaration> getFields() {
82 return this.fields;
83 }
84
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 */
89 public List<String> getFieldsList() {
90 return this.fieldsList;
91 }
92
93 @Override
94 public long getAlignment() {
95 return this.maxAlign;
96 }
97
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
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 */
113 public void addField(String name, IDeclaration declaration) {
114 this.fields.put(name, declaration);
115 this.fieldsList.add(name);
116 maxAlign = Math.max(maxAlign, declaration.getAlignment());
117 if (maxAlign == 1) {
118 maxAlign = 1;
119 }
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
128 /*
129 * (non-Javadoc)
130 *
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) + fieldsList.hashCode();
138 result = (prime * result) + (int) (maxAlign ^ (maxAlign >>> 32));
139 return result;
140 }
141
142 /*
143 * (non-Javadoc)
144 *
145 * @see java.lang.Object#equals(java.lang.Object)
146 */
147 @Override
148 public boolean equals(Object obj) {
149 if (this == obj) {
150 return true;
151 }
152 if (obj == null) {
153 return false;
154 }
155 if (!(obj instanceof StructDeclaration)) {
156 return false;
157 }
158 StructDeclaration other = (StructDeclaration) obj;
159 if (!fieldsList.equals(other.fieldsList)) {
160 return false;
161 }
162 if (maxAlign != other.maxAlign) {
163 return false;
164 }
165 return true;
166 }
167
168 }
This page took 0.033527 seconds and 5 git commands to generate.