From bdceaa139d042375b22a07b9bd542df55f05e3a6 Mon Sep 17 00:00:00 2001 From: Anders Blomdell <anders.blomdell@control.lth.se> Date: Thu, 28 May 2015 11:46:19 +0200 Subject: [PATCH] Changed notion of sample identity for C# to enable renaming --- compiler/2014/CS_CodeGen.jrag | 41 ++++--------------- lib/csharp/Makefile | 5 ++- .../control/labcomm2014/AbstractDecoder.cs | 18 ++++++++ .../se/lth/control/labcomm2014/Decoder.cs | 8 +--- .../lth/control/labcomm2014/DecoderChannel.cs | 4 +- .../control/labcomm2014/DecoderRegistry.cs | 10 ++--- .../se/lth/control/labcomm2014/Encoder.cs | 6 +-- .../lth/control/labcomm2014/EncoderChannel.cs | 8 ++-- .../control/labcomm2014/EncoderRegistry.cs | 12 +++--- .../control/labcomm2014/SampleDispatcher.cs | 4 +- 10 files changed, 53 insertions(+), 63 deletions(-) create mode 100644 lib/csharp/se/lth/control/labcomm2014/AbstractDecoder.cs diff --git a/compiler/2014/CS_CodeGen.jrag b/compiler/2014/CS_CodeGen.jrag index c05e6b2..17fa08e 100644 --- a/compiler/2014/CS_CodeGen.jrag +++ b/compiler/2014/CS_CodeGen.jrag @@ -13,7 +13,6 @@ aspect CS_CodeGenEnv { private int indent; private int depth; private CS_printer printer; - private HashMap unique = new HashMap(); final private static class CS_printer { @@ -159,15 +158,6 @@ aspect CS_CodeGenEnv { println("}"); } - public String getUnique(Object o) { - String result = (String)unique.get(o); - if (result == null) { - result = "_" + (unique.size() + 1) + "_"; - } - unique.put(o, result); - return result; - } - } } @@ -377,6 +367,7 @@ aspect CS_Class { CS_emitSignature(env); env.println("}"); + env.unindent(); } public void Decl.CS_emitSignature(CS_env env) { @@ -422,12 +413,12 @@ aspect CS_Class { env.unindent(); env.println("}"); env.println(); - env.println("private class Dispatcher : SampleDispatcher{"); + env.println("private class Dispatcher : SampleDispatcher {"); env.indent(); env.println(); - env.println("public Type getSampleClass() {"); + env.println("public SampleDispatcher getSampleIdentity() {"); env.indent(); - env.println("return typeof(" + getName() + ");"); + env.println("return dispatcher;"); env.unindent(); env.println("}"); env.println(); @@ -459,21 +450,6 @@ aspect CS_Class { env.unindent(); env.println("}"); env.println(); -// env.println("public void encodeSignature(Encoder e) throws IOException{"); -// env.indent(); -// env.println("emitSignature(e);"); -// env.unindent(); -// env.println("}"); -// env.println(); -// env.println("public void encodeSignatureMetadata(Encoder e, int index){"); -// env.indent(); -// env.println("e.encodePacked32(Constant.TYPE_DEF);"); -// env.println("e.encodePacked32(index);"); -// env.println("e.encodeString(getName());"); -// env.println("emitSignature(e);"); -// env.unindent(); -// env.println("}"); -// env.println(); env.println("public bool canDecodeAndHandle() {"); env.indent(); env.println("return "+isSample+";"); @@ -499,7 +475,6 @@ aspect CS_Class { env.unindent(); env.println("}"); env.println(""); - } //TODO, fix above method public void TypeDecl.CS_emitEncoder(CS_env env) { @@ -526,9 +501,9 @@ aspect CS_Class { } env.println(") {"); env.indent(); - env.println("e.begin(typeof(" + getName() + "));"); + env.println("e.begin(dispatcher);"); getDataType().CS_emitEncoder(env, "value"); - env.println("e.end(typeof(" + getName() + "));"); + env.println("e.end(dispatcher);"); env.unindent(); env.println("}"); env.println(); @@ -736,7 +711,7 @@ aspect CS_Class { public void PrimType.CS_emitTypePrefix(CS_env env) { switch (getToken()) { case LABCOMM_STRING: { env.print("String"); } break; - case LABCOMM_SAMPLE: { env.print("Type"); } break; + case LABCOMM_SAMPLE: { env.print("SampleDispatcher"); } break; default: { env.print(getName()); } break; } } @@ -866,7 +841,7 @@ aspect CS_Class { switch (getToken()) { case LABCOMM_STRING: { env.print("String"); } break; case LABCOMM_BOOLEAN: { env.print("bool"); } break; - case LABCOMM_SAMPLE: { env.print("Type"); } break; + case LABCOMM_SAMPLE: { env.print("SampleDispatcher"); } break; default: { env.print(getName()); } break; } } diff --git a/lib/csharp/Makefile b/lib/csharp/Makefile index c689902..0c67e05 100644 --- a/lib/csharp/Makefile +++ b/lib/csharp/Makefile @@ -1,4 +1,5 @@ -MODULES=Constant\ +MODULES=AbstractDecoder \ + Constant\ Decoder \ DecoderChannel \ DecoderRegistry \ @@ -8,7 +9,7 @@ MODULES=Constant\ Sample \ SampleDispatcher \ SampleHandler \ - SampleType + SampleType .PHONY: all all: labcomm2014.dll diff --git a/lib/csharp/se/lth/control/labcomm2014/AbstractDecoder.cs b/lib/csharp/se/lth/control/labcomm2014/AbstractDecoder.cs new file mode 100644 index 0000000..4ea8760 --- /dev/null +++ b/lib/csharp/se/lth/control/labcomm2014/AbstractDecoder.cs @@ -0,0 +1,18 @@ +using System; + +namespace se.lth.control.labcomm2014 { + + public interface AbstractDecoder { + + void runOne(); + + void run(); + + void register(SampleDispatcher dispatcher, + SampleHandler handler); + + void registerSampleRef(SampleDispatcher dispatcher); + + } + +} diff --git a/lib/csharp/se/lth/control/labcomm2014/Decoder.cs b/lib/csharp/se/lth/control/labcomm2014/Decoder.cs index ddc3b59..0d79bca 100644 --- a/lib/csharp/se/lth/control/labcomm2014/Decoder.cs +++ b/lib/csharp/se/lth/control/labcomm2014/Decoder.cs @@ -2,11 +2,7 @@ using System; namespace se.lth.control.labcomm2014 { - public interface Decoder { - - void register(SampleDispatcher dispatcher, - SampleHandler handler); - void registerSampleRef(SampleDispatcher dispatcher); + public interface Decoder : AbstractDecoder { bool decodeBoolean(); byte decodeByte(); @@ -17,7 +13,7 @@ namespace se.lth.control.labcomm2014 { double decodeDouble(); String decodeString(); int decodePacked32(); - Type decodeSampleRef(); + SampleDispatcher decodeSampleRef(); } diff --git a/lib/csharp/se/lth/control/labcomm2014/DecoderChannel.cs b/lib/csharp/se/lth/control/labcomm2014/DecoderChannel.cs index a56a604..4c02d83 100644 --- a/lib/csharp/se/lth/control/labcomm2014/DecoderChannel.cs +++ b/lib/csharp/se/lth/control/labcomm2014/DecoderChannel.cs @@ -166,11 +166,11 @@ namespace se.lth.control.labcomm2014 { return (int) (res & 0xffffffff); } - public Type decodeSampleRef() { + public SampleDispatcher decodeSampleRef() { int index = (int)ReadInt(4); try { DecoderRegistry.Entry e = ref_registry.get(index); - return e.getSampleDispatcher().getSampleClass(); + return e.getSampleDispatcher().getSampleIdentity(); } catch (NullReferenceException) { return null; } diff --git a/lib/csharp/se/lth/control/labcomm2014/DecoderRegistry.cs b/lib/csharp/se/lth/control/labcomm2014/DecoderRegistry.cs index bebcff2..6c7a4a4 100644 --- a/lib/csharp/se/lth/control/labcomm2014/DecoderRegistry.cs +++ b/lib/csharp/se/lth/control/labcomm2014/DecoderRegistry.cs @@ -90,11 +90,11 @@ namespace se.lth.control.labcomm2014 { } } - private Dictionary<Type, Entry> byClass; + private Dictionary<SampleDispatcher, Entry> byIdentity; private Dictionary<int, Entry> byIndex; public DecoderRegistry() { - byClass = new Dictionary<Type, Entry>(); + byIdentity = new Dictionary<SampleDispatcher, Entry>(); byIndex = new Dictionary<int, Entry>(); } @@ -102,7 +102,7 @@ namespace se.lth.control.labcomm2014 { SampleHandler handler) { lock(this) { Entry e; - byClass.TryGetValue(dispatcher.getSampleClass(), out e); + byIdentity.TryGetValue(dispatcher.getSampleIdentity(), out e); if (e != null) { e.check(dispatcher.getName(), dispatcher.getSignature()); e.setHandler(handler); @@ -117,7 +117,7 @@ namespace se.lth.control.labcomm2014 { } if (e == null) { e = new Entry(dispatcher, handler); - byClass.Add(dispatcher.getSampleClass(), e); + byIdentity.Add(dispatcher.getSampleIdentity(), e); } } } @@ -132,7 +132,7 @@ namespace se.lth.control.labcomm2014 { if (e != null) { e.check(name, signature); } else { - foreach (Entry e2 in byClass.Values) { + foreach (Entry e2 in byIdentity.Values) { if (e2.match(name, signature)) { e2.setIndex(index); e = e2; diff --git a/lib/csharp/se/lth/control/labcomm2014/Encoder.cs b/lib/csharp/se/lth/control/labcomm2014/Encoder.cs index 4aac7b3..08d7751 100644 --- a/lib/csharp/se/lth/control/labcomm2014/Encoder.cs +++ b/lib/csharp/se/lth/control/labcomm2014/Encoder.cs @@ -6,8 +6,8 @@ namespace se.lth.control.labcomm2014 { void register(SampleDispatcher dispatcher); void registerSampleRef(SampleDispatcher dispatcher); - void begin(Type c); - void end(Type c); + void begin(SampleDispatcher dispatcher); + void end(SampleDispatcher dispatcher); void encodeBoolean(bool value); void encodeByte(byte value); @@ -18,7 +18,7 @@ namespace se.lth.control.labcomm2014 { void encodeDouble(double value); void encodeString(String value); void encodePacked32(Int64 value); - void encodeSampleRef(Type value); + void encodeSampleRef(SampleDispatcher value); } diff --git a/lib/csharp/se/lth/control/labcomm2014/EncoderChannel.cs b/lib/csharp/se/lth/control/labcomm2014/EncoderChannel.cs index 20536e4..cba6072 100644 --- a/lib/csharp/se/lth/control/labcomm2014/EncoderChannel.cs +++ b/lib/csharp/se/lth/control/labcomm2014/EncoderChannel.cs @@ -53,11 +53,11 @@ namespace se.lth.control.labcomm2014 { bytes.SetLength(0); } - public void begin(Type c) { - begin(def_registry.getTag(c)); + public void begin(SampleDispatcher identity) { + begin(def_registry.getTag(identity)); } - public void end(Type c) { + public void end(SampleDispatcher identity) { WritePacked32(writer, current_tag); WritePacked32(writer, bytes.Length); bytes.WriteTo(writer); @@ -132,7 +132,7 @@ namespace se.lth.control.labcomm2014 { WritePacked32(bytes, value); } - public void encodeSampleRef(Type value) { + public void encodeSampleRef(SampleDispatcher value) { int index = 0; try { index = ref_registry.getTag(value); diff --git a/lib/csharp/se/lth/control/labcomm2014/EncoderRegistry.cs b/lib/csharp/se/lth/control/labcomm2014/EncoderRegistry.cs index 5d35ecb..72ec5f2 100644 --- a/lib/csharp/se/lth/control/labcomm2014/EncoderRegistry.cs +++ b/lib/csharp/se/lth/control/labcomm2014/EncoderRegistry.cs @@ -26,29 +26,29 @@ namespace se.lth.control.labcomm2014 { } private int userIndex = Constant.FIRST_USER_INDEX; - private Dictionary<Type, Entry> byClass; + private Dictionary<SampleDispatcher, Entry> byIdentity; public EncoderRegistry() { - byClass = new Dictionary<Type, Entry>(); + byIdentity = new Dictionary<SampleDispatcher, Entry>(); } public int add(SampleDispatcher dispatcher) { lock(this) { Entry e; - byClass.TryGetValue(dispatcher.getSampleClass(), out e); + byIdentity.TryGetValue(dispatcher.getSampleIdentity(), out e); if (e == null) { e = new Entry(dispatcher, userIndex); - byClass.Add(dispatcher.getSampleClass(), e); + byIdentity.Add(dispatcher.getSampleIdentity(), e); userIndex++; } return e.getIndex(); } } - public int getTag(Type sample) { + public int getTag(SampleDispatcher sample) { lock(this) { Entry e; - byClass.TryGetValue(sample, out e); + byIdentity.TryGetValue(sample, out e); if (e == null) { throw new Exception("'" + sample.ToString() + diff --git a/lib/csharp/se/lth/control/labcomm2014/SampleDispatcher.cs b/lib/csharp/se/lth/control/labcomm2014/SampleDispatcher.cs index b7104d1..de405d1 100644 --- a/lib/csharp/se/lth/control/labcomm2014/SampleDispatcher.cs +++ b/lib/csharp/se/lth/control/labcomm2014/SampleDispatcher.cs @@ -4,12 +4,12 @@ namespace se.lth.control.labcomm2014 { public interface SampleDispatcher { - Type getSampleClass(); + SampleDispatcher getSampleIdentity(); String getName(); byte[] getSignature(); - + void decodeAndHandle(Decoder decoder, SampleHandler handler); -- GitLab