private static uint GetNumberBytes(XerusStruct dataType, TargetLanguage targetLanguage) { uint factor = 1; uint size = 0; var fieldInfos = dataType.GetType().GetFields(); foreach (var field in fieldInfos) { var value = field.GetValue(dataType); var fieldType = field.FieldType; if (fieldType.IsArray) { var array = value as Array; for (var rank = 0; rank < fieldType.GetArrayRank(); rank++) { if (array != null) { factor = factor * (uint)array.GetLength(rank); } } size += GetNumberBytes(fieldType.GetElementType(), targetLanguage) * factor; factor = 1; } } return(size); }
/// <summary> /// Creates the the type definition for specified structure data type. /// </summary> /// <param name="structDataType">Type of the structure data.</param> /// <exception cref="ModelValidationException">If an error within the model exists</exception> private void Create(XerusStruct structDataType) { this.AddMultiLineDescription(structDataType.Description); this.AppendLine("struct " + structDataType.Name); this.AppendLine("{"); this.PushIndent(); foreach (var memberInfo in structDataType.GetType().GetFields()) { var fieldType = memberInfo.FieldType; if (fieldType.IsArray) { var memberType = fieldType.GetElementType().XerusTypeName(TargetLanguage.Cpp); var arrayRank = fieldType.GetArrayRank(); var value = memberInfo.GetValue(structDataType); if (value == null) { throw new ModelValidationException(string.Format("The dimension of the array {0}.{1} has to be specifed. e.g. DataType[,] Member1 = new DataType[5,2]", structDataType.Name, memberType)); } this.Append(memberType + " " + memberInfo.Name); for (int rank = 0; rank < arrayRank; rank++) { var array = value as Array; this.Append(string.Format("[{0}]", array.GetLength(rank))); } this.Append(";"); } else { this.Append(memberInfo.FieldType.XerusTypeName(TargetLanguage.Cpp) + " " + memberInfo.Name + ";"); } var documentation = memberInfo.GetDocumentation(); if (documentation != null) { this.AddSingleLineDescription(documentation); } this.AppendLine(); } this.PopIndent(); this.AppendLine("};"); }
/// <summary> /// Generates the struct deserializer code. /// </summary> /// <param name="dataType">Type of the data.</param> /// <returns>Serializer code for structs</returns> private string GenerateDeserializerDefinition(XerusStruct dataType, string serializerName) { string name = dataType.Name; var tb = new TextBuilder(); tb.AppendLine(string.Format( @"/** Deserializer for data type {0} * @param input Data type * @param data Buffer with the serialized data * @return Deserialized data type */", name)); tb.AppendLine("uint16 " + serializerName + "::Bytes_To_{0}(const uint8 *const data, {0} &output)", name); tb.AppendLine("{"); tb.PushIndent(); tb.AppendLine("int offset = 0;"); foreach (var memberInfo in dataType.GetType().GetFields()) { var fieldType = memberInfo.FieldType; if (fieldType.IsArray) { var arrayRank = fieldType.GetArrayRank(); var value = memberInfo.GetValue(dataType); if (value == null) { throw new ModelValidationException(string.Format("The dimension of the array {0}.{1} has to be specifed. e.g. DataType[,] Member1 = new DataType[5,2]", dataType.Name, memberInfo.Name)); } var array = value as Array; var indexString = new StringBuilder(); for (int currentRank = 0; currentRank < arrayRank; currentRank++) { tb.AppendLine("for (int index{0} = 0; index{0} < {1}; index{0}++)", currentRank, array.GetLength(currentRank)); tb.AppendLine("{"); tb.PushIndent(); indexString.Append(string.Format("[index{0}]", currentRank)); } tb.AppendLine("offset+=Bytes_To_{0}(data+offset, output.{1}{2});", fieldType.XerusTypeName(TargetLanguage.Cpp), memberInfo.Name, indexString.ToString()); for (int currentRank = 0; currentRank < arrayRank; currentRank++) { tb.PopIndent(); tb.AppendLine("}"); } } else { tb.AppendLine("offset+=Bytes_To_{0}(data+offset, output.{1});", fieldType.XerusTypeName(TargetLanguage.Cpp), memberInfo.Name); } } tb.AppendLine("return offset;"); tb.PopIndent(); tb.AppendLine("}"); return(tb.ToString()); }