Skip to content

Commit

Permalink
Remove conditional compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Muximize committed Mar 3, 2024
1 parent 40bbd6f commit 6fd3a85
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 49 deletions.
8 changes: 1 addition & 7 deletions CodeGen/Generators/UnitsNetGen/NumberExtensionsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ public string Generate()
Writer.WL(
$@"
using System;
#if NET7_0_OR_GREATER
using System.Numerics;
#endif
#nullable enable
Expand All @@ -50,10 +47,7 @@ public static class NumberTo{_quantityName}Extensions
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit.ObsoleteText));

Writer.WL(2, $@"public static {_quantityName} {unit.PluralName}<T>(this T value)
where T : notnull
#if NET7_0_OR_GREATER
, INumber<T>
#endif
where T : notnull, INumber<T>
=> {_quantityName}.From{unit.PluralName}(Convert.ToDouble(value));
");
}
Expand Down
38 changes: 14 additions & 24 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ public string Generate()
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;");
if (_quantity.Relations.Any(r => r.Operator is "*" or "/"))
Writer.WL(@"#if NET7_0_OR_GREATER
using System.Numerics;
#endif");
Writer.WLCondition(_quantity.Relations.Any(r => r.Operator is "*" or "/"),
@"using System.Numerics;");
Writer.WL(@"using System.Runtime.Serialization;
using UnitsNet.InternalHelpers;
using UnitsNet.Units;
Expand Down Expand Up @@ -69,33 +67,25 @@ namespace UnitsNet
public readonly partial struct {_quantity.Name} :
{(_quantity.GenerateArithmetic ? "IArithmeticQuantity" : "IQuantity")}<{_quantity.Name}, {_unitEnumName}>,");

if (_quantity.Relations.Any(r => r.Operator is "*" or "/"))
foreach (var relation in _quantity.Relations)
{
Writer.WL(@$"
#if NET7_0_OR_GREATER");
foreach (var relation in _quantity.Relations)
if (relation.LeftQuantity == _quantity)
{
if (relation.LeftQuantity == _quantity)
switch (relation.Operator)
{
switch (relation.Operator)
{
case "*":
Writer.W(@"
case "*":
Writer.W(@"
IMultiplyOperators");
break;
case "/":
Writer.W(@"
break;
case "/":
Writer.W(@"
IDivisionOperators");
break;
default:
continue;
}
Writer.WL($"<{relation.LeftQuantity.Name}, {relation.RightQuantity.Name}, {relation.ResultQuantity.Name}>,");
break;
default:
continue;
}
Writer.WL($"<{relation.LeftQuantity.Name}, {relation.RightQuantity.Name}, {relation.ResultQuantity.Name}>,");
}

Writer.WL(@$"
#endif");
}

Writer.WL(@$"
Expand Down
3 changes: 0 additions & 3 deletions UnitsNet.Tests/GenericMathExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.

#if NET7_0_OR_GREATER
using UnitsNet.GenericMath;
using Xunit;

Expand All @@ -25,5 +24,3 @@ public void CanCalcAverage_ForQuantitiesWithDoubleValueType()
Assert.Equal(Length.FromCentimeters(150), values.Average());
}
}

#endif
2 changes: 0 additions & 2 deletions UnitsNet.Tests/Serialization/CustomSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace UnitsNet.Tests.Serialization;

public class CustomSerializationTests
{
#if NET7_0_OR_GREATER
private record QuantityDto(double Value, string QuantityName, string UnitName);

/// <summary>
Expand Down Expand Up @@ -48,5 +47,4 @@ public void CanMapToJsonAndBackViaCustomDto()
Assert.Equal(5, deserializedQuantity!.Value);
Assert.Equal(LengthUnit.Centimeter, deserializedQuantity.Unit);
}
#endif
}
2 changes: 0 additions & 2 deletions UnitsNet/GenericMath/GenericMathExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.

#if NET7_0_OR_GREATER
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
Expand Down Expand Up @@ -65,4 +64,3 @@ public static T Average<T>(this IEnumerable<T> source)
return result.value / result.count;
}
}
#endif
4 changes: 0 additions & 4 deletions UnitsNet/IArithmeticQuantity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,17 @@ namespace UnitsNet;
/// <typeparam name="TSelf">The type itself, for the CRT pattern.</typeparam>
/// <typeparam name="TUnitType">The underlying unit enum type.</typeparam>
public interface IArithmeticQuantity<TSelf, TUnitType> : IQuantity<TSelf, TUnitType>
#if NET7_0_OR_GREATER
, IAdditionOperators<TSelf, TSelf, TSelf>
, IAdditiveIdentity<TSelf, TSelf>
, ISubtractionOperators<TSelf, TSelf, TSelf>
, IMultiplyOperators<TSelf, double, TSelf>
, IDivisionOperators<TSelf, double, TSelf>
, IUnaryNegationOperators<TSelf, TSelf>
#endif
where TSelf : IArithmeticQuantity<TSelf, TUnitType>
where TUnitType : Enum
{
#if NET7_0_OR_GREATER
/// <summary>
/// The zero value of this quantity.
/// </summary>
static abstract TSelf Zero { get; }
#endif
}
7 changes: 0 additions & 7 deletions UnitsNet/IQuantity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

using System;
using System.Globalization;
#if NET7_0_OR_GREATER
using System.Numerics;
#endif
using UnitsNet.Units;

namespace UnitsNet
Expand Down Expand Up @@ -147,15 +145,10 @@ public interface IQuantity<TUnitType> : IQuantity
/// </summary>
/// <typeparam name="TSelf">The type itself, for the CRT pattern.</typeparam>
/// <typeparam name="TUnitType">The underlying unit enum type.</typeparam>
#if NET7_0_OR_GREATER
public interface IQuantity<TSelf, TUnitType>
: IQuantity<TUnitType>
, IComparisonOperators<TSelf, TSelf, bool>
, IParsable<TSelf>
#else
public interface IQuantity<in TSelf, TUnitType>
: IQuantity<TUnitType>
#endif
where TSelf : IQuantity<TSelf, TUnitType>
where TUnitType : Enum
{
Expand Down

0 comments on commit 6fd3a85

Please sign in to comment.