mirror of
https://github.com/Ellpeck/DynamicEnums.git
synced 2024-11-26 18:38:34 +01:00
Compare commits
No commits in common. "1fe7725ca98f3a910f8a7a572eb9a84c7968874a" and "6305ee9d8cb1335d433c00e929605e49c8b484f6" have entirely different histories.
1fe7725ca9
...
6305ee9d8c
4 changed files with 9 additions and 64 deletions
|
@ -195,34 +195,17 @@ namespace DynamicEnums {
|
||||||
/// A value counts as explicitly defined if it has been added using <see cref="Add{T}"/>, <see cref="AddValue{T}"/> or <see cref="AddFlag{T}"/>.
|
/// A value counts as explicitly defined if it has been added using <see cref="Add{T}"/>, <see cref="AddValue{T}"/> or <see cref="AddFlag{T}"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type whose values to get</typeparam>
|
/// <typeparam name="T">The type whose values to get</typeparam>
|
||||||
/// <returns>The defined values for the given type, in the order they were added.</returns>
|
/// <returns>The defined values for the given type</returns>
|
||||||
public static IEnumerable<T> GetValues<T>() where T : DynamicEnum {
|
public static IEnumerable<T> GetValues<T>() where T : DynamicEnum {
|
||||||
return DynamicEnum.GetValues(typeof(T)).Cast<T>();
|
return DynamicEnum.GetValues(typeof(T)).Cast<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a collection of all of the enum values that are explicitly defined for the given dynamic enum type <typeparamref name="T"/>, excluding any explicitly defined combined flags.
|
|
||||||
/// A value counts as explicitly defined if it has been added using <see cref="Add{T}"/>, <see cref="AddValue{T}"/> or <see cref="AddFlag{T}"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type whose values to get.</typeparam>
|
|
||||||
/// <returns>The defined values for the given type, in ascending value order, excluding combined flags.</returns>
|
|
||||||
public static IEnumerable<T> GetUniqueValues<T>() where T : DynamicEnum {
|
|
||||||
var used = BigInteger.Zero;
|
|
||||||
foreach (var value in DynamicEnum.GetValues<T>().OrderBy(DynamicEnum.GetValue)) {
|
|
||||||
var iValue = DynamicEnum.GetValue(value);
|
|
||||||
if ((used & iValue) == 0) {
|
|
||||||
yield return value;
|
|
||||||
used |= iValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns a collection of all of the enum values that are explicitly defined for the given dynamic enum type <paramref name="type"/>.
|
/// Returns a collection of all of the enum values that are explicitly defined for the given dynamic enum type <paramref name="type"/>.
|
||||||
/// A value counts as explicitly defined if it has been added using <see cref="Add{T}"/>, <see cref="AddValue{T}"/> or <see cref="AddFlag{T}"/>.
|
/// A value counts as explicitly defined if it has been added using <see cref="Add{T}"/>, <see cref="AddValue{T}"/> or <see cref="AddFlag{T}"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type">The type whose values to get</param>
|
/// <param name="type">The type whose values to get</param>
|
||||||
/// <returns>The defined values for the given type, in the order they were added.</returns>
|
/// <returns>The defined values for the given type</returns>
|
||||||
public static IEnumerable<DynamicEnum> GetValues(Type type) {
|
public static IEnumerable<DynamicEnum> GetValues(Type type) {
|
||||||
return DynamicEnum.GetStorage(type).Values.Values;
|
return DynamicEnum.GetStorage(type).Values.Values;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
<RepositoryUrl>https://github.com/Ellpeck/DynamicEnums</RepositoryUrl>
|
<RepositoryUrl>https://github.com/Ellpeck/DynamicEnums</RepositoryUrl>
|
||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||||
<VersionPrefix>1.4.1</VersionPrefix>
|
<VersionPrefix>1.3.0</VersionPrefix>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -12,7 +12,7 @@ namespace DynamicEnums {
|
||||||
/// Note that this method is a version-independent equivalent of .NET 5's <c>Enum.GetValues<TEnum></c>.
|
/// Note that this method is a version-independent equivalent of .NET 5's <c>Enum.GetValues<TEnum></c>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type whose enum to get</typeparam>
|
/// <typeparam name="T">The type whose enum to get</typeparam>
|
||||||
/// <returns>An enumerable of the values of the enum, in ascending value order.</returns>
|
/// <returns>An enumerable of the values of the enum, in declaration order.</returns>
|
||||||
public static T[] GetValues<T>() where T : struct, Enum {
|
public static T[] GetValues<T>() where T : struct, Enum {
|
||||||
#if NET6_0_OR_GREATER
|
#if NET6_0_OR_GREATER
|
||||||
return Enum.GetValues<T>();
|
return Enum.GetValues<T>();
|
||||||
|
@ -21,22 +21,6 @@ namespace DynamicEnums {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns an array containing all of the values of the given enum type, excluding any explicitly defined combined flags.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type whose enum to get.</typeparam>
|
|
||||||
/// <returns>An enumerable of the values of the enum, in ascending value order, excluding combined flags.</returns>
|
|
||||||
public static IEnumerable<T> GetUniqueValues<T>() where T : struct, Enum {
|
|
||||||
var used = 0L;
|
|
||||||
foreach (var value in EnumHelper.GetValues<T>()) {
|
|
||||||
var lValue = Convert.ToInt64(value);
|
|
||||||
if ((used & lValue) == 0) {
|
|
||||||
yield return value;
|
|
||||||
used |= lValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns true if the given <paramref name="value"/> has all of the given <see cref="Enum"/> flags on it.
|
/// Returns true if the given <paramref name="value"/> has all of the given <see cref="Enum"/> flags on it.
|
||||||
/// This operation is equivalent to <see cref="Enum.HasFlag"/>, but doesn't do any additional checks, making it faster.
|
/// This operation is equivalent to <see cref="Enum.HasFlag"/>, but doesn't do any additional checks, making it faster.
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using DynamicEnums;
|
using DynamicEnums;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
@ -10,18 +9,8 @@ public class EnumTests {
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestRegularEnums() {
|
public void TestRegularEnums() {
|
||||||
Assert.AreEqual(
|
|
||||||
new[] {TestEnum.Zero, TestEnum.One, TestEnum.Two, TestEnum.Eight, TestEnum.Sixteen, TestEnum.EightSixteen, TestEnum.ThirtyTwo, TestEnum.OneTwentyEight, TestEnum.OneTwentyEightTwoOne},
|
|
||||||
EnumHelper.GetValues<TestEnum>());
|
|
||||||
Assert.AreEqual(
|
|
||||||
new[] {TestEnum.Zero, TestEnum.One, TestEnum.Two, TestEnum.Eight, TestEnum.Sixteen, TestEnum.ThirtyTwo, TestEnum.OneTwentyEight},
|
|
||||||
EnumHelper.GetUniqueValues<TestEnum>());
|
|
||||||
|
|
||||||
Assert.AreEqual(
|
Assert.AreEqual(
|
||||||
new[] {TestEnum.One, TestEnum.Two, TestEnum.Eight, TestEnum.Sixteen, TestEnum.EightSixteen},
|
new[] {TestEnum.One, TestEnum.Two, TestEnum.Eight, TestEnum.Sixteen, TestEnum.EightSixteen},
|
||||||
EnumHelper.GetFlags(TestEnum.One | TestEnum.Sixteen | TestEnum.Eight | TestEnum.Two, false));
|
|
||||||
Assert.AreEqual(
|
|
||||||
new[] {TestEnum.Zero, TestEnum.One, TestEnum.Two, TestEnum.Eight, TestEnum.Sixteen, TestEnum.EightSixteen},
|
|
||||||
EnumHelper.GetFlags(TestEnum.One | TestEnum.Sixteen | TestEnum.Eight | TestEnum.Two));
|
EnumHelper.GetFlags(TestEnum.One | TestEnum.Sixteen | TestEnum.Eight | TestEnum.Two));
|
||||||
|
|
||||||
Assert.AreEqual(
|
Assert.AreEqual(
|
||||||
|
@ -40,22 +29,15 @@ public class EnumTests {
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestDynamicEnums() {
|
public void TestDynamicEnums() {
|
||||||
var combined = DynamicEnum.Add<TestDynamicEnum>("Combined", (1 << 7) | (1 << 13));
|
|
||||||
var flags = new TestDynamicEnum[100];
|
var flags = new TestDynamicEnum[100];
|
||||||
for (var i = 0; i < flags.Length; i++)
|
for (var i = 0; i < flags.Length; i++)
|
||||||
flags[i] = DynamicEnum.AddFlag<TestDynamicEnum>("Flag" + i);
|
flags[i] = DynamicEnum.AddFlag<TestDynamicEnum>("Flag" + i);
|
||||||
var zero = DynamicEnum.Add<TestDynamicEnum>("Zero", 0);
|
var zero = DynamicEnum.Add<TestDynamicEnum>("Zero", 0);
|
||||||
|
var combined = DynamicEnum.Add<TestDynamicEnum>("Combined", DynamicEnum.GetValue(DynamicEnum.Or(flags[7], flags[13])));
|
||||||
|
|
||||||
DynamicEnum.Add<TestEnumWithConstructor>("Test", 10);
|
DynamicEnum.Add<TestEnumWithConstructor>("Test", 10);
|
||||||
Assert.AreEqual(DynamicEnum.GetEnumValue<TestEnumWithConstructor>(10).ToString(), "TestModified");
|
Assert.AreEqual(DynamicEnum.GetEnumValue<TestEnumWithConstructor>(10).ToString(), "TestModified");
|
||||||
|
|
||||||
Assert.AreEqual(
|
|
||||||
flags.Append(zero).Prepend(combined),
|
|
||||||
DynamicEnum.GetValues<TestDynamicEnum>());
|
|
||||||
Assert.AreEqual(
|
|
||||||
flags.Prepend(zero),
|
|
||||||
DynamicEnum.GetUniqueValues<TestDynamicEnum>());
|
|
||||||
|
|
||||||
Assert.AreEqual(DynamicEnum.GetValue(flags[7]), BigInteger.One << 7);
|
Assert.AreEqual(DynamicEnum.GetValue(flags[7]), BigInteger.One << 7);
|
||||||
Assert.AreEqual(DynamicEnum.GetEnumValue<TestDynamicEnum>(BigInteger.One << 75), flags[75]);
|
Assert.AreEqual(DynamicEnum.GetEnumValue<TestDynamicEnum>(BigInteger.One << 75), flags[75]);
|
||||||
|
|
||||||
|
@ -89,11 +71,8 @@ public class EnumTests {
|
||||||
Assert.False(DynamicEnum.IsDefined(DynamicEnum.Or(combined, flags[49])));
|
Assert.False(DynamicEnum.IsDefined(DynamicEnum.Or(combined, flags[49])));
|
||||||
|
|
||||||
Assert.AreEqual(
|
Assert.AreEqual(
|
||||||
new[] {combined, flags[0], flags[7], flags[13]},
|
new[] {flags[0], flags[7], flags[13], combined},
|
||||||
DynamicEnum.GetFlags(DynamicEnum.Or(DynamicEnum.Or(flags[0], flags[13]), flags[7]), false));
|
DynamicEnum.GetFlags(DynamicEnum.Or(DynamicEnum.Or(flags[0], flags[13]), flags[7]), false));
|
||||||
Assert.AreEqual(
|
|
||||||
new[] {combined, flags[0], flags[7], flags[13], zero},
|
|
||||||
DynamicEnum.GetFlags(DynamicEnum.Or(DynamicEnum.Or(flags[0], flags[13]), flags[7])));
|
|
||||||
|
|
||||||
Assert.AreEqual(
|
Assert.AreEqual(
|
||||||
new[] {flags[0], flags[7], flags[13]},
|
new[] {flags[0], flags[7], flags[13]},
|
||||||
|
@ -103,15 +82,14 @@ public class EnumTests {
|
||||||
[Flags]
|
[Flags]
|
||||||
private enum TestEnum {
|
private enum TestEnum {
|
||||||
|
|
||||||
Zero = 0,
|
|
||||||
One = 1,
|
One = 1,
|
||||||
Two = 2,
|
Two = 2,
|
||||||
EightSixteen = TestEnum.Eight | TestEnum.Sixteen,
|
|
||||||
Eight = 8,
|
Eight = 8,
|
||||||
OneTwentyEightTwoOne = TestEnum.OneTwentyEight | TestEnum.Two | TestEnum.One,
|
|
||||||
ThirtyTwo = 32,
|
|
||||||
Sixteen = 16,
|
Sixteen = 16,
|
||||||
|
EightSixteen = TestEnum.Eight | TestEnum.Sixteen,
|
||||||
|
ThirtyTwo = 32,
|
||||||
OneTwentyEight = 128,
|
OneTwentyEight = 128,
|
||||||
|
OneTwentyEightTwoOne = TestEnum.OneTwentyEight | TestEnum.Two | TestEnum.One
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue