private int GetByteSize(Element element) { switch (element.TypeOfElement) { case ElementType.Collection: CollectionElement collection = element as CollectionElement ?? throw new InvalidDataException("Element with type Collection is not a CollectionElement."); return(GetByteSize(collection)); case ElementType.Vector: VectorElement vector = element as VectorElement ?? throw new InvalidDataException("Element with type Vector is not a VectorElement."); return(GetByteSize(vector)); case ElementType.Scalar: ScalarElement scalar = element as ScalarElement ?? throw new InvalidDataException("Element with type Scalar is not a ScalarElement."); return(GetByteSize(scalar)); default: return(0); } }
/// <summary> /// Updates the values contained by the vector element identified by the given tag /// or adds a new vector element if one does not already exist. /// </summary> /// <param name="tag">The tag which identifies the vector element to be updated.</param> /// <param name="type">The physical type of the values contained in the vector element.</param> /// <param name="bytes">The values to be entered into the vector element.</param> /// <returns>The vector element which was updated, or a new vector element if one did not already exist.</returns> public VectorElement AddOrUpdateVector(Guid tag, PhysicalType type, byte[] bytes) { VectorElement vectorElement = GetOrAddVector(tag); vectorElement.TypeOfValue = type; vectorElement.Size = bytes.Length / type.GetByteSize(); vectorElement.SetValues(bytes, 0); return(vectorElement); }
/// <summary> /// Gets the vector element identified by the given tag /// or adds a new vector element if one does not already exist. /// </summary> /// <param name="tag">The tag which identifies the vector element to be retrieved.</param> /// <returns>The vector element identified by the tag, or a new vector element if one did not already exist.</returns> public VectorElement GetOrAddVector(Guid tag) { VectorElement?vectorElement = GetVectorByTag(tag); if (vectorElement == null) { vectorElement = new VectorElement(); vectorElement.TagOfElement = tag; AddElement(vectorElement); } return(vectorElement); }
private int GetByteSize(VectorElement vector) => 4 + vector.Size * vector.TypeOfValue.GetByteSize();
private void WriteVector(BinaryWriter writer, VectorElement vector) { writer.Write(vector.Size); writer.Write(vector.GetValues()); }
private void WriteCollection(BinaryWriter writer, CollectionElement collection) { int linkPosition = (int)writer.BaseStream.Position + 4 + 28 * collection.Size; writer.Write(collection.Size); foreach (Element element in collection.Elements) { bool isEmbedded = IsEmbedded(element); writer.Write(element.TagOfElement.ToByteArray()); writer.Write((byte)element.TypeOfElement); writer.Write((byte)element.TypeOfValue); writer.Write(isEmbedded ? (byte)1 : (byte)0); writer.Write((byte)0); if (!isEmbedded) { int padSize = GetPaddedByteSize(element); writer.Write(linkPosition); writer.Write(padSize); linkPosition += padSize; } else { ScalarElement scalar = element as ScalarElement ?? throw new InvalidDataException("Embedded element is not a scalar element."); WriteScalar(writer, scalar); for (int i = element.TypeOfValue.GetByteSize(); i < 8; i++) { writer.Write((byte)0); } } } foreach (Element element in collection.Elements) { if (IsEmbedded(element)) { continue; } switch (element.TypeOfElement) { case ElementType.Collection: CollectionElement nestedCollection = element as CollectionElement ?? throw new InvalidDataException("Element with type Collection is not a CollectionElement."); WriteCollection(writer, nestedCollection); break; case ElementType.Vector: VectorElement vector = element as VectorElement ?? throw new InvalidDataException("Element with type Vector is not a VectorElement."); WriteVector(writer, vector); break; case ElementType.Scalar: ScalarElement scalar = element as ScalarElement ?? throw new InvalidDataException("Element with type Scalar is not a ScalarElement."); WriteScalar(writer, scalar); break; } int byteSize = GetByteSize(element); int padSize = GetPaddedByteSize(element); for (int i = 0; i < padSize - byteSize; i++) { writer.Write((byte)0); } } }