Skip to content

Commit d48d1e3

Browse files
committed
Merge remote-tracking branch 'origin/master' into AndreasLeeb/genericMath
2 parents 65b5a1e + 48b2e88 commit d48d1e3

File tree

131 files changed

+4010
-2851
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+4010
-2851
lines changed

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public string Generate()
3737
Writer.WL(GeneratedFileHeader);
3838
Writer.WL(@"
3939
using System;
40+
using System.Diagnostics.CodeAnalysis;
4041
using System.Globalization;
4142
using System.Linq;
4243
using System.Runtime.Serialization;
@@ -771,7 +772,7 @@ private void GenerateEqualityAndComparison()
771772
/// <summary>Indicates strict equality of two <see cref=""{_quantity.Name}""/> quantities, where both <see cref=""Value"" /> and <see cref=""Unit"" /> are exactly equal.</summary>
772773
/// <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>
773774
[Obsolete(""Consider using Equals(Angle, {_valueType}, ComparisonType) to check equality across different units and to specify a floating-point number error tolerance."")]
774-
public override bool Equals(object obj)
775+
public override bool Equals(object? obj)
775776
{{
776777
if (obj is null || !(obj is {_quantity.Name} otherQuantity))
777778
return false;
@@ -803,7 +804,7 @@ public bool Equals({_quantity.Name} other)
803804
/// <item><term> Greater than zero</term><description> This instance follows <paramref name=""obj"" /> in the sort order.</description></item>
804805
/// </list>
805806
/// </returns>
806-
public int CompareTo(object obj)
807+
public int CompareTo(object? obj)
807808
{{
808809
if (obj is null) throw new ArgumentNullException(nameof(obj));
809810
if (!(obj is {_quantity.Name} otherQuantity)) throw new ArgumentException(""Expected type {_quantity.Name}."", nameof(obj));
@@ -1005,15 +1006,15 @@ double IQuantity.As(Enum unit)
10051006
/// <param name=""unit"">The unit to convert to.</param>
10061007
/// <param name=""converted"">The converted <see cref=""{_quantity.Name}""/> in <paramref name=""unit""/>, if successful.</param>
10071008
/// <returns>True if successful, otherwise false.</returns>
1008-
private bool TryToUnit({_quantity.Name}Unit unit, out {_quantity.Name}? converted)
1009+
private bool TryToUnit({_quantity.Name}Unit unit, [NotNullWhen(true)] out {_quantity.Name}? converted)
10091010
{{
10101011
if (Unit == unit)
10111012
{{
10121013
converted = this;
10131014
return true;
10141015
}}
10151016
1016-
converted = (Unit, unit) switch
1017+
{_quantity.Name}? convertedOrNull = (Unit, unit) switch
10171018
{{
10181019
// {_quantity.Name}Unit -> BaseUnit");
10191020

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

10421043
Writer.WL();
10431044
Writer.WL($@"
1044-
_ => null!
1045+
_ => null
10451046
}};
10461047
1047-
return converted is not null;
1048+
if (convertedOrNull is null)
1049+
{{
1050+
converted = default;
1051+
return false;
1052+
}}
1053+
1054+
converted = convertedOrNull.Value;
1055+
return true;
10481056
}}
10491057
10501058
/// <inheritdoc />
@@ -1114,7 +1122,7 @@ public string ToString(IFormatProvider? provider)
11141122
/// </summary>
11151123
/// <param name=""format"">The format string.</param>
11161124
/// <returns>The string representation.</returns>
1117-
public string ToString(string format)
1125+
public string ToString(string? format)
11181126
{{
11191127
return ToString(format, CultureInfo.CurrentCulture);
11201128
}}
@@ -1126,7 +1134,7 @@ public string ToString(string format)
11261134
/// <param name=""format"">The format string.</param>
11271135
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
11281136
/// <returns>The string representation.</returns>
1129-
public string ToString(string format, IFormatProvider? provider)
1137+
public string ToString(string? format, IFormatProvider? provider)
11301138
{{
11311139
return QuantityFormatter.Format<{_unitEnumName}>(this, format, provider);
11321140
}}
@@ -1137,75 +1145,75 @@ public string ToString(string format, IFormatProvider? provider)
11371145

11381146
private void GenerateIConvertibleMethods()
11391147
{
1140-
Writer.WL($@"
1148+
Writer.WL($@"
11411149
#region IConvertible Methods
11421150
11431151
TypeCode IConvertible.GetTypeCode()
11441152
{{
11451153
return TypeCode.Object;
11461154
}}
11471155
1148-
bool IConvertible.ToBoolean(IFormatProvider provider)
1156+
bool IConvertible.ToBoolean(IFormatProvider? provider)
11491157
{{
11501158
throw new InvalidCastException($""Converting {{typeof({_quantity.Name})}} to bool is not supported."");
11511159
}}
11521160
1153-
byte IConvertible.ToByte(IFormatProvider provider)
1161+
byte IConvertible.ToByte(IFormatProvider? provider)
11541162
{{
11551163
return Convert.ToByte(_value);
11561164
}}
11571165
1158-
char IConvertible.ToChar(IFormatProvider provider)
1166+
char IConvertible.ToChar(IFormatProvider? provider)
11591167
{{
11601168
throw new InvalidCastException($""Converting {{typeof({_quantity.Name})}} to char is not supported."");
11611169
}}
11621170
1163-
DateTime IConvertible.ToDateTime(IFormatProvider provider)
1171+
DateTime IConvertible.ToDateTime(IFormatProvider? provider)
11641172
{{
11651173
throw new InvalidCastException($""Converting {{typeof({_quantity.Name})}} to DateTime is not supported."");
11661174
}}
11671175
1168-
decimal IConvertible.ToDecimal(IFormatProvider provider)
1176+
decimal IConvertible.ToDecimal(IFormatProvider? provider)
11691177
{{
11701178
return Convert.ToDecimal(_value);
11711179
}}
11721180
1173-
double IConvertible.ToDouble(IFormatProvider provider)
1181+
double IConvertible.ToDouble(IFormatProvider? provider)
11741182
{{
11751183
return Convert.ToDouble(_value);
11761184
}}
11771185
1178-
short IConvertible.ToInt16(IFormatProvider provider)
1186+
short IConvertible.ToInt16(IFormatProvider? provider)
11791187
{{
11801188
return Convert.ToInt16(_value);
11811189
}}
11821190
1183-
int IConvertible.ToInt32(IFormatProvider provider)
1191+
int IConvertible.ToInt32(IFormatProvider? provider)
11841192
{{
11851193
return Convert.ToInt32(_value);
11861194
}}
11871195
1188-
long IConvertible.ToInt64(IFormatProvider provider)
1196+
long IConvertible.ToInt64(IFormatProvider? provider)
11891197
{{
11901198
return Convert.ToInt64(_value);
11911199
}}
11921200
1193-
sbyte IConvertible.ToSByte(IFormatProvider provider)
1201+
sbyte IConvertible.ToSByte(IFormatProvider? provider)
11941202
{{
11951203
return Convert.ToSByte(_value);
11961204
}}
11971205
1198-
float IConvertible.ToSingle(IFormatProvider provider)
1206+
float IConvertible.ToSingle(IFormatProvider? provider)
11991207
{{
12001208
return Convert.ToSingle(_value);
12011209
}}
12021210
1203-
string IConvertible.ToString(IFormatProvider provider)
1211+
string IConvertible.ToString(IFormatProvider? provider)
12041212
{{
12051213
return ToString(""g"", provider);
12061214
}}
12071215
1208-
object IConvertible.ToType(Type conversionType, IFormatProvider provider)
1216+
object IConvertible.ToType(Type conversionType, IFormatProvider? provider)
12091217
{{
12101218
if (conversionType == typeof({_quantity.Name}))
12111219
return this;
@@ -1219,17 +1227,17 @@ object IConvertible.ToType(Type conversionType, IFormatProvider provider)
12191227
throw new InvalidCastException($""Converting {{typeof({_quantity.Name})}} to {{conversionType}} is not supported."");
12201228
}}
12211229
1222-
ushort IConvertible.ToUInt16(IFormatProvider provider)
1230+
ushort IConvertible.ToUInt16(IFormatProvider? provider)
12231231
{{
12241232
return Convert.ToUInt16(_value);
12251233
}}
12261234
1227-
uint IConvertible.ToUInt32(IFormatProvider provider)
1235+
uint IConvertible.ToUInt32(IFormatProvider? provider)
12281236
{{
12291237
return Convert.ToUInt32(_value);
12301238
}}
12311239
1232-
ulong IConvertible.ToUInt64(IFormatProvider provider)
1240+
ulong IConvertible.ToUInt64(IFormatProvider? provider)
12331241
{{
12341242
return Convert.ToUInt64(_value);
12351243
}}

CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public string Generate()
2020
using System.Globalization;
2121
using UnitsNet.Units;
2222
using System.Collections.Generic;
23+
using System.Diagnostics.CodeAnalysis;
2324
2425
#nullable enable
2526
@@ -70,7 +71,7 @@ public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, QuantityValu
7071
/// <param name=""unit"">Unit enum value.</param>
7172
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>
7273
/// <returns><c>True</c> if successful with <paramref name=""quantity""/> assigned the value, otherwise <c>false</c>.</returns>
73-
public static bool TryFrom(QuantityValue value, Enum unit, out IQuantity? quantity)
74+
public static bool TryFrom(QuantityValue value, Enum unit, [NotNullWhen(true)] out IQuantity? quantity)
7475
{
7576
switch (unit)
7677
{");
@@ -102,7 +103,7 @@ public static bool TryFrom(QuantityValue value, Enum unit, out IQuantity? quanti
102103
/// <param name=""quantityString"">Quantity string representation, such as ""1.5 kg"". Must be compatible with given quantity type.</param>
103104
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>
104105
/// <returns>The parsed quantity.</returns>
105-
public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, string quantityString, out IQuantity? quantity)
106+
public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, string quantityString, [NotNullWhen(true)] out IQuantity? quantity)
106107
{
107108
quantity = default(IQuantity);
108109

Directory.Build.props

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<!-- This file will be implicitly linked by all projects in folder -folders -->
33
<PropertyGroup>
44
<!-- Default to output to Artifacts folder -->
5-
<OutputPath>$(MSBuildThisFileDirectory)Artifacts/$(MSBuildProjectName)</OutputPath>
5+
<OutputPath>$(MSBuildThisFileDirectory)Artifacts/$(MSBuildProjectName)</OutputPath>
66
<!-- Specific output folder for .NET nanoFramework projects -->
7-
<OutputPath Condition=" '$(TargetFrameworkIdentifier)' == '.NETnanoFramework'">$(MSBuildThisFileDirectory)Artifacts/UnitsNet.NanoFramework/$(MSBuildProjectName)</OutputPath>
7+
<OutputPath Condition=" '$(TargetFrameworkIdentifier)' == '.NETnanoFramework'">$(MSBuildThisFileDirectory)Artifacts/UnitsNet.NanoFramework/$(MSBuildProjectName)</OutputPath>
88

99
</PropertyGroup>
1010

@@ -17,6 +17,12 @@
1717
<PropertyGroup>
1818
<!-- Warning instead of compile error on obsolete errors.-->
1919
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
20+
<!-- 612: obsolete, 618: obsolete with message -->
2021
<WarningsNotAsErrors>612,618</WarningsNotAsErrors>
2122
</PropertyGroup>
23+
24+
<ItemGroup>
25+
<Compile Include="$(MSBuildThisFileDirectory)/NullableAttributes.cs" />
26+
</ItemGroup>
27+
2228
</Project>

0 commit comments

Comments
 (0)