Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the missing BaseUnits #1473

Merged
merged 9 commits into from
Dec 30, 2024
  •  
  •  
  •  
89 changes: 48 additions & 41 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ private void GenerateInstanceConstructors()
Writer.WL($@"
_unit = unit;
}}

");
if (!_isDimensionless)
{
Writer.WL($@"
/// <summary>
/// Creates an instance of the quantity with the given numeric value in units compatible with the given <see cref=""UnitSystem""/>.
/// If multiple compatible units were found, the first match is used.
Expand All @@ -225,18 +228,11 @@ private void GenerateInstanceConstructors()
/// <exception cref=""ArgumentException"">No unit was found for the given <see cref=""UnitSystem""/>.</exception>
public {_quantity.Name}(double value, UnitSystem unitSystem)
{{
if (unitSystem is null) throw new ArgumentNullException(nameof(unitSystem));

var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);
var firstUnitInfo = unitInfos.FirstOrDefault();
");

Writer.WL(@"
_value = value;");
Writer.WL($@"
_unit = firstUnitInfo?.Value ?? throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));
_value = value;
_unit = Info.GetDefaultUnit(unitSystem);
}}
");
}
}

private void GenerateStaticProperties()
Expand Down Expand Up @@ -1000,34 +996,28 @@ public double As({_unitEnumName} unit)
}}
");

Writer.WL($@"
Writer.WL(_isDimensionless
? $@"

/// <inheritdoc cref=""IQuantity.As(UnitSystem)""/>
public double As(UnitSystem unitSystem)
{{
if (unitSystem is null)
throw new ArgumentNullException(nameof(unitSystem));

var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);

var firstUnitInfo = unitInfos.FirstOrDefault();
if (firstUnitInfo == null)
throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));

return As(firstUnitInfo.Value);
return As(BaseUnit);
}}
");
"
: $@"

Writer.WL($@"
/// <inheritdoc />
double IQuantity.As(Enum unit)
/// <inheritdoc cref=""IQuantity.As(UnitSystem)""/>
public double As(UnitSystem unitSystem)
{{
if (!(unit is {_unitEnumName} typedUnit))
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));

return As(typedUnit);
return As(Info.GetDefaultUnit(unitSystem));
}}
");

Writer.WL($@"
/// <summary>
/// Converts this {_quantity.Name} to another {_quantity.Name} with the unit representation <paramref name=""unit"" />.
/// </summary>
Expand Down Expand Up @@ -1123,29 +1113,44 @@ private bool TryToUnit({_unitEnumName} unit, [NotNullWhen(true)] out {_quantity.
converted = convertedOrNull.Value;
return true;
}}

/// <inheritdoc />
IQuantity IQuantity.ToUnit(Enum unit)
");
Writer.WL(_isDimensionless
? $@"
/// <inheritdoc cref=""IQuantity.ToUnit(UnitSystem)""/>
public {_quantity.Name} ToUnit(UnitSystem unitSystem)
{{
if (!(unit is {_unitEnumName} typedUnit))
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));
if (unitSystem is null)
throw new ArgumentNullException(nameof(unitSystem));

return ToUnit(typedUnit, DefaultConversionFunctions);
return ToUnit(BaseUnit);
}}

"
: $@"
/// <inheritdoc cref=""IQuantity.ToUnit(UnitSystem)""/>
public {_quantity.Name} ToUnit(UnitSystem unitSystem)
{{
if (unitSystem is null)
throw new ArgumentNullException(nameof(unitSystem));
return ToUnit(Info.GetDefaultUnit(unitSystem));
}}
");
lipchev marked this conversation as resolved.
Show resolved Hide resolved

var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);
Writer.WL($@"
#region Explicit implementations

var firstUnitInfo = unitInfos.FirstOrDefault();
if (firstUnitInfo == null)
throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));
double IQuantity.As(Enum unit)
{{
if (unit is not {_unitEnumName} typedUnit)
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));

return As(typedUnit);
}}

/// <inheritdoc />
IQuantity IQuantity.ToUnit(Enum unit)
{{
if (!(unit is {_unitEnumName} typedUnit))
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));

return ToUnit(firstUnitInfo.Value);
return ToUnit(typedUnit, DefaultConversionFunctions);
}}

/// <inheritdoc />
Expand All @@ -1158,6 +1163,8 @@ IQuantity IQuantity.ToUnit(Enum unit)
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem);

#endregion

#endregion
");
}

Expand Down
Loading