Skip to content

Commit

Permalink
IUnaryPlusOperators, IUnaryNegationOperators are added on UnitGenerat…
Browse files Browse the repository at this point in the history
…eOptions.Addition/Substraction

ulong can not geenrate when UnitGenerateOptions.ArithmeticOperator
  • Loading branch information
neuecc committed Jul 29, 2024
1 parent 25fca23 commit 4c074f1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
5 changes: 4 additions & 1 deletion sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

Console.WriteLine(json);


[UnitOf(typeof(ulong), UnitGenerateOptions.ArithmeticOperator)]
public readonly partial struct Money
{
}


[UnitOf(typeof(int))]
Expand Down
26 changes: 15 additions & 11 deletions src/UnitGenerator/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,12 @@ readonly partial struct {{unitTypeName}}
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Addition))
{
net7Interfaces.Add($"IAdditionOperators<{unitTypeName}, {unitTypeName}, {unitTypeName}>");
net7Interfaces.Add($"IUnaryPlusOperators<{unitTypeName}, {unitTypeName}>");
}
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Subtraction))
{
net7Interfaces.Add($"ISubtractionOperators<{unitTypeName}, {unitTypeName}, {unitTypeName}>");
net7Interfaces.Add($"IUnaryNegationOperators<{unitTypeName}, {unitTypeName}>");
}
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Multiply))
{
Expand All @@ -330,12 +332,6 @@ readonly partial struct {{unitTypeName}}
{
net7Interfaces.Add($"IDivisionOperators<{unitTypeName}, {unitTypeName}, {unitTypeName}>");
}
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Multiply) ||
prop.HasArithmeticOperator(UnitArithmeticOperators.Division))
{
net7Interfaces.Add($"IUnaryPlusOperators<{unitTypeName}, {unitTypeName}>");
net7Interfaces.Add($"IUnaryNegationOperators<{unitTypeName}, {unitTypeName}>");
}
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Increment))
{
net7Interfaces.Add($"IIncrementOperators<{unitTypeName}>");
Expand Down Expand Up @@ -773,6 +769,9 @@ public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provid
}
}
""");
sb.AppendLine($$"""
public static {{unitTypeName}} operator +({{unitTypeName}} value) => new(({{innerTypeName}})(+value.value));
""");
}
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Subtraction))
Expand All @@ -786,17 +785,22 @@ public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provid
}
}
""");
string zero = "";
if (innerTypeName == "ulong")
{
zero = "0UL ";
}

sb.AppendLine($$"""
public static {{unitTypeName}} operator -({{unitTypeName}} value) => new(({{innerTypeName}})({{zero}}-value.value));
""");
}

if (prop.HasArithmeticOperator(UnitArithmeticOperators.Multiply) ||
prop.HasArithmeticOperator(UnitArithmeticOperators.Division))
{
sb.AppendLine($$"""
public static {{unitTypeName}} operator +({{unitTypeName}} value) => new(({{innerTypeName}})(+value.value));
public static {{unitTypeName}} operator -({{unitTypeName}} value) => new(({{innerTypeName}})(-value.value));
""");
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Multiply))
{
sb.AppendLine($$"""
Expand Down

0 comments on commit 4c074f1

Please sign in to comment.