GlueGen: src/java/com/jogamp/gluegen/cgram/types/EnumType.java Source File

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. */
54 public 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}
com.jogamp.gluegen.ASTLocusTag
An AST location tag.
Definition: ASTLocusTag.java:33
com.jogamp.gluegen.ConstantDefinition.CNumber
A Number, either integer, optionally [long, unsigned], or floating point, optionally [double].
Definition: ConstantDefinition.java:62
com.jogamp.gluegen.ConstantDefinition.CNumber.toJavaString
final String toJavaString()
Definition: ConstantDefinition.java:113
com.jogamp.gluegen.ConstantDefinition
Represents a [native] constant expression, comprises the [native] expression, see getNativeExpr() and...
Definition: ConstantDefinition.java:54
com.jogamp.gluegen.ConstantDefinition.decodeIntegerNumber
static CNumber decodeIntegerNumber(final String v)
If the given string isIntegerNumber(String), return the decoded integer value, represented as a ANumb...
Definition: ConstantDefinition.java:577
com.jogamp.gluegen.cgram.types.EnumType.Enumerator
Definition: EnumType.java:56
com.jogamp.gluegen.cgram.types.EnumType.Enumerator.hashCode
int hashCode()
Definition: EnumType.java:83
com.jogamp.gluegen.cgram.types.EnumType.Enumerator.getNumber
CNumber getNumber()
Definition: EnumType.java:79
com.jogamp.gluegen.cgram.types.EnumType.Enumerator.toString
String toString()
Definition: EnumType.java:112
com.jogamp.gluegen.cgram.types.EnumType.Enumerator.getName
String getName()
Definition: EnumType.java:77
com.jogamp.gluegen.cgram.types.EnumType.Enumerator.getExpr
String getExpr()
Definition: EnumType.java:78
com.jogamp.gluegen.cgram.types.EnumType.Enumerator.Enumerator
Enumerator(final String name, final long value)
Definition: EnumType.java:61
com.jogamp.gluegen.cgram.types.EnumType.Enumerator.equalSemantics
boolean equalSemantics(final SemanticEqualityOp arg)
Semantic equality test for Types exclusive its given name.
Definition: EnumType.java:107
com.jogamp.gluegen.cgram.types.EnumType.Enumerator.equals
boolean equals(final Object arg)
Definition: EnumType.java:90
com.jogamp.gluegen.cgram.types.EnumType.Enumerator.hashCodeSemantics
int hashCodeSemantics()
Semantic hashcode for Types exclusive its given name.
Definition: EnumType.java:102
com.jogamp.gluegen.cgram.types.EnumType.Enumerator.Enumerator
Enumerator(final String name, final String value)
Definition: EnumType.java:71
com.jogamp.gluegen.cgram.types.EnumType.Enumerator.Enumerator
Enumerator(final String name, final CNumber number)
Definition: EnumType.java:66
com.jogamp.gluegen.cgram.types.EnumType.Enumerator.hasNumber
boolean hasNumber()
Definition: EnumType.java:80
com.jogamp.gluegen.cgram.types.EnumType
Describes enumerated types.
Definition: EnumType.java:54
com.jogamp.gluegen.cgram.types.EnumType.equalSemanticsImpl
boolean equalSemanticsImpl(final Type arg)
Definition: EnumType.java:166
com.jogamp.gluegen.cgram.types.EnumType.EnumType
EnumType(final String name)
Definition: EnumType.java:118
com.jogamp.gluegen.cgram.types.EnumType.getUnderlyingType
Type getUnderlyingType()
Definition: EnumType.java:178
com.jogamp.gluegen.cgram.types.EnumType.containsEnumerate
boolean containsEnumerate(final String name)
Does this enum type contain an enumerate with the given name?
Definition: EnumType.java:212
com.jogamp.gluegen.cgram.types.EnumType.equalsImpl
boolean equalsImpl(final Type arg)
Definition: EnumType.java:150
com.jogamp.gluegen.cgram.types.EnumType.removeEnumerate
boolean removeEnumerate(final String name)
Remove the enumerate with the given name.
Definition: EnumType.java:224
com.jogamp.gluegen.cgram.types.EnumType.getNumEnumerates
int getNumEnumerates()
Number of enumerates defined in this enum.
Definition: EnumType.java:189
com.jogamp.gluegen.cgram.types.EnumType.getEnum
Enumerator getEnum(final int i)
Fetch ith (0..getNumEnumerates() - 1) Enumerator.
Definition: EnumType.java:194
com.jogamp.gluegen.cgram.types.EnumType.asEnum
EnumType asEnum()
Casts this to an EnumType or returns null if not an EnumType.
Definition: EnumType.java:174
com.jogamp.gluegen.cgram.types.EnumType.visit
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
com.jogamp.gluegen.cgram.types.EnumType.appendEnums
StringBuilder appendEnums(final StringBuilder sb, final boolean cr)
Definition: EnumType.java:236
com.jogamp.gluegen.cgram.types.EnumType.hashCodeSemanticsImpl
int hashCodeSemanticsImpl()
Definition: EnumType.java:158
com.jogamp.gluegen.cgram.types.EnumType.getEnum
Enumerator getEnum(final String name)
Fetch the enumerate with the given name.
Definition: EnumType.java:199
com.jogamp.gluegen.cgram.types.EnumType.EnumType
EnumType(final String name, final SizeThunk enumSizeInBytes, final ASTLocusTag astLocus)
Definition: EnumType.java:123
com.jogamp.gluegen.cgram.types.EnumType.addEnum
void addEnum(final String name, final Enumerator newEnum)
Definition: EnumType.java:180
com.jogamp.gluegen.cgram.types.EnumType.hashCodeImpl
int hashCodeImpl()
Definition: EnumType.java:142
com.jogamp.gluegen.cgram.types.IntType
Definition: IntType.java:44
com.jogamp.gluegen.cgram.types.IntType.IntType
IntType(final String name, final SizeThunk size, final boolean unsigned, final int cvAttributes)
Definition: IntType.java:49
com.jogamp.gluegen.cgram.types.SizeThunk
Provides a level of indirection between the definition of a type's size and the absolute value of thi...
Definition: SizeThunk.java:51
com.jogamp.gluegen.cgram.types.SizeThunk.LONG
static final SizeThunk LONG
Definition: SizeThunk.java:187
com.jogamp.gluegen.cgram.types.TypeComparator
Definition: TypeComparator.java:32
com.jogamp.gluegen.cgram.types.Type
Models a C type.
Definition: Type.java:54
com.jogamp.gluegen.cgram.types.Type.hashCodeSemantics
final int hashCodeSemantics()
Semantic hashcode for Types exclusive its given name.
Definition: Type.java:497
com.jogamp.gluegen.cgram.types.Type.equalSemanticsImpl
abstract boolean equalSemanticsImpl(final Type t)
com.jogamp.gluegen.cgram.types.Type.hashCode
final int hashCode()
Hashcode for Types.
Definition: Type.java:446
com.jogamp.gluegen.cgram.types.Type.equalsImpl
abstract boolean equalsImpl(final Type t)
com.jogamp.gluegen.cgram.types.Type.clearCache
final void clearCache()
Definition: Type.java:87
com.jogamp.gluegen.cgram.types.Type.equalSemantics
final boolean equalSemantics(final SemanticEqualityOp arg)
Semantic equality test for Types exclusive its given name.
Definition: Type.java:514
com.jogamp.gluegen.cgram.types.Type.getName
final String getName()
Returns the name of this type.
Definition: Type.java:142
com.jogamp.gluegen.cgram.types.Type.visit
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
com.jogamp.gluegen.cgram.types.Type.equals
final boolean equals(final Object arg)
Equality test for Types inclusive its given name.
Definition: Type.java:468
com.jogamp.gluegen.cgram.types.Type.newCVVariant
final Type newCVVariant(final int cvAttributes)
Return a variant of this type matching the given const/volatile attributes.
Definition: Type.java:98
com.jogamp.gluegen.cgram.types.CVAttributes
Enumeration for const/volatile attributes.
Definition: CVAttributes.java:45
com.jogamp.gluegen.cgram.types.CVAttributes.CONST
static final int CONST
Definition: CVAttributes.java:46
com.jogamp.gluegen.cgram.types.TypeComparator.SemanticEqualityOp
Supports semantic equality and hash functions for types.
Definition: TypeComparator.java:36
com.jogamp.gluegen.cgram.types.TypeVisitor
A visitor for Type's visitor model.
Definition: TypeVisitor.java:45