| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | |
| | | 4 | | namespace NanoCLang.Entities { |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides a class for type parameters binding a type to a name. |
| | | 7 | | /// </summary> |
| | | 8 | | public class TypeParameter : Base, IEquatable<TypeParameter?> { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Creates a new type parameter instance binding the <paramref name="type"/> to the <paramref name="name"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="name">Name that the <paramref name="type"/> is bound to.</param> |
| | | 13 | | /// <param name="type">Type that is bound to the <paramref name="name"/>.</param> |
| | 142 | 14 | | public TypeParameter(string name, Type type) { |
| | 71 | 15 | | Name = name; |
| | 71 | 16 | | Type = type; |
| | 71 | 17 | | } |
| | | 18 | | /// <summary> |
| | | 19 | | /// Name that the <see cref="Type"/> is bound to. |
| | | 20 | | /// </summary> |
| | 274 | 21 | | public string Name { get; } |
| | | 22 | | /// <summary> |
| | | 23 | | /// Type that is bound to the <see cref="Name"/>. |
| | | 24 | | /// </summary> |
| | 261 | 25 | | public Type Type { get; } |
| | | 26 | | /// <inheritdoc/> |
| | 36 | 27 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 36 | 28 | | yield return Name; |
| | 72 | 29 | | if (args.SpaceBeforeTypeOperator) yield return " "; |
| | 36 | 30 | | yield return ":"; |
| | 72 | 31 | | if (args.SpaceAfterTypeOperator) yield return " "; |
| | 2280 | 32 | | foreach (var tk in Type.Tokens(args)) yield return tk; |
| | 36 | 33 | | } |
| | | 34 | | /// <inheritdoc/> |
| | 26 | 35 | | public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) { |
| | 171 | 36 | | foreach (var tk in Type.Tokens(args)) yield return tk; |
| | 26 | 37 | | yield return " "; |
| | 26 | 38 | | yield return Name; |
| | 26 | 39 | | } |
| | | 40 | | #region Equality checks |
| | | 41 | | /// <inheritdoc/> |
| | 0 | 42 | | public override bool Equals(object? obj) => Equals(obj as TypeParameter); |
| | | 43 | | /// <inheritdoc/> |
| | 36 | 44 | | public bool Equals(TypeParameter? other) => !(other is null) && Name == other.Name && EqualityComparer<Type>.Def |
| | | 45 | | /// <inheritdoc/> |
| | 0 | 46 | | public override int GetHashCode() => System.HashCode.Combine(Name, Type); |
| | | 47 | | /// <inheritdoc/> |
| | 0 | 48 | | public static bool operator ==(TypeParameter? left, TypeParameter? right) => EqualityComparer<TypeParameter?>.De |
| | | 49 | | /// <inheritdoc/> |
| | 0 | 50 | | public static bool operator !=(TypeParameter? left, TypeParameter? right) => !(left == right); |
| | | 51 | | #endregion |
| | | 52 | | } |
| | | 53 | | } |