Skip to content

Instantly share code, notes, and snippets.

@LizzyFox-code
Created March 21, 2024 09:48
Show Gist options
  • Save LizzyFox-code/08989f156a2bf68b989864a29a6c3500 to your computer and use it in GitHub Desktop.
Save LizzyFox-code/08989f156a2bf68b989864a29a6c3500 to your computer and use it in GitHub Desktop.
Id template
[Il2CppSetOption(Option.DivideByZeroChecks, false)]
[StructLayout(LayoutKind.Sequential, Pack = 2)]
[DebuggerDisplay("Value = {Value}")]
public readonly struct SomeId : IEquatable<SomeId>
{
public static readonly SomeId Invalid = new SomeId(-1);
public static readonly SomeId Zero = new SomeId(0);
public readonly short Value;
public SomeId(short value)
{
Value = value;
}
public static bool operator ==(in SomeId a, in SomeId b)
{
return a.Value == b.Value;
}
public static bool operator !=(in SomeId a, in SomeId b)
{
return !(a == b);
}
public static SomeId operator ++(in SomeId id)
{
return new SomeId((short) (id.Value + 1));
}
public static SomeId operator --(in SomeId id)
{
return new SomeId((short) (id.Value - 1));
}
public static bool operator >=(in SomeId id, short value)
{
return id.Value >= value;
}
public static bool operator <=(in SomeId id, short value)
{
return id.Value <= value;
}
public static implicit operator short(SomeId id)
{
return id.Value;
}
public static explicit operator SomeId(short value)
{
return new SomeId(value);
}
public override string ToString()
{
return $"{Value.ToString()}";
}
public bool Equals(SomeId other)
{
return Value == other.Value;
}
public override bool Equals(object obj)
{
return obj is SomeId other && Equals(other);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment