Skip to content
Snippets Groups Projects
Commit 3d74e0ce authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

reimplemented hierarchical typedefs in Java

parent 6c91178e
No related branches found
No related tags found
No related merge requests found
...@@ -454,10 +454,10 @@ aspect Java_Class { ...@@ -454,10 +454,10 @@ aspect Java_Class {
env.println(); env.println();
} }
//public void TypeDecl.Java_emitSignature(Java_env env) { public void TypeDecl.Java_emitSignature(Java_env env) {
// Signature signature = getSignature(); Signature signature = getSignature();
// signature.Java_emitSignature(env, true); signature.Java_emitSignature(env, true);
//} }
public void Decl.Java_emitSignature(Java_env env) { public void Decl.Java_emitSignature(Java_env env) {
//always emit the flat signature, as it is needed //always emit the flat signature, as it is needed
...@@ -465,10 +465,10 @@ aspect Java_Class { ...@@ -465,10 +465,10 @@ aspect Java_Class {
//the type_ids of dependent types. Therefore, flat sigs //the type_ids of dependent types. Therefore, flat sigs
//are used for matching //are used for matching
Java_emitFlatSignature(env); Java_emitFlatSignature(env);
//if(isReferenced() || isSampleDecl()){ if(isReferenced() || isSampleDecl()){
// Signature signature = getSignature(); Signature signature = getSignature();
// signature.Java_emitSignature(env, !isSampleDecl()); signature.Java_emitSignature(env, !isSampleDecl());
//} }
} }
public void Decl.Java_emitFlatSignature(Java_env env){ public void Decl.Java_emitFlatSignature(Java_env env){
...@@ -497,7 +497,8 @@ aspect Java_Class { ...@@ -497,7 +497,8 @@ aspect Java_Class {
//XXX TODO: refactor: split into a static class ("TypeDefSingleton"?)and a (smaller) dispatcher //XXX TODO: refactor: split into a static class ("TypeDefSingleton"?)and a (smaller) dispatcher
public void Decl.Java_emitDispatcher(Java_env env, boolean isSample) { public void Decl.Java_emitDispatcher(Java_env env, boolean isSample) {
String genericStr = ""; //env.versionHasMetaData()?"<"+getName()+">":""; // String genericStr = ""; //env.versionHasMetaData()?"<"+getName()+">":"";
String genericStr = "<"+getName()+">";
env.println("private static Dispatcher dispatcher = new Dispatcher();"); env.println("private static Dispatcher dispatcher = new Dispatcher();");
env.println(); env.println();
env.println("public SampleDispatcher getDispatcher() {"); env.println("public SampleDispatcher getDispatcher() {");
...@@ -553,7 +554,11 @@ aspect Java_Class { ...@@ -553,7 +554,11 @@ aspect Java_Class {
env.println("/** return the flat signature. */"); env.println("/** return the flat signature. */");
env.println("public byte[] getSignature() {"); env.println("public byte[] getSignature() {");
env.indent(); env.indent();
if(isSample) {
env.println("return signature;"); env.println("return signature;");
} else {
env.println("throw new Error(\"a TYPE_DEF has no flat signature\");");
}
env.unindent(); env.unindent();
env.println("}"); env.println("}");
env.println(); env.println();
...@@ -563,15 +568,16 @@ aspect Java_Class { ...@@ -563,15 +568,16 @@ aspect Java_Class {
// env.unindent(); // env.unindent();
// env.println("}"); // env.println("}");
// env.println(); // env.println();
// env.println("public void encodeSignatureMetadata(Encoder e, int index) throws IOException{"); env.println("public void encodeTypeDef(Encoder e, int index) throws IOException{");
// env.indent(); env.indent();
// env.println("e.encodePacked32(Constant.TYPE_DEF);"); env.println("e.begin(Constant.TYPE_DEF);");
// env.println("e.encodePacked32(index);"); env.println("e.encodePacked32(index);");
// env.println("e.encodeString(getName());"); env.println("e.encodeString(getName());");
// env.println("emitSignature(e);"); env.println("emitSignature(e);");
// env.unindent(); env.println("e.end(null);");
// env.println("}"); env.unindent();
// env.println(); env.println("}");
env.println();
env.println("public boolean canDecodeAndHandle() {"); env.println("public boolean canDecodeAndHandle() {");
env.indent(); env.indent();
env.println("return "+isSample+";"); env.println("return "+isSample+";");
......
...@@ -6,8 +6,11 @@ public interface Encoder { ...@@ -6,8 +6,11 @@ public interface Encoder {
public void register(SampleDispatcher dispatcher) throws IOException; public void register(SampleDispatcher dispatcher) throws IOException;
public void registerSampleRef(SampleDispatcher dispatcher) throws IOException; public void registerSampleRef(SampleDispatcher dispatcher) throws IOException;
public void begin(Class<? extends Sample> c) throws IOException; public void begin(Class<? extends SampleType> c) throws IOException;
public void end(Class<? extends Sample> c) throws IOException; public void end(Class<? extends SampleType> c) throws IOException;
public void begin(int t) throws IOException;
public int getTypeId(Class<? extends SampleType> c) throws IOException;
public void encodeBoolean(boolean value) throws IOException; public void encodeBoolean(boolean value) throws IOException;
public void encodeByte(byte value) throws IOException; public void encodeByte(byte value) throws IOException;
......
...@@ -10,8 +10,9 @@ public class EncoderChannel implements Encoder { ...@@ -10,8 +10,9 @@ public class EncoderChannel implements Encoder {
private Writer writer; private Writer writer;
private ByteArrayOutputStream bytes = new ByteArrayOutputStream(); private ByteArrayOutputStream bytes = new ByteArrayOutputStream();
private DataOutputStream data = new DataOutputStream(bytes); private DataOutputStream data = new DataOutputStream(bytes);
private EncoderRegistry def_registry = new EncoderRegistry(); private EncoderRegistry sample_def_registry = new EncoderRegistry();
private EncoderRegistry ref_registry = new EncoderRegistry(); private EncoderRegistry sample_ref_registry = new EncoderRegistry();
private EncoderRegistry type_def_registry = new EncoderRegistry();
private int current_tag; private int current_tag;
public EncoderChannel(Writer writer) throws IOException { public EncoderChannel(Writer writer) throws IOException {
...@@ -26,10 +27,15 @@ public class EncoderChannel implements Encoder { ...@@ -26,10 +27,15 @@ public class EncoderChannel implements Encoder {
this(new WriterWrapper(writer)); this(new WriterWrapper(writer));
} }
public void register(SampleDispatcher dispatcher) throws IOException { private void bindType(int sampleId, int typeId) throws IOException {
switch (dispatcher.getTypeDeclTag()) { begin(Constant.TYPE_BINDING);
case Constant.SAMPLE_DEF: { encodePacked32(sampleId);
int index = def_registry.add(dispatcher); encodePacked32(typeId);
end(null);
}
private void registerSample(SampleDispatcher dispatcher) throws IOException {
int index = sample_def_registry.add(dispatcher);
begin(dispatcher.getTypeDeclTag()); begin(dispatcher.getTypeDeclTag());
encodePacked32(index); encodePacked32(index);
encodeString(dispatcher.getName()); encodeString(dispatcher.getName());
...@@ -39,19 +45,35 @@ public class EncoderChannel implements Encoder { ...@@ -39,19 +45,35 @@ public class EncoderChannel implements Encoder {
encodeByte(signature[i]); encodeByte(signature[i]);
} }
end(null); end(null);
break; int tindex = registerTypeDef(dispatcher);
bindType(index, tindex);
} }
case Constant.TYPE_DEF: {
int index = def_registry.add(dispatcher); private int registerTypeDef(SampleDispatcher dispatcher) throws IOException {
//XXX A bit crude; maybe add boolean registry.contains(...) and check
// if already registered
try {
return type_def_registry.getTag(dispatcher);
} catch (IOException e) {
int index = type_def_registry.add(dispatcher);
begin(dispatcher.getTypeDeclTag()); begin(dispatcher.getTypeDeclTag());
encodePacked32(index); encodePacked32(index);
encodeString(dispatcher.getName()); encodeString(dispatcher.getName());
byte[] signature = dispatcher.getSignature(); dispatcher.encodeTypeDef(this, index);
encodePacked32(8);
for (int i = 0 ; i < 8; i++) {
encodeByte((byte) 0xff);
}
end(null); end(null);
return index;
}
}
public void register(SampleDispatcher dispatcher) throws IOException {
switch (dispatcher.getTypeDeclTag()) {
case Constant.SAMPLE_DEF: {
registerSample(dispatcher);
break;
}
case Constant.TYPE_DEF: {
registerTypeDef(dispatcher);
break; break;
} }
default: default:
...@@ -61,7 +83,7 @@ public class EncoderChannel implements Encoder { ...@@ -61,7 +83,7 @@ public class EncoderChannel implements Encoder {
public void registerSampleRef(SampleDispatcher dispatcher) throws IOException { public void registerSampleRef(SampleDispatcher dispatcher) throws IOException {
System.err.println(dispatcher); System.err.println(dispatcher);
int index = ref_registry.add(dispatcher); int index = sample_ref_registry.add(dispatcher);
begin(Constant.SAMPLE_REF); begin(Constant.SAMPLE_REF);
encodePacked32(index); encodePacked32(index);
encodeString(dispatcher.getName()); encodeString(dispatcher.getName());
...@@ -73,16 +95,16 @@ public class EncoderChannel implements Encoder { ...@@ -73,16 +95,16 @@ public class EncoderChannel implements Encoder {
end(null); end(null);
} }
private void begin(int tag) { public void begin(int tag) {
current_tag = tag; current_tag = tag;
bytes.reset(); bytes.reset();
} }
public void begin(Class<? extends Sample> c) throws IOException { public void begin(Class<? extends SampleType> c) throws IOException {
begin(def_registry.getTag(c)); begin(sample_def_registry.getTag(c));
} }
public void end(Class<? extends Sample> c) throws IOException { public void end(Class<? extends SampleType> c) throws IOException {
data.flush(); data.flush();
WritePacked32(writer, current_tag); WritePacked32(writer, current_tag);
WritePacked32(writer, bytes.size()); WritePacked32(writer, bytes.size());
...@@ -90,6 +112,13 @@ public class EncoderChannel implements Encoder { ...@@ -90,6 +112,13 @@ public class EncoderChannel implements Encoder {
bytes.reset(); bytes.reset();
} }
/**
* @return the id of a TYPE_DEF
*/
public int getTypeId(Class<? extends SampleType> c) throws IOException {
return type_def_registry.getTag(c);
}
private void WritePacked32(Writer s, long value) throws IOException { private void WritePacked32(Writer s, long value) throws IOException {
byte[] tmp1 = new byte[5]; byte[] tmp1 = new byte[5];
byte[] tmp2 = new byte[1]; byte[] tmp2 = new byte[1];
...@@ -167,7 +196,7 @@ public class EncoderChannel implements Encoder { ...@@ -167,7 +196,7 @@ public class EncoderChannel implements Encoder {
public void encodeSampleRef(Class value) throws IOException { public void encodeSampleRef(Class value) throws IOException {
int index = 0; int index = 0;
try { try {
index = ref_registry.getTag(value); index = sample_ref_registry.getTag(value);
} catch (NullPointerException e) { } catch (NullPointerException e) {
} }
data.writeInt(index); data.writeInt(index);
......
...@@ -42,7 +42,11 @@ public class EncoderRegistry { ...@@ -42,7 +42,11 @@ public class EncoderRegistry {
return e.getIndex(); return e.getIndex();
} }
public int getTag(Class<? extends Sample> sample) throws IOException { public int getTag(SampleDispatcher d) throws IOException {
return getTag(d.getSampleClass());
}
public int getTag(Class<? extends SampleType> sample) throws IOException {
Entry e = byClass.get(sample); Entry e = byClass.get(sample);
if (e == null) { if (e == null) {
throw new IOException("'" + throw new IOException("'" +
...@@ -52,4 +56,8 @@ public class EncoderRegistry { ...@@ -52,4 +56,8 @@ public class EncoderRegistry {
return e.index; return e.index;
} }
public boolean contains(Class<? extends SampleType> sample) {
return byClass.containsKey(sample);
}
} }
package se.lth.control.labcomm; package se.lth.control.labcomm;
public interface Sample { public interface Sample extends SampleType {
public SampleDispatcher getDispatcher(); public SampleDispatcher getDispatcher();
......
package se.lth.control.labcomm; package se.lth.control.labcomm;
public interface SampleDispatcher { import java.io.IOException;
public Class getSampleClass(); public interface SampleDispatcher <T extends SampleType>{
public Class<T> getSampleClass();
public String getName(); public String getName();
...@@ -11,6 +13,8 @@ public interface SampleDispatcher { ...@@ -11,6 +13,8 @@ public interface SampleDispatcher {
public void decodeAndHandle(Decoder decoder, public void decodeAndHandle(Decoder decoder,
SampleHandler handler) throws Exception; SampleHandler handler) throws Exception;
public void encodeTypeDef(Encoder e, int index) throws IOException;
/** return the tag SAMPLE_DEF or TYPE_DEF, for use /** return the tag SAMPLE_DEF or TYPE_DEF, for use
* by encoder.register. * by encoder.register.
* TODO: refactor types, moving this to a super-interface * TODO: refactor types, moving this to a super-interface
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment