@@ -60,7 +60,25 @@ public readonly partial struct Hp { }
6060// -- generates
6161
6262[System .ComponentModel .TypeConverter (typeof (HpTypeConverter ))]
63- public readonly partial struct Hp : IEquatable <Hp > , IComparable <Hp >
63+ public readonly partial struct Hp
64+ : IEquatable <Hp >
65+ #if NET7_0_OR_GREATER
66+ , IEqualityOperators <Hp , Hp , bool >
67+ #endif
68+ , IComparable <Hp >
69+ #if NET7_0_OR_GREATER
70+ , IComparisonOperators <Hp , Hp , bool >
71+ #endif
72+ #if NET7_0_OR_GREATER
73+ , IAdditionOperators <Hp , Hp , Hp >
74+ , ISubtractionOperators <Hp , Hp , Hp >
75+ , IMultiplyOperators <Hp , Hp , Hp >
76+ , IDivisionOperators <Hp , Hp , Hp >
77+ , IUnaryPlusOperators <Hp , Hp >
78+ , IUnaryNegationOperators <Hp , Hp >
79+ , IIncrementOperators <Hp >
80+ , IDecrementOperators <Hp >
81+ #endif
6482{
6583 readonly int value ;
6684
@@ -81,25 +99,27 @@ public readonly partial struct Hp : IEquatable<Hp> , IComparable<Hp>
8199 private class HpTypeConverter : System .ComponentModel .TypeConverter { /* snip... */ }
82100
83101 // UnitGenerateOptions.ArithmeticOperator
84- public static Hp operator + (in Hp x , in Hp y ) => new Hp (checked ((int )(x .value + y .value )));
85- public static Hp operator - (in Hp x , in Hp y ) => new Hp (checked ((int )(x .value - y .value )));
86- public static Hp operator * (in Hp x , in Hp y ) => new Hp (checked ((int )(x .value * y .value )));
87- public static Hp operator / (in Hp x , in Hp y ) => new Hp (checked ((int )(x .value / y .value )));
102+ public static Hp operator + (Hp x , Hp y ) => new Hp (checked ((int )(x .value + y .value )));
103+ public static Hp operator - (Hp x , Hp y ) => new Hp (checked ((int )(x .value - y .value )));
104+ public static Hp operator * (Hp x , Hp y ) => new Hp (checked ((int )(x .value * y .value )));
105+ public static Hp operator / (Hp x , Hp y ) => new Hp (checked ((int )(x .value / y .value )));
106+ public static Hp operator ++ (Hp x ) => new Hp (checked ((int )(x .value + 1 )));
107+ public static Hp operator -- (Hp x ) => new Hp (checked ((int )(x .value - 1 )));
108+ public static Hp operator + (A value ) => new ((int )(+ value .value ));
109+ public static Hp operator - (A value ) => new ((int )(- value .value ));
88110
89111 // UnitGenerateOptions.ValueArithmeticOperator
90- public static Hp operator ++ (in Hp x ) => new Hp (checked ((int )(x .value + 1 )));
91- public static Hp operator -- (in Hp x ) => new Hp (checked ((int )(x .value - 1 )));
92- public static Hp operator + (in Hp x , in int y ) => new Hp (checked ((int )(x .value + y )));
93- public static Hp operator - (in Hp x , in int y ) => new Hp (checked ((int )(x .value - y )));
94- public static Hp operator * (in Hp x , in int y ) => new Hp (checked ((int )(x .value * y )));
95- public static Hp operator / (in Hp x , in int y ) => new Hp (checked ((int )(x .value / y )));
112+ public static Hp operator + (Hp x , in int y ) => new Hp (checked ((int )(x .value + y )));
113+ public static Hp operator - (Hp x , in int y ) => new Hp (checked ((int )(x .value - y )));
114+ public static Hp operator * (Hp x , in int y ) => new Hp (checked ((int )(x .value * y )));
115+ public static Hp operator / (Hp x , in int y ) => new Hp (checked ((int )(x .value / y )));
96116
97117 // UnitGenerateOptions.Comparable
98118 public int CompareTo (Hp other ) => value .CompareTo (other .value );
99- public static bool operator > (in Hp x , in Hp y ) => x .value > y .value ;
100- public static bool operator < (in Hp x , in Hp y ) => x .value < y .value ;
101- public static bool operator >= (in Hp x , in Hp y ) => x .value >= y .value ;
102- public static bool operator <= (in Hp x , in Hp y ) => x .value <= y .value ;
119+ public static bool operator > (Hp x , Hp y ) => x .value > y .value ;
120+ public static bool operator < (Hp x , Hp y ) => x .value < y .value ;
121+ public static bool operator >= (Hp x , Hp y ) => x .value >= y .value ;
122+ public static bool operator <= (Hp x , Hp y ) => x .value <= y .value ;
103123
104124 // UnitGenerateOptions.MinMaxMethod
105125 public static Hp Min (Hp x , Hp y ) => new Hp (Math .Min (x .value , y .value ));
@@ -193,7 +213,12 @@ namespace UnitGenerator
193213 [AttributeUsage (AttributeTargets .Struct , AllowMultiple = false )]
194214 internal class UnitOfAttribute : Attribute
195215 {
196- public UnitOfAttribute (Type type , UnitGenerateOptions options = UnitGenerateOptions .None , string toStringFormat = null )
216+ public Type Type { get ; }
217+ public UnitGenerateOptions Options { get ; }
218+ public UnitArithmeticOperators ArithmeticOperators { get ; set ; }
219+ public string ToStringFormat { get ; set ; }
220+
221+ public UnitOfAttribute (Type type , UnitGenerateOptions options = UnitGenerateOptions .None ) { .. . }
197222 }
198223}
199224```
@@ -241,7 +266,9 @@ public static GroupId NewGroupId();
241266
242267Second parameter ` UnitGenerateOptions options ` can configure which method to implement, default is ` None ` .
243268
244- Third parameter `strign toStringFormat` can configure `ToString` format. Default is null and output as $`{0}`.
269+ Optional named parameter: ` ArithmeticOperators ` can configure which generates operators specifically. Default is ` Number ` . (This can be used if UnitGenerateOptions.ArithmeticOperator is specified.)
270+
271+ Optional named parameter: ` ToStringFormat ` can configure ` ToString ` format. Default is null and output as $` {0} ` .
245272
246273## UnitGenerateOptions
247274
@@ -324,13 +351,36 @@ public static T operator +(in T x, in T y) => new T(checked((U)(x.value + y.valu
324351public static T operator - (in T x , in T y ) => new T (checked ((U )(x .value - y .value )));
325352public static T operator * (in T x , in T y ) => new T (checked ((U )(x .value * y .value )));
326353public static T operator / (in T x , in T y ) => new T (checked ((U )(x .value / y .value )));
354+ public static T operator + (T value ) => new ((U )(+ value .value ));
355+ public static T operator - (T value ) => new ((U )(- value .value ));
356+ public static T operator ++ (T x ) => new T (checked ((U )(x .value + 1 )));
357+ public static T operator -- (T x ) => new T (checked ((U )(x .value - 1 )));
358+ ```
359+
360+ In addition, all members conforming to [ System.Numerics.INumber<T >] ( https://learn.microsoft.com/ja-jp/dotnet/api/system.numerics.inumber-1 ) are generated.
361+
362+ If you want to suppress this and generate only certain operators, you can use the the ` ArithmeticOperatros ` option of ` [UnitOf] ` attribute as follows:
363+
364+ ``` csharp
365+ [UnitOf (
366+ typeof (int ),
367+ UnitGenerateOptions .ArithmeticOperator ,
368+ ArithmeticOperators = UnitArithmeticOperators .Addition | UnitArithmeticOperators .Subtraction )]
369+ public readonly partial struct Hp { }
327370```
328371
372+ | Value | Generates |
373+ | -------------------------------------| ----------------------------------------------------------------------------------------|
374+ | UnitArithmeticOperators.Addition | ` T operator +(T, T) ` |
375+ | UnitArithmeticOperators.Subtraction | ` T operator -(T, T) ` |
376+ | UnitArithmeticOperators.Multiply | ` T operator *(T, T) ` , ` T operator +(T) ` , ` T operator-(T) ` |
377+ | UnitArithmeticOperators.Division | ` T operator /(T, T) ` , ` T operator +(T) ` , ` T operator-(T) ` |
378+ | UnitArithmeticOperators.Increment | ` T operator ++(T) ` |
379+ | UnitArithmeticOperators.Decrement | ` T operator --(T) ` |
380+
329381### ValueArithmeticOperator
330382
331383``` csharp
332- public static T operator ++ (in T x ) => new T (checked ((U )(x .value + 1 )));
333- public static T operator -- (in T x ) => new T (checked ((U )(x .value - 1 )));
334384public static T operator + (in T x , in U y ) => new T (checked ((U )(x .value + y )));
335385public static T operator - (in T x , in U y ) => new T (checked ((U )(x .value - y )));
336386public static T operator * (in T x , in U y ) => new T (checked ((U )(x .value * y )));
0 commit comments