Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into AndreasLeeb/generic…
Browse files Browse the repository at this point in the history
…Math
  • Loading branch information
angularsen committed Dec 28, 2022
2 parents 65b5a1e + 48b2e88 commit d48d1e3
Show file tree
Hide file tree
Showing 131 changed files with 4,010 additions and 2,851 deletions.
58 changes: 33 additions & 25 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public string Generate()
Writer.WL(GeneratedFileHeader);
Writer.WL(@"
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
Expand Down Expand Up @@ -771,7 +772,7 @@ private void GenerateEqualityAndComparison()
/// <summary>Indicates strict equality of two <see cref=""{_quantity.Name}""/> quantities, where both <see cref=""Value"" /> and <see cref=""Unit"" /> are exactly equal.</summary>
/// <remarks>Consider using <see cref=""Equals({_quantity.Name}, {_valueType}, ComparisonType)""/> to check equality across different units and to specify a floating-point number error tolerance.</remarks>
[Obsolete(""Consider using Equals(Angle, {_valueType}, ComparisonType) to check equality across different units and to specify a floating-point number error tolerance."")]
public override bool Equals(object obj)
public override bool Equals(object? obj)
{{
if (obj is null || !(obj is {_quantity.Name} otherQuantity))
return false;
Expand Down Expand Up @@ -803,7 +804,7 @@ public bool Equals({_quantity.Name} other)
/// <item><term> Greater than zero</term><description> This instance follows <paramref name=""obj"" /> in the sort order.</description></item>
/// </list>
/// </returns>
public int CompareTo(object obj)
public int CompareTo(object? obj)
{{
if (obj is null) throw new ArgumentNullException(nameof(obj));
if (!(obj is {_quantity.Name} otherQuantity)) throw new ArgumentException(""Expected type {_quantity.Name}."", nameof(obj));
Expand Down Expand Up @@ -1005,15 +1006,15 @@ double IQuantity.As(Enum unit)
/// <param name=""unit"">The unit to convert to.</param>
/// <param name=""converted"">The converted <see cref=""{_quantity.Name}""/> in <paramref name=""unit""/>, if successful.</param>
/// <returns>True if successful, otherwise false.</returns>
private bool TryToUnit({_quantity.Name}Unit unit, out {_quantity.Name}? converted)
private bool TryToUnit({_quantity.Name}Unit unit, [NotNullWhen(true)] out {_quantity.Name}? converted)
{{
if (Unit == unit)
{{
converted = this;
return true;
}}
converted = (Unit, unit) switch
{_quantity.Name}? convertedOrNull = (Unit, unit) switch
{{
// {_quantity.Name}Unit -> BaseUnit");

Expand Down Expand Up @@ -1041,10 +1042,17 @@ private bool TryToUnit({_quantity.Name}Unit unit, out {_quantity.Name}? converte

Writer.WL();
Writer.WL($@"
_ => null!
_ => null
}};
return converted is not null;
if (convertedOrNull is null)
{{
converted = default;
return false;
}}
converted = convertedOrNull.Value;
return true;
}}
/// <inheritdoc />
Expand Down Expand Up @@ -1114,7 +1122,7 @@ public string ToString(IFormatProvider? provider)
/// </summary>
/// <param name=""format"">The format string.</param>
/// <returns>The string representation.</returns>
public string ToString(string format)
public string ToString(string? format)
{{
return ToString(format, CultureInfo.CurrentCulture);
}}
Expand All @@ -1126,7 +1134,7 @@ public string ToString(string format)
/// <param name=""format"">The format string.</param>
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
/// <returns>The string representation.</returns>
public string ToString(string format, IFormatProvider? provider)
public string ToString(string? format, IFormatProvider? provider)
{{
return QuantityFormatter.Format<{_unitEnumName}>(this, format, provider);
}}
Expand All @@ -1137,75 +1145,75 @@ public string ToString(string format, IFormatProvider? provider)

private void GenerateIConvertibleMethods()
{
Writer.WL($@"
Writer.WL($@"
#region IConvertible Methods
TypeCode IConvertible.GetTypeCode()
{{
return TypeCode.Object;
}}
bool IConvertible.ToBoolean(IFormatProvider provider)
bool IConvertible.ToBoolean(IFormatProvider? provider)
{{
throw new InvalidCastException($""Converting {{typeof({_quantity.Name})}} to bool is not supported."");
}}
byte IConvertible.ToByte(IFormatProvider provider)
byte IConvertible.ToByte(IFormatProvider? provider)
{{
return Convert.ToByte(_value);
}}
char IConvertible.ToChar(IFormatProvider provider)
char IConvertible.ToChar(IFormatProvider? provider)
{{
throw new InvalidCastException($""Converting {{typeof({_quantity.Name})}} to char is not supported."");
}}
DateTime IConvertible.ToDateTime(IFormatProvider provider)
DateTime IConvertible.ToDateTime(IFormatProvider? provider)
{{
throw new InvalidCastException($""Converting {{typeof({_quantity.Name})}} to DateTime is not supported."");
}}
decimal IConvertible.ToDecimal(IFormatProvider provider)
decimal IConvertible.ToDecimal(IFormatProvider? provider)
{{
return Convert.ToDecimal(_value);
}}
double IConvertible.ToDouble(IFormatProvider provider)
double IConvertible.ToDouble(IFormatProvider? provider)
{{
return Convert.ToDouble(_value);
}}
short IConvertible.ToInt16(IFormatProvider provider)
short IConvertible.ToInt16(IFormatProvider? provider)
{{
return Convert.ToInt16(_value);
}}
int IConvertible.ToInt32(IFormatProvider provider)
int IConvertible.ToInt32(IFormatProvider? provider)
{{
return Convert.ToInt32(_value);
}}
long IConvertible.ToInt64(IFormatProvider provider)
long IConvertible.ToInt64(IFormatProvider? provider)
{{
return Convert.ToInt64(_value);
}}
sbyte IConvertible.ToSByte(IFormatProvider provider)
sbyte IConvertible.ToSByte(IFormatProvider? provider)
{{
return Convert.ToSByte(_value);
}}
float IConvertible.ToSingle(IFormatProvider provider)
float IConvertible.ToSingle(IFormatProvider? provider)
{{
return Convert.ToSingle(_value);
}}
string IConvertible.ToString(IFormatProvider provider)
string IConvertible.ToString(IFormatProvider? provider)
{{
return ToString(""g"", provider);
}}
object IConvertible.ToType(Type conversionType, IFormatProvider provider)
object IConvertible.ToType(Type conversionType, IFormatProvider? provider)
{{
if (conversionType == typeof({_quantity.Name}))
return this;
Expand All @@ -1219,17 +1227,17 @@ object IConvertible.ToType(Type conversionType, IFormatProvider provider)
throw new InvalidCastException($""Converting {{typeof({_quantity.Name})}} to {{conversionType}} is not supported."");
}}
ushort IConvertible.ToUInt16(IFormatProvider provider)
ushort IConvertible.ToUInt16(IFormatProvider? provider)
{{
return Convert.ToUInt16(_value);
}}
uint IConvertible.ToUInt32(IFormatProvider provider)
uint IConvertible.ToUInt32(IFormatProvider? provider)
{{
return Convert.ToUInt32(_value);
}}
ulong IConvertible.ToUInt64(IFormatProvider provider)
ulong IConvertible.ToUInt64(IFormatProvider? provider)
{{
return Convert.ToUInt64(_value);
}}
Expand Down
5 changes: 3 additions & 2 deletions CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public string Generate()
using System.Globalization;
using UnitsNet.Units;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#nullable enable
Expand Down Expand Up @@ -70,7 +71,7 @@ public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, QuantityValu
/// <param name=""unit"">Unit enum value.</param>
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>
/// <returns><c>True</c> if successful with <paramref name=""quantity""/> assigned the value, otherwise <c>false</c>.</returns>
public static bool TryFrom(QuantityValue value, Enum unit, out IQuantity? quantity)
public static bool TryFrom(QuantityValue value, Enum unit, [NotNullWhen(true)] out IQuantity? quantity)
{
switch (unit)
{");
Expand Down Expand Up @@ -102,7 +103,7 @@ public static bool TryFrom(QuantityValue value, Enum unit, out IQuantity? quanti
/// <param name=""quantityString"">Quantity string representation, such as ""1.5 kg"". Must be compatible with given quantity type.</param>
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>
/// <returns>The parsed quantity.</returns>
public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, string quantityString, out IQuantity? quantity)
public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, string quantityString, [NotNullWhen(true)] out IQuantity? quantity)
{
quantity = default(IQuantity);
Expand Down
10 changes: 8 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<!-- This file will be implicitly linked by all projects in folder -folders -->
<PropertyGroup>
<!-- Default to output to Artifacts folder -->
<OutputPath>$(MSBuildThisFileDirectory)Artifacts/$(MSBuildProjectName)</OutputPath>
<OutputPath>$(MSBuildThisFileDirectory)Artifacts/$(MSBuildProjectName)</OutputPath>
<!-- Specific output folder for .NET nanoFramework projects -->
<OutputPath Condition=" '$(TargetFrameworkIdentifier)' == '.NETnanoFramework'">$(MSBuildThisFileDirectory)Artifacts/UnitsNet.NanoFramework/$(MSBuildProjectName)</OutputPath>
<OutputPath Condition=" '$(TargetFrameworkIdentifier)' == '.NETnanoFramework'">$(MSBuildThisFileDirectory)Artifacts/UnitsNet.NanoFramework/$(MSBuildProjectName)</OutputPath>

</PropertyGroup>

Expand All @@ -17,6 +17,12 @@
<PropertyGroup>
<!-- Warning instead of compile error on obsolete errors.-->
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<!-- 612: obsolete, 618: obsolete with message -->
<WarningsNotAsErrors>612,618</WarningsNotAsErrors>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)/NullableAttributes.cs" />
</ItemGroup>

</Project>
Loading

0 comments on commit d48d1e3

Please sign in to comment.