/// <summary> /// Initializes a new instance of the <see cref="EnumValue"/> class. /// </summary> /// <param name="theEnumDefinition">The enum definition.</param> /// <param name="parent">The parent.</param> public EnumValue(EnumDefinition theEnumDefinition, Value parent) : base(theEnumDefinition, parent) { m_EnumDefinition = theEnumDefinition; IntegerValue = m_EnumDefinition.Literals[0].Value; StringValue = m_EnumDefinition.Literals[0].Name; }
/// <summary> /// Initializes a new instance of the <see cref="AttributeValue"/> class. /// </summary> /// <param name="theDefinition">The definition.</param> /// <param name="theValue">The value.</param> /// <param name="theParentStruct">The parent struct.</param> public AttributeValue(AttributeDefinition theDefinition, Value theValue, StructureValue theParentStruct) { Definition = theDefinition; Name = theDefinition.Name; Value = theValue; ParentStruct = theParentStruct; }
/// <summary> /// Initializes a new instance of the <see cref="ArrayTypeValue"/> class. /// Values are decoded from the byte store /// </summary> /// <param name="theArrayTypeDefinition">The array type definition.</param> /// <param name="theBytes">The bytes.</param> /// <param name="parent">The parent.</param> public ArrayTypeValue(ArrayTypeDefinition theArrayTypeDefinition, ByteStore theBytes, Value parent) : base(theArrayTypeDefinition, parent) { m_ArrayTypeDefinition = theArrayTypeDefinition; m_ArrayValues = new ArrayValueContainer(m_ArrayTypeDefinition.UpperBound); if (m_ArrayTypeDefinition.Rank > 0) { // Fixed-size arrays - populate with decoded elements of the base type m_ArrayValues.Populate(() => m_ArrayTypeDefinition.ElementType.Decode(theBytes, this)); } else if (!string.IsNullOrEmpty(m_ArrayTypeDefinition.UpperBoundVariable)) { // Variable size array. Locate the discriminator if there is one // TODO } else { // Variable size array - number of elements limited by the number of bytes available or an end marker being discovered bool done = false; do { Value theValue = m_ArrayTypeDefinition.ElementType.Decode(theBytes, this); m_ArrayValues.Add(theValue); if (theValue.FundamentalType.TypeId == TypeId.StructType) { done = IsEndMarker(theValue as StructureValue); } } while (!done); } }
/// <summary> /// Creates a new Value object, deriving the value by decoding the specified bytes. /// Each derived class creates its corresponding value type /// </summary> /// <param name="theBytes">The collection of bytes containing the value to be decoded</param> /// <param name="parent">The parent.</param> /// <returns> /// The decoded value object /// </returns> public override Value Decode(ByteStore theBytes, Value parent) { TypeDefinition baseType = DictionaryManager.DereferenceTypeDef(this); Value theValue = baseType.Decode(theBytes, parent); theValue.OverrideInitialType(this); return theValue; }
/// <summary> /// Initializes a new instance of the <see cref="ArrayTypeValue"/> class. /// Values are default values /// </summary> /// <param name="theArrayTypeDefinition">The array type definition.</param> /// <param name="parent">The parent.</param> public ArrayTypeValue(ArrayTypeDefinition theArrayTypeDefinition, Value parent) : base(theArrayTypeDefinition, parent) { m_ArrayTypeDefinition = theArrayTypeDefinition; m_ArrayValues = new ArrayValueContainer(m_ArrayTypeDefinition.UpperBound); if (m_ArrayTypeDefinition.Rank > 0) { // Fixed-size arrays - populate with instantiate elements // Variable-size arrays are populated on demand m_ArrayValues.Populate(() => m_ArrayTypeDefinition.ElementType.Instantiate(this)); } }
/// <summary> /// Initializes a new instance of the <see cref="StringValue"/> class. /// </summary> /// <param name="theBytes">The bytes.</param> /// <param name="theStringDefinition">The string definition.</param> /// <param name="parent">The parent.</param> public StringValue(ByteStore theBytes, StringDefinition theStringDefinition, Value parent) : base(theStringDefinition, parent) { List<Byte> stringBytes = new List<byte>(); // Read bytes until they run out or we hit null terminator Byte theByte = 0xff; while (theBytes.ReadPosition < theBytes.PayloadLength && theByte != '\0') { theByte = theBytes.GetByte(); stringBytes.Add(theByte); } m_Value = Encoding.ASCII.GetString(stringBytes.ToArray()); }
/// <summary> /// Encodes the signal element in /// </summary> /// <param name="signalElement">The signal element.</param> /// <param name="signalText">The signal text.</param> /// <param name="indent">The indent.</param> /// <param name="valueName">Name of the value.</param> private void EncodeSignalElement(Value signalElement, StringBuilder signalText, string indent, string valueName) { switch (signalElement.FundamentalType.TypeId) { case TypeId.StructType: EncodeStructType(signalElement as StructureValue, signalText, indent + " ", valueName); break; case TypeId.BaseType: EncodeBaseType(signalElement as BaseTypeValue, signalText, indent + " ", valueName); break; case TypeId.EnumType: EncodeEnumType(signalElement as EnumValue, signalText, indent + " ", valueName); break; case TypeId.ArrayType: EncodeArrayType(signalElement as ArrayTypeValue, signalText, indent + " ", valueName); break; default: signalText.AppendFormat("{0}Cannot display {1} type {2}", indent, valueName, signalElement.FundamentalType.Name); break; } }
/// <summary> /// Initializes a new instance of the <see cref="SwitchValue"/> class. /// </summary> /// <param name="theSwitchDefinition">The switch definition.</param> /// <param name="parent">The parent.</param> /// <param name="wrappedValue">The wrapped value.</param> /// <param name="theDiscriminatorValue">The discriminator value.</param> public SwitchValue(SwitchDefinition theSwitchDefinition, Value parent, Value wrappedValue, int theDiscriminatorValue) : base(theSwitchDefinition, parent) { DiscriminatorValue = theDiscriminatorValue; m_WrappedValue = wrappedValue; }
/// <summary> /// Initializes a new instance of the <see cref="BaseTypeValue"/> class. /// </summary> /// <param name="theValue">The value.</param> /// <param name="theBaseTypeDefinition">The base type definition.</param> /// <param name="parent">The parent.</param> public BaseTypeValue(long theValue, BaseTypeDefinition theBaseTypeDefinition, Value parent) : base(theBaseTypeDefinition, parent) { SignedValue = theValue; m_BaseTypeDefinition = theBaseTypeDefinition; }
/// <summary> /// Initializes a new instance of the <see cref="BaseTypeValue"/> class with /// values extracted from the byte store /// </summary> /// <param name="baseTypeDefinition">The base type definition.</param> /// <param name="theBytes">The bytes.</param> /// <param name="parent">The parent.</param> public BaseTypeValue(BaseTypeDefinition baseTypeDefinition, ByteStore theBytes, Value parent) : this(baseTypeDefinition, parent) { ulong tempValue = 0; switch (m_BaseTypeDefinition.FixedSizeBytes.Value) { case 0: // Special case for end-markers for lists of elements break; case 1: tempValue = theBytes.GetByte(); break; case 2: tempValue = theBytes.GetUint16(); break; case 4: tempValue = theBytes.GetUint32(); break; case 8: tempValue = theBytes.GetUint64(); break; default: throw new DataDictionaryException(String.Format("{0} Illegal ByteSize {1}", m_BaseTypeDefinition.Name, m_BaseTypeDefinition.FixedSizeBytes.Value)); } if (m_BaseTypeDefinition.IsSigned) { SignedValue = (long)tempValue; } else { UnsignedValue = tempValue; } }
/// <summary> /// Initializes a new instance of the <see cref="BaseTypeValue"/> class /// with default values /// </summary> /// <param name="theBaseTypeDefinition">The base type definition.</param> /// <param name="parent">The parent.</param> public BaseTypeValue(BaseTypeDefinition theBaseTypeDefinition, Value parent) : base(theBaseTypeDefinition, parent) { m_BaseTypeDefinition = theBaseTypeDefinition; }
/// <summary> /// Initializes a new instance of the <see cref="Value"/> class. /// </summary> /// <param name="theTypeDefinition">The type definition.</param> /// <param name="parent">The parent.</param> public Value(TypeDefinition theTypeDefinition, Value parent) { FundamentalType = InitialType = theTypeDefinition; Parent = parent; }
/// <summary> /// Create a new instance of the type populated with default values. /// </summary> /// <param name="parent"></param> /// <returns> /// New instance of the type populated with default values /// </returns> public override Value Instantiate(Value parent) { return new BaseTypeValue(this, parent); }
/// <summary> /// Creates a new Value object, deriving the value by decoding the specified bytes. /// Each derived class creates its corresponding value type /// </summary> /// <param name="theBytes">The collection of bytes containing the value to be /// decoded</param> /// <param name="parent"></param> /// <returns> /// The decoded value object /// </returns> public override Value Decode(ByteStore theBytes, Value parent) { return new EnumValue(this, theBytes, parent); }
private static string GetEnumValue(Value theValue) { string enumValue = string.Empty; EnumValue theEnumValue = theValue as EnumValue; if (theEnumValue != null) { enumValue = string.Format("{0} ({1})", theEnumValue.StringValue, theEnumValue.IntegerValue); } else { throw new InvalidDataException(string.Format("Unsupported type {0}", theValue.GetType())); } return enumValue; }
private static string GetIntegerValue(Value theValue) { string integerValue = string.Empty; BaseTypeValue theBaseTypeValue = theValue as BaseTypeValue; if (theBaseTypeValue != null) { integerValue = theBaseTypeValue.IsSigned ? theBaseTypeValue.SignedValue.ToString() : theBaseTypeValue.UnsignedValue.ToString(); } else { throw new InvalidDataException(string.Format("Unsupported type {0}", theValue.GetType())); } return integerValue; }
/// <summary> /// Create a new instance of the type populated with default values. /// </summary> /// <returns>New instance of the type populated with default values</returns> /// <param name="parent">The parent.</param> public override Value Instantiate(Value parent) { return null; }
/// <summary> /// Initializes a new instance of the <see cref="EnumValue"/> class by extracting the /// encoded value from the byte store /// </summary> /// <param name="theEnumDefinition">The enum definition.</param> /// <param name="theBytes">The bytes.</param> /// <param name="parent">The parent.</param> public EnumValue(EnumDefinition theEnumDefinition, ByteStore theBytes, Value parent) : this(theEnumDefinition, parent) { switch (theEnumDefinition.FixedSizeBytes) { case 1: IntegerValue = (int)theBytes.GetByte(); break; case 2: IntegerValue = (int)theBytes.GetUint16(); break; case 4: IntegerValue = (int)theBytes.GetUint32(); break; default: throw new DataDictionaryException(string.Format("Illegal byte size {0} for enum {1}", theEnumDefinition.FixedSizeBytes, m_EnumDefinition.Name)); } StringValue = m_EnumDefinition.IntegerValueToString(IntegerValue); }
/// <summary> /// Creates a new Value object, deriving the value by decoding the specified bytes. /// Each derived class creates its corresponding value type /// </summary> /// <param name="theBytes">The collection of bytes containing the value to be decoded</param> /// <param name="parent">The parent.</param> /// <returns> /// The decoded value object /// </returns> public virtual Value Decode(ByteStore theBytes, Value parent) { throw new DataDictionaryException("Cannot decode type {0}", Name); }
/// <summary> /// Creates a new Value object, deriving the value by decoding the specified bytes. /// Each derived class creates its corresponding value type /// </summary> /// <param name="theBytes">The collection of bytes containing the value to be /// decoded</param> /// <param name="parent"></param> /// <returns> /// The decoded value object /// </returns> public override Value Decode(ByteStore theBytes, Value parent) { // Find the discriminator for the switch and look up its value StructureValue theParentStruct = parent as StructureValue; if (theParentStruct == null) { throw new DataDictionaryException("SwitchDefinition {0}: Parent {1} is not a struct value", Name, parent.FundamentalType.Name); } SwitchCaseDefinition caseDefinition = GetSwitchCaseDefinition(theParentStruct); if (caseDefinition == null) { throw new DataDictionaryException("SwitchDefinition {0}: case value not found", Name); } TypeDefinition typeToDecode = caseDefinition.Type; return typeToDecode.Decode(theBytes, parent); }
/// <summary> /// Create a new instance of the type selected by the discriminator value, populated with default values, /// or null if the discriminator value is not recognised /// </summary> /// <param name="parent">The parent structure value</param> /// <returns> /// New instance of the type selected by the discriminator value, populated with default values, or null /// </returns> public override Value Instantiate(Value parent) { StructureValue theParentStruct = parent as StructureValue; SwitchCaseDefinition theCase = GetSwitchCaseDefinition(theParentStruct); Value theValue = null; if (theCase != null) { theValue = theCase.Type.Instantiate(parent); } return new SwitchValue(this, parent, theValue, GetDiscriminatorValue(theParentStruct)); }
/// <summary> /// Create a new instance of the type populated with default values. /// </summary> /// <param name="parent">The parent.</param> /// <returns> /// New instance of the type populated with default values /// </returns> public override Value Instantiate(Value parent) { TypeDefinition baseType = DictionaryManager.DereferenceTypeDef(this); Value theValue = baseType.Instantiate(parent); if (theValue != null) { theValue.OverrideInitialType(this); } return theValue; }
/// <summary> /// Create a new instance of the type defined by the structure definition populated /// with default values. /// </summary> /// <param name="parent"></param> /// <returns> /// New instance of the type populated with default values /// </returns> public override Value Instantiate(Value parent) { return new StructureValue(this, parent); }
/// <summary> /// Create a new instance of the type populated with default values. /// </summary> /// <param name="parent">The parent.</param> /// <returns> /// New instance of the type populated with default values /// </returns> public virtual Value Instantiate(Value parent) { return null; }
/// <summary> /// Create a new instance of the type populated with default values. /// </summary> /// <param name="parent"></param> /// <returns> /// New instance of the type populated with default values /// </returns> public override Value Instantiate(Value parent) { return new EnumValue(this, parent); }
/// <summary> /// Initializes a new instance of the <see cref="PartialDecodeException" /> class. /// </summary> /// <param name="valueBeingDecoded">The value being decoded.</param> /// <param name="message">The exception message.</param> public PartialDecodeException(Value valueBeingDecoded, string message) : base(message) { ValueBeingDecoded = valueBeingDecoded; }