@MappedType is now optional.

copyTo now only copies (SIZEOF - padding) bytes.
This commit is contained in:
Ioannis Tsakpinis 2011-07-24 09:38:46 +00:00
parent 054a5c5ae9
commit d3d14b6f3c
11 changed files with 23 additions and 34 deletions

View File

@ -32,10 +32,8 @@
package org.lwjgl.test.mapped;
import org.lwjgl.util.mapped.MappedObject;
import org.lwjgl.util.mapped.MappedType;
/** @author Riven */
@MappedType
public class MappedFloat extends MappedObject {
public MappedFloat() {

View File

@ -35,7 +35,6 @@ import org.lwjgl.MemoryUtil;
import org.lwjgl.util.mapped.MappedObject;
import org.lwjgl.util.mapped.MappedSet;
import org.lwjgl.util.mapped.MappedSet2;
import org.lwjgl.util.mapped.MappedType;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@ -82,7 +81,6 @@ public class MappedObjectTests3 {
System.out.println("current.view=" + some.view + ", not " + elementCount + ", as you might expect");
}
@MappedType
public static class Xyz extends MappedObject {
int x, y, z;

View File

@ -35,7 +35,6 @@ import org.lwjgl.MemoryUtil;
import org.lwjgl.PointerBuffer;
import org.lwjgl.opengl.Display;
import org.lwjgl.util.mapped.MappedObject;
import org.lwjgl.util.mapped.MappedType;
import org.lwjgl.util.mapped.Pointer;
import java.io.File;
@ -116,7 +115,6 @@ public class MappedObjectTests4 {
}
}
@MappedType
public static class MappedPointer extends MappedObject {
int foo;

View File

@ -33,12 +33,10 @@ package org.lwjgl.test.mapped;
import org.lwjgl.util.mapped.MappedField;
import org.lwjgl.util.mapped.MappedObject;
import org.lwjgl.util.mapped.MappedType;
import java.nio.ByteBuffer;
/** @author Riven */
@MappedType
public class MappedSomething extends MappedObject {
@MappedField(byteOffset = 0)

View File

@ -32,10 +32,8 @@
package org.lwjgl.test.mapped;
import org.lwjgl.util.mapped.MappedObject;
import org.lwjgl.util.mapped.MappedType;
/** @author Riven */
@MappedType
public class MappedVec2 extends MappedObject {
public float x;

View File

@ -32,10 +32,8 @@
package org.lwjgl.test.mapped;
import org.lwjgl.util.mapped.MappedObject;
import org.lwjgl.util.mapped.MappedType;
/** @author Riven */
@MappedType
public class MappedVec3 extends MappedObject {
public float x;

View File

@ -204,7 +204,6 @@ public final class SpriteShootoutMapped {
return texID;
}
@MappedType
public static class Pixel4b extends MappedObject {
public byte r, g, b, a;
@ -413,7 +412,6 @@ public final class SpriteShootoutMapped {
Display.destroy();
}
@MappedType
public static class Sprite extends MappedObject {
public float x, dx;
@ -421,7 +419,6 @@ public final class SpriteShootoutMapped {
}
@MappedType
public static class SpriteRender extends MappedObject {
public float x, y;

View File

@ -37,7 +37,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation can be used on fields of {@link MappedType} classes,
* This annotation can be used on fields of {@link MappedObject} subclasses,
* to manually specify byte offsets and lengths. This is useful when the
* mapped fields require custom alignment. {@link java.nio.ByteBuffer}
* fields are required to have this annotation with a hardcoded byte length.

View File

@ -39,8 +39,8 @@ import java.nio.ByteBuffer;
/**
* Base superclass of all mapped objects. Classes that require
* data mapping should extend this class and also be annotated
* with {@link MappedType}.
* data mapping should extend this class and registered with
* {@link MappedObjectTransformer#register(Class)}.
* <p/>
* Subclasses may only specify the default constructor. Any code
* inside that constructor is optional, but will not run when the
@ -215,7 +215,7 @@ public abstract class MappedObject {
}
/**
* Copies and amount of <code>SIZEOF</code> bytes, from the current
* Copies and amount of <code>SIZEOF - padding</code> bytes, from the current
* mapped object, to the specified mapped object.
*/
@SuppressWarnings("unused")
@ -225,8 +225,9 @@ public abstract class MappedObject {
}
/**
* Copies and amount of <code>SIZEOF*instances<c/ode> bytes, from the
* current mapped object, to the specified mapped object.
* Copies and amount of <code>SIZEOF * instances</code> bytes, from the
* current mapped object, to the specified mapped object. Note that
* this includes any padding bytes that are part of SIZEOF.
*/
@SuppressWarnings("unused")
public final <T extends MappedObject> void copyRange(T target, int instances) {

View File

@ -113,7 +113,7 @@ public class MappedObjectTransformer {
// => IADD
// => PUTFIELD MyMappedType.view
//
className_to_subtype.put(MAPPED_OBJECT_JVM, new MappedSubtypeInfo(MAPPED_OBJECT_JVM, null, -1, -1));
className_to_subtype.put(MAPPED_OBJECT_JVM, new MappedSubtypeInfo(MAPPED_OBJECT_JVM, null, -1, -1, -1));
}
final String vmName = System.getProperty("java.vm.name");
@ -128,15 +128,13 @@ public class MappedObjectTransformer {
*
* @param type the mapped object class.
*/
public static void register(Class<?> type) {
public static void register(Class<? extends MappedObject> type) {
if ( MappedObjectClassLoader.FORKED )
return;
final MappedType mapped = type.getAnnotation(MappedType.class);
if ( mapped == null )
throw new ClassFormatError("missing " + MappedType.class.getName() + " annotation");
if ( mapped.padding() < 0 )
if ( mapped != null && mapped.padding() < 0 )
throw new ClassFormatError("Invalid mapped type padding: " + mapped.padding());
if ( type.getEnclosingClass() != null && !Modifier.isStatic(type.getModifiers()) )
@ -148,7 +146,7 @@ public class MappedObjectTransformer {
int advancingOffset = 0;
long sizeof = 0;
for ( Field field : type.getDeclaredFields() ) {
FieldInfo fieldInfo = registerField(mapped, className, advancingOffset, field);
FieldInfo fieldInfo = registerField(mapped == null || mapped.autoGenerateOffsets(), className, advancingOffset, field);
if ( fieldInfo == null )
continue;
@ -158,14 +156,17 @@ public class MappedObjectTransformer {
sizeof = Math.max(sizeof, fieldInfo.offset + fieldInfo.length);
}
sizeof += mapped.padding();
final int align = mapped == null ? 4 : mapped.align();
final int padding = mapped == null ? 0 : mapped.padding();
final MappedSubtypeInfo mappedType = new MappedSubtypeInfo(className, fields, (int)sizeof, mapped.align());
sizeof += padding;
final MappedSubtypeInfo mappedType = new MappedSubtypeInfo(className, fields, (int)sizeof, align, padding);
if ( className_to_subtype.put(className, mappedType) != null )
throw new InternalError("duplicate mapped type: " + mappedType.className);
}
private static FieldInfo registerField(final MappedType mapped, final String className, int advancingOffset, final Field field) {
private static FieldInfo registerField(final boolean autoGenerateOffsets, final String className, int advancingOffset, final Field field) {
if ( Modifier.isStatic(field.getModifiers()) ) // static fields are never mapped
return null;
@ -174,7 +175,7 @@ public class MappedObjectTransformer {
throw new ClassFormatError("field '" + className + "." + field.getName() + "' not supported: " + field.getType());
MappedField meta = field.getAnnotation(MappedField.class);
if ( meta == null && !mapped.autoGenerateOffsets() )
if ( meta == null && !autoGenerateOffsets )
throw new ClassFormatError("field '" + className + "." + field.getName() + "' missing annotation " + MappedField.class.getName() + ": " + className);
Pointer pointer = field.getAnnotation(Pointer.class);
@ -688,7 +689,7 @@ public class MappedObjectTransformer {
final InsnList list = new InsnList();
// stack: target, this
list.add(getIntNode(mappedType.sizeof));
list.add(getIntNode(mappedType.sizeof - mappedType.padding));
// stack: sizeof, target, this
list.add(new MethodInsnNode(INVOKESTATIC, MAPPED_HELPER_JVM, "copy", "(L" + MAPPED_OBJECT_JVM + ";L" + MAPPED_OBJECT_JVM + ";I)V"));
// stack: -
@ -1073,10 +1074,11 @@ public class MappedObjectTransformer {
final int sizeof;
final int sizeof_shift;
final int align;
final int padding;
final Map<String, FieldInfo> fields;
MappedSubtypeInfo(String className, Map<String, FieldInfo> fields, int sizeof, int align) {
MappedSubtypeInfo(String className, Map<String, FieldInfo> fields, int sizeof, int align, int padding) {
this.className = className;
this.sizeof = sizeof;
@ -1085,6 +1087,7 @@ public class MappedObjectTransformer {
else
this.sizeof_shift = 0;
this.align = align;
this.padding = padding;
this.fields = fields;
}

View File

@ -37,7 +37,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation can be used on long fields of {@link MappedType} classes,
* This annotation can be used on long fields of {@link MappedObject} subclasses,
* to specify that the long value should be interpreted as a pointer. This
* will determine the actual byte size of the field at runtime (4 or 8 bytes).
*