@@ -17,6 +17,8 @@ internal class QuantityGenerator : GeneratorBase
17
17
private readonly string _valueType ;
18
18
private readonly Unit _baseUnit ;
19
19
20
+ private readonly string [ ] _decimalTypes = { "BitRate" , "Information" , "Power" } ;
21
+
20
22
public QuantityGenerator ( Quantity quantity )
21
23
{
22
24
_quantity = quantity ?? throw new ArgumentNullException ( nameof ( quantity ) ) ;
@@ -39,8 +41,12 @@ public string Generate()
39
41
using System;
40
42
using System.Diagnostics.CodeAnalysis;
41
43
using System.Globalization;
42
- using System.Linq;
43
- using System.Runtime.Serialization;
44
+ using System.Linq;" ) ;
45
+ if ( _quantity . Relations . Any ( r => r . Operator is "*" or "/" ) )
46
+ Writer . WL ( @"#if NET7_0_OR_GREATER
47
+ using System.Numerics;
48
+ #endif" ) ;
49
+ Writer . WL ( @"using System.Runtime.Serialization;
44
50
using UnitsNet.InternalHelpers;
45
51
using UnitsNet.Units;
46
52
@@ -67,6 +73,35 @@ namespace UnitsNet
67
73
public readonly partial struct { _quantity . Name } :
68
74
{ ( _quantity . GenerateArithmetic ? "IArithmeticQuantity" : "IQuantity" ) } <{ _quantity . Name } , { _unitEnumName } , { _quantity . ValueType } >," ) ;
69
75
76
+ if ( _quantity . Relations . Any ( r => r . Operator is "*" or "/" ) )
77
+ {
78
+ Writer . WL ( @$ "
79
+ #if NET7_0_OR_GREATER" ) ;
80
+ foreach ( var relation in _quantity . Relations )
81
+ {
82
+ if ( relation . LeftQuantity == _quantity . Name )
83
+ {
84
+ switch ( relation . Operator )
85
+ {
86
+ case "*" :
87
+ Writer . W ( @"
88
+ IMultiplyOperators" ) ;
89
+ break ;
90
+ case "/" :
91
+ Writer . W ( @"
92
+ IDivisionOperators" ) ;
93
+ break ;
94
+ default :
95
+ continue ;
96
+ }
97
+ Writer . WL ( $ "<{ relation . LeftQuantity } , { relation . RightQuantity } , { relation . ResultQuantity } >,") ;
98
+ }
99
+ }
100
+
101
+ Writer . WL ( @$ "
102
+ #endif" ) ;
103
+ }
104
+
70
105
if ( _quantity . ValueType == "decimal" ) Writer . WL ( @$ "
71
106
IDecimalQuantity," ) ;
72
107
@@ -100,6 +135,7 @@ namespace UnitsNet
100
135
GenerateStaticFactoryMethods ( ) ;
101
136
GenerateStaticParseMethods ( ) ;
102
137
GenerateArithmeticOperators ( ) ;
138
+ GenerateRelationalOperators ( ) ;
103
139
GenerateEqualityAndComparison ( ) ;
104
140
GenerateConversionMethods ( ) ;
105
141
GenerateToString ( ) ;
@@ -696,6 +732,81 @@ private void GenerateLogarithmicArithmeticOperators()
696
732
" ) ;
697
733
}
698
734
735
+ private void GenerateRelationalOperators ( )
736
+ {
737
+ if ( ! _quantity . Relations . Any ( ) ) return ;
738
+
739
+ Writer . WL ( $@ "
740
+ #region Relational Operators
741
+ " ) ;
742
+
743
+ foreach ( Relation relation in _quantity . Relations )
744
+ {
745
+ if ( relation . Operator == "inverse" )
746
+ {
747
+ Writer . WL ( $@ "
748
+ /// <summary>Calculates the inverse of this quantity.</summary>
749
+ /// <returns>The corresponding inverse quantity, <see cref=""{ relation . RightQuantity } ""/>.</returns>
750
+ public { relation . RightQuantity } Inverse()
751
+ {{
752
+ return { relation . Left . Split ( '.' ) [ 1 ] } == 0.0 ? { relation . RightQuantity } .Zero : { relation . RightQuantity } .From{ relation . Right . Split ( '.' ) [ 1 ] } (1 / { relation . Left . Split ( '.' ) [ 1 ] } );
753
+ }}
754
+ " ) ;
755
+ }
756
+ else
757
+ {
758
+ var leftParameter = relation . LeftQuantity . ToCamelCase ( ) ;
759
+ var leftConversionProperty = relation . Left . Split ( '.' ) . ElementAtOrDefault ( 1 ) ;
760
+ var rightParameter = relation . RightQuantity . ToCamelCase ( ) ;
761
+ var rightConversionProperty = relation . Right . Split ( '.' ) . ElementAtOrDefault ( 1 ) ;
762
+
763
+ if ( leftParameter == rightParameter )
764
+ {
765
+ leftParameter = "left" ;
766
+ rightParameter = "right" ;
767
+ }
768
+
769
+ var leftPart = $ "{ leftParameter } .{ leftConversionProperty } ";
770
+ var rightPart = $ "{ rightParameter } .{ rightConversionProperty } ";
771
+
772
+ if ( leftParameter == "double" )
773
+ {
774
+ leftParameter = "value" ;
775
+ leftPart = "value" ;
776
+ }
777
+
778
+ if ( rightParameter == "double" )
779
+ {
780
+ rightParameter = "value" ;
781
+ rightPart = "value" ;
782
+ }
783
+
784
+ var leftCast = _decimalTypes . Contains ( relation . LeftQuantity ) ? "(double)" : "" ;
785
+ var rightCast = _decimalTypes . Contains ( relation . RightQuantity ) ? "(double)" : "" ;
786
+
787
+ var expression = $ "{ leftCast } { leftPart } { relation . Operator } { rightCast } { rightPart } ";
788
+
789
+ if ( relation . Result is not ( "double" or "decimal" ) )
790
+ {
791
+ expression = $ "{ relation . Result } ({ expression } )";
792
+ }
793
+
794
+ Writer . WL ( $@ "
795
+ /// <summary>Get <see cref=""{ relation . ResultQuantity } ""/> from <see cref=""{ relation . LeftQuantity } ""/> { relation . Operator } <see cref=""{ relation . RightQuantity } ""/>.</summary>
796
+ public static { relation . ResultQuantity } operator { relation . Operator } ({ relation . LeftQuantity } { leftParameter } , { relation . RightQuantity } { rightParameter } )
797
+ {{
798
+ return { expression } ;
799
+ }}
800
+ " ) ;
801
+ }
802
+ }
803
+
804
+ Writer . WL ( $@ "
805
+
806
+ #endregion
807
+ " ) ;
808
+ }
809
+
699
810
private void GenerateEqualityAndComparison ( )
700
811
{
701
812
Writer . WL ( $@ "
0 commit comments