1 /*
2  * Copyright (c) 2002-2008 LWJGL Project
3  * 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  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions 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 'LWJGL' nor the names of
17  *   its contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package org.lwjgl.util.generator.openal;
34 
35 /**
36  *
37  * The OpenAL specific generator behaviour
38  *
39  * @author elias_naur <elias_naur@users.sourceforge.net>
40  * @version $Revision: 2983 $
41  * $Id: ALTypeMap.java 2983 2008-04-07 18:36:09Z matzon $
42  */
43 
44 import org.lwjgl.util.generator.Signedness;
45 import org.lwjgl.util.generator.TypeMap;
46 
47 import java.io.PrintWriter;
48 import java.lang.annotation.Annotation;
49 import java.nio.*;
50 import java.util.HashMap;
51 import java.util.Map;
52 import javax.lang.model.element.AnnotationMirror;
53 import javax.lang.model.element.ExecutableElement;
54 import javax.lang.model.type.TypeKind;
55 
56 public class ALTypeMap implements TypeMap {
57 	private static final Map<Class, TypeKind> native_types_to_primitive;
58 
59 	static {
60 		native_types_to_primitive = new HashMap<Class, TypeKind>();
native_types_to_primitive.put(ALboolean.class, TypeKind.BOOLEAN)61 		native_types_to_primitive.put(ALboolean.class, TypeKind.BOOLEAN);
native_types_to_primitive.put(ALbyte.class, TypeKind.BYTE)62 		native_types_to_primitive.put(ALbyte.class, TypeKind.BYTE);
native_types_to_primitive.put(ALenum.class, TypeKind.INT)63 		native_types_to_primitive.put(ALenum.class, TypeKind.INT);
native_types_to_primitive.put(ALfloat.class, TypeKind.FLOAT)64 		native_types_to_primitive.put(ALfloat.class, TypeKind.FLOAT);
native_types_to_primitive.put(ALdouble.class, TypeKind.DOUBLE)65 		native_types_to_primitive.put(ALdouble.class, TypeKind.DOUBLE);
native_types_to_primitive.put(ALint.class, TypeKind.INT)66 		native_types_to_primitive.put(ALint.class, TypeKind.INT);
native_types_to_primitive.put(ALshort.class, TypeKind.SHORT)67 		native_types_to_primitive.put(ALshort.class, TypeKind.SHORT);
native_types_to_primitive.put(ALsizei.class, TypeKind.INT)68 		native_types_to_primitive.put(ALsizei.class, TypeKind.INT);
native_types_to_primitive.put(ALubyte.class, TypeKind.BYTE)69 		native_types_to_primitive.put(ALubyte.class, TypeKind.BYTE);
native_types_to_primitive.put(ALuint.class, TypeKind.INT)70 		native_types_to_primitive.put(ALuint.class, TypeKind.INT);
native_types_to_primitive.put(ALvoid.class, TypeKind.BYTE)71 		native_types_to_primitive.put(ALvoid.class, TypeKind.BYTE);
72 	}
73 
74 	@Override
getPrimitiveTypeFromNativeType(Class native_type)75 	public TypeKind getPrimitiveTypeFromNativeType(Class native_type) {
76 		TypeKind kind = native_types_to_primitive.get(native_type);
77 		if ( kind == null )
78 			throw new RuntimeException("Unsupported type " + native_type);
79 		return kind;
80 	}
81 
82 	@Override
getSignednessFromType(Class type)83 	public Signedness getSignednessFromType(Class type) {
84 		if ( ALuint.class.equals(type) )
85 			return Signedness.UNSIGNED;
86 		else if ( ALint.class.equals(type) )
87 			return Signedness.SIGNED;
88 		else if ( ALshort.class.equals(type) )
89 			return Signedness.SIGNED;
90 		else if ( ALbyte.class.equals(type) )
91 			return Signedness.SIGNED;
92 		else
93 			return Signedness.NONE;
94 	}
95 
96 	@Override
translateAnnotation(Class annotation_type)97 	public String translateAnnotation(Class annotation_type) {
98 		if ( annotation_type.equals(ALuint.class) )
99 			return "i";
100 		else if ( annotation_type.equals(ALint.class) )
101 			return "i";
102 		else if ( annotation_type.equals(ALshort.class) )
103 			return "s";
104 		else if ( annotation_type.equals(ALbyte.class) )
105 			return "b";
106 		else if ( annotation_type.equals(ALfloat.class) )
107 			return "f";
108 		else if ( annotation_type.equals(ALdouble.class) )
109 			return "d";
110 		else if ( annotation_type.equals(ALboolean.class) || annotation_type.equals(ALvoid.class) )
111 			return "";
112 		else
113 			throw new RuntimeException(annotation_type + " is not allowed");
114 	}
115 
116 	@Override
getNativeTypeFromPrimitiveType(TypeKind kind)117 	public Class getNativeTypeFromPrimitiveType(TypeKind kind) {
118 		Class type;
119 		switch ( kind ) {
120 			case INT:
121 				type = ALint.class;
122 				break;
123 			case FLOAT:
124 				type = ALfloat.class;
125 				break;
126 			case DOUBLE:
127 				type = ALdouble.class;
128 				break;
129 			case SHORT:
130 				type = ALshort.class;
131 				break;
132 			case BYTE:
133 				type = ALbyte.class;
134 				break;
135 			case BOOLEAN:
136 				type = ALboolean.class;
137 				break;
138 			default:
139 				throw new RuntimeException(kind + " is not allowed");
140 		}
141 		return type;
142 	}
143 
getValidBufferTypes(Class type)144 	private static Class[] getValidBufferTypes(Class type) {
145 		if ( type.equals(IntBuffer.class) )
146 			return new Class[] { ALenum.class, ALint.class, ALsizei.class, ALuint.class };
147 		else if ( type.equals(FloatBuffer.class) )
148 			return new Class[] { ALfloat.class };
149 		else if ( type.equals(ByteBuffer.class) )
150 			return new Class[] { ALboolean.class, ALbyte.class, ALvoid.class };
151 		else if ( type.equals(ShortBuffer.class) )
152 			return new Class[] { ALshort.class };
153 		else if ( type.equals(DoubleBuffer.class) )
154 			return new Class[] { ALdouble.class };
155 		else
156 			return new Class[] { };
157 	}
158 
getValidPrimitiveTypes(Class type)159 	private static Class[] getValidPrimitiveTypes(Class type) {
160 		if ( type.equals(int.class) )
161 			return new Class[] { ALenum.class, ALint.class, ALsizei.class, ALuint.class };
162 		else if ( type.equals(double.class) )
163 			return new Class[] { ALdouble.class };
164 		else if ( type.equals(float.class) )
165 			return new Class[] { ALfloat.class };
166 		else if ( type.equals(short.class) )
167 			return new Class[] { ALshort.class };
168 		else if ( type.equals(byte.class) )
169 			return new Class[] { ALbyte.class };
170 		else if ( type.equals(boolean.class) )
171 			return new Class[] { ALboolean.class };
172 		else if ( type.equals(void.class) )
173 			return new Class[] { ALvoid.class };
174 		else
175 			return new Class[] { };
176 	}
177 
178 	@Override
printCapabilitiesInit(final PrintWriter writer)179 	public void printCapabilitiesInit(final PrintWriter writer) {
180 		throw new UnsupportedOperationException();
181 	}
182 
183 	@Override
getCapabilities()184 	public String getCapabilities() {
185 		throw new UnsupportedOperationException();
186 	}
187 
188 	@Override
getAPIUtilParam(boolean comma)189 	public String getAPIUtilParam(boolean comma) {
190 		return "";
191 	}
192 
193 	@Override
printErrorCheckMethod(final PrintWriter writer, final ExecutableElement method, final String tabs)194 	public void printErrorCheckMethod(final PrintWriter writer, final ExecutableElement method, final String tabs) {
195 		writer.println(tabs + "Util.checkALError();");
196 	}
197 
198 	@Override
getRegisterNativesFunctionName()199 	public String getRegisterNativesFunctionName() {
200 		return "extal_InitializeClass";
201 	}
202 
203 	@Override
getTypedefPostfix()204 	public String getTypedefPostfix() {
205 		return "";
206 	}
207 
208 	@Override
getFunctionPrefix()209 	public String getFunctionPrefix() {
210 		return "ALAPIENTRY";
211 	}
212 
213 	@Override
printNativeIncludes(PrintWriter writer)214 	public void printNativeIncludes(PrintWriter writer) {
215 		writer.println("#include \"extal.h\"");
216 	}
217 
218 	@Override
getStringElementType()219 	public Class<? extends Annotation> getStringElementType() {
220 		return ALubyte.class;
221 	}
222 
223 	@Override
getStringArrayType()224 	public Class<? extends Annotation> getStringArrayType() {
225 		return ALubyte.class;
226 	}
227 
228 	@Override
getByteBufferArrayType()229 	public Class<? extends Annotation> getByteBufferArrayType() {
230 		return ALubyte.class;
231 	}
232 
233 	@Override
getValidAnnotationTypes(Class type)234 	public Class[] getValidAnnotationTypes(Class type) {
235 		Class[] valid_types;
236 		if ( Buffer.class.isAssignableFrom(type) )
237 			valid_types = getValidBufferTypes(type);
238 		else if ( type.isPrimitive() )
239 			valid_types = getValidPrimitiveTypes(type);
240 		else if ( type.equals(String.class) )
241 			valid_types = new Class[] { ALubyte.class };
242 		else
243 			valid_types = new Class[] { };
244 		return valid_types;
245 	}
246 
247 	@Override
getVoidType()248 	public Class<? extends Annotation> getVoidType() {
249 		return ALvoid.class;
250 	}
251 
252 	@Override
getInverseType(Class type)253 	public Class<? extends Annotation> getInverseType(Class type) {
254 		if ( ALuint.class.equals(type) )
255 			return ALint.class;
256 		else if ( ALint.class.equals(type) )
257 			return ALuint.class;
258 		else
259 			return null;
260 	}
261 
262 	@Override
getAutoTypeFromAnnotation(AnnotationMirror annotation)263 	public String getAutoTypeFromAnnotation(AnnotationMirror annotation) {
264 		return null;
265 	}
266 }
267