Skip to content

Commit 4c074f1

Browse files
committed
IUnaryPlusOperators, IUnaryNegationOperators are added on UnitGenerateOptions.Addition/Substraction
ulong can not geenrate when UnitGenerateOptions.ArithmeticOperator
1 parent 25fca23 commit 4c074f1

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

sandbox/ConsoleApp/Program.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919

2020
Console.WriteLine(json);
2121

22-
22+
[UnitOf(typeof(ulong), UnitGenerateOptions.ArithmeticOperator)]
23+
public readonly partial struct Money
24+
{
25+
}
2326

2427

2528
[UnitOf(typeof(int))]

src/UnitGenerator/SourceGenerator.cs

+15-11
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,12 @@ readonly partial struct {{unitTypeName}}
317317
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Addition))
318318
{
319319
net7Interfaces.Add($"IAdditionOperators<{unitTypeName}, {unitTypeName}, {unitTypeName}>");
320+
net7Interfaces.Add($"IUnaryPlusOperators<{unitTypeName}, {unitTypeName}>");
320321
}
321322
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Subtraction))
322323
{
323324
net7Interfaces.Add($"ISubtractionOperators<{unitTypeName}, {unitTypeName}, {unitTypeName}>");
325+
net7Interfaces.Add($"IUnaryNegationOperators<{unitTypeName}, {unitTypeName}>");
324326
}
325327
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Multiply))
326328
{
@@ -330,12 +332,6 @@ readonly partial struct {{unitTypeName}}
330332
{
331333
net7Interfaces.Add($"IDivisionOperators<{unitTypeName}, {unitTypeName}, {unitTypeName}>");
332334
}
333-
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Multiply) ||
334-
prop.HasArithmeticOperator(UnitArithmeticOperators.Division))
335-
{
336-
net7Interfaces.Add($"IUnaryPlusOperators<{unitTypeName}, {unitTypeName}>");
337-
net7Interfaces.Add($"IUnaryNegationOperators<{unitTypeName}, {unitTypeName}>");
338-
}
339335
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Increment))
340336
{
341337
net7Interfaces.Add($"IIncrementOperators<{unitTypeName}>");
@@ -773,6 +769,9 @@ public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provid
773769
}
774770
}
775771
772+
""");
773+
sb.AppendLine($$"""
774+
public static {{unitTypeName}} operator +({{unitTypeName}} value) => new(({{innerTypeName}})(+value.value));
776775
""");
777776
}
778777
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Subtraction))
@@ -786,17 +785,22 @@ public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provid
786785
}
787786
}
788787
788+
""");
789+
string zero = "";
790+
if (innerTypeName == "ulong")
791+
{
792+
zero = "0UL ";
793+
}
794+
795+
sb.AppendLine($$"""
796+
public static {{unitTypeName}} operator -({{unitTypeName}} value) => new(({{innerTypeName}})({{zero}}-value.value));
797+
789798
""");
790799
}
791800

792801
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Multiply) ||
793802
prop.HasArithmeticOperator(UnitArithmeticOperators.Division))
794803
{
795-
sb.AppendLine($$"""
796-
public static {{unitTypeName}} operator +({{unitTypeName}} value) => new(({{innerTypeName}})(+value.value));
797-
public static {{unitTypeName}} operator -({{unitTypeName}} value) => new(({{innerTypeName}})(-value.value));
798-
799-
""");
800804
if (prop.HasArithmeticOperator(UnitArithmeticOperators.Multiply))
801805
{
802806
sb.AppendLine($$"""

0 commit comments

Comments
 (0)