PkmnLibRSharp/PkmnLibRSharp/StaticData/TypeIdentifier.cs

64 lines
1.8 KiB
C#

using System;
using System.Runtime.InteropServices;
namespace PkmnLibSharp.StaticData
{
/// <summary>
/// An identifier for a type. This is stored as a single byte internally, but is represented as a struct so that
/// we can use the type system to ensure that we don't accidentally mix up type identifiers with other bytes.
/// </summary>
[StructLayout(LayoutKind.Explicit)]
public readonly struct TypeIdentifier : IEquatable<TypeIdentifier>
{
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
[FieldOffset(0)] private readonly byte _identifier;
/// <summary>
/// Creates a new type identifier.
/// </summary>
public TypeIdentifier(byte b)
{
_identifier = b;
}
/// <inheritdoc />
public bool Equals(TypeIdentifier other)
{
return _identifier == other._identifier;
}
/// <inheritdoc />
public override bool Equals(object? obj)
{
return obj is TypeIdentifier other && Equals(other);
}
/// <inheritdoc />
public override int GetHashCode()
{
return _identifier.GetHashCode();
}
/// <summary>
/// Equality operator for type identifiers.
/// </summary>
public static bool operator ==(TypeIdentifier left, TypeIdentifier right)
{
return left.Equals(right);
}
/// <summary>
/// Inequality operator for type identifiers.
/// </summary>
public static bool operator !=(TypeIdentifier left, TypeIdentifier right)
{
return !left.Equals(right);
}
/// <inheritdoc />
public override string ToString()
{
return $"Type({_identifier})";
}
}
}