This commit is contained in:
melekhin
2026-06-05 12:13:35 +07:00
parent 96bbbbd292
commit 4ff3cc7042
30 changed files with 1875 additions and 1478 deletions

View File

@@ -0,0 +1,638 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using System.Text;
namespace G;
[Generator]
public class ComplexUnitGenerator : IIncrementalGenerator
{
private const string AttributeName = "ComplexUnitGenerator";
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// 1. Генерируем определение атрибута
context.RegisterPostInitializationOutput(ctx =>
{
string attributeSource = @"
namespace QWERTYkez.Mensura
{
[System.AttributeUsage(System.AttributeTargets.Struct, AllowMultiple = false)]
public sealed class ComplexUnitGeneratorAttribute : System.Attribute
{
public string TypeNameA { get; }
public string TypeNameB { get; }
public string TypeNameZ { get; }
public ComplexUnitGeneratorAttribute(string typeNameA, string typeNameB, string typeNameZ)
{
TypeNameA = typeNameA;
TypeNameB = typeNameB;
TypeNameZ = typeNameZ;
}
}
}";
ctx.AddSource(".ComplexUnitGeneratorAttribute.g.cs", SourceText.From(attributeSource, Encoding.UTF8));
});
// 2. Ищем все readonly partial record struct с этим атрибутом
var structsProvider = context.SyntaxProvider
.CreateSyntaxProvider(
predicate: static (node, _) => IsTargetStruct(node),
transform: static (ctx, _) => GetStructInfo(ctx))
.Where(info => info.HasValue)
.Select((info, _) => info!.Value)
.Collect();
// 3. Регистрируем вывод для каждой найденной структуры
context.RegisterSourceOutput(structsProvider, (spc, structs) =>
{
foreach (var structInfo in structs)
{
string generatedCode = GeneratePartial(structInfo.TypeName, structInfo.Namespace,
structInfo.TypeNameA, structInfo.TypeNameB, structInfo.TypeNameZ);
spc.AddSource($"{structInfo.TypeName}.g.cs", SourceText.From(generatedCode, Encoding.UTF8));
}
});
}
private static bool IsTargetStruct(SyntaxNode node)
{
if (node is not RecordDeclarationSyntax record)
return false;
// Должен быть record struct с модификаторами readonly и partial
if (!record.Modifiers.Any(SyntaxKind.ReadOnlyKeyword) ||
!record.Modifiers.Any(SyntaxKind.PartialKeyword) ||
!record.Keyword.IsKind(SyntaxKind.RecordKeyword) ||
!record.ClassOrStructKeyword.IsKind(SyntaxKind.StructKeyword))
return false;
// Проверяем наличие атрибута [ComplexUnitGenerator] или [ComplexUnitGeneratorAttribute]
foreach (var attrList in record.AttributeLists)
foreach (var attr in attrList.Attributes)
{
string name = attr.Name.ToString();
if (name == AttributeName || name == AttributeName + "Attribute")
return true;
}
return false;
}
private static StructInfo? GetStructInfo(GeneratorSyntaxContext context)
{
var record = (RecordDeclarationSyntax)context.Node;
var semanticModel = context.SemanticModel;
if (semanticModel.GetDeclaredSymbol(record) is not INamedTypeSymbol typeSymbol)
return null;
string namespaceName = typeSymbol.ContainingNamespace?.ToString() ?? "";
if (string.IsNullOrEmpty(namespaceName))
return null;
// Извлекаем аргументы атрибута
string typeNameA = null;
string typeNameB = null;
string typeNameZ = null;
foreach (var attrList in record.AttributeLists)
foreach (var attr in attrList.Attributes)
{
string name = attr.Name.ToString();
if (name == AttributeName || name == AttributeName + "Attribute")
{
// Ищем аргументы конструктора
var args = attr.ArgumentList?.Arguments;
if (args.HasValue && args.Value.Count >= 3)
{
typeNameA = GetStringFromExpression(args.Value[0].Expression, semanticModel);
typeNameB = GetStringFromExpression(args.Value[1].Expression, semanticModel);
typeNameZ = GetStringFromExpression(args.Value[2].Expression, semanticModel);
}
break;
}
}
if (typeNameA == null || typeNameB == null || typeNameZ == null)
return null;
return new StructInfo(namespaceName, typeSymbol.Name, typeNameA, typeNameB, typeNameZ);
}
private static string GetStringFromExpression(ExpressionSyntax expr, SemanticModel semanticModel)
{
// Поддерживаем строковые литералы и константы
if (expr is LiteralExpressionSyntax literal && literal.IsKind(SyntaxKind.StringLiteralExpression))
return literal.Token.ValueText;
// Если это доступ к константе, пытаемся вычислить через семантику
var constantValue = semanticModel.GetConstantValue(expr);
if (constantValue.HasValue && constantValue.Value is string s)
return s;
return null;
}
private static string GeneratePartial(string typeName, string ns, string typeNameA, string typeNameB, string typeNameZ)
{
// Шаблон, который вы вставите сами.
// В нём используйте {typeName}, {ns}, {typeNameA}, {typeNameB}, {typeNameZ}
// Например:
string template = @"
global using {typeNameZ}Extensions = QWERTYkez.Mensura.Units.Complex.{typeNameZ}Extensions;
global using {typeNameZ}Converter = QWERTYkez.Mensura.Units.Complex.{typeNameZ}Converter;
global using {typeNameZ} = QWERTYkez.Mensura.Units.Complex.{typeNameZ};
using System.Globalization;
using System.Runtime.Serialization;
namespace QWERTYkez.Mensura.Units
{
public readonly partial record struct {typeNameB}
{
public static {typeNameZ} operator /({typeNameA} left, {typeNameB} right) => new(left._Value / right._Value);
public static {typeNameZ} operator /({typeNameA}? left, {typeNameB} right) => new(left.Protected() / right._Value);
public static {typeNameZ} operator /({typeNameA} left, {typeNameB}? right) => new(left._Value / right.Protected());
public static {typeNameZ} operator /({typeNameA}? left, {typeNameB}? right) => new(left.Protected() / right.Protected());
// === Array ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}[] operator
*({typeNameZ}[] units, {typeNameB} multiplicator) => units.Multiply<{typeNameZ}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}?[] operator
*({typeNameZ}?[] units, {typeNameB} multiplicator) => units.Multiply<{typeNameZ}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}[] operator
*({typeNameB} multiplicator, {typeNameZ}[] units) => units.Multiply<{typeNameZ}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}?[] operator
*({typeNameB} multiplicator, {typeNameZ}?[] units) => units.Multiply<{typeNameZ}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}[] operator *({typeNameZ}[] units, {typeNameB}? multiplicator) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new {typeNameA}[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}?[] operator *({typeNameZ}?[] units, {typeNameB}? multiplicator) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new {typeNameA}?[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}[] operator *({typeNameB}? multiplicator, {typeNameZ}[] units) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new {typeNameA}[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}?[] operator *({typeNameB}? multiplicator, {typeNameZ}?[] units) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new {typeNameA}?[units.Length]);
// === List<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}> operator
*(List<{typeNameZ}> units, {typeNameB} multiplicator) => units.Multiply<{typeNameZ}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}?> operator
*(List<{typeNameZ}?> units, {typeNameB} multiplicator) => units.Multiply<{typeNameZ}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}> operator
*({typeNameB} multiplicator, List<{typeNameZ}> units) => units.Multiply<{typeNameZ}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}?> operator
*({typeNameB} multiplicator, List<{typeNameZ}?> units) => units.Multiply<{typeNameZ}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}> operator *(List<{typeNameZ}> units, {typeNameB}? multiplicator) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List<{typeNameA}>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}?> operator *(List<{typeNameZ}?> units, {typeNameB}? multiplicator) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List<{typeNameA}?>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}> operator *({typeNameB}? multiplicator, List<{typeNameZ}> units) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List<{typeNameA}>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}?> operator *({typeNameB}? multiplicator, List<{typeNameZ}?> units) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List<{typeNameA}?>(units.Count));
// === IEnumerable<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}> operator
*(IEnumerable<{typeNameZ}> units, {typeNameB} multiplicator) => units.Multiply<{typeNameZ}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}?> operator
*(IEnumerable<{typeNameZ}?> units, {typeNameB} multiplicator) => units.Multiply<{typeNameZ}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}> operator
*({typeNameB} multiplicator, IEnumerable<{typeNameZ}> units) => units.Multiply<{typeNameZ}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}?> operator
*({typeNameB} multiplicator, IEnumerable<{typeNameZ}?> units) => units.Multiply<{typeNameZ}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}> operator *(IEnumerable<{typeNameZ}> units, {typeNameB}? multiplicator) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => new {typeNameA}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}?> operator *(IEnumerable<{typeNameZ}?> units, {typeNameB}? multiplicator) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => u is null ? ({typeNameA}?)null : new {typeNameA}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}> operator *({typeNameB}? multiplicator, IEnumerable<{typeNameZ}> units) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => new {typeNameA}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}?> operator *({typeNameB}? multiplicator, IEnumerable<{typeNameZ}?> units) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => u is null ? ({typeNameA}?)null : new {typeNameA}(0d)));
// === Array ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}[] operator
/({typeNameA}[] units, {typeNameB} divisor) => units.Divide<{typeNameA}, {typeNameZ}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}?[] operator
/({typeNameA}?[] units, {typeNameB} divisor) => units.Divide<{typeNameA}, {typeNameZ}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}[] operator
/({typeNameB} dividend, {typeNameA}[] units) => dividend._Value.Divide<{typeNameA}, {typeNameZ}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}?[] operator
/({typeNameB} dividend, {typeNameA}?[] units) => dividend._Value.Divide<{typeNameA}, {typeNameZ}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}[] operator /({typeNameA}[] units, {typeNameB}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new {typeNameZ}[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}?[] operator /({typeNameA}?[] units, {typeNameB}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new {typeNameZ}?[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}[] operator /({typeNameB}? dividend, {typeNameA}[] units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new {typeNameZ}[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}?[] operator /({typeNameB}? dividend, {typeNameA}?[] units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new {typeNameZ}?[units.Length]);
// === List<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}> operator
/(List<{typeNameA}> units, {typeNameB} divisor) => units.Divide<{typeNameA}, {typeNameZ}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}?> operator
/(List<{typeNameA}?> units, {typeNameB} divisor) => units.Divide<{typeNameA}, {typeNameZ}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}> operator
/({typeNameB} dividend, List<{typeNameA}> units) => dividend._Value.Divide<{typeNameA}, {typeNameZ}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}?> operator
/({typeNameB} dividend, List<{typeNameA}?> units) => dividend._Value.Divide<{typeNameA}, {typeNameZ}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}> operator /(List<{typeNameA}> units, {typeNameB}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new List<{typeNameZ}>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}?> operator /(List<{typeNameA}?> units, {typeNameB}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new List<{typeNameZ}?>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}> operator /({typeNameB}? dividend, List<{typeNameA}> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new List<{typeNameZ}>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}?> operator /({typeNameB}? dividend, List<{typeNameA}?> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new List<{typeNameZ}?>(units.Count));
// === IEnumerable<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}> operator
/(IEnumerable<{typeNameA}> units, {typeNameB} divisor) => units.Divide<{typeNameA}, {typeNameZ}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}?> operator
/(IEnumerable<{typeNameA}?> units, {typeNameB} divisor) => units.Divide<{typeNameA}, {typeNameZ}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}> operator
/({typeNameB} dividend, IEnumerable<{typeNameA}> units) => dividend._Value.Divide<{typeNameA}, {typeNameZ}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}?> operator
/({typeNameB} dividend, IEnumerable<{typeNameA}?> units) => dividend._Value.Divide<{typeNameA}, {typeNameZ}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}> operator /(IEnumerable<{typeNameA}> units, {typeNameB}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : units.Select(u => new {typeNameZ}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}?> operator /(IEnumerable<{typeNameA}?> units, {typeNameB}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : units.Select(u => u is null ? ({typeNameZ}?)null : new {typeNameZ}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}> operator /({typeNameB}? dividend, IEnumerable<{typeNameA}> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : units.Select(u => new {typeNameZ}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}?> operator /({typeNameB}? dividend, IEnumerable<{typeNameA}?> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : units.Select(u => u is null ? ({typeNameZ}?)null : new {typeNameZ}(0d)));
}
public readonly partial record struct {typeNameA}
{
// === Array ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}[] operator
/({typeNameZ}[] units, {typeNameA} divisor) => units.Divide<{typeNameZ}, {typeNameB}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}?[] operator
/({typeNameZ}?[] units, {typeNameA} divisor) => units.Divide<{typeNameZ}, {typeNameB}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}[] operator
/({typeNameA} dividend, {typeNameZ}[] units) => dividend._Value.Divide<{typeNameZ}, {typeNameB}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}?[] operator
/({typeNameA} dividend, {typeNameZ}?[] units) => dividend._Value.Divide<{typeNameZ}, {typeNameB}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}[] operator /({typeNameZ}[] units, {typeNameA}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new {typeNameB}[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}?[] operator /({typeNameZ}?[] units, {typeNameA}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new {typeNameB}?[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}[] operator /({typeNameA}? dividend, {typeNameZ}[] units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new {typeNameB}[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}?[] operator /({typeNameA}? dividend, {typeNameZ}?[] units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new {typeNameB}?[units.Length]);
// === List<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}> operator
/(List<{typeNameZ}> units, {typeNameA} divisor) => units.Divide<{typeNameZ}, {typeNameB}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}?> operator
/(List<{typeNameZ}?> units, {typeNameA} divisor) => units.Divide<{typeNameZ}, {typeNameB}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}> operator
/({typeNameA} dividend, List<{typeNameZ}> units) => dividend._Value.Divide<{typeNameZ}, {typeNameB}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}?> operator
/({typeNameA} dividend, List<{typeNameZ}?> units) => dividend._Value.Divide<{typeNameZ}, {typeNameB}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}> operator /(List<{typeNameZ}> units, {typeNameA}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new List<{typeNameB}>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}?> operator /(List<{typeNameZ}?> units, {typeNameA}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new List<{typeNameB}?>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}> operator /({typeNameA}? dividend, List<{typeNameZ}> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new List<{typeNameB}>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}?> operator /({typeNameA}? dividend, List<{typeNameZ}?> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new List<{typeNameB}?>(units.Count));
// === IEnumerable<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}> operator
/(IEnumerable<{typeNameZ}> units, {typeNameA} divisor) => units.Divide<{typeNameZ}, {typeNameB}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}?> operator
/(IEnumerable<{typeNameZ}?> units, {typeNameA} divisor) => units.Divide<{typeNameZ}, {typeNameB}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}> operator
/({typeNameA} dividend, IEnumerable<{typeNameZ}> units) => dividend._Value.Divide<{typeNameZ}, {typeNameB}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}?> operator
/({typeNameA} dividend, IEnumerable<{typeNameZ}?> units) => dividend._Value.Divide<{typeNameZ}, {typeNameB}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}> operator /(IEnumerable<{typeNameZ}> units, {typeNameA}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : units.Select(u => new {typeNameB}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}?> operator /(IEnumerable<{typeNameZ}?> units, {typeNameA}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : units.Select(u => u is null ? ({typeNameB}?)null : new {typeNameB}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}> operator /({typeNameA}? dividend, IEnumerable<{typeNameZ}> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : units.Select(u => new {typeNameB}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}?> operator /({typeNameA}? dividend, IEnumerable<{typeNameZ}?> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : units.Select(u => u is null ? ({typeNameB}?)null : new {typeNameB}(0d)));
// === Array ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}[] operator
/({typeNameB}[] units, {typeNameA} divisor) => units.Divide<{typeNameB}, {typeNameZ}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}?[] operator
/({typeNameB}?[] units, {typeNameA} divisor) => units.Divide<{typeNameB}, {typeNameZ}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}[] operator
/({typeNameA} dividend, {typeNameB}[] units) => dividend._Value.Divide<{typeNameB}, {typeNameZ}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}?[] operator
/({typeNameA} dividend, {typeNameB}?[] units) => dividend._Value.Divide<{typeNameB}, {typeNameZ}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}[] operator /({typeNameB}[] units, {typeNameA}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new {typeNameZ}[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}?[] operator /({typeNameB}?[] units, {typeNameA}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new {typeNameZ}?[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}[] operator /({typeNameA}? dividend, {typeNameB}[] units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new {typeNameZ}[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameZ}?[] operator /({typeNameA}? dividend, {typeNameB}?[] units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new {typeNameZ}?[units.Length]);
// === List<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}> operator
/(List<{typeNameB}> units, {typeNameA} divisor) => units.Divide<{typeNameB}, {typeNameZ}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}?> operator
/(List<{typeNameB}?> units, {typeNameA} divisor) => units.Divide<{typeNameB}, {typeNameZ}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}> operator
/({typeNameA} dividend, List<{typeNameB}> units) => dividend._Value.Divide<{typeNameB}, {typeNameZ}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}?> operator
/({typeNameA} dividend, List<{typeNameB}?> units) => dividend._Value.Divide<{typeNameB}, {typeNameZ}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}> operator /(List<{typeNameB}> units, {typeNameA}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new List<{typeNameZ}>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}?> operator /(List<{typeNameB}?> units, {typeNameA}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new List<{typeNameZ}?>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}> operator /({typeNameA}? dividend, List<{typeNameB}> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new List<{typeNameZ}>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameZ}?> operator /({typeNameA}? dividend, List<{typeNameB}?> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new List<{typeNameZ}?>(units.Count));
// === IEnumerable<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}> operator
/(IEnumerable<{typeNameB}> units, {typeNameA} divisor) => units.Divide<{typeNameB}, {typeNameZ}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}?> operator
/(IEnumerable<{typeNameB}?> units, {typeNameA} divisor) => units.Divide<{typeNameB}, {typeNameZ}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}> operator
/({typeNameA} dividend, IEnumerable<{typeNameB}> units) => dividend._Value.Divide<{typeNameB}, {typeNameZ}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}?> operator
/({typeNameA} dividend, IEnumerable<{typeNameB}?> units) => dividend._Value.Divide<{typeNameB}, {typeNameZ}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}> operator /(IEnumerable<{typeNameB}> units, {typeNameA}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : units.Select(u => new {typeNameZ}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}?> operator /(IEnumerable<{typeNameB}?> units, {typeNameA}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : units.Select(u => u is null ? ({typeNameZ}?)null : new {typeNameZ}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}> operator /({typeNameA}? dividend, IEnumerable<{typeNameB}> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : units.Select(u => new {typeNameZ}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameZ}?> operator /({typeNameA}? dividend, IEnumerable<{typeNameB}?> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : units.Select(u => u is null ? ({typeNameZ}?)null : new {typeNameZ}(0d)));
}
}
namespace QWERTYkez.Mensura.Units.Complex
{
[JsonConverter(typeof({typeNameZ}Converter))]
public readonly partial record struct {typeNameZ} : IMensuraUnit<{typeNameZ}>, IEquatable<{typeNameZ}>, IMensuraUnit
{
[JsonInclude, DataMember, JsonPropertyName(""v""), Obsolete] // для JSON / EF на случай сбоев, если пробелма с _Value
internal double Value { get => _Value; init => _Value = value; }
internal readonly double _Value;
internal {typeNameZ}(double value) => _Value = value;
[NotMapped, JsonIgnore] internal {typeNameA} PerValue
{ get => ({typeNameA})_Value; init => _Value = (double)value; }
[JsonIgnore, IgnoreDataMember] public bool IsPositive => _Value >= 0;
[JsonIgnore, IgnoreDataMember] public bool IsGreaterThanZero => _Value > 0;
[JsonIgnore, IgnoreDataMember] public bool IsNegative => double.IsNegative(_Value);
[JsonIgnore, IgnoreDataMember] public bool IsZero => _Value == 0;
[JsonIgnore, IgnoreDataMember] public bool IsNaN => double.IsNaN(_Value);
[JsonIgnore, IgnoreDataMember] public bool IsFinite => double.IsFinite(_Value);
[JsonIgnore, IgnoreDataMember] public bool IsInfinity => double.IsInfinity(_Value);
[JsonIgnore, IgnoreDataMember] public bool IsPositiveInfinity => double.IsPositiveInfinity(_Value);
[JsonIgnore, IgnoreDataMember] public bool IsNegativeInfinity => double.IsNegativeInfinity(_Value);
public static {typeNameA} operator *({typeNameZ} left, {typeNameB} right) => new(left._Value * right._Value);
public static {typeNameA} operator *({typeNameZ}? left, {typeNameB} right) => new(left.Protected() * right._Value);
public static {typeNameA} operator *({typeNameZ} left, {typeNameB}? right) => new(left._Value * right.Protected());
public static {typeNameA} operator *({typeNameZ}? left, {typeNameB}? right) => new(left.Protected() * right.Protected());
public static {typeNameA} operator *({typeNameB} left, {typeNameZ} right) => new(right._Value * left._Value);
public static {typeNameA} operator *({typeNameB}? left, {typeNameZ} right) => new(right._Value * left.Protected());
public static {typeNameA} operator *({typeNameB} left, {typeNameZ}? right) => new(right.Protected() * left._Value);
public static {typeNameA} operator *({typeNameB}? left, {typeNameZ}? right) => new(right.Protected() * left.Protected());
public static {typeNameB} operator /({typeNameA} left, {typeNameZ} right) => new(left._Value / right._Value);
public static {typeNameB} operator /({typeNameA}? left, {typeNameZ} right) => new(left.Protected() / right._Value);
public static {typeNameB} operator /({typeNameA} left, {typeNameZ}? right) => new(left._Value / right.Protected());
public static {typeNameB} operator /({typeNameA}? left, {typeNameZ}? right) => new(left.Protected() / right.Protected());
// === Array ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}[] operator
*({typeNameB}[] units, {typeNameZ} multiplicator) => units.Multiply<{typeNameB}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}?[] operator
*({typeNameB}?[] units, {typeNameZ} multiplicator) => units.Multiply<{typeNameB}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}[] operator
*({typeNameZ} multiplicator, {typeNameB}[] units) => units.Multiply<{typeNameB}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}?[] operator
*({typeNameZ} multiplicator, {typeNameB}?[] units) => units.Multiply<{typeNameB}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}[] operator *({typeNameB}[] units, {typeNameZ}? multiplicator) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new {typeNameA}[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}?[] operator *({typeNameB}?[] units, {typeNameZ}? multiplicator) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new {typeNameA}?[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}[] operator *({typeNameZ}? multiplicator, {typeNameB}[] units) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new {typeNameA}[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameA}?[] operator *({typeNameZ}? multiplicator, {typeNameB}?[] units) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new {typeNameA}?[units.Length]);
// === List<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}> operator
*(List<{typeNameB}> units, {typeNameZ} multiplicator) => units.Multiply<{typeNameB}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}?> operator
*(List<{typeNameB}?> units, {typeNameZ} multiplicator) => units.Multiply<{typeNameB}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}> operator
*({typeNameZ} multiplicator, List<{typeNameB}> units) => units.Multiply<{typeNameB}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}?> operator
*({typeNameZ} multiplicator, List<{typeNameB}?> units) => units.Multiply<{typeNameB}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}> operator *(List<{typeNameB}> units, {typeNameZ}? multiplicator) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List<{typeNameA}>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}?> operator *(List<{typeNameB}?> units, {typeNameZ}? multiplicator) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List<{typeNameA}?>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}> operator *({typeNameZ}? multiplicator, List<{typeNameB}> units) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List<{typeNameA}>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameA}?> operator *({typeNameZ}? multiplicator, List<{typeNameB}?> units) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List<{typeNameA}?>(units.Count));
// === IEnumerable<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}> operator
*(IEnumerable<{typeNameB}> units, {typeNameZ} multiplicator) => units.Multiply<{typeNameB}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}?> operator
*(IEnumerable<{typeNameB}?> units, {typeNameZ} multiplicator) => units.Multiply<{typeNameB}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}> operator
*({typeNameZ} multiplicator, IEnumerable<{typeNameB}> units) => units.Multiply<{typeNameB}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}?> operator
*({typeNameZ} multiplicator, IEnumerable<{typeNameB}?> units) => units.Multiply<{typeNameB}, {typeNameA}>(multiplicator._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}> operator *(IEnumerable<{typeNameB}> units, {typeNameZ}? multiplicator) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => new {typeNameA}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}?> operator *(IEnumerable<{typeNameB}?> units, {typeNameZ}? multiplicator) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => u is null ? ({typeNameA}?)null : new {typeNameA}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}> operator *({typeNameZ}? multiplicator, IEnumerable<{typeNameB}> units) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => new {typeNameA}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameA}?> operator *({typeNameZ}? multiplicator, IEnumerable<{typeNameB}?> units) =>
multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => u is null ? ({typeNameA}?)null : new {typeNameA}(0d)));
// === Array ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}[] operator
/({typeNameA}[] units, {typeNameZ} divisor) => units.Divide<{typeNameA}, {typeNameB}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}?[] operator
/({typeNameA}?[] units, {typeNameZ} divisor) => units.Divide<{typeNameA}, {typeNameB}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}[] operator
/({typeNameZ} dividend, {typeNameA}[] units) => dividend._Value.Divide<{typeNameA}, {typeNameB}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}?[] operator
/({typeNameZ} dividend, {typeNameA}?[] units) => dividend._Value.Divide<{typeNameA}, {typeNameB}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}[] operator /({typeNameA}[] units, {typeNameZ}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new {typeNameB}[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}?[] operator /({typeNameA}?[] units, {typeNameZ}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new {typeNameB}?[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}[] operator /({typeNameZ}? dividend, {typeNameA}[] units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new {typeNameB}[units.Length]);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static {typeNameB}?[] operator /({typeNameZ}? dividend, {typeNameA}?[] units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new {typeNameB}?[units.Length]);
// === List<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}> operator
/(List<{typeNameA}> units, {typeNameZ} divisor) => units.Divide<{typeNameA}, {typeNameB}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}?> operator
/(List<{typeNameA}?> units, {typeNameZ} divisor) => units.Divide<{typeNameA}, {typeNameB}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}> operator
/({typeNameZ} dividend, List<{typeNameA}> units) => dividend._Value.Divide<{typeNameA}, {typeNameB}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}?> operator
/({typeNameZ} dividend, List<{typeNameA}?> units) => dividend._Value.Divide<{typeNameA}, {typeNameB}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}> operator /(List<{typeNameA}> units, {typeNameZ}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new List<{typeNameB}>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}?> operator /(List<{typeNameA}?> units, {typeNameZ}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : new List<{typeNameB}?>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}> operator /({typeNameZ}? dividend, List<{typeNameA}> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new List<{typeNameB}>(units.Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<{typeNameB}?> operator /({typeNameZ}? dividend, List<{typeNameA}?> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : new List<{typeNameB}?>(units.Count));
// === IEnumerable<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}> operator
/(IEnumerable<{typeNameA}> units, {typeNameZ} divisor) => units.Divide<{typeNameA}, {typeNameB}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}?> operator
/(IEnumerable<{typeNameA}?> units, {typeNameZ} divisor) => units.Divide<{typeNameA}, {typeNameB}>(divisor._Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}> operator
/({typeNameZ} dividend, IEnumerable<{typeNameA}> units) => dividend._Value.Divide<{typeNameA}, {typeNameB}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}?> operator
/({typeNameZ} dividend, IEnumerable<{typeNameA}?> units) => dividend._Value.Divide<{typeNameA}, {typeNameB}>(units);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}> operator /(IEnumerable<{typeNameA}> units, {typeNameZ}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : units.Select(u => new {typeNameB}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}?> operator /(IEnumerable<{typeNameA}?> units, {typeNameZ}? divisor) =>
divisor.HasValue ? units / divisor.Value : (units is null ? null! : units.Select(u => u is null ? ({typeNameB}?)null : new {typeNameB}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}> operator /({typeNameZ}? dividend, IEnumerable<{typeNameA}> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : units.Select(u => new {typeNameB}(0d)));
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<{typeNameB}?> operator /({typeNameZ}? dividend, IEnumerable<{typeNameA}?> units) =>
dividend.HasValue ? units / dividend.Value : (units is null ? null! : units.Select(u => u is null ? ({typeNameB}?)null : new {typeNameB}(0d)));
}
internal static class {typeNameZ}Extensions
{
public static double Protected(this {typeNameZ}? unit) => unit is null ? 0d : unit.Value._Value;
internal static double ToDouble(this {typeNameB}? unit) => unit?._Value ?? 0d;
}
public class {typeNameZ}Converter : JsonConverter<{typeNameZ}>
{
// Используем инвариантную культуру, чтобы разделителем всегда была точка (10.5, а не 10,5)
private static readonly CultureInfo Culture = CultureInfo.InvariantCulture;
public override {typeNameZ} Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
double double_Value;
if (reader.TokenType == JsonTokenType.String)
{
// Безопасно парсим double из строки с поддержкой точки как разделителя
if (!double.TryParse(reader.GetString(), NumberStyles.Float, Culture, out double_Value))
{
throw new JsonException($""Не удалось преобразовать строковое значение в double для метрики {nameof({typeNameZ})}."");
}
}
else
{
// Прямое быстрое чтение числа из JSON
double_Value = reader.GetDouble();
}
return new(double_Value);
}
public override void Write(Utf8JsonWriter writer, {typeNameZ} value, JsonSerializerOptions options)
{
// Записываем число напрямую в байтовый буфер без выделения памяти под строки
writer.WriteNumberValue(value._Value);
}
public override void WriteAsPropertyName(Utf8JsonWriter writer, {typeNameZ} value, JsonSerializerOptions options)
{
// Ключи JSON-объектов всегда должны быть строками.
// Форматируем double в строку с точкой, чтобы другие сервисы экосистемы прочитали её корректно.
// Формат ""R"" (Round-trip) гарантирует, что число не потеряет точность при обратном парсинге.
writer.WritePropertyName(value._Value.ToString(""R"", Culture));
}
public override {typeNameZ} ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string propertyName = reader.GetString()!;
if (!double.TryParse(propertyName, NumberStyles.Float, Culture, out double double_Value))
{
throw new JsonException($""Невалидное числовое значение в ключе свойства JSON: '{propertyName}' для метрики {nameof({typeNameZ})}."");
}
return new(double_Value);
}
}
}
";
return template
.Replace("{ns}", ns)
.Replace("{typeName}", typeName)
.Replace("{typeNameA}", typeNameA)
.Replace("{typeNameB}", typeNameB)
.Replace("{typeNameZ}", typeNameZ);
}
private readonly struct StructInfo(string ns, string name, string a, string b, string z)
{
public string Namespace { get; } = ns;
public string TypeName { get; } = name;
public string TypeNameA { get; } = a;
public string TypeNameB { get; } = b;
public string TypeNameZ { get; } = z;
}
}

View File

@@ -1,226 +1,195 @@
//using Microsoft.CodeAnalysis;
//using Microsoft.CodeAnalysis.CSharp.Syntax;
//using Microsoft.CodeAnalysis.Text;
//using System.Collections.Generic;
//using System.Collections.Immutable;
//using System.Linq;
//using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
//namespace QWERTYkez.Mensura.Generator;
namespace G;
//[Generator(LanguageNames.CSharp)]
//internal class CollectionsOperatorsGenerator : IIncrementalGenerator
//{
// private const string AttributeShortName = "CollectionsOperatorsGenerator";
// private const string AttributeFullName = AttributeShortName + "Attribute";
[Generator(LanguageNames.CSharp)]
public class CollectionsOperatorsGenerator : IIncrementalGenerator
{
private const string AttributeShortName = "CollectionsOperatorsGenerator";
private const string AttributeFullName = AttributeShortName + "Attribute";
// private const string AttributeSource = @"namespace QWERTYkez.Mensura;
private const string AttributeSource = @"namespace QWERTYkez.Mensura;
//[System.AttributeUsage(System.AttributeTargets.Struct | System.AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
//internal sealed class CollectionsOperatorsGeneratorAttribute : System.Attribute { }";
[System.AttributeUsage(System.AttributeTargets.Struct | System.AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
internal sealed class CollectionsOperatorsGeneratorAttribute : System.Attribute { }";
// public void Initialize(IncrementalGeneratorInitializationContext context)
// {
// context.RegisterPostInitializationOutput(context =>
// context.AddSource(
// $"{AttributeFullName}.g",
// SourceText.From(AttributeSource, Encoding.UTF8)));
// Какие типы коллекций поддерживаем
private static readonly (string Type, string Selector)[] CollectionTypes =
[
("T[]", "array => array.Select(u => left * u).ToArray()"), // но нужно адаптировать под конкретный тип
// Проще генерировать отдельные методы для каждого типа
];
// var operatorsPipeline =
// context.SyntaxProvider.CreateSyntaxProvider<KeyValuePair<ClassData, ImmutableArray<Operation>>>(
// (node, _) =>
// {
// if (node is ClassDeclarationSyntax cds)
// {
// SyntaxNode sn = cds;
// while (sn.Parent is not null &&
// sn.Parent is not FileScopedNamespaceDeclarationSyntax)
// {
// sn = sn.Parent;
// }
// if (sn.Parent is FileScopedNamespaceDeclarationSyntax
// && cds.TypeParameterList is null
// &&
// ((cds.Members.OfType<OperatorDeclarationSyntax>()
// .Where(m => m.AttributeLists.SelectMany(al => al.Attributes)
// .Any(a => a.Name.GetText().ToString().Contains(AttributeShortName)))
// .Any())
// ||
// (cds.Modifiers.Any(m => m.Text == "partial")
// && cds.AttributeLists
// .SelectMany(al => al.Attributes)
// .Any(a => a.Name
// .GetText()
// .ToString()
// .Contains(AttributeShortName)))))
// {
// return true;
// }
// }
// return false;
// },
// (syntax, _) =>
// {
// string nameSpace;
// var cds = (ClassDeclarationSyntax)syntax.Node;
// {
// SyntaxNode sn = cds;
// while (sn.Parent is not FileScopedNamespaceDeclarationSyntax)
// {
// sn = sn.Parent!;
// }
// var nds = (FileScopedNamespaceDeclarationSyntax)sn.Parent;
// nameSpace = nds.Name.ToString();
// }
// var Res = new StringBuilder();
// {
// Res.Append(cds.Modifiers);
// Res.Append(" class ");
// Res.Append(cds.Identifier.Text);
// Res.Append(" ");
// Res.Append(cds.BaseList);
// Res.Append(" ");
// Res.Append(cds.ConstraintClauses);
// }
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterPostInitializationOutput(ctx =>
ctx.AddSource($"{AttributeFullName}.g", SourceText.From(AttributeSource, Encoding.UTF8)));
// var operators = cds.Members.OfType<OperatorDeclarationSyntax>()
// .Where(m => m.AttributeLists
// .SelectMany(al => al.Attributes)
// .Any(a => a.Name.GetText().ToString().Contains(AttributeShortName)))
// .Where(mb => mb.ParameterList.Parameters.Count == 2)
// .Select(mb => new Operation()
// {
// ReturnType = mb.ReturnType.ToString(),
// OperatorToken = mb.OperatorToken.Text,
// TypeA = mb.ParameterList.Parameters[0].Type!.ToString(),
// TypeB = mb.ParameterList.Parameters[1].Type!.ToString(),
// });
// return new(new(nameSpace, cds.Identifier.Text, Res.ToString()), [.. operators]);
// })
// .Collect();
var operatorsPipeline = context.SyntaxProvider
.CreateSyntaxProvider(
predicate: static (node, _) => IsTargetType(node),
transform: static (ctx, _) => GetTypeInfo(ctx))
.Where(info => info.HasValue) // использование nullable value type
.Select((info, _) => info!.Value)
.Collect();
// context.RegisterSourceOutput(operatorsPipeline, GenerateOperators);
// }
context.RegisterSourceOutput(operatorsPipeline, GenerateOperators);
}
// readonly static string[] CollectionTypes = ["MetricArray", "MetricList", "MetricObservableCollection"];
// static void GenerateOperators(SourceProductionContext context, ImmutableArray<KeyValuePair<ClassData, ImmutableArray<Operation>>> pairs)
// {
// foreach (var ng in pairs.GroupBy(c => c.Key.NameSpace))
// {
// StringBuilder document = new("namespace ");
// document.Append(ng.Key);
// document.Append(";");
private static bool IsTargetType(SyntaxNode node)
{
if (node is not TypeDeclarationSyntax typeDecl)
return false;
// var classes = ng.ToList().Select(c => (ClassData?)c.Key).ToList();
// var operations = ng.ToList().SelectMany(c => c.Value).ToList();
// var multiplications = operations.Where(op => op.OperatorToken == "*").ToList();
// var divisions = operations.Where(op => op.OperatorToken == "/").ToList();
// Должен быть partial и не generic
if (!typeDecl.Modifiers.Any(SyntaxKind.PartialKeyword))
return false;
if (typeDecl.TypeParameterList != null)
return false;
// foreach (var ops in multiplications.GroupBy(op => op.TypeA))
// {
// var Class = classes.FirstOrDefault(cl => cl!.Value.ClassName == ops.Key);
// if (Class is not null)
// {
// document.AppendLine();
// document.AppendLine();
// document.AppendLine(Class.Value.ClassHeader);
// document.AppendLine("{");
// foreach (var op in ops)
// foreach (var ct in CollectionTypes)
// {
// document.AppendLine(@$"
// public static {ct}<{op.ReturnType}> operator *({op.TypeA} left, {ct}<{op.TypeB}> right) => right.MetricSelect(UU => left * UU);
// public static {ct}<{op.ReturnType}> operator *({ct}<{op.TypeB}> left, {op.TypeA} right) => left.MetricSelect(UU => UU * right);
//");
// }
// document.Append("}");
// }
// else
// {
// context.ReportDiagnostic(Diagnostic.Create(new(
// "MSG0001",
// "Need a class with an attribute",
// $"It is necessary to have a empty partial class \"{ops.Key}\" with the attribute \"{AttributeShortName}\"",
// "category",
// DiagnosticSeverity.Error,
// true), null, ops.Key));
// }
// }
// Проверяем наличие атрибута на самом типе
foreach (var attrList in typeDecl.AttributeLists)
foreach (var attr in attrList.Attributes)
{
string name = attr.Name.ToString();
if (name == AttributeShortName || name == AttributeFullName)
return true;
}
// foreach (var ops in divisions.GroupBy(op => op.TypeA))
// {
// var Class = classes.FirstOrDefault(cl => cl!.Value.ClassName == ops.Key);
// if (Class is not null)
// {
// document.AppendLine();
// document.AppendLine();
// document.AppendLine(Class.Value.ClassHeader);
// document.AppendLine("{");
// foreach (var op in ops)
// foreach (var ct in CollectionTypes)
// {
// document.AppendLine(@$"
// public static {ct}<{op.ReturnType}> operator /({op.TypeA} left, {ct}<{op.TypeB}> right) => right.MetricSelect(UU => left / UU);
//");
// }
// document.Append("}");
// }
// else
// {
// context.ReportDiagnostic(Diagnostic.Create(new(
// "MSG0001",
// "Need a class with an attribute",
// $"It is necessary to have a empty partial class \"{ops.Key}\" with the attribute \"{AttributeShortName}\"",
// "category",
// DiagnosticSeverity.Error,
// true), null, ops.Key));
// }
// }
// foreach (var ops in divisions.GroupBy(op => op.TypeB))
// {
// var Class = classes.FirstOrDefault(cl => cl!.Value.ClassName == ops.Key);
// if (Class is not null)
// {
// document.AppendLine();
// document.AppendLine();
// document.AppendLine(Class.Value.ClassHeader);
// document.AppendLine("{");
// foreach (var op in ops)
// foreach (var ct in CollectionTypes)
// {
// document.AppendLine(@$"
// public static {ct}<{op.ReturnType}> operator /({ct}<{op.TypeA}> left, {op.TypeB} right) => left.MetricSelect(UU => UU / right);
//");
// }
// document.Append("}");
// }
// else
// {
// context.ReportDiagnostic(Diagnostic.Create(new(
// "MSG0001",
// "Need a class with an attribute",
// $"It is necessary to have a empty partial class \"{ops.Key}\" with the attribute \"{AttributeShortName}\"",
// "category",
// DiagnosticSeverity.Error,
// true), null, ops.Key));
// }
// }
// Или на операторах внутри
foreach (var member in typeDecl.Members)
{
if (member is OperatorDeclarationSyntax opDecl &&
opDecl.AttributeLists.SelectMany(al => al.Attributes)
.Any(a => a.Name.ToString().Contains(AttributeShortName)))
return true;
}
return false;
}
// context.AddSource($"operations.{ng.Key}.g", document.ToString());
// }
// }
//}
private static TypeInfoData? GetTypeInfo(GeneratorSyntaxContext ctx)
{
var typeDecl = (TypeDeclarationSyntax)ctx.Node;
var semanticModel = ctx.SemanticModel;
//public struct ClassData(string nameSpace, string className, string classHeader)
//{
// public string NameSpace = nameSpace;
// public string ClassName = className;
// public string ClassHeader = classHeader;
//}
//public struct Operation(string operatorToken, string typeA, string typeB, string returnType)
//{
// public string ReturnType = returnType;
// public string OperatorToken = operatorToken;
// public string TypeA = typeA;
// public string TypeB = typeB;
//}
var symbol = semanticModel.GetDeclaredSymbol(typeDecl);
if (symbol == null) return null;
string namespaceName = symbol.ContainingNamespace?.ToString() ?? "";
// Формируем заголовок (для отладочных целей)
var headerBuilder = new StringBuilder();
foreach (var modifier in typeDecl.Modifiers)
headerBuilder.Append(modifier.Text).Append(' ');
if (typeDecl is RecordDeclarationSyntax)
headerBuilder.Append("record ");
headerBuilder.Append(typeDecl.Identifier.Text);
string header = headerBuilder.ToString();
// Собираем операторы
var operators = ImmutableArray.CreateBuilder<Operation>();
foreach (var member in typeDecl.Members)
{
if (member is not OperatorDeclarationSyntax opDecl) continue;
if (opDecl.ParameterList.Parameters.Count != 2) continue;
if (!opDecl.AttributeLists.SelectMany(al => al.Attributes)
.Any(a => a.Name.ToString().Contains(AttributeShortName))) continue;
operators.Add(new Operation(
opDecl.OperatorToken.Text,
opDecl.ParameterList.Parameters[0].Type!.ToString(),
opDecl.ParameterList.Parameters[1].Type!.ToString(),
opDecl.ReturnType.ToString()
));
}
if (operators.Count == 0) return null;
return new TypeInfoData(namespaceName, typeDecl.Identifier.Text, header, operators.ToImmutable());
}
private static void GenerateOperators(SourceProductionContext context, ImmutableArray<TypeInfoData> types)
{
foreach (var group in types.GroupBy(t => t.Namespace))
{
var document = new StringBuilder();
document.AppendLine("namespace ").Append(group.Key).AppendLine(";");
document.AppendLine("using System.Collections.Generic;");
document.AppendLine("using System.Linq;");
var allOperators = group.SelectMany(t => t.Operators).ToList();
var multiplications = allOperators.Where(op => op.OperatorToken == "*").ToList();
var divisions = allOperators.Where(op => op.OperatorToken == "/").ToList();
// Генерация для умножения: left * collection и collection * left
foreach (var mul in multiplications)
{
// left * collection
document.AppendLine($@"
public static {mul.ReturnType}[] operator *({mul.TypeA} left, {mul.TypeB}[] right) =>
right.Select(u => left * u).ToArray();
public static List<{mul.ReturnType}> operator *({mul.TypeA} left, List<{mul.TypeB}> right) =>
right.Select(u => left * u).ToList();
public static IEnumerable<{mul.ReturnType}> operator *({mul.TypeA} left, IEnumerable<{mul.TypeB}> right) =>
right.Select(u => left * u);
");
// collection * left
document.AppendLine($@"
public static {mul.ReturnType}[] operator *({mul.TypeB}[] left, {mul.TypeA} right) =>
left.Select(u => u * right).ToArray();
public static List<{mul.ReturnType}> operator *(List<{mul.TypeB}> left, {mul.TypeA} right) =>
left.Select(u => u * right).ToList();
public static IEnumerable<{mul.ReturnType}> operator *(IEnumerable<{mul.TypeB}> left, {mul.TypeA} right) =>
left.Select(u => u * right);
");
}
// Деление: left / collection (left - тип A, collection - тип B)
foreach (var div in divisions.Where(op => op.TypeA != null)) // все деления
{
// left / collection
document.AppendLine($@"
public static {div.ReturnType}[] operator /({div.TypeA} left, {div.TypeB}[] right) =>
right.Select(u => left / u).ToArray();
public static List<{div.ReturnType}> operator /({div.TypeA} left, List<{div.TypeB}> right) =>
right.Select(u => left / u).ToList();
public static IEnumerable<{div.ReturnType}> operator /({div.TypeA} left, IEnumerable<{div.TypeB}> right) =>
right.Select(u => left / u);
");
// collection / right (где right - тип B)
document.AppendLine($@"
public static {div.ReturnType}[] operator /({div.TypeA}[] left, {div.TypeB} right) =>
left.Select(u => u / right).ToArray();
public static List<{div.ReturnType}> operator /(List<{div.TypeA}> left, {div.TypeB} right) =>
left.Select(u => u / right).ToList();
public static IEnumerable<{div.ReturnType}> operator /(IEnumerable<{div.TypeA}> left, {div.TypeB} right) =>
left.Select(u => u / right);
");
}
context.AddSource($"operations.{group.Key}.g", document.ToString());
}
}
private readonly struct TypeInfoData(string ns, string name, string header, ImmutableArray<Operation> ops)
{
public string Namespace { get; } = ns;
public string TypeName { get; } = name;
public string Header { get; } = header;
public ImmutableArray<Operation> Operators { get; } = ops;
}
private readonly struct Operation(string token, string a, string b, string ret)
{
public string OperatorToken { get; } = token;
public string TypeA { get; } = a;
public string TypeB { get; } = b;
public string ReturnType { get; } = ret;
}
}

View File

@@ -4,14 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;IDE0079;MVVMTK0034</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<NoWarn>1701;1702;IDE0079;MVVMTK0034</NoWarn>
<NoWarn>1701;1702;IDE0079;MVVMTK0034;IDE0130</NoWarn>
</PropertyGroup>
<ItemGroup>

View File

@@ -4,12 +4,12 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using System.Text;
namespace QWERTYkez.Mensura.Generator;
namespace G;
[Generator]
public class UnitOperatorsGenerator : IIncrementalGenerator
public class UnitGenerator : IIncrementalGenerator
{
private const string AttributeName = "UnitOperatorsGenerator";
private const string AttributeName = "UnitGenerator";
public void Initialize(IncrementalGeneratorInitializationContext context)
{
@@ -20,9 +20,9 @@ public class UnitOperatorsGenerator : IIncrementalGenerator
namespace QWERTYkez.Mensura
{
[System.AttributeUsage(System.AttributeTargets.Struct, AllowMultiple = false)]
public sealed class UnitOperatorsGeneratorAttribute : System.Attribute { }
public sealed class UnitGeneratorAttribute : System.Attribute { }
}";
ctx.AddSource("UnitOperatorsGeneratorAttribute.g.cs", SourceText.From(attributeSource, Encoding.UTF8));
ctx.AddSource(".UnitGeneratorAttribute.g.cs", SourceText.From(attributeSource, Encoding.UTF8));
});
// Ищем все readonly partial record struct с атрибутом
@@ -39,7 +39,7 @@ namespace QWERTYkez.Mensura
foreach (var structInfo in structs)
{
string generatedCode = GeneratePartial(structInfo);
spc.AddSource($"{structInfo.TypeName}.Generated.g.cs", SourceText.From(generatedCode, Encoding.UTF8));
spc.AddSource($"{structInfo.TypeName}.g.cs", SourceText.From(generatedCode, Encoding.UTF8));
}
});
}
@@ -56,7 +56,7 @@ namespace QWERTYkez.Mensura
!record.ClassOrStructKeyword.IsKind(SyntaxKind.StructKeyword))
return false;
// Проверяем наличие атрибута [UnitOperatorsGenerator]
// Проверяем наличие атрибута [UnitGenerator]
foreach (var attrList in record.AttributeLists)
foreach (var attr in attrList.Attributes)
{