using System;
using System.Linq;
namespace DTS.Serialization.IRIGCh10.Attributes
{
///
/// allows annotating an enum with a value,
/// see TABLE 10-7. DATA TYPE NAMES AND DESCRIPTIONS in Chapter 10 pdf
///
[AttributeUsage(AttributeTargets.All)]
public class PacketHeaderValueAttribute : Attribute
{
/// Specifies the default value for the , which is 0x00. This field is read-only.
public static readonly PacketHeaderValueAttribute Default = new PacketHeaderValueAttribute();
private byte _packetHeader;
/// Initializes a new instance of the class with no parameters.
public PacketHeaderValueAttribute()
: this(0x00)
{
}
/// Initializes a new instance of the class with a PacketHeader.
/// packet header value.
public PacketHeaderValueAttribute(byte packetHeader)
{
_packetHeader = packetHeader;
}
/// Gets the packet header value stored in this attribute.
/// The packet header value stored in this attribute.
public virtual byte PacketHeader => PacketHeaderValue;
/// Gets or sets the byte stored as the packet header value.
/// The byte stored as the packet header version value. The default value is 0x00.
protected byte PacketHeaderValue
{
get => _packetHeader;
set => _packetHeader = value;
}
/// Returns whether the value of the given object is equal to the current .
/// The object to test the value equality of.
///
/// if the value of the given object is equal to that of the current; otherwise, .
public override bool Equals(object obj)
{
if (obj == this)
return true;
if (obj is PacketHeaderValueAttribute packetHeaderAttribute)
return packetHeaderAttribute.PacketHeader == PacketHeader;
return false;
}
/// Returns the hash code for this instance.
/// A 32-bit signed integer hash code.
public override int GetHashCode()
{
return PacketHeader.GetHashCode();
}
/// Returns a value indicating whether this is the default instance.
///
/// , if this is the default instance; otherwise, .
public override bool IsDefaultAttribute()
{
return Equals(Default);
}
public static byte GetPacketHeaderValue(Enum value)
{
var fi = value.GetType().GetField(value.ToString());
var attributes = (PacketHeaderValueAttribute[])fi.GetCustomAttributes(
typeof(PacketHeaderValueAttribute), false);
if (attributes.Any())
{
return attributes.First().PacketHeaderValue;
}
return Default.PacketHeaderValue;
}
}
}