84 lines
3.8 KiB
Plaintext
84 lines
3.8 KiB
Plaintext
|
|
using System;
|
||
|
|
using System.Linq;
|
||
|
|
|
||
|
|
namespace DTS.Serialization.IRIGCh10.Attributes
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// this class is used to annotate enums with a version attribute
|
||
|
|
/// for example each data packet type has a corresponding version number
|
||
|
|
/// example TABLE 10-7. DATA TYPE NAMES AND DESCRIPTIONS in chapter 10.pdf
|
||
|
|
/// </summary>
|
||
|
|
[AttributeUsage(AttributeTargets.All)]
|
||
|
|
public class DataTypeVersionValueAttribute : Attribute
|
||
|
|
{
|
||
|
|
/// <summary>Specifies the default value for the <see cref="DataTypeVersionValueAttribute" />, which is 0x00. This <see langword="static" /> field is read-only.</summary>
|
||
|
|
public static readonly DataTypeVersionValueAttribute Default = new DataTypeVersionValueAttribute();
|
||
|
|
private byte _dataTypeVersion;
|
||
|
|
|
||
|
|
/// <summary>Initializes a new instance of the <see cref="DataTypeVersionValueAttribute" /> class with no parameters.</summary>
|
||
|
|
public DataTypeVersionValueAttribute()
|
||
|
|
: this(0x00)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Initializes a new instance of the <see cref="DataTypeVersionValueAttribute" /> class with a DataTypeVersionValue.</summary>
|
||
|
|
/// <param name="dataTypeVersion">The data type version. </param>
|
||
|
|
public DataTypeVersionValueAttribute(byte dataTypeVersion)
|
||
|
|
{
|
||
|
|
_dataTypeVersion = dataTypeVersion;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Gets the data type version stored in this attribute.</summary>
|
||
|
|
/// <returns>The data type version stored in this attribute.</returns>
|
||
|
|
public virtual byte DataTypeVersion => DataTypeVersionValue;
|
||
|
|
|
||
|
|
/// <summary>Gets or sets the byte stored as the datatype version.</summary>
|
||
|
|
/// <returns>The byte stored as the data type version. The default value is 0x00.</returns>
|
||
|
|
protected byte DataTypeVersionValue
|
||
|
|
{
|
||
|
|
get => _dataTypeVersion;
|
||
|
|
set => _dataTypeVersion = value;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Returns whether the value of the given object is equal to the current <see cref="DataTypeVersionValueAttribute" />.</summary>
|
||
|
|
/// <param name="obj">The object to test the value equality of. </param>
|
||
|
|
/// <returns>
|
||
|
|
/// <see langword="true" /> if the value of the given object is equal to that of the current; otherwise, <see langword="false" />.</returns>
|
||
|
|
public override bool Equals(object obj)
|
||
|
|
{
|
||
|
|
if (obj == this)
|
||
|
|
return true;
|
||
|
|
if (obj is DataTypeVersionValueAttribute attribute)
|
||
|
|
return attribute.DataTypeVersion == DataTypeVersion;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Returns the hash code for this instance.</summary>
|
||
|
|
/// <returns>A 32-bit signed integer hash code.</returns>
|
||
|
|
public override int GetHashCode()
|
||
|
|
{
|
||
|
|
return DataTypeVersion.GetHashCode();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Returns a value indicating whether this is the default <see cref="DataTypeVersionValueAttribute" /> instance.</summary>
|
||
|
|
/// <returns>
|
||
|
|
/// <see langword="true" />, if this is the default <see cref="DataTypeVersionValueAttribute" /> instance; otherwise, <see langword="false" />.</returns>
|
||
|
|
public override bool IsDefaultAttribute()
|
||
|
|
{
|
||
|
|
return Equals(Default);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static byte GetDataTypeVersionValue(Enum value)
|
||
|
|
{
|
||
|
|
var fi = value.GetType().GetField(value.ToString());
|
||
|
|
var attributes = (DataTypeVersionValueAttribute[])fi.GetCustomAttributes(
|
||
|
|
typeof(DataTypeVersionValueAttribute), false);
|
||
|
|
if (attributes.Any())
|
||
|
|
{
|
||
|
|
return attributes.First().DataTypeVersionValue;
|
||
|
|
}
|
||
|
|
return Default.DataTypeVersionValue;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|