GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
EnumType.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (c) 2010 JogAmp Community. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use
34 * in the design, construction, operation or maintenance of any nuclear
35 * facility.
36 *
37 * Sun gratefully acknowledges that this software was originally authored
38 * and developed by Kenneth Bradley Russell and Christopher John Kline.
39 */
40package com.jogamp.gluegen.cgram.types;
41
42import java.util.ArrayList;
43import java.util.NoSuchElementException;
44
45import com.jogamp.gluegen.ASTLocusTag;
46import com.jogamp.gluegen.ConstantDefinition;
47import com.jogamp.gluegen.ConstantDefinition.CNumber;
48import com.jogamp.gluegen.GlueGenException;
49import com.jogamp.gluegen.cgram.types.TypeComparator.SemanticEqualityOp;
50
51
52/** Describes enumerated types. Enumerations are like ints except that
53they have a set of named values. */
54public class EnumType extends IntType implements Cloneable {
55
56 public static class Enumerator implements TypeComparator.SemanticEqualityOp {
57 private final String name;
58 private final String expr;
59 private final CNumber number;
60
61 public Enumerator(final String name, final long value) {
62 this.name = name;
63 this.number = new CNumber(false, false, value);
64 this.expr = this.number.toJavaString();
65 }
66 public Enumerator(final String name, final CNumber number) {
67 this.name = name;
68 this.number = number;
69 this.expr = this.number.toJavaString();
70 }
71 public Enumerator(final String name, final String value) {
72 this.name = name;
73 this.expr = value;
74 this.number = ConstantDefinition.decodeIntegerNumber(value);
75 }
76
77 public String getName() { return name; }
78 public String getExpr() { return expr; }
79 public CNumber getNumber() { return number; }
80 public boolean hasNumber() { return null != number; }
81
82 @Override
83 public int hashCode() {
84 // 31 * x == (x << 5) - x
85 final int hash = name.hashCode();
86 return ((hash << 5) - hash) + expr.hashCode();
87 }
88
89 @Override
90 public boolean equals(final Object arg) {
91 if (arg == this) {
92 return true;
93 } else if ( !(arg instanceof Enumerator) ) {
94 return false;
95 }
96 final Enumerator t = (Enumerator) arg;
97 return name.equals(t.name) &&
98 expr.equals(t.expr);
99 }
100
101 @Override
102 public int hashCodeSemantics() {
103 return hashCode();
104 }
105
106 @Override
107 public boolean equalSemantics(final SemanticEqualityOp arg) {
108 return equals(arg);
109 }
110
111 @Override
112 public String toString() { return "["+name+" = ["+expr+", "+number+"]"; }
113 }
114
115 private final IntType underlyingType;
116 private ArrayList<Enumerator> enums;
117
118 public EnumType(final String name) {
119 super(name, SizeThunk.LONG, false, CVAttributes.CONST);
120 this.underlyingType = new IntType(name, SizeThunk.LONG, false, CVAttributes.CONST);
121 }
122
123 public EnumType(final String name, final SizeThunk enumSizeInBytes, final ASTLocusTag astLocus) {
124 super(name, enumSizeInBytes, false, CVAttributes.CONST, astLocus);
125 this.underlyingType = new IntType(name, enumSizeInBytes, false, CVAttributes.CONST, astLocus);
126 }
127
128 private EnumType(final EnumType o, final int cvAttributes, final ASTLocusTag astLocus) {
129 super(o, cvAttributes, astLocus);
130 underlyingType = o.underlyingType;
131 if(null != o.enums) {
132 enums = new ArrayList<Enumerator>(o.enums);
133 }
134 }
135
136 @Override
137 Type newVariantImpl(final boolean newCVVariant, final int cvAttributes, final ASTLocusTag astLocus) {
138 return new EnumType(this, cvAttributes, astLocus);
139 }
140
141 @Override
142 protected int hashCodeImpl() {
143 // 31 * x == (x << 5) - x
144 int hash = super.hashCodeImpl();
145 hash = ((hash << 5) - hash) + underlyingType.hashCode();
146 return ((hash << 5) - hash) + TypeComparator.listsHashCode(enums);
147 }
148
149 @Override
150 protected boolean equalsImpl(final Type arg) {
151 final EnumType t = (EnumType) arg;
152 return super.equalsImpl(arg) &&
153 underlyingType.equals(t.underlyingType) &&
154 TypeComparator.listsEqual(enums, t.enums);
155 }
156
157 @Override
158 protected int hashCodeSemanticsImpl() {
159 // 31 * x == (x << 5) - x
160 int hash = super.hashCodeSemanticsImpl();
161 hash = ((hash << 5) - hash) + underlyingType.hashCodeSemantics();
162 return ((hash << 5) - hash) + TypeComparator.listsHashCodeSemantics(enums);
163 }
164
165 @Override
166 protected boolean equalSemanticsImpl(final Type arg) {
167 final EnumType t = (EnumType) arg;
168 return super.equalSemanticsImpl(arg) &&
169 underlyingType.equalSemantics(t.underlyingType) &&
170 TypeComparator.listsEqualSemantics(enums, t.enums);
171 }
172
173 @Override
174 public EnumType asEnum() {
175 return this;
176 }
177
178 public Type getUnderlyingType() { return this.underlyingType; }
179
180 public void addEnum(final String name, final Enumerator newEnum) {
181 if (enums == null) {
182 enums = new ArrayList<Enumerator>();
183 }
184 enums.add(newEnum);
185 clearCache();
186 }
187
188 /** Number of enumerates defined in this enum. */
189 public int getNumEnumerates() {
190 return enums.size();
191 }
192
193 /** Fetch <i>i</i>th (0..getNumEnumerates() - 1) {@link Enumerator} */
194 public Enumerator getEnum(final int i) {
195 return enums.get(i);
196 }
197
198 /** Fetch the enumerate with the given name. */
199 public Enumerator getEnum(final String name) {
200 for (int i = 0; i < enums.size(); ++i) {
201 final Enumerator n = (enums.get(i));
202 if (n.getName().equals(name)) {
203 return n;
204 }
205 }
206 throw new NoSuchElementException(
207 "No enumerate named \"" + name + "\" in EnumType \""
208 + getName() + "\"");
209 }
210
211 /** Does this enum type contain an enumerate with the given name? */
212 public boolean containsEnumerate(final String name) {
213 for (int i = 0; i < enums.size(); ++i) {
214 if ((enums.get(i)).getName().equals(name)) {
215 return true;
216 }
217 }
218 return false;
219 }
220
221 /** Remove the enumerate with the given name. Returns true if it was found
222 * and removed; false if it was not found.
223 */
224 public boolean removeEnumerate(final String name) {
225 for (int i = 0; i < enums.size(); ++i) {
226 final Enumerator e = enums.get(i);
227 if (e.getName().equals(name)) {
228 enums.remove(e);
229 clearCache();
230 return true;
231 }
232 }
233 return false;
234 }
235
236 public StringBuilder appendEnums(final StringBuilder sb, final boolean cr) {
237 for(int i=0; i<enums.size(); i++) {
238 sb.append(enums.get(i)).append(", ");
239 if( cr ) {
240 sb.append(String.format("%n"));
241 }
242 }
243 sb.append("}");
244 return sb;
245 }
246
247 @Override
248 public void visit(final TypeVisitor arg) {
249 super.visit(arg);
250 underlyingType.visit(arg);
251 }
252}
A Number, either integer, optionally [long, unsigned], or floating point, optionally [double].
Represents a [native] constant expression, comprises the [native] expression, see getNativeExpr() and...
static CNumber decodeIntegerNumber(final String v)
If the given string isIntegerNumber(String), return the decoded integer value, represented as a ANumb...
Enumerator(final String name, final long value)
Definition: EnumType.java:61
boolean equalSemantics(final SemanticEqualityOp arg)
Semantic equality test for Types exclusive its given name.
Definition: EnumType.java:107
int hashCodeSemantics()
Semantic hashcode for Types exclusive its given name.
Definition: EnumType.java:102
Enumerator(final String name, final String value)
Definition: EnumType.java:71
Enumerator(final String name, final CNumber number)
Definition: EnumType.java:66
Describes enumerated types.
Definition: EnumType.java:54
boolean equalSemanticsImpl(final Type arg)
Definition: EnumType.java:166
boolean containsEnumerate(final String name)
Does this enum type contain an enumerate with the given name?
Definition: EnumType.java:212
boolean equalsImpl(final Type arg)
Definition: EnumType.java:150
boolean removeEnumerate(final String name)
Remove the enumerate with the given name.
Definition: EnumType.java:224
int getNumEnumerates()
Number of enumerates defined in this enum.
Definition: EnumType.java:189
Enumerator getEnum(final int i)
Fetch ith (0..getNumEnumerates() - 1) Enumerator.
Definition: EnumType.java:194
EnumType asEnum()
Casts this to an EnumType or returns null if not an EnumType.
Definition: EnumType.java:174
void visit(final TypeVisitor arg)
Traverse this Type and all of its component types; for example, the return type and argument types of...
Definition: EnumType.java:248
StringBuilder appendEnums(final StringBuilder sb, final boolean cr)
Definition: EnumType.java:236
Enumerator getEnum(final String name)
Fetch the enumerate with the given name.
Definition: EnumType.java:199
EnumType(final String name, final SizeThunk enumSizeInBytes, final ASTLocusTag astLocus)
Definition: EnumType.java:123
void addEnum(final String name, final Enumerator newEnum)
Definition: EnumType.java:180
IntType(final String name, final SizeThunk size, final boolean unsigned, final int cvAttributes)
Definition: IntType.java:49
Provides a level of indirection between the definition of a type's size and the absolute value of thi...
Definition: SizeThunk.java:51
final int hashCodeSemantics()
Semantic hashcode for Types exclusive its given name.
Definition: Type.java:497
abstract boolean equalSemanticsImpl(final Type t)
final int hashCode()
Hashcode for Types.
Definition: Type.java:446
abstract boolean equalsImpl(final Type t)
final boolean equalSemantics(final SemanticEqualityOp arg)
Semantic equality test for Types exclusive its given name.
Definition: Type.java:514
final String getName()
Returns the name of this type.
Definition: Type.java:142
void visit(final TypeVisitor visitor)
Traverse this Type and all of its component types; for example, the return type and argument types of...
Definition: Type.java:544
final boolean equals(final Object arg)
Equality test for Types inclusive its given name.
Definition: Type.java:468
final Type newCVVariant(final int cvAttributes)
Return a variant of this type matching the given const/volatile attributes.
Definition: Type.java:98
Enumeration for const/volatile attributes.
Supports semantic equality and hash functions for types.
A visitor for Type's visitor model.