diff --git a/csharp/Lib/Units/ActivePower.cs b/csharp/Lib/Units/ActivePower.cs index 85a5cd6de..d1645cdf2 100644 --- a/csharp/Lib/Units/ActivePower.cs +++ b/csharp/Lib/Units/ActivePower.cs @@ -1,20 +1,20 @@ +using InnovEnergy.Lib.SrcGen; +using InnovEnergy.Lib.SrcGen.Attributes; using InnovEnergy.Lib.Time.Unix; -using InnovEnergy.Lib.Units.Generator; namespace InnovEnergy.Lib.Units; -[Generate] + +[Generate][Generate] public readonly partial struct ActivePower { public static String Unit => "W"; public static String Symbol => "P"; - public ActivePower(Double value) => Value = value; - // P=UI public static Voltage operator /(ActivePower power, Current current) => new Voltage(power.Value / current.Value); public static Current operator /(ActivePower power, Voltage voltage) => new Current(power.Value / voltage.Value); - + public static Energy operator *(ActivePower power, TimeSpan timeSpan) => power.Value / 1000 * timeSpan.TotalHours; public static Energy operator *(TimeSpan timeSpan, ActivePower power) => power.Value / 1000 * timeSpan.TotalHours; diff --git a/csharp/Lib/Units/ActivePower.generated.cs b/csharp/Lib/Units/ActivePower.generated.cs deleted file mode 100644 index 65a5b9fa2..000000000 --- a/csharp/Lib/Units/ActivePower.generated.cs +++ /dev/null @@ -1,68 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -#define Generate - -using static System.Math; -using System.Text.Json; -using System.Text.Json.Serialization; -using InnovEnergy.Lib.Utils; -using System.CodeDom.Compiler; - -namespace InnovEnergy.Lib.Units; - -using T = ActivePower; - -[GeneratedCode("generate.sh", "1")] -[JsonConverter(typeof(ActivePowerConverter))] -public readonly partial struct ActivePower -{ - public Double Value { get; } - public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit; - - // scalar multiplication - - public static T operator *(Double scalar, T t) => new T(scalar * t.Value); - public static T operator *(T t, Double scalar) => new T(scalar * t.Value); - public static T operator /(T t, Double scalar) => new T(t.Value / scalar); - - // addition - - public static T operator +(T left, T right) => new T(left.Value + right.Value); - public static T operator -(T left, T right) => new T(left.Value - right.Value); - public static T operator -(T t) => new T(-t.Value); - - // compare - - public static Boolean operator ==(T left, T right) => left.Value == right.Value; - public static Boolean operator !=(T left, T right) => left.Value != right.Value; - public static Boolean operator > (T left, T right) => left.Value > right.Value; - public static Boolean operator < (T left, T right) => left.Value < right.Value; - public static Boolean operator >=(T left, T right) => left.Value >= right.Value; - public static Boolean operator <=(T left, T right) => left.Value <= right.Value; - - // conversion - - public static implicit operator T(Double d) => new T(d); - - // equality - - public Boolean Equals(T other) => Value == other.Value; - public override Boolean Equals(Object? obj) => obj is T other && Equals(other); - public override Int32 GetHashCode() => Value.GetHashCode(); - -} - - -internal class ActivePowerConverter : JsonConverter -{ - public override ActivePower Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return new ActivePower(reader.GetDouble()); - } - - public override void Write(Utf8JsonWriter writer, ActivePower value, JsonSerializerOptions options) - { - var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits); - - writer.WriteNumberValue(rounded); - } -} \ No newline at end of file diff --git a/csharp/Lib/Units/Angle.cs b/csharp/Lib/Units/Angle.cs index 14b269eb2..28be3b6de 100644 --- a/csharp/Lib/Units/Angle.cs +++ b/csharp/Lib/Units/Angle.cs @@ -1,9 +1,10 @@ -using InnovEnergy.Lib.Units.Generator; +using InnovEnergy.Lib.SrcGen; +using InnovEnergy.Lib.SrcGen.Attributes; using static System.Math; namespace InnovEnergy.Lib.Units; -[Generate] +[Generate("HAS_CONSTRUCTOR")][Generate] public readonly partial struct Angle { public static String Unit => "rad"; diff --git a/csharp/Lib/Units/Angle.generated.cs b/csharp/Lib/Units/Angle.generated.cs deleted file mode 100644 index 0a7764526..000000000 --- a/csharp/Lib/Units/Angle.generated.cs +++ /dev/null @@ -1,68 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -#define Generate - -using static System.Math; -using System.Text.Json; -using System.Text.Json.Serialization; -using InnovEnergy.Lib.Utils; -using System.CodeDom.Compiler; - -namespace InnovEnergy.Lib.Units; - -using T = Angle; - -[GeneratedCode("generate.sh", "1")] -[JsonConverter(typeof(AngleConverter))] -public readonly partial struct Angle -{ - public Double Value { get; } - public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit; - - // scalar multiplication - - public static T operator *(Double scalar, T t) => new T(scalar * t.Value); - public static T operator *(T t, Double scalar) => new T(scalar * t.Value); - public static T operator /(T t, Double scalar) => new T(t.Value / scalar); - - // addition - - public static T operator +(T left, T right) => new T(left.Value + right.Value); - public static T operator -(T left, T right) => new T(left.Value - right.Value); - public static T operator -(T t) => new T(-t.Value); - - // compare - - public static Boolean operator ==(T left, T right) => left.Value == right.Value; - public static Boolean operator !=(T left, T right) => left.Value != right.Value; - public static Boolean operator > (T left, T right) => left.Value > right.Value; - public static Boolean operator < (T left, T right) => left.Value < right.Value; - public static Boolean operator >=(T left, T right) => left.Value >= right.Value; - public static Boolean operator <=(T left, T right) => left.Value <= right.Value; - - // conversion - - public static implicit operator T(Double d) => new T(d); - - // equality - - public Boolean Equals(T other) => Value == other.Value; - public override Boolean Equals(Object? obj) => obj is T other && Equals(other); - public override Int32 GetHashCode() => Value.GetHashCode(); - -} - - -internal class AngleConverter : JsonConverter -{ - public override Angle Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return new Angle(reader.GetDouble()); - } - - public override void Write(Utf8JsonWriter writer, Angle value, JsonSerializerOptions options) - { - var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits); - - writer.WriteNumberValue(rounded); - } -} \ No newline at end of file diff --git a/csharp/Lib/Units/ApparentPower.cs b/csharp/Lib/Units/ApparentPower.cs index 605ac5b49..6acce8f7b 100644 --- a/csharp/Lib/Units/ApparentPower.cs +++ b/csharp/Lib/Units/ApparentPower.cs @@ -1,13 +1,14 @@ -using InnovEnergy.Lib.Units.Generator; +using InnovEnergy.Lib.SrcGen; +using InnovEnergy.Lib.SrcGen.Attributes; namespace InnovEnergy.Lib.Units; -[Generate] +[Generate("HAS_CONSTRUCTOR")][Generate] public readonly partial struct ApparentPower { public static String Unit => "VA"; public static String Symbol => "S"; - + public ApparentPower(Double value) { if (value < 0) throw new ArgumentException("Apparent power cannot be negative", nameof(value)); diff --git a/csharp/Lib/Units/ApparentPower.generated.cs b/csharp/Lib/Units/ApparentPower.generated.cs deleted file mode 100644 index d192def16..000000000 --- a/csharp/Lib/Units/ApparentPower.generated.cs +++ /dev/null @@ -1,68 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -#define Generate - -using static System.Math; -using System.Text.Json; -using System.Text.Json.Serialization; -using InnovEnergy.Lib.Utils; -using System.CodeDom.Compiler; - -namespace InnovEnergy.Lib.Units; - -using T = ApparentPower; - -[GeneratedCode("generate.sh", "1")] -[JsonConverter(typeof(ApparentPowerConverter))] -public readonly partial struct ApparentPower -{ - public Double Value { get; } - public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit; - - // scalar multiplication - - public static T operator *(Double scalar, T t) => new T(scalar * t.Value); - public static T operator *(T t, Double scalar) => new T(scalar * t.Value); - public static T operator /(T t, Double scalar) => new T(t.Value / scalar); - - // addition - - public static T operator +(T left, T right) => new T(left.Value + right.Value); - public static T operator -(T left, T right) => new T(left.Value - right.Value); - public static T operator -(T t) => new T(-t.Value); - - // compare - - public static Boolean operator ==(T left, T right) => left.Value == right.Value; - public static Boolean operator !=(T left, T right) => left.Value != right.Value; - public static Boolean operator > (T left, T right) => left.Value > right.Value; - public static Boolean operator < (T left, T right) => left.Value < right.Value; - public static Boolean operator >=(T left, T right) => left.Value >= right.Value; - public static Boolean operator <=(T left, T right) => left.Value <= right.Value; - - // conversion - - public static implicit operator T(Double d) => new T(d); - - // equality - - public Boolean Equals(T other) => Value == other.Value; - public override Boolean Equals(Object? obj) => obj is T other && Equals(other); - public override Int32 GetHashCode() => Value.GetHashCode(); - -} - - -internal class ApparentPowerConverter : JsonConverter -{ - public override ApparentPower Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return new ApparentPower(reader.GetDouble()); - } - - public override void Write(Utf8JsonWriter writer, ApparentPower value, JsonSerializerOptions options) - { - var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits); - - writer.WriteNumberValue(rounded); - } -} \ No newline at end of file diff --git a/csharp/Lib/Units/Composite/Ac1Bus.cs b/csharp/Lib/Units/Composite/Ac1Bus.cs index 7a802e1e8..226f2104c 100644 --- a/csharp/Lib/Units/Composite/Ac1Bus.cs +++ b/csharp/Lib/Units/Composite/Ac1Bus.cs @@ -1,9 +1,11 @@ namespace InnovEnergy.Lib.Units.Composite; -public class Ac1Bus : AcPhase +public record Ac1Bus : AcPhase { - public Frequency Frequency { get; internal init; } + protected Ac1Bus() {} + + public Frequency Frequency { get; protected init; } public static Ac1Bus FromVoltageCurrentFrequencyPhi(Double voltageRms, @@ -15,22 +17,8 @@ public class Ac1Bus : AcPhase Current = currentRms, Voltage = voltageRms, Power = AcPower.FromVoltageCurrentPhi(voltageRms, currentRms, phi) - }; + }; } - // [SuppressMessage("ReSharper", "RedundantCast")] - // public static Ac1Bus operator |(Ac1Bus left, Ac1Bus right) - // { - // var f = left.Frequency | right.Frequency; - // var p = (AcPhase)left | (AcPhase)right; - // - // return new Ac1Bus - // { - // Frequency = f, - // Current = p.Current, - // Voltage = p.Voltage, - // Phi = p.Phi - // }; - // } \ No newline at end of file diff --git a/csharp/Lib/Units/Composite/Ac3Bus.cs b/csharp/Lib/Units/Composite/Ac3Bus.cs index d9a1e2b42..8eaba22ad 100644 --- a/csharp/Lib/Units/Composite/Ac3Bus.cs +++ b/csharp/Lib/Units/Composite/Ac3Bus.cs @@ -4,13 +4,15 @@ namespace InnovEnergy.Lib.Units.Composite; #pragma warning disable CS8618 -public class Ac3Bus +public record Ac3Bus { - public AcPhase L1 { get; internal init; } - public AcPhase L2 { get; internal init; } - public AcPhase L3 { get; internal init; } - public AcPower Power { get; internal init; } - public Frequency Frequency { get; internal init; } + protected Ac3Bus() {} + + public AcPhase L1 { get; protected init; } + public AcPhase L2 { get; protected init; } + public AcPhase L3 { get; protected init; } + public AcPower Power { get; protected init; } + public Frequency Frequency { get; protected init; } public static Ac3Bus FromPhasesAndFrequency(AcPhase l1, AcPhase l2, @@ -23,4 +25,4 @@ public class Ac3Bus Power = AcPower.SumOf(l1.Power, l2.Power, l3.Power), Frequency = frequency, }; -} \ No newline at end of file +} \ No newline at end of file diff --git a/csharp/Lib/Units/Composite/AcPhase.cs b/csharp/Lib/Units/Composite/AcPhase.cs index 1f99214fe..2ca82572d 100644 --- a/csharp/Lib/Units/Composite/AcPhase.cs +++ b/csharp/Lib/Units/Composite/AcPhase.cs @@ -2,9 +2,11 @@ namespace InnovEnergy.Lib.Units.Composite; #pragma warning disable CS8618 -public class AcPhase : Bus +public record AcPhase : Bus { - public AcPower Power { get; internal init; } + protected AcPhase(){} + + public AcPower Power { get; protected init; } public static AcPhase FromVoltageCurrentPhi(Voltage voltageRms, Current currentRms, diff --git a/csharp/Lib/Units/Composite/AcPower.cs b/csharp/Lib/Units/Composite/AcPower.cs index 68e9a1727..df5be0992 100644 --- a/csharp/Lib/Units/Composite/AcPower.cs +++ b/csharp/Lib/Units/Composite/AcPower.cs @@ -2,15 +2,16 @@ using static System.Math; namespace InnovEnergy.Lib.Units.Composite; -public class AcPower +public record AcPower { - public ApparentPower Apparent { get; internal init; } - public ActivePower Active { get; internal init; } - public ReactivePower Reactive { get; internal init; } - public Angle Phi { get; internal init; } - public Double CosPhi { get; internal init; } - - + protected AcPower(){} + + public ApparentPower Apparent { get; protected init; } + public ActivePower Active { get; protected init; } + public ReactivePower Reactive { get; protected init; } + public Angle Phi { get; protected init; } + public Double CosPhi { get; protected init; } + public static AcPower FromActiveReactiveApparent(ActivePower activePower, ReactivePower reactivePower, ApparentPower apparentPower) { var q = reactivePower.Value; @@ -24,7 +25,7 @@ public class AcPower Reactive = q, Apparent = s, Phi = phi, - CosPhi = p/s, + CosPhi = Cos(phi), }; } @@ -81,8 +82,4 @@ public class AcPower CosPhi = Cos(phi.Value) }; } - - private static Double NormalizePhi(Double phi) => (phi + PI) % Tau - PI; // Tau is 2pi - - } \ No newline at end of file diff --git a/csharp/Lib/Units/Composite/Bus.cs b/csharp/Lib/Units/Composite/Bus.cs index 396723c69..1f2f9867f 100644 --- a/csharp/Lib/Units/Composite/Bus.cs +++ b/csharp/Lib/Units/Composite/Bus.cs @@ -4,8 +4,8 @@ namespace InnovEnergy.Lib.Units.Composite; [SuppressMessage("ReSharper", "MemberCanBeProtected.Global")] -public abstract class Bus +public abstract record Bus { - public Voltage Voltage { get; internal init; } - public Current Current { get; internal init; } + public Voltage Voltage { get; protected init; } + public Current Current { get; protected init; } } \ No newline at end of file diff --git a/csharp/Lib/Units/Composite/DcBus.cs b/csharp/Lib/Units/Composite/DcBus.cs index 9e9f3d373..e0ea1c504 100644 --- a/csharp/Lib/Units/Composite/DcBus.cs +++ b/csharp/Lib/Units/Composite/DcBus.cs @@ -1,15 +1,15 @@ namespace InnovEnergy.Lib.Units.Composite; -public class DcBus : Bus +public record DcBus : Bus { - private DcBus() {} + protected DcBus() {} - public ActivePower Power { get; internal init; } + public ActivePower Power { get; protected init; } public static DcBus FromVoltageCurrent(Voltage voltage, Current current) => new() { Voltage = voltage, Current = current, - Power = current * voltage, + Power = current.Value * voltage.Value, }; } \ No newline at end of file diff --git a/csharp/Lib/Units/Current.cs b/csharp/Lib/Units/Current.cs index 15df03a03..db85db843 100644 --- a/csharp/Lib/Units/Current.cs +++ b/csharp/Lib/Units/Current.cs @@ -1,15 +1,16 @@ -using InnovEnergy.Lib.Units.Generator; +using InnovEnergy.Lib.SrcGen; +using InnovEnergy.Lib.SrcGen.Attributes; namespace InnovEnergy.Lib.Units; -[Generate] +[Generate][Generate] public readonly partial struct Current { public static String Unit => "A"; public static String Symbol => "I"; - public Current(Double value) => Value = value; - + + // P=UI public static ActivePower operator *(Current current, Voltage voltage) => new ActivePower(current.Value * voltage.Value); diff --git a/csharp/Lib/Units/Current.generated.cs b/csharp/Lib/Units/Current.generated.cs deleted file mode 100644 index 6496827fe..000000000 --- a/csharp/Lib/Units/Current.generated.cs +++ /dev/null @@ -1,68 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -#define Generate - -using static System.Math; -using System.Text.Json; -using System.Text.Json.Serialization; -using InnovEnergy.Lib.Utils; -using System.CodeDom.Compiler; - -namespace InnovEnergy.Lib.Units; - -using T = Current; - -[GeneratedCode("generate.sh", "1")] -[JsonConverter(typeof(CurrentConverter))] -public readonly partial struct Current -{ - public Double Value { get; } - public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit; - - // scalar multiplication - - public static T operator *(Double scalar, T t) => new T(scalar * t.Value); - public static T operator *(T t, Double scalar) => new T(scalar * t.Value); - public static T operator /(T t, Double scalar) => new T(t.Value / scalar); - - // addition - - public static T operator +(T left, T right) => new T(left.Value + right.Value); - public static T operator -(T left, T right) => new T(left.Value - right.Value); - public static T operator -(T t) => new T(-t.Value); - - // compare - - public static Boolean operator ==(T left, T right) => left.Value == right.Value; - public static Boolean operator !=(T left, T right) => left.Value != right.Value; - public static Boolean operator > (T left, T right) => left.Value > right.Value; - public static Boolean operator < (T left, T right) => left.Value < right.Value; - public static Boolean operator >=(T left, T right) => left.Value >= right.Value; - public static Boolean operator <=(T left, T right) => left.Value <= right.Value; - - // conversion - - public static implicit operator T(Double d) => new T(d); - - // equality - - public Boolean Equals(T other) => Value == other.Value; - public override Boolean Equals(Object? obj) => obj is T other && Equals(other); - public override Int32 GetHashCode() => Value.GetHashCode(); - -} - - -internal class CurrentConverter : JsonConverter -{ - public override Current Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return new Current(reader.GetDouble()); - } - - public override void Write(Utf8JsonWriter writer, Current value, JsonSerializerOptions options) - { - var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits); - - writer.WriteNumberValue(rounded); - } -} \ No newline at end of file diff --git a/csharp/Lib/Units/DcPower.cs b/csharp/Lib/Units/DcPower.cs new file mode 100644 index 000000000..d124d9f9e --- /dev/null +++ b/csharp/Lib/Units/DcPower.cs @@ -0,0 +1,22 @@ +using InnovEnergy.Lib.SrcGen.Attributes; +using InnovEnergy.Lib.Time.Unix; + +namespace InnovEnergy.Lib.Units; + + +[Generate][Generate] +public readonly partial struct DcPower +{ + public static String Unit => "W"; + public static String Symbol => "P"; + + // P=UI + public static Voltage operator /(DcPower power, Current current) => new Voltage(power.Value / current.Value); + public static Current operator /(DcPower power, Voltage voltage) => new Current(power.Value / voltage.Value); + + public static Energy operator *(DcPower power, TimeSpan timeSpan) => power.Value / 1000 * timeSpan.TotalHours; + public static Energy operator *(TimeSpan timeSpan, DcPower power) => power.Value / 1000 * timeSpan.TotalHours; + + public static Energy operator *(DcPower power, UnixTimeSpan timeSpan) => power.Value * timeSpan.Ticks / 3_600_000; + public static Energy operator *(UnixTimeSpan timeSpan, DcPower power) => power.Value * timeSpan.Ticks / 3_600_000; +} \ No newline at end of file diff --git a/csharp/Lib/Units/Energy.cs b/csharp/Lib/Units/Energy.cs index 474b8449b..310591c93 100644 --- a/csharp/Lib/Units/Energy.cs +++ b/csharp/Lib/Units/Energy.cs @@ -1,16 +1,15 @@ +using InnovEnergy.Lib.SrcGen; +using InnovEnergy.Lib.SrcGen.Attributes; using InnovEnergy.Lib.Time.Unix; -using InnovEnergy.Lib.Units.Generator; namespace InnovEnergy.Lib.Units; -[Generate] +[Generate][Generate] public readonly partial struct Energy { public static String Unit => "kWh"; public static String Symbol => "E"; - public Energy(Double value) => Value = value; - public static ActivePower operator /(Energy energy, TimeSpan timeSpan) => energy.Value * 1000 / timeSpan.TotalHours ; public static ActivePower operator /(Energy energy, UnixTimeSpan timeSpan) => energy.Value * 3_600_000 / timeSpan.Ticks; } \ No newline at end of file diff --git a/csharp/Lib/Units/Energy.generated.cs b/csharp/Lib/Units/Energy.generated.cs deleted file mode 100644 index 9e8a95005..000000000 --- a/csharp/Lib/Units/Energy.generated.cs +++ /dev/null @@ -1,68 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -#define Generate - -using static System.Math; -using System.Text.Json; -using System.Text.Json.Serialization; -using InnovEnergy.Lib.Utils; -using System.CodeDom.Compiler; - -namespace InnovEnergy.Lib.Units; - -using T = Energy; - -[GeneratedCode("generate.sh", "1")] -[JsonConverter(typeof(EnergyConverter))] -public readonly partial struct Energy -{ - public Double Value { get; } - public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit; - - // scalar multiplication - - public static T operator *(Double scalar, T t) => new T(scalar * t.Value); - public static T operator *(T t, Double scalar) => new T(scalar * t.Value); - public static T operator /(T t, Double scalar) => new T(t.Value / scalar); - - // addition - - public static T operator +(T left, T right) => new T(left.Value + right.Value); - public static T operator -(T left, T right) => new T(left.Value - right.Value); - public static T operator -(T t) => new T(-t.Value); - - // compare - - public static Boolean operator ==(T left, T right) => left.Value == right.Value; - public static Boolean operator !=(T left, T right) => left.Value != right.Value; - public static Boolean operator > (T left, T right) => left.Value > right.Value; - public static Boolean operator < (T left, T right) => left.Value < right.Value; - public static Boolean operator >=(T left, T right) => left.Value >= right.Value; - public static Boolean operator <=(T left, T right) => left.Value <= right.Value; - - // conversion - - public static implicit operator T(Double d) => new T(d); - - // equality - - public Boolean Equals(T other) => Value == other.Value; - public override Boolean Equals(Object? obj) => obj is T other && Equals(other); - public override Int32 GetHashCode() => Value.GetHashCode(); - -} - - -internal class EnergyConverter : JsonConverter -{ - public override Energy Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return new Energy(reader.GetDouble()); - } - - public override void Write(Utf8JsonWriter writer, Energy value, JsonSerializerOptions options) - { - var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits); - - writer.WriteNumberValue(rounded); - } -} \ No newline at end of file diff --git a/csharp/Lib/Units/Frequency.cs b/csharp/Lib/Units/Frequency.cs index 6f7e2c075..f0a227c14 100644 --- a/csharp/Lib/Units/Frequency.cs +++ b/csharp/Lib/Units/Frequency.cs @@ -1,9 +1,9 @@ -using InnovEnergy.Lib.Units.Generator; +using InnovEnergy.Lib.SrcGen; +using InnovEnergy.Lib.SrcGen.Attributes; namespace InnovEnergy.Lib.Units; - -[Generate] +[Generate("HAS_CONSTRUCTOR")][Generate] public readonly partial struct Frequency { public static String Unit => "Hz"; diff --git a/csharp/Lib/Units/Frequency.generated.cs b/csharp/Lib/Units/Frequency.generated.cs deleted file mode 100644 index 06e4d7fce..000000000 --- a/csharp/Lib/Units/Frequency.generated.cs +++ /dev/null @@ -1,68 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -#define Generate - -using static System.Math; -using System.Text.Json; -using System.Text.Json.Serialization; -using InnovEnergy.Lib.Utils; -using System.CodeDom.Compiler; - -namespace InnovEnergy.Lib.Units; - -using T = Frequency; - -[GeneratedCode("generate.sh", "1")] -[JsonConverter(typeof(FrequencyConverter))] -public readonly partial struct Frequency -{ - public Double Value { get; } - public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit; - - // scalar multiplication - - public static T operator *(Double scalar, T t) => new T(scalar * t.Value); - public static T operator *(T t, Double scalar) => new T(scalar * t.Value); - public static T operator /(T t, Double scalar) => new T(t.Value / scalar); - - // addition - - public static T operator +(T left, T right) => new T(left.Value + right.Value); - public static T operator -(T left, T right) => new T(left.Value - right.Value); - public static T operator -(T t) => new T(-t.Value); - - // compare - - public static Boolean operator ==(T left, T right) => left.Value == right.Value; - public static Boolean operator !=(T left, T right) => left.Value != right.Value; - public static Boolean operator > (T left, T right) => left.Value > right.Value; - public static Boolean operator < (T left, T right) => left.Value < right.Value; - public static Boolean operator >=(T left, T right) => left.Value >= right.Value; - public static Boolean operator <=(T left, T right) => left.Value <= right.Value; - - // conversion - - public static implicit operator T(Double d) => new T(d); - - // equality - - public Boolean Equals(T other) => Value == other.Value; - public override Boolean Equals(Object? obj) => obj is T other && Equals(other); - public override Int32 GetHashCode() => Value.GetHashCode(); - -} - - -internal class FrequencyConverter : JsonConverter -{ - public override Frequency Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return new Frequency(reader.GetDouble()); - } - - public override void Write(Utf8JsonWriter writer, Frequency value, JsonSerializerOptions options) - { - var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits); - - writer.WriteNumberValue(rounded); - } -} \ No newline at end of file diff --git a/csharp/Lib/Units/Generator/Average.cs b/csharp/Lib/Units/Generator/Average.cs new file mode 100644 index 000000000..6ec0c6ad5 --- /dev/null +++ b/csharp/Lib/Units/Generator/Average.cs @@ -0,0 +1,11 @@ + +// ReSharper disable once CheckNamespace +namespace InnovEnergy.Lib.Units; + +public readonly struct Average +{ + public Average(Double value) => throw new NotImplementedException(); + public Double Value => throw new NotImplementedException(); + + public static Average operator |(Average left, Average right) => new((left.Value + right.Value) / 2); +} \ No newline at end of file diff --git a/csharp/Lib/Units/Generator/GenerateAttribute.cs b/csharp/Lib/Units/Generator/GenerateAttribute.cs deleted file mode 100644 index 12337cf50..000000000 --- a/csharp/Lib/Units/Generator/GenerateAttribute.cs +++ /dev/null @@ -1,6 +0,0 @@ -using System.Text.Json.Serialization; - -namespace InnovEnergy.Lib.Units.Generator; - -internal class GenerateAttribute : Attribute -{} \ No newline at end of file diff --git a/csharp/Lib/Units/Generator/Operators.cs b/csharp/Lib/Units/Generator/Operators.cs new file mode 100644 index 000000000..1c28c9427 --- /dev/null +++ b/csharp/Lib/Units/Generator/Operators.cs @@ -0,0 +1,51 @@ +using System.Diagnostics.CodeAnalysis; +using InnovEnergy.Lib.Utils; + +// ReSharper disable once CheckNamespace +namespace InnovEnergy.Lib.Units; + +[SuppressMessage("ReSharper", "CompareOfFloatsByEqualityOperator")] +public readonly struct Operators +{ + public static String Unit => throw new NotImplementedException(); + public static String Symbol => throw new NotImplementedException(); + + #if !HAS_CONSTRUCTOR + public Operators(Double value) => Value = value; + #endif + + public Double Value { get; } + public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit; + + // scalar multiplication + + public static Operators operator *(Double scalar, Operators t) => new Operators(scalar * t.Value); + public static Operators operator *(Operators t, Double scalar) => new Operators(scalar * t.Value); + public static Operators operator /(Operators t, Double scalar) => new Operators(t.Value / scalar); + + // addition + + public static Operators operator +(Operators left, Operators right) => new Operators(left.Value + right.Value); + public static Operators operator -(Operators left, Operators right) => new Operators(left.Value - right.Value); + public static Operators operator -(Operators t) => new Operators(-t.Value); + + // compare + + public static Boolean operator ==(Operators left, Operators right) => left.Value == right.Value; + public static Boolean operator !=(Operators left, Operators right) => left.Value != right.Value; + public static Boolean operator > (Operators left, Operators right) => left.Value > right.Value; + public static Boolean operator < (Operators left, Operators right) => left.Value < right.Value; + public static Boolean operator >=(Operators left, Operators right) => left.Value >= right.Value; + public static Boolean operator <=(Operators left, Operators right) => left.Value <= right.Value; + + // conversion + + public static implicit operator Operators(Double d) => new Operators(d); + + // equality + + public Boolean Equals(Operators other) => Value == other.Value; + public override Boolean Equals(Object? obj) => obj is Operators other && Equals(other); + public override Int32 GetHashCode() => Value.GetHashCode(); + +} \ No newline at end of file diff --git a/csharp/Lib/Units/Generator/Parallel.cs b/csharp/Lib/Units/Generator/Parallel.cs new file mode 100644 index 000000000..942cb416e --- /dev/null +++ b/csharp/Lib/Units/Generator/Parallel.cs @@ -0,0 +1,12 @@ +using System.Diagnostics.CodeAnalysis; + +// ReSharper disable once CheckNamespace +namespace InnovEnergy.Lib.Units; + +public readonly struct Parallel +{ + public Parallel(Double value) => throw new NotImplementedException(); + public Double Value => throw new NotImplementedException(); + + public static Parallel operator |(Parallel left, Parallel right) => new((left.Value * right.Value) / (left.Value + right.Value)); +} \ No newline at end of file diff --git a/csharp/Lib/Units/Generator/Sum.cs b/csharp/Lib/Units/Generator/Sum.cs new file mode 100644 index 000000000..30b06abff --- /dev/null +++ b/csharp/Lib/Units/Generator/Sum.cs @@ -0,0 +1,11 @@ + +// ReSharper disable once CheckNamespace +namespace InnovEnergy.Lib.Units; + +public readonly struct Sum +{ + public Sum(Double value) => throw new NotImplementedException(); + public Double Value => throw new NotImplementedException(); + + public static Sum operator |(Sum left, Sum right) => new(left.Value + right.Value); +} \ No newline at end of file diff --git a/csharp/Lib/Units/Generator/Template.txt b/csharp/Lib/Units/Generator/Template.txt deleted file mode 100644 index d88787a50..000000000 --- a/csharp/Lib/Units/Generator/Template.txt +++ /dev/null @@ -1,68 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -#define AggregationType - -using static System.Math; -using System.Text.Json; -using System.Text.Json.Serialization; -using InnovEnergy.Lib.Utils; -using System.CodeDom.Compiler; - -namespace InnovEnergy.Lib.Units; - -using T = Template; - -[GeneratedCode("generate.sh", "1")] -[JsonConverter(typeof(TemplateConverter))] -public readonly partial struct Template -{ - public Double Value { get; } - public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit; - - // scalar multiplication - - public static T operator *(Double scalar, T t) => new T(scalar * t.Value); - public static T operator *(T t, Double scalar) => new T(scalar * t.Value); - public static T operator /(T t, Double scalar) => new T(t.Value / scalar); - - // addition - - public static T operator +(T left, T right) => new T(left.Value + right.Value); - public static T operator -(T left, T right) => new T(left.Value - right.Value); - public static T operator -(T t) => new T(-t.Value); - - // compare - - public static Boolean operator ==(T left, T right) => left.Value == right.Value; - public static Boolean operator !=(T left, T right) => left.Value != right.Value; - public static Boolean operator > (T left, T right) => left.Value > right.Value; - public static Boolean operator < (T left, T right) => left.Value < right.Value; - public static Boolean operator >=(T left, T right) => left.Value >= right.Value; - public static Boolean operator <=(T left, T right) => left.Value <= right.Value; - - // conversion - - public static implicit operator T(Double d) => new T(d); - - // equality - - public Boolean Equals(T other) => Value == other.Value; - public override Boolean Equals(Object? obj) => obj is T other && Equals(other); - public override Int32 GetHashCode() => Value.GetHashCode(); - -} - - -internal class TemplateConverter : JsonConverter