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