Commit | Line | Data |
---|---|---|
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 | ||
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 | * <b><u>StructDeclaration</u></b> | |
21 | */ | |
22 | public class StructDeclaration implements IDeclaration { | |
23 | ||
24 | // ------------------------------------------------------------------------ | |
25 | // Attributes | |
26 | // ------------------------------------------------------------------------ | |
27 | ||
28 | private final HashMap<String, IDeclaration> fields = new HashMap<String, IDeclaration>(); | |
29 | private final List<String> fieldsList = new LinkedList<String>(); | |
2b7f6f09 | 30 | private long maxAlign; |
866e5b51 FC |
31 | |
32 | // ------------------------------------------------------------------------ | |
33 | // Constructors | |
34 | // ------------------------------------------------------------------------ | |
35 | ||
9ac2eb62 MK |
36 | /** |
37 | * The struct declaration, add fields later | |
38 | * | |
39 | * @param align | |
40 | * the minimum alignment of the struct. (if a struct is 8bit | |
41 | * aligned and has a 32 bit aligned field, the struct becomes 32 | |
42 | * bit aligned. | |
43 | */ | |
2b7f6f09 MK |
44 | public StructDeclaration(long align) { |
45 | this.maxAlign = Math.max(align, 1); | |
866e5b51 FC |
46 | } |
47 | ||
48 | // ------------------------------------------------------------------------ | |
49 | // Getters/Setters/Predicates | |
50 | // ------------------------------------------------------------------------ | |
51 | ||
9ac2eb62 MK |
52 | /** |
53 | * Get current alignment | |
54 | * @return the alignment of the struct and all its fields | |
55 | */ | |
2b7f6f09 MK |
56 | public long getMaxAlign() { |
57 | return maxAlign; | |
866e5b51 FC |
58 | } |
59 | ||
9ac2eb62 MK |
60 | /** |
61 | * Query if the struct has a given field | |
62 | * @param name the name of the field, scopeless please | |
63 | * @return does the field exist? | |
64 | */ | |
866e5b51 FC |
65 | public boolean hasField(String name) { |
66 | return this.fields.containsKey(name); | |
67 | } | |
68 | ||
9ac2eb62 MK |
69 | /** |
70 | * get the fields of the struct in a map. Faster access time than a list. | |
71 | * @return a HashMap of the fields (key is the name) | |
72 | */ | |
866e5b51 FC |
73 | public HashMap<String, IDeclaration> getFields() { |
74 | return this.fields; | |
75 | } | |
76 | ||
9ac2eb62 MK |
77 | /** |
78 | * Gets the field list. Very important since the map of fields does not retain the order of the fields. | |
79 | * @return the field list. | |
80 | */ | |
866e5b51 FC |
81 | public List<String> getFieldsList() { |
82 | return this.fieldsList; | |
83 | } | |
84 | ||
fd74e6c1 MK |
85 | @Override |
86 | public long getAlignment() { | |
2b7f6f09 | 87 | return this.maxAlign; |
fd74e6c1 | 88 | } |
9ac2eb62 | 89 | |
866e5b51 FC |
90 | // ------------------------------------------------------------------------ |
91 | // Operations | |
92 | // ------------------------------------------------------------------------ | |
93 | ||
94 | @Override | |
95 | public StructDefinition createDefinition(IDefinitionScope definitionScope, | |
96 | String fieldName) { | |
97 | return new StructDefinition(this, definitionScope, fieldName); | |
98 | } | |
99 | ||
9ac2eb62 MK |
100 | /** |
101 | * Add a field to the struct | |
102 | * @param name the name of the field, scopeless | |
103 | * @param declaration the declaration of the field | |
104 | */ | |
866e5b51 | 105 | public void addField(String name, IDeclaration declaration) { |
866e5b51 FC |
106 | this.fields.put(name, declaration); |
107 | this.fieldsList.add(name); | |
2b7f6f09 | 108 | maxAlign = Math.max(maxAlign, declaration.getAlignment()); |
9ac2eb62 MK |
109 | if (maxAlign == 1) { |
110 | maxAlign = 1; | |
2b7f6f09 | 111 | } |
866e5b51 FC |
112 | } |
113 | ||
114 | @Override | |
115 | public String toString() { | |
116 | /* Only used for debugging */ | |
117 | return "[declaration] struct[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ | |
118 | } | |
119 | ||
9ac2eb62 MK |
120 | /* |
121 | * (non-Javadoc) | |
122 | * | |
4dd0eaed MK |
123 | * @see java.lang.Object#hashCode() |
124 | */ | |
125 | @Override | |
126 | public int hashCode() { | |
127 | final int prime = 31; | |
128 | int result = 1; | |
129 | result = (prime * result) | |
130 | + ((fieldsList == null) ? 0 : fieldsList.hashCode()); | |
131 | result = (prime * result) + (int) (maxAlign ^ (maxAlign >>> 32)); | |
132 | return result; | |
133 | } | |
134 | ||
9ac2eb62 MK |
135 | /* |
136 | * (non-Javadoc) | |
137 | * | |
4dd0eaed MK |
138 | * @see java.lang.Object#equals(java.lang.Object) |
139 | */ | |
140 | @Override | |
141 | public boolean equals(Object obj) { | |
142 | if (this == obj) { | |
143 | return true; | |
144 | } | |
145 | if (obj == null) { | |
146 | return false; | |
147 | } | |
148 | if (!(obj instanceof StructDeclaration)) { | |
149 | return false; | |
150 | } | |
151 | StructDeclaration other = (StructDeclaration) obj; | |
152 | if (fieldsList == null) { | |
153 | if (other.fieldsList != null) { | |
154 | return false; | |
155 | } | |
156 | } else if (!fieldsList.equals(other.fieldsList)) { | |
157 | return false; | |
158 | } | |
159 | if (maxAlign != other.maxAlign) { | |
160 | return false; | |
161 | } | |
162 | return true; | |
163 | } | |
164 | ||
866e5b51 | 165 | } |