I threw my 2 cents worth into the discussion with this response. Sorry for not cross-posting, but I’m feeling lazy. ;D
Edit: Ah, hell. I guess I’m not that lazy. Here it is:
You’re looking for something like this?
//File: Test.struct
//Purpose: Demonstrates the Struct class type
package com.mypackage;
public struct TestStruct output Bean order Big
{
byte b;
short s;
int i;
long l;
float f;
double d;
double[3] vertex;
int[i] texture;
}
Which then outputs a java file like this:
package com.mypackage;
import java.nio.*;
public class TestStruct
{
private ByteBuffer buffer;
private boolean hasArray;
private int[] positions = new int[8];
public TestStruct(ByteBuffer buffer)
{
ByteBuffer temp;
int position = 0;
this.buffer = buffer;
this.hasArray = buffer.hasArray();
this.positions[0] = position;
buffer.position(buffer.position()+1);
position += 1;
this.positions[1] = position;
buffer.position(buffer.position()+2);
position += 2;
this.positions[2] = position;
buffer.position(buffer.position()+4);
position += 4;
this.positions[3] = position;
buffer.position(buffer.position()+8);
position += 8;
this.positions[4] = position;
buffer.position(buffer.position()+4);
position += 4;
this.positions[5] = position;
buffer.position(buffer.position()+8);
position += 8;
this.positions[6] = position;
temp = buffer.slice();
temp.limit(3);
this.vertex = temp.asDoubleBuffer().array();
position += 3 * 8;
this.positions[7] = position;
temp = buffer.slice();
temp.limit(getI() * 4);
this.texture = temp.asIntBuffer().array();
position += (int)getI() * 4;
}
public byte getB()
{
return buffer.getByte(positions[0]);
}
public void setB(byte b)
{
return buffer.setByte(positions[0], b);
}
public short getS()
{
return buffer.getShort(positions[1]);
}
public void setS(short s)
{
buffer.setShort(positions[1], s);
}
//More getters and setters here
}
Here’s the “Struct” format:
package [packagename];
public struct [ClassName] [output Bean|Struct] [order Little|Big]
{
[primitive] [name];
}
Bean vs. Struct was an alternate method of accessing the data I was working on. Bean is the default. Order controls the little or big endianess of the ByteBuffer. To convert the struct from C to Java, you can simply wrap the structure in a ByteBuffer and pass it to the constructor of your Java “Struct”. The current code requires that the buffer positions be predictable, although I was spending some time working out how they could be make dynamic.
I was working on this in response to Caspian Prince’s desire for a Struct feature. The way I see it, there’s no need to change the Java language when a preprocessor can convert the structures just fine. I stopped working on the project when I realized that J.A.D.E. already had a similar structure concept.
If anyone wants the code I’ve done, I could probably send it to you. Just shoot me an email.