Skip to content
  •  
  •  
  •  
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));
}}
");

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