fixed formatting
This commit is contained in:
parent
e8f76d43fe
commit
dccec876c5
@ -16,9 +16,8 @@ import org.lwjgl.system.MemoryUtil;
|
|||||||
import static org.lwjgl.system.MemoryUtil.*;
|
import static org.lwjgl.system.MemoryUtil.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class for alternative API functions. Instead of the user
|
* Helper class for alternative API functions. Instead of the user passing their
|
||||||
* passing their own buffer, thread-local instances of this class
|
* own buffer, thread-local instances of this class are used internally instead.
|
||||||
* are used internally instead.
|
|
||||||
*/
|
*/
|
||||||
public class APIBuffer {
|
public class APIBuffer {
|
||||||
|
|
||||||
@ -37,16 +36,21 @@ public class APIBuffer {
|
|||||||
address = memAddress(buffer);
|
address = memAddress(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Resets the parameter offset to 0. */
|
/**
|
||||||
|
* Resets the parameter offset to 0.
|
||||||
|
*/
|
||||||
public APIBuffer reset() {
|
public APIBuffer reset() {
|
||||||
offset = 0;
|
offset = 0;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Pushes the current parameter offset to a stack. */
|
/**
|
||||||
|
* Pushes the current parameter offset to a stack.
|
||||||
|
*/
|
||||||
public APIBuffer push() {
|
public APIBuffer push() {
|
||||||
if ( stackDepth == stack.length )
|
if (stackDepth == stack.length) {
|
||||||
stack = Arrays.copyOf(stack, stack.length << 1);
|
stack = Arrays.copyOf(stack, stack.length << 1);
|
||||||
|
}
|
||||||
|
|
||||||
stack[stackDepth++] = offset;
|
stack[stackDepth++] = offset;
|
||||||
|
|
||||||
@ -56,48 +60,66 @@ public class APIBuffer {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Restores the last pushed parameter offset. */
|
/**
|
||||||
|
* Restores the last pushed parameter offset.
|
||||||
|
*/
|
||||||
public APIBuffer pop() {
|
public APIBuffer pop() {
|
||||||
offset = stack[--stackDepth];
|
offset = stack[--stackDepth];
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the current parameter offset. */
|
/**
|
||||||
|
* Returns the current parameter offset.
|
||||||
|
*/
|
||||||
public int getOffset() {
|
public int getOffset() {
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the current parameter offset. */
|
/**
|
||||||
|
* Sets the current parameter offset.
|
||||||
|
*/
|
||||||
public void setOffset(int offset) {
|
public void setOffset(int offset) {
|
||||||
this.offset = offset;
|
this.offset = offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the memory address of the internal {@link ByteBuffer}. This address may change after a call to one of the {@code <type>Param()} methods. */
|
/**
|
||||||
|
* Returns the memory address of the internal {@link ByteBuffer}. This
|
||||||
|
* address may change after a call to one of the {@code <type>Param()}
|
||||||
|
* methods.
|
||||||
|
*/
|
||||||
public long address() {
|
public long address() {
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the memory address of the specified {@code offset}. This address may change after a call to one of the {@code <type>Param()} methods. */
|
/**
|
||||||
|
* Returns the memory address of the specified {@code offset}. This address
|
||||||
|
* may change after a call to one of the {@code <type>Param()} methods.
|
||||||
|
*/
|
||||||
public long address(int offset) {
|
public long address(int offset) {
|
||||||
return address + offset;
|
return address + offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the memory address of the specified {@code offset} or {@link MemoryUtil#NULL NULL} if the specified {@code value} is null. This address may
|
* Returns the memory address of the specified {@code offset} or
|
||||||
* change after a call to one of the {@code <type>Param()} methods.
|
* {@link MemoryUtil#NULL NULL} if the specified {@code value} is null. This
|
||||||
|
* address may change after a call to one of the {@code <type>Param()}
|
||||||
|
* methods.
|
||||||
*/
|
*/
|
||||||
public long addressSafe(Object value, int offset) {
|
public long addressSafe(Object value, int offset) {
|
||||||
return value == null ? NULL : address(offset);
|
return value == null ? NULL : address(offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the {@link ByteBuffer} that backs this {@link APIBuffer}. */
|
/**
|
||||||
|
* Returns the {@link ByteBuffer} that backs this {@link APIBuffer}.
|
||||||
|
*/
|
||||||
public ByteBuffer buffer() {
|
public ByteBuffer buffer() {
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureCapacity(int capacity) {
|
private void ensureCapacity(int capacity) {
|
||||||
if ( capacity <= buffer.capacity() )
|
if (capacity <= buffer.capacity()) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ByteBuffer resized = BufferUtils.createByteBuffer(mathRoundPoT(capacity));
|
ByteBuffer resized = BufferUtils.createByteBuffer(mathRoundPoT(capacity));
|
||||||
|
|
||||||
@ -109,7 +131,6 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
private int param(int bytes) {
|
private int param(int bytes) {
|
||||||
return param(bytes, bytes);
|
return param(bytes, bytes);
|
||||||
}
|
}
|
||||||
@ -121,85 +142,152 @@ public class APIBuffer {
|
|||||||
return param;
|
return param;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional boolean value and returns the address offset. */
|
/**
|
||||||
public int booleanParam() { return param(1); }
|
* Ensures space for an additional boolean value and returns the address
|
||||||
|
* offset.
|
||||||
|
*/
|
||||||
|
public int booleanParam() {
|
||||||
|
return param(1);
|
||||||
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional byte value and returns the address offset. */
|
/**
|
||||||
public int byteParam() { return param(1); }
|
* Ensures space for an additional byte value and returns the address
|
||||||
|
* offset.
|
||||||
|
*/
|
||||||
|
public int byteParam() {
|
||||||
|
return param(1);
|
||||||
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional short value and returns the address offset. */
|
/**
|
||||||
public int shortParam() { return param(2); }
|
* Ensures space for an additional short value and returns the address
|
||||||
|
* offset.
|
||||||
|
*/
|
||||||
|
public int shortParam() {
|
||||||
|
return param(2);
|
||||||
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional int value and returns the address offset. */
|
/**
|
||||||
public int intParam() { return param(4); }
|
* Ensures space for an additional int value and returns the address offset.
|
||||||
|
*/
|
||||||
|
public int intParam() {
|
||||||
|
return param(4);
|
||||||
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional long value and returns the address offset. */
|
/**
|
||||||
public int longParam() { return param(8); }
|
* Ensures space for an additional long value and returns the address
|
||||||
|
* offset.
|
||||||
|
*/
|
||||||
|
public int longParam() {
|
||||||
|
return param(8);
|
||||||
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional float value and returns the address offset. */
|
/**
|
||||||
public int floatParam() { return param(4); }
|
* Ensures space for an additional float value and returns the address
|
||||||
|
* offset.
|
||||||
|
*/
|
||||||
|
public int floatParam() {
|
||||||
|
return param(4);
|
||||||
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional double value and returns the address offset. */
|
/**
|
||||||
public int doubleParam() { return param(8); }
|
* Ensures space for an additional double value and returns the address
|
||||||
|
* offset.
|
||||||
|
*/
|
||||||
|
public int doubleParam() {
|
||||||
|
return param(8);
|
||||||
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional pointer value and returns the address offset. */
|
/**
|
||||||
public int pointerParam() { return param(POINTER_SIZE); }
|
* Ensures space for an additional pointer value and returns the address
|
||||||
|
* offset.
|
||||||
|
*/
|
||||||
|
public int pointerParam() {
|
||||||
|
return param(POINTER_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional buffer with the specified size (in bytes) and returns the address offset. */
|
/**
|
||||||
public int bufferParam(int size) { return param(size, POINTER_SIZE); }
|
* Ensures space for an additional buffer with the specified size (in bytes)
|
||||||
|
* and returns the address offset.
|
||||||
|
*/
|
||||||
|
public int bufferParam(int size) {
|
||||||
|
return param(size, POINTER_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
/**
|
||||||
/** Ensures space for an additional boolean value, sets the specified value at the allocated offset and returns that offset. */
|
* Ensures space for an additional boolean value, sets the specified value
|
||||||
|
* at the allocated offset and returns that offset.
|
||||||
|
*/
|
||||||
public int booleanParam(boolean value) {
|
public int booleanParam(boolean value) {
|
||||||
int offset = booleanParam();
|
int offset = booleanParam();
|
||||||
buffer.put(offset, value ? (byte)1 : (byte)0);
|
buffer.put(offset, value ? (byte) 1 : (byte) 0);
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional byte value, sets the specified value at the allocated offset and returns that offset. */
|
/**
|
||||||
|
* Ensures space for an additional byte value, sets the specified value at
|
||||||
|
* the allocated offset and returns that offset.
|
||||||
|
*/
|
||||||
public int byteParam(byte value) {
|
public int byteParam(byte value) {
|
||||||
int offset = byteParam();
|
int offset = byteParam();
|
||||||
buffer.put(offset, value);
|
buffer.put(offset, value);
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional short value, sets the specified value at the allocated offset and returns that offset. */
|
/**
|
||||||
|
* Ensures space for an additional short value, sets the specified value at
|
||||||
|
* the allocated offset and returns that offset.
|
||||||
|
*/
|
||||||
public int shortParam(short value) {
|
public int shortParam(short value) {
|
||||||
int offset = shortParam();
|
int offset = shortParam();
|
||||||
buffer.putShort(offset, value);
|
buffer.putShort(offset, value);
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional int value, sets the specified value at the allocated offset and returns that offset. */
|
/**
|
||||||
|
* Ensures space for an additional int value, sets the specified value at
|
||||||
|
* the allocated offset and returns that offset.
|
||||||
|
*/
|
||||||
public int intParam(int value) {
|
public int intParam(int value) {
|
||||||
int offset = intParam();
|
int offset = intParam();
|
||||||
buffer.putInt(offset, value);
|
buffer.putInt(offset, value);
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional long value, sets the specified value at the allocated offset and returns that offset. */
|
/**
|
||||||
|
* Ensures space for an additional long value, sets the specified value at
|
||||||
|
* the allocated offset and returns that offset.
|
||||||
|
*/
|
||||||
public int longParam(long value) {
|
public int longParam(long value) {
|
||||||
int offset = longParam();
|
int offset = longParam();
|
||||||
buffer.putLong(offset, value);
|
buffer.putLong(offset, value);
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional float value, sets the specified value at the allocated offset and returns that offset. */
|
/**
|
||||||
|
* Ensures space for an additional float value, sets the specified value at
|
||||||
|
* the allocated offset and returns that offset.
|
||||||
|
*/
|
||||||
public int floatParam(float value) {
|
public int floatParam(float value) {
|
||||||
int offset = floatParam();
|
int offset = floatParam();
|
||||||
buffer.putFloat(offset, value);
|
buffer.putFloat(offset, value);
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional double value, sets the specified value at the allocated offset and returns that offset. */
|
/**
|
||||||
|
* Ensures space for an additional double value, sets the specified value at
|
||||||
|
* the allocated offset and returns that offset.
|
||||||
|
*/
|
||||||
public int doubleParam(double value) {
|
public int doubleParam(double value) {
|
||||||
int offset = doubleParam();
|
int offset = doubleParam();
|
||||||
buffer.putDouble(offset, value);
|
buffer.putDouble(offset, value);
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional pointer value, sets the specified value at the allocated offset and returns that offset. */
|
/**
|
||||||
|
* Ensures space for an additional pointer value, sets the specified value
|
||||||
|
* at the allocated offset and returns that offset.
|
||||||
|
*/
|
||||||
public int pointerParam(long value) {
|
public int pointerParam(long value) {
|
||||||
int offset = pointerParam();
|
int offset = pointerParam();
|
||||||
PointerBuffer.put(buffer, offset, value);
|
PointerBuffer.put(buffer, offset, value);
|
||||||
@ -207,50 +295,63 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
|
|
||||||
/** Ensures space for an additional pointer buffer, sets the specified memory addresses and returns the address offset. */
|
/**
|
||||||
|
* Ensures space for an additional pointer buffer, sets the specified memory
|
||||||
|
* addresses and returns the address offset.
|
||||||
|
*/
|
||||||
public int pointerArrayParam(long... pointers) {
|
public int pointerArrayParam(long... pointers) {
|
||||||
int buffersAddress = bufferParam(pointers.length << POINTER_SHIFT);
|
int buffersAddress = bufferParam(pointers.length << POINTER_SHIFT);
|
||||||
for ( int i = 0; i < pointers.length; i++ )
|
for (int i = 0; i < pointers.length; i++) {
|
||||||
pointerParam(buffersAddress, i, pointers[i]);
|
pointerParam(buffersAddress, i, pointers[i]);
|
||||||
|
|
||||||
return buffersAddress;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures space for an additional pointer buffer, sets the memory addresses of the specified buffers and returns the address offset. */
|
|
||||||
public int pointerArrayParam(ByteBuffer... buffers) {
|
|
||||||
int buffersAddress = bufferParam(buffers.length << POINTER_SHIFT);
|
|
||||||
for ( int i = 0; i < buffers.length; i++ )
|
|
||||||
pointerParam(buffersAddress, i, memAddress(buffers[i]));
|
|
||||||
|
|
||||||
return buffersAddress;
|
return buffersAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensures space for two additional pointer buffers, sets the memory addresses and remaining bytes of the specified buffers and returns the address
|
* Ensures space for an additional pointer buffer, sets the memory addresses
|
||||||
* offset.
|
* of the specified buffers and returns the address offset.
|
||||||
|
*/
|
||||||
|
public int pointerArrayParam(ByteBuffer... buffers) {
|
||||||
|
int buffersAddress = bufferParam(buffers.length << POINTER_SHIFT);
|
||||||
|
for (int i = 0; i < buffers.length; i++) {
|
||||||
|
pointerParam(buffersAddress, i, memAddress(buffers[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffersAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensures space for two additional pointer buffers, sets the memory
|
||||||
|
* addresses and remaining bytes of the specified buffers and returns the
|
||||||
|
* address offset.
|
||||||
*/
|
*/
|
||||||
public int pointerArrayParamp(ByteBuffer... buffers) {
|
public int pointerArrayParamp(ByteBuffer... buffers) {
|
||||||
int buffersAddress = pointerArrayParam(buffers);
|
int buffersAddress = pointerArrayParam(buffers);
|
||||||
|
|
||||||
int buffersLengths = bufferParam(buffers.length << POINTER_SHIFT);
|
int buffersLengths = bufferParam(buffers.length << POINTER_SHIFT);
|
||||||
for ( int i = 0; i < buffers.length; i++ )
|
for (int i = 0; i < buffers.length; i++) {
|
||||||
pointerParam(buffersLengths, i, buffers[i].remaining());
|
pointerParam(buffersLengths, i, buffers[i].remaining());
|
||||||
|
}
|
||||||
|
|
||||||
return buffersAddress;
|
return buffersAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ASCII encodes the specified strings with a null-terminator and ensures space for a buffer filled with the memory addresses of the encoded strings.
|
* ASCII encodes the specified strings with a null-terminator and ensures
|
||||||
|
* space for a buffer filled with the memory addresses of the encoded
|
||||||
|
* strings.
|
||||||
*
|
*
|
||||||
* <p>The encoded buffers must be later freed with {@link #pointerArrayFree(int, int)}.</p>
|
* <p>
|
||||||
|
* The encoded buffers must be later freed with
|
||||||
|
* {@link #pointerArrayFree(int, int)}.</p>
|
||||||
*
|
*
|
||||||
* @return the offset to the memory address buffer
|
* @return the offset to the memory address buffer
|
||||||
*/
|
*/
|
||||||
public int pointerArrayParamASCII(CharSequence... strings) {
|
public int pointerArrayParamASCII(CharSequence... strings) {
|
||||||
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
||||||
for ( int i = 0; i < strings.length; i++ ) {
|
for (int i = 0; i < strings.length; i++) {
|
||||||
ByteBuffer buffer = MemoryUtil.memASCII(strings[i]);
|
ByteBuffer buffer = MemoryUtil.memASCII(strings[i]);
|
||||||
|
|
||||||
pointerParam(buffersAddress, i, memAddress(buffer));
|
pointerParam(buffersAddress, i, memAddress(buffer));
|
||||||
@ -260,10 +361,14 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ASCII encodes the specified strings and ensures space for two additional buffers filled with the lengths and memory addresses of the encoded strings,
|
* ASCII encodes the specified strings and ensures space for two additional
|
||||||
* respectively. The lengths are 4-bytes integers and the memory address buffer starts immediately after the lengths buffer.
|
* buffers filled with the lengths and memory addresses of the encoded
|
||||||
|
* strings, respectively. The lengths are 4-bytes integers and the memory
|
||||||
|
* address buffer starts immediately after the lengths buffer.
|
||||||
*
|
*
|
||||||
* <p>The encoded buffers must be later freed with {@link #pointerArrayFree(int, int)}.</p>
|
* <p>
|
||||||
|
* The encoded buffers must be later freed with
|
||||||
|
* {@link #pointerArrayFree(int, int)}.</p>
|
||||||
*
|
*
|
||||||
* @return the offset to the lengths buffer
|
* @return the offset to the lengths buffer
|
||||||
*/
|
*/
|
||||||
@ -271,7 +376,7 @@ public class APIBuffer {
|
|||||||
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
||||||
int lengthsAddress = bufferParam(strings.length << 2);
|
int lengthsAddress = bufferParam(strings.length << 2);
|
||||||
|
|
||||||
for ( int i = 0; i < strings.length; i++ ) {
|
for (int i = 0; i < strings.length; i++) {
|
||||||
ByteBuffer buffer = MemoryUtil.memASCII(strings[i]);
|
ByteBuffer buffer = MemoryUtil.memASCII(strings[i]);
|
||||||
|
|
||||||
pointerParam(buffersAddress, i, memAddress(buffer));
|
pointerParam(buffersAddress, i, memAddress(buffer));
|
||||||
@ -282,10 +387,14 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ASCII encodes the specified strings and ensures space for two additional buffers filled with the lengths and memory addresses of the encoded strings,
|
* ASCII encodes the specified strings and ensures space for two additional
|
||||||
* respectively. The lengths are pointer-sized integers and the memory address buffer starts immediately after the lengths buffer.
|
* buffers filled with the lengths and memory addresses of the encoded
|
||||||
|
* strings, respectively. The lengths are pointer-sized integers and the
|
||||||
|
* memory address buffer starts immediately after the lengths buffer.
|
||||||
*
|
*
|
||||||
* <p>The encoded buffers must be later freed with {@link #pointerArrayFree(int, int)}.</p>
|
* <p>
|
||||||
|
* The encoded buffers must be later freed with
|
||||||
|
* {@link #pointerArrayFree(int, int)}.</p>
|
||||||
*
|
*
|
||||||
* @return the offset to the lengths buffer
|
* @return the offset to the lengths buffer
|
||||||
*/
|
*/
|
||||||
@ -293,7 +402,7 @@ public class APIBuffer {
|
|||||||
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
||||||
int lengthsAddress = bufferParam(strings.length << POINTER_SHIFT);
|
int lengthsAddress = bufferParam(strings.length << POINTER_SHIFT);
|
||||||
|
|
||||||
for ( int i = 0; i < strings.length; i++ ) {
|
for (int i = 0; i < strings.length; i++) {
|
||||||
ByteBuffer buffer = MemoryUtil.memASCII(strings[i]);
|
ByteBuffer buffer = MemoryUtil.memASCII(strings[i]);
|
||||||
|
|
||||||
pointerParam(buffersAddress, i, memAddress(buffer));
|
pointerParam(buffersAddress, i, memAddress(buffer));
|
||||||
@ -304,15 +413,19 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UTF8 encodes the specified strings with a null-terminator and ensures space for a buffer filled with the memory addresses of the encoded strings.
|
* UTF8 encodes the specified strings with a null-terminator and ensures
|
||||||
|
* space for a buffer filled with the memory addresses of the encoded
|
||||||
|
* strings.
|
||||||
*
|
*
|
||||||
* <p>The encoded buffers must be later freed with {@link #pointerArrayFree(int, int)}.</p>
|
* <p>
|
||||||
|
* The encoded buffers must be later freed with
|
||||||
|
* {@link #pointerArrayFree(int, int)}.</p>
|
||||||
*
|
*
|
||||||
* @return the offset to the memory address buffer
|
* @return the offset to the memory address buffer
|
||||||
*/
|
*/
|
||||||
public int pointerArrayParamUTF8(CharSequence... strings) {
|
public int pointerArrayParamUTF8(CharSequence... strings) {
|
||||||
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
||||||
for ( int i = 0; i < strings.length; i++ ) {
|
for (int i = 0; i < strings.length; i++) {
|
||||||
ByteBuffer buffer = MemoryUtil.memUTF8(strings[i]);
|
ByteBuffer buffer = MemoryUtil.memUTF8(strings[i]);
|
||||||
|
|
||||||
pointerParam(buffersAddress, i, memAddress(buffer));
|
pointerParam(buffersAddress, i, memAddress(buffer));
|
||||||
@ -322,10 +435,14 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UTF8 encodes the specified strings and ensures space for two additional buffers filled with the lengths and memory addresses of the encoded strings,
|
* UTF8 encodes the specified strings and ensures space for two additional
|
||||||
* respectively. The lengths are 4-bytes integers and the memory address buffer starts immediately after the lengths buffer.
|
* buffers filled with the lengths and memory addresses of the encoded
|
||||||
|
* strings, respectively. The lengths are 4-bytes integers and the memory
|
||||||
|
* address buffer starts immediately after the lengths buffer.
|
||||||
*
|
*
|
||||||
* <p>The encoded buffers must be later freed with {@link #pointerArrayFree(int, int)}.</p>
|
* <p>
|
||||||
|
* The encoded buffers must be later freed with
|
||||||
|
* {@link #pointerArrayFree(int, int)}.</p>
|
||||||
*
|
*
|
||||||
* @return the offset to the lengths buffer
|
* @return the offset to the lengths buffer
|
||||||
*/
|
*/
|
||||||
@ -333,7 +450,7 @@ public class APIBuffer {
|
|||||||
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
||||||
int lengthsAddress = bufferParam(strings.length << 2);
|
int lengthsAddress = bufferParam(strings.length << 2);
|
||||||
|
|
||||||
for ( int i = 0; i < strings.length; i++ ) {
|
for (int i = 0; i < strings.length; i++) {
|
||||||
ByteBuffer buffer = MemoryUtil.memUTF8(strings[i]);
|
ByteBuffer buffer = MemoryUtil.memUTF8(strings[i]);
|
||||||
|
|
||||||
pointerParam(buffersAddress, i, memAddress(buffer));
|
pointerParam(buffersAddress, i, memAddress(buffer));
|
||||||
@ -344,10 +461,14 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UTF8 encodes the specified strings and ensures space for two additional buffers filled with the lengths and memory addresses of the encoded strings,
|
* UTF8 encodes the specified strings and ensures space for two additional
|
||||||
* respectively. The lengths are pointer-sized integers and the memory address buffer starts immediately after the lengths buffer.
|
* buffers filled with the lengths and memory addresses of the encoded
|
||||||
|
* strings, respectively. The lengths are pointer-sized integers and the
|
||||||
|
* memory address buffer starts immediately after the lengths buffer.
|
||||||
*
|
*
|
||||||
* <p>The encoded buffers must be later freed with {@link #pointerArrayFree(int, int)}.</p>
|
* <p>
|
||||||
|
* The encoded buffers must be later freed with
|
||||||
|
* {@link #pointerArrayFree(int, int)}.</p>
|
||||||
*
|
*
|
||||||
* @return the offset to the lengths buffer
|
* @return the offset to the lengths buffer
|
||||||
*/
|
*/
|
||||||
@ -355,7 +476,7 @@ public class APIBuffer {
|
|||||||
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
||||||
int lengthsAddress = bufferParam(strings.length << POINTER_SHIFT);
|
int lengthsAddress = bufferParam(strings.length << POINTER_SHIFT);
|
||||||
|
|
||||||
for ( int i = 0; i < strings.length; i++ ) {
|
for (int i = 0; i < strings.length; i++) {
|
||||||
ByteBuffer buffer = MemoryUtil.memUTF8(strings[i]);
|
ByteBuffer buffer = MemoryUtil.memUTF8(strings[i]);
|
||||||
|
|
||||||
pointerParam(buffersAddress, i, memAddress(buffer));
|
pointerParam(buffersAddress, i, memAddress(buffer));
|
||||||
@ -366,15 +487,19 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UTF16 encodes the specified strings with a null-terminator and ensures space for a buffer filled with the memory addresses of the encoded strings.
|
* UTF16 encodes the specified strings with a null-terminator and ensures
|
||||||
|
* space for a buffer filled with the memory addresses of the encoded
|
||||||
|
* strings.
|
||||||
*
|
*
|
||||||
* <p>The encoded buffers must be later freed with {@link #pointerArrayFree(int, int)}.</p>
|
* <p>
|
||||||
|
* The encoded buffers must be later freed with
|
||||||
|
* {@link #pointerArrayFree(int, int)}.</p>
|
||||||
*
|
*
|
||||||
* @return the offset to the memory address buffer
|
* @return the offset to the memory address buffer
|
||||||
*/
|
*/
|
||||||
public int pointerArrayParamUTF16(CharSequence... strings) {
|
public int pointerArrayParamUTF16(CharSequence... strings) {
|
||||||
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
||||||
for ( int i = 0; i < strings.length; i++ ) {
|
for (int i = 0; i < strings.length; i++) {
|
||||||
ByteBuffer buffer = MemoryUtil.memUTF16(strings[i]);
|
ByteBuffer buffer = MemoryUtil.memUTF16(strings[i]);
|
||||||
|
|
||||||
pointerParam(buffersAddress, i, memAddress(buffer));
|
pointerParam(buffersAddress, i, memAddress(buffer));
|
||||||
@ -384,10 +509,14 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UTF16 encodes the specified strings and ensures space for two additional buffers filled with the lengths and memory addresses of the encoded strings,
|
* UTF16 encodes the specified strings and ensures space for two additional
|
||||||
* respectively. The lengths are 4-bytes integers and the memory address buffer starts immediately after the lengths buffer.
|
* buffers filled with the lengths and memory addresses of the encoded
|
||||||
|
* strings, respectively. The lengths are 4-bytes integers and the memory
|
||||||
|
* address buffer starts immediately after the lengths buffer.
|
||||||
*
|
*
|
||||||
* <p>The encoded buffers must be later freed with {@link #pointerArrayFree(int, int)}.</p>
|
* <p>
|
||||||
|
* The encoded buffers must be later freed with
|
||||||
|
* {@link #pointerArrayFree(int, int)}.</p>
|
||||||
*
|
*
|
||||||
* @return the offset to the lengths buffer
|
* @return the offset to the lengths buffer
|
||||||
*/
|
*/
|
||||||
@ -395,7 +524,7 @@ public class APIBuffer {
|
|||||||
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
||||||
int lengthsAddress = bufferParam(strings.length << 2);
|
int lengthsAddress = bufferParam(strings.length << 2);
|
||||||
|
|
||||||
for ( int i = 0; i < strings.length; i++ ) {
|
for (int i = 0; i < strings.length; i++) {
|
||||||
ByteBuffer buffer = MemoryUtil.memUTF16(strings[i]);
|
ByteBuffer buffer = MemoryUtil.memUTF16(strings[i]);
|
||||||
|
|
||||||
pointerParam(buffersAddress, i, memAddress(buffer));
|
pointerParam(buffersAddress, i, memAddress(buffer));
|
||||||
@ -406,10 +535,14 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UTF16 encodes the specified strings and ensures space for two additional buffers filled with the lengths and memory addresses of the encoded strings,
|
* UTF16 encodes the specified strings and ensures space for two additional
|
||||||
* respectively. The lengths are pointer-sized integers and the memory address buffer starts immediately after the lengths buffer.
|
* buffers filled with the lengths and memory addresses of the encoded
|
||||||
|
* strings, respectively. The lengths are pointer-sized integers and the
|
||||||
|
* memory address buffer starts immediately after the lengths buffer.
|
||||||
*
|
*
|
||||||
* <p>The encoded buffers must be later freed with {@link #pointerArrayFree(int, int)}.</p>
|
* <p>
|
||||||
|
* The encoded buffers must be later freed with
|
||||||
|
* {@link #pointerArrayFree(int, int)}.</p>
|
||||||
*
|
*
|
||||||
* @return the offset to the lengths buffer
|
* @return the offset to the lengths buffer
|
||||||
*/
|
*/
|
||||||
@ -417,7 +550,7 @@ public class APIBuffer {
|
|||||||
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
int buffersAddress = bufferParam(strings.length << POINTER_SHIFT);
|
||||||
int lengthsAddress = bufferParam(strings.length << POINTER_SHIFT);
|
int lengthsAddress = bufferParam(strings.length << POINTER_SHIFT);
|
||||||
|
|
||||||
for ( int i = 0; i < strings.length; i++ ) {
|
for (int i = 0; i < strings.length; i++) {
|
||||||
ByteBuffer buffer = MemoryUtil.memUTF16(strings[i]);
|
ByteBuffer buffer = MemoryUtil.memUTF16(strings[i]);
|
||||||
|
|
||||||
pointerParam(buffersAddress, i, memAddress(buffer));
|
pointerParam(buffersAddress, i, memAddress(buffer));
|
||||||
@ -428,41 +561,56 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
/**
|
||||||
/** Frees {@code length} memory blocks stored in the APIBuffer, starting at the specified {@code offset}. */
|
* Frees {@code length} memory blocks stored in the APIBuffer, starting at
|
||||||
|
* the specified {@code offset}.
|
||||||
|
*/
|
||||||
public void pointerArrayFree(int offset, int length) {
|
public void pointerArrayFree(int offset, int length) {
|
||||||
for ( int i = 0; i < length; i++ )
|
for (int i = 0; i < length; i++) {
|
||||||
nmemFree(pointerValue(offset + (i << POINTER_SHIFT)));
|
nmemFree(pointerValue(offset + (i << POINTER_SHIFT)));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
/**
|
||||||
/** Sets an int value at the specified index of the int buffer that starts at the specified offset. */
|
* Sets an int value at the specified index of the int buffer that starts at
|
||||||
|
* the specified offset.
|
||||||
|
*/
|
||||||
public void intParam(int offset, int index, int value) {
|
public void intParam(int offset, int index, int value) {
|
||||||
buffer.putInt(offset + (index << 2), value);
|
buffer.putInt(offset + (index << 2), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets a pointer value at the specified index of the pointer buffer that starts at the specified offset. */
|
/**
|
||||||
|
* Sets a pointer value at the specified index of the pointer buffer that
|
||||||
|
* starts at the specified offset.
|
||||||
|
*/
|
||||||
public void pointerParam(int offset, int index, long value) {
|
public void pointerParam(int offset, int index, long value) {
|
||||||
PointerBuffer.put(buffer, offset + (index << POINTER_SHIFT), value);
|
PointerBuffer.put(buffer, offset + (index << POINTER_SHIFT), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
/**
|
||||||
/** Ensures space for the specified string encoded in ASCII, encodes the string at the allocated offset and returns that offset. */
|
* Ensures space for the specified string encoded in ASCII, encodes the
|
||||||
|
* string at the allocated offset and returns that offset.
|
||||||
|
*/
|
||||||
public int stringParamASCII(CharSequence value, boolean nullTerminated) {
|
public int stringParamASCII(CharSequence value, boolean nullTerminated) {
|
||||||
if ( value == null )
|
if (value == null) {
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int offset = bufferParam(value.length() + (nullTerminated ? 1 : 0));
|
int offset = bufferParam(value.length() + (nullTerminated ? 1 : 0));
|
||||||
MemoryUtil.memASCII(value, nullTerminated, buffer, offset);
|
MemoryUtil.memASCII(value, nullTerminated, buffer, offset);
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures space for the specified string encoded in UTF-8, encodes the string at the allocated offset and returns that offset. */
|
/**
|
||||||
|
* Ensures space for the specified string encoded in UTF-8, encodes the
|
||||||
|
* string at the allocated offset and returns that offset.
|
||||||
|
*/
|
||||||
public int stringParamUTF8(CharSequence value, boolean nullTerminated) {
|
public int stringParamUTF8(CharSequence value, boolean nullTerminated) {
|
||||||
if ( value == null )
|
if (value == null) {
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int encodedLen = MemoryUtil.memLengthUTF8(value, nullTerminated);
|
int encodedLen = MemoryUtil.memLengthUTF8(value, nullTerminated);
|
||||||
int offset = bufferParam(encodedLen);
|
int offset = bufferParam(encodedLen);
|
||||||
@ -470,10 +618,14 @@ public class APIBuffer {
|
|||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures space for the specified string encoded in UTF-16, encodes the string at the allocated offset and returns that offset. */
|
/**
|
||||||
|
* Ensures space for the specified string encoded in UTF-16, encodes the
|
||||||
|
* string at the allocated offset and returns that offset.
|
||||||
|
*/
|
||||||
public int stringParamUTF16(CharSequence value, boolean nullTerminated) {
|
public int stringParamUTF16(CharSequence value, boolean nullTerminated) {
|
||||||
if ( value == null )
|
if (value == null) {
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int offset = bufferParam((value.length() + (nullTerminated ? 1 : 0)) << 1);
|
int offset = bufferParam((value.length() + (nullTerminated ? 1 : 0)) << 1);
|
||||||
MemoryUtil.memUTF16(value, nullTerminated, buffer, offset);
|
MemoryUtil.memUTF16(value, nullTerminated, buffer, offset);
|
||||||
@ -481,32 +633,65 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* Returns the boolean value at the specified offset.
|
||||||
|
*/
|
||||||
|
public boolean booleanValue(int offset) {
|
||||||
|
return buffer.get(offset) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns the boolean value at the specified offset. */
|
/**
|
||||||
public boolean booleanValue(int offset) { return buffer.get(offset) != 0; }
|
* Returns the boolean value at the specified offset.
|
||||||
|
*/
|
||||||
|
public byte byteValue(int offset) {
|
||||||
|
return buffer.get(offset);
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns the boolean value at the specified offset. */
|
/**
|
||||||
public byte byteValue(int offset) { return buffer.get(offset); }
|
* Returns the short value at the specified offset.
|
||||||
|
*/
|
||||||
|
public short shortValue(int offset) {
|
||||||
|
return buffer.getShort(offset);
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns the short value at the specified offset. */
|
/**
|
||||||
public short shortValue(int offset) { return buffer.getShort(offset); }
|
* Returns the int value at the specified offset.
|
||||||
|
*/
|
||||||
|
public int intValue(int offset) {
|
||||||
|
return buffer.getInt(offset);
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns the int value at the specified offset. */
|
/**
|
||||||
public int intValue(int offset) { return buffer.getInt(offset); }
|
* Returns the long value at the specified offset.
|
||||||
|
*/
|
||||||
|
public long longValue(int offset) {
|
||||||
|
return buffer.getLong(offset);
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns the long value at the specified offset. */
|
/**
|
||||||
public long longValue(int offset) { return buffer.getLong(offset); }
|
* Returns the float value at the specified offset.
|
||||||
|
*/
|
||||||
|
public float floatValue(int offset) {
|
||||||
|
return buffer.getFloat(offset);
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns the float value at the specified offset. */
|
/**
|
||||||
public float floatValue(int offset) { return buffer.getFloat(offset); }
|
* Returns the double value at the specified offset.
|
||||||
|
*/
|
||||||
|
public double doubleValue(int offset) {
|
||||||
|
return buffer.getDouble(offset);
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns the double value at the specified offset. */
|
/**
|
||||||
public double doubleValue(int offset) { return buffer.getDouble(offset); }
|
* Returns the pointer value at the specified offset.
|
||||||
|
*/
|
||||||
|
public long pointerValue(int offset) {
|
||||||
|
return PointerBuffer.get(buffer, offset);
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns the pointer value at the specified offset. */
|
/**
|
||||||
public long pointerValue(int offset) { return PointerBuffer.get(buffer, offset); }
|
* Returns the ASCII string value at the specified byte range.
|
||||||
|
*/
|
||||||
/** Returns the ASCII string value at the specified byte range. */
|
|
||||||
public String stringValueASCII(int offset, int limit) {
|
public String stringValueASCII(int offset, int limit) {
|
||||||
buffer.position(offset);
|
buffer.position(offset);
|
||||||
buffer.limit(limit);
|
buffer.limit(limit);
|
||||||
@ -517,7 +702,9 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the UTF8 string value at the specified byte range. */
|
/**
|
||||||
|
* Returns the UTF8 string value at the specified byte range.
|
||||||
|
*/
|
||||||
public String stringValueUTF8(int offset, int limit) {
|
public String stringValueUTF8(int offset, int limit) {
|
||||||
buffer.position(offset);
|
buffer.position(offset);
|
||||||
buffer.limit(limit);
|
buffer.limit(limit);
|
||||||
@ -528,7 +715,9 @@ public class APIBuffer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the UTF16 string value at the specified byte range. */
|
/**
|
||||||
|
* Returns the UTF16 string value at the specified byte range.
|
||||||
|
*/
|
||||||
public String stringValueUTF16(int offset, int limit) {
|
public String stringValueUTF16(int offset, int limit) {
|
||||||
buffer.position(offset);
|
buffer.position(offset);
|
||||||
buffer.limit(limit);
|
buffer.limit(limit);
|
||||||
|
@ -4,12 +4,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.jme3.lwjgl3.utils;
|
package com.jme3.lwjgl3.utils;
|
||||||
|
|
||||||
import com.jme3.lwjgl3.utils.APIBuffer;
|
|
||||||
import org.lwjgl.system.linux.LinuxLibrary;
|
|
||||||
import org.lwjgl.system.macosx.MacOSXLibrary;
|
|
||||||
import org.lwjgl.system.windows.WindowsLibrary;
|
|
||||||
|
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -20,7 +14,9 @@ import java.util.regex.Pattern;
|
|||||||
/**
|
/**
|
||||||
* Utility class useful to API bindings. [INTERNAL USE ONLY]
|
* Utility class useful to API bindings. [INTERNAL USE ONLY]
|
||||||
*
|
*
|
||||||
* <p>Method names in this class are prefixed with {@code api} to avoid ambiguities when used with static imports.</p>
|
* <p>
|
||||||
|
* Method names in this class are prefixed with {@code api} to avoid ambiguities
|
||||||
|
* when used with static imports.</p>
|
||||||
*/
|
*/
|
||||||
public final class APIUtil {
|
public final class APIUtil {
|
||||||
|
|
||||||
@ -34,15 +30,18 @@ public final class APIUtil {
|
|||||||
private APIUtil() {
|
private APIUtil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
/** Returns a thread-local {@link APIBuffer} that has been reset. */
|
* Returns a thread-local {@link APIBuffer} that has been reset.
|
||||||
|
*/
|
||||||
public static APIBuffer apiBuffer() {
|
public static APIBuffer apiBuffer() {
|
||||||
return API_BUFFERS.get().reset();
|
return API_BUFFERS.get().reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a thread-local {@link APIBuffer}, without resetting it. This makes the APIBuffer work like a stack when used in nested API calls. The user is
|
* Returns a thread-local {@link APIBuffer}, without resetting it. This
|
||||||
* responsible for resetting the {@link APIBuffer} to an appropriate state before the nested call returns.
|
* makes the APIBuffer work like a stack when used in nested API calls. The
|
||||||
|
* user is responsible for resetting the {@link APIBuffer} to an appropriate
|
||||||
|
* state before the nested call returns.
|
||||||
*
|
*
|
||||||
* @see APIBuffer#pop
|
* @see APIBuffer#pop
|
||||||
*/
|
*/
|
||||||
@ -50,17 +49,28 @@ public final class APIUtil {
|
|||||||
return API_BUFFERS.get().push();
|
return API_BUFFERS.get().push();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A data class for API versioning information. */
|
/**
|
||||||
|
* A data class for API versioning information.
|
||||||
|
*/
|
||||||
public static class APIVersion {
|
public static class APIVersion {
|
||||||
|
|
||||||
/** Returns the API major version. */
|
/**
|
||||||
|
* Returns the API major version.
|
||||||
|
*/
|
||||||
public final int major;
|
public final int major;
|
||||||
/** Returns the API minor version. */
|
/**
|
||||||
|
* Returns the API minor version.
|
||||||
|
*/
|
||||||
public final int minor;
|
public final int minor;
|
||||||
|
|
||||||
/** Returns the API revision. May be null. */
|
/**
|
||||||
|
* Returns the API revision. May be null.
|
||||||
|
*/
|
||||||
public final String revision;
|
public final String revision;
|
||||||
/** Returns the API implementation-specific versioning information. May be null. */
|
/**
|
||||||
|
* Returns the API implementation-specific versioning information. May
|
||||||
|
* be null.
|
||||||
|
*/
|
||||||
public final String implementation;
|
public final String implementation;
|
||||||
|
|
||||||
public APIVersion(int major, int minor) {
|
public APIVersion(int major, int minor) {
|
||||||
@ -77,9 +87,11 @@ public final class APIUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses a version string. The version string must have the format {@code MAJOR.MINOR.REVISION IMPL}, where {@code MAJOR} is the major version (integer),
|
* Parses a version string. The version string must have the format
|
||||||
* {@code MINOR} is the minor version (integer), {@code REVISION} is the revision version (string, optional) and {@code IMPL} is implementation-specific
|
* {@code MAJOR.MINOR.REVISION IMPL}, where {@code MAJOR} is the major
|
||||||
* information (string, optional).
|
* version (integer), {@code MINOR} is the minor version (integer),
|
||||||
|
* {@code REVISION} is the revision version (string, optional) and
|
||||||
|
* {@code IMPL} is implementation-specific information (string, optional).
|
||||||
*
|
*
|
||||||
* @param version the API version string
|
* @param version the API version string
|
||||||
*
|
*
|
||||||
@ -90,9 +102,12 @@ public final class APIUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses a version string. The version string must have the format {@code PREFIX MAJOR.MINOR.REVISION IMPL}, where {@code PREFIX} is the specified prefix
|
* Parses a version string. The version string must have the format
|
||||||
* (string, optional), {@code MAJOR} is the major version (integer), {@code MINOR} is the minor version (integer), {@code REVISION} is the revision version
|
* {@code PREFIX MAJOR.MINOR.REVISION IMPL}, where {@code PREFIX} is the
|
||||||
* (string, optional) and {@code IMPL} is implementation-specific information (string, optional).
|
* specified prefix (string, optional), {@code MAJOR} is the major version
|
||||||
|
* (integer), {@code MINOR} is the minor version (integer), {@code REVISION}
|
||||||
|
* is the revision version (string, optional) and {@code IMPL} is
|
||||||
|
* implementation-specific information (string, optional).
|
||||||
*
|
*
|
||||||
* @param version the version string
|
* @param version the version string
|
||||||
* @param prefix the version string prefix, may be null
|
* @param prefix the version string prefix, may be null
|
||||||
@ -101,12 +116,14 @@ public final class APIUtil {
|
|||||||
*/
|
*/
|
||||||
public static APIVersion apiParseVersion(String version, String prefix) {
|
public static APIVersion apiParseVersion(String version, String prefix) {
|
||||||
String pattern = "([0-9]+)[.]([0-9]+)([.]\\S+)?\\s*(.+)?";
|
String pattern = "([0-9]+)[.]([0-9]+)([.]\\S+)?\\s*(.+)?";
|
||||||
if ( prefix != null )
|
if (prefix != null) {
|
||||||
pattern = prefix + "\\s+" + pattern;
|
pattern = prefix + "\\s+" + pattern;
|
||||||
|
}
|
||||||
|
|
||||||
Matcher matcher = Pattern.compile(pattern).matcher(version);
|
Matcher matcher = Pattern.compile(pattern).matcher(version);
|
||||||
if ( !matcher.matches() )
|
if (!matcher.matches()) {
|
||||||
throw new IllegalArgumentException(String.format("Malformed API version string [%s]", version));
|
throw new IllegalArgumentException(String.format("Malformed API version string [%s]", version));
|
||||||
|
}
|
||||||
|
|
||||||
return new APIVersion(
|
return new APIVersion(
|
||||||
Integer.parseInt(matcher.group(1)),
|
Integer.parseInt(matcher.group(1)),
|
||||||
@ -125,10 +142,14 @@ public final class APIUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a map of public static final integer fields in the specified classes, to their String representations. An optional filter can be specified to
|
* Returns a map of public static final integer fields in the specified
|
||||||
* only include specific fields. The target map may be null, in which case a new map is allocated and returned.
|
* classes, to their String representations. An optional filter can be
|
||||||
|
* specified to only include specific fields. The target map may be null, in
|
||||||
|
* which case a new map is allocated and returned.
|
||||||
*
|
*
|
||||||
* <p>This method is useful when debugging to quickly identify values returned from an API.</p>
|
* <p>
|
||||||
|
* This method is useful when debugging to quickly identify values returned
|
||||||
|
* from an API.</p>
|
||||||
*
|
*
|
||||||
* @param filter the filter to use (optional)
|
* @param filter the filter to use (optional)
|
||||||
* @param target the target map (optional)
|
* @param target the target map (optional)
|
||||||
@ -137,22 +158,25 @@ public final class APIUtil {
|
|||||||
* @return the token map
|
* @return the token map
|
||||||
*/
|
*/
|
||||||
public static Map<Integer, String> apiClassTokens(TokenFilter filter, Map<Integer, String> target, Class<?>... tokenClasses) {
|
public static Map<Integer, String> apiClassTokens(TokenFilter filter, Map<Integer, String> target, Class<?>... tokenClasses) {
|
||||||
if ( target == null )
|
if (target == null) {
|
||||||
target = new HashMap<Integer, String>(64);
|
target = new HashMap<Integer, String>(64);
|
||||||
|
}
|
||||||
|
|
||||||
int TOKEN_MODIFIERS = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
|
int TOKEN_MODIFIERS = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
|
||||||
|
|
||||||
for ( Class<?> tokenClass : tokenClasses ) {
|
for (Class<?> tokenClass : tokenClasses) {
|
||||||
if ( tokenClass == null )
|
if (tokenClass == null) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
for ( Field field : tokenClass.getDeclaredFields() ) {
|
for (Field field : tokenClass.getDeclaredFields()) {
|
||||||
// Get only <public static final int> fields.
|
// Get only <public static final int> fields.
|
||||||
if ( (field.getModifiers() & TOKEN_MODIFIERS) == TOKEN_MODIFIERS && field.getType() == int.class ) {
|
if ((field.getModifiers() & TOKEN_MODIFIERS) == TOKEN_MODIFIERS && field.getType() == int.class) {
|
||||||
try {
|
try {
|
||||||
int value = field.getInt(null);
|
int value = field.getInt(null);
|
||||||
if ( filter != null && !filter.accept(field, value) )
|
if (filter != null && !filter.accept(field, value)) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
String name = target.get(value);
|
String name = target.get(value);
|
||||||
target.put(value, name == null ? field.getName() : name + "|" + field.getName());
|
target.put(value, name == null ? field.getName() : name + "|" + field.getName());
|
||||||
@ -174,7 +198,9 @@ public final class APIUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Simple interface for Field filtering. */
|
/**
|
||||||
|
* Simple interface for Field filtering.
|
||||||
|
*/
|
||||||
public interface TokenFilter {
|
public interface TokenFilter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user