mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2024-12-28 23:42:49 +01:00
116 lines
4.0 KiB
Plaintext
116 lines
4.0 KiB
Plaintext
<#@ template language="C#"#>
|
|
<#@ assembly name="System.Core" #>
|
|
<#@ import namespace="System.Collections.Generic" #>
|
|
<#@ output extension=".g.cs"#>
|
|
// =====================
|
|
// Auto generated file
|
|
// =====================
|
|
<#+
|
|
/// <summary>
|
|
/// A model representing the info on an enumerable type
|
|
/// </summary>
|
|
sealed class EnumerableTypeInfo
|
|
{
|
|
public EnumerableTypeInfo(
|
|
string type,
|
|
string xmlType,
|
|
string name,
|
|
string size,
|
|
string destinationType,
|
|
string cast)
|
|
{
|
|
Type = type;
|
|
XmlType = xmlType;
|
|
Name = name;
|
|
Size = size;
|
|
DestinationType = destinationType;
|
|
Cast = cast;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the name of the current type
|
|
/// </summary>
|
|
public string Type { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the XML-formatted name of the current type (eg. with {T} instead of <T>)
|
|
/// </summary>
|
|
public string XmlType { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the variable name to use
|
|
/// </summary>
|
|
public string Name { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the name of the property to use to retrieve the length of the current type
|
|
/// </summary>
|
|
public string Size { get; }
|
|
|
|
/// <summary>
|
|
/// Gets whether or not the current type has a "Count" property
|
|
/// </summary>
|
|
public bool HasCountProperty => Size == "Count";
|
|
|
|
/// <summary>
|
|
/// Gets the name of the destination type, when comparing counts across different collections
|
|
/// </summary>
|
|
public string DestinationType { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the (optional) casting to resolve the diamond problem between different interfaces
|
|
/// </summary>
|
|
public string Cast { get; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the list of available enumerable types to generate APIs for
|
|
/// </summary>
|
|
static readonly IReadOnlyList<EnumerableTypeInfo> EnumerableTypes = new[]
|
|
{
|
|
new EnumerableTypeInfo("Span<T>", "<see cref=\"Span{T}\"/>", "span", "Length", "Span<T>", ""),
|
|
new EnumerableTypeInfo("ReadOnlySpan<T>", "<see cref=\"ReadOnlySpan{T}\"/>", "span", "Length", "Span<T>", ""),
|
|
new EnumerableTypeInfo("Memory<T>", "<see cref=\"Memory{T}\"/>", "memory", "Length", "Memory<T>", ""),
|
|
new EnumerableTypeInfo("ReadOnlyMemory<T>", "<see cref=\"ReadOnlyMemory{T}\"/>", "memory", "Length", "Memory<T>", ""),
|
|
new EnumerableTypeInfo("T[]", "<see typeparamref=\"T\"/> array", "array", "Length", "T[]", ""),
|
|
new EnumerableTypeInfo("List<T>", "<see cref=\"List{T}\"/>", "list", "Count", "List<T>", "(ICollection<T>)"),
|
|
new EnumerableTypeInfo("ICollection<T>", "<see cref=\"ICollection{T}\"/>", "collection", "Count", "ICollection<T>", ""),
|
|
new EnumerableTypeInfo("IReadOnlyCollection<T>", "<see cref=\"IReadOnlyCollection{T}\"/>", "collection", "Count", "ICollection<T>", ""),
|
|
};
|
|
|
|
/// <summary>
|
|
/// Gets the list of available numeric types to generate APIs for
|
|
/// </summary>
|
|
static readonly IReadOnlyList<(string Name, string Prefix)> NumericTypes = new[]
|
|
{
|
|
("byte", "cref"),
|
|
("sbyte", "cref"),
|
|
("short", "cref"),
|
|
("ushort", "cref"),
|
|
("char", "cref"),
|
|
("int", "cref"),
|
|
("uint", "cref"),
|
|
("float", "cref"),
|
|
("long", "cref"),
|
|
("ulong", "cref"),
|
|
("double", "cref"),
|
|
("decimal", "cref"),
|
|
("nint", "langword"),
|
|
("nuint", "langword")
|
|
};
|
|
|
|
/// <summary>
|
|
/// Generates text for a given sequence of items, automatically adding the necessary spacing
|
|
/// </summary>
|
|
void GenerateTextForItems<T>(IReadOnlyList<T> items, Action<T> factory)
|
|
{
|
|
for (int i = 0; i < items.Count; i++)
|
|
{
|
|
// Insert a blank line after the first item
|
|
if (i > 0) WriteLine("");
|
|
|
|
// Invoke the factory with the current item
|
|
factory(items [i]);
|
|
}
|
|
}
|
|
#> |