//FIXME: needs to know about sizes of known structs //FIXME: needs to know about Size=0 structs/unions static int StructSize(object[] sItems) { if (sItems == null) { return(0); } int offset = 0; foreach (object ob in sItems) { if (ob is field) { field f = ob as field; string fType = TypeToCs(f.type); offset += SizeOfType(fType); } else if (ob is pad) { pad p = ob as pad; int padding = Int32.Parse(p.bytes); offset += padding; } } return(offset); }
// Overrides the ConvertFrom method of TypeConverter. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { pad result = ushort.Parse((string)value); return(result); } return(base.ConvertFrom(context, culture, value)); }
public void TestPad() { pad value; value = (ushort)0; Assert.Equal(pad.RESERVED_VALUE, (ushort)value); var registers = value.ToRegisters(); Assert.NotEmpty(registers); Assert.Single(registers); value = registers; Assert.Equal(pad.RESERVED_VALUE, (ushort)value); string json = JsonConvert.SerializeObject(value); var convert = JsonConvert.DeserializeObject <pad>(json); Assert.Equal((ushort)convert, (ushort)value); string text = Convert.ToString(value); convert = Convert.ToUInt16(text); pad data = convert; Assert.Equal((ushort)data, (ushort)value); }
static int GenClassData(string sName, object[] sItems, string suffix, bool withOffsets) { string sizeString = ""; //FIXME: EndsWith hack bool isRequest = sName.EndsWith("RequestData"); bool isEvent = sName.EndsWith("EventData"); bool isError = sName.EndsWith("ErrorData"); //FIXME: Rep shouldn't have offsets/inherits bool isReply = sName.EndsWith("ReplyData"); bool isStruct = (!isRequest && !isEvent && !isError && !isReply && !sName.EndsWith("RepData")); if (sName.EndsWith("EventData") || sName.EndsWith("ErrorData")) { sizeString = ", Size=" + 28; } //FIXME: incorrect hack //if (sName.EndsWith ("Reply")) // sizeString = ", Size=" + 32; //sName = NewTypeToCs (sName); //FIXME: figure out actual size first //if (isStruct) //cwt.WriteLine ("public struct @" + sName); //else // cwt.WriteLine ("public class @" + sName + suffix); //cwt.WriteLine ("{"); Dictionary <string, int> sizeParams = new Dictionary <string, int> (); int index = 0, offset = 0; if (sItems != null) { if (isRequest) { offset += 4; } if (isError || isEvent) { offset += 4; } if (isReply && !sName.EndsWith("RepData")) { offset += 8; } bool first = true; foreach (object ob in sItems) { //bool isData = (offset == startOffset) && (isReply || (isRequest && !isExtension)); bool isData = first && (isReply || (isRequest && !isExtension)); first = false; if (ob is field) { field f = ob as field; if (f.name != null) { string fName = ToCs(f.name); if (fName == sName || fName + "Data" == sName) { Console.Error.WriteLine("Warning: field renamed: " + fName); fName = "Value"; } string fType = TypeToCs(f.type); //in non-extension requests, the data field carries the first element if (withOffsets) { if (isData) { cwt.WriteLine("[FieldOffset (" + 1 + ")]"); } else { cwt.WriteLine("[FieldOffset (" + offset + ")]"); offset += SizeOfType(fType); } } /* * //TODO: use fieldref * if (f.name.EndsWith ("_len")) { * string fNameArray = f.name.Substring (0, f.name.Length - 4); * sizeParams[fNameArray] = index; * cwt.WriteLine ("//TODO: private"); * cwt.WriteLine ("//public " + fType + " @" + fName + ";"); * } else { */ if (withOffsets) { cwt.WriteLine("public " + fType + " @" + fName + ";"); } else { cwt.WriteLine("public " + fType + " @" + fName); cwt.WriteLine("{"); cwt.WriteLine("get {"); cwt.WriteLine("return MessageData.@" + fName + ";"); cwt.WriteLine("} set {"); cwt.WriteLine("MessageData.@" + fName + " = value;"); cwt.WriteLine("}"); cwt.WriteLine("}"); } //} index++; } } else if (ob is pad) { if (!withOffsets) { continue; } pad p = ob as pad; int padding = Int32.Parse(p.bytes); if (isData) { padding--; } if (padding > 0) { cwt.WriteLine("//byte[" + padding + "]"); offset += padding; } } } } return(offset); }
Tuple <MemberDeclarationSyntax, int> GenClassData(string sName, object[] sItems, string suffix, bool withOffsets) { string sizeString = ""; //FIXME: EndsWith hack bool isRequest = sName.EndsWith("RequestData"); bool isEvent = sName.EndsWith("EventData"); bool isError = sName.EndsWith("ErrorData"); //FIXME: Rep shouldn't have offsets/inherits bool isReply = sName.EndsWith("ReplyData"); bool isStruct = (!isRequest && !isEvent && !isError && !isReply && !sName.EndsWith("RepData")); if (sName.EndsWith("EventData") || sName.EndsWith("ErrorData")) { sizeString = ", Size=" + 28; } Dictionary <string, int> sizeParams = new Dictionary <string, int>(); int offset = 0; if (sItems != null) { if (isRequest) { offset += 4; } if (isError || isEvent) { offset += 4; } if (isReply && !sName.EndsWith("RepData")) { offset += 8; } bool first = true; foreach (object ob in sItems) { bool isData = first && (isReply || (isRequest && !isExtension)); first = false; if (ob is field) { field f = ob as field; if (f.name == null) { continue; } string fName = GeneratorUtil.ToCs(f.name); if (fName == sName || fName + "Data" == sName) { Console.Error.WriteLine("Warning: field renamed: " + fName); fName = "Value"; } string fType = typeMap.TypeToCs(f.type); //in non-extension requests, the data field carries the first element if (withOffsets) { if (isData) { cwt.WriteLine("[FieldOffset (" + 1 + ")]"); } else { cwt.WriteLine("[FieldOffset (" + offset + ")]"); offset += typeMap.SizeOfType(fType); } } if (withOffsets) { cwt.WriteLine("public " + fType + " @" + fName + ";"); } else { cwt.WriteLine("public " + fType + " @" + fName); cwt.WriteLine("{"); cwt.WriteLine("get {"); cwt.WriteLine("return MessageData.@" + fName + ";"); cwt.WriteLine("} set {"); cwt.WriteLine("MessageData.@" + fName + " = value;"); cwt.WriteLine("}"); cwt.WriteLine("}"); } } else if (ob is pad) { if (!withOffsets) { continue; } pad p = ob as pad; int padding = Int32.Parse(p.bytes); if (isData) { padding--; } if (padding > 0) { cwt.WriteLine("//byte[" + padding + "]"); offset += padding; } } } } return(offset); }