using System.Runtime.CompilerServices;
namespace Brutal.Collections;
///
/// A pool-based (see: ) unique identifier for various entities in a system.
///
public readonly struct Name : IEquatable>
{
///
/// The underlying string value of the Name.
///
public readonly string Value;
///
/// Construct a Name from a string, using the shared pool for the type T.
///
public Name(string id)
: this(id, Namespace.Shared) { }
///
/// Construct a Name from a string, using the given pool.
///
public Name(string id, Namespace pool)
{
Value = pool.GetOrAdd(id);
}
///
/// Get the underlying string value of the Name.
/// public new string ToString() => Value;
///
/// Checks equality with another Name, based on reference equality of the underlying string.
///
public bool Equals(Name other)
{
return ReferenceEquals(Value, other.Value);
}
///
/// Checks equality with another object.
///
public override bool Equals(object? obj)
{
return obj is Name other && Equals(other);
}
///
/// Calculates the hash code for the Name (based on the reference of the Value).
///
public override int GetHashCode()
{
// Hash the reference of the Value for performance
return RuntimeHelpers.GetHashCode(Value);
}
/// Equality operator for Name.
public static bool operator ==(Name left, Name right)
{
return left.Equals(right);
}
/// Inequality operator for Name.
public static bool operator !=(Name left, Name right)
{
return !left.Equals(right);
}
/// Equality operator for Name and string.
public static bool operator ==(Name left, string right)
{
return left.Value == right;
}
/// Inequality operator for Name and string.
public static bool operator !=(Name left, string right)
{
return left.Value != right;
}
/// Equality operator for string and Name.
public static bool operator ==(string left, Name right)
{
return left == right.Value;
}
/// Inequality operator for string and Name.
public static bool operator !=(string left, Name right)
{
return left != right.Value;
}
///
/// Implicitly construct a Name from a string.
///
public static implicit operator Name(string id) => new(id);
///
/// Get the underlying string value of the Name.
///
public override string ToString() => Value;
}