mirror of
https://github.com/Ellpeck/DynamicEnums.git
synced 2024-11-30 03:58:34 +01:00
Compare commits
5 commits
6305ee9d8c
...
1fe7725ca9
Author | SHA1 | Date | |
---|---|---|---|
1fe7725ca9 | |||
bbd723f13c | |||
eb944df3de | |||
baa0b8e5f8 | |||
c57f9fdbf6 |
4 changed files with 64 additions and 9 deletions
|
@ -195,17 +195,34 @@ 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}"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type whose values to get</typeparam>
|
||||
/// <returns>The defined values for the given type</returns>
|
||||
/// <returns>The defined values for the given type, in the order they were added.</returns>
|
||||
public static IEnumerable<T> GetValues<T>() where T : DynamicEnum {
|
||||
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>
|
||||
/// 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}"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">The type whose values to get</param>
|
||||
/// <returns>The defined values for the given type</returns>
|
||||
/// <returns>The defined values for the given type, in the order they were added.</returns>
|
||||
public static IEnumerable<DynamicEnum> GetValues(Type type) {
|
||||
return DynamicEnum.GetStorage(type).Values.Values;
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
<RepositoryUrl>https://github.com/Ellpeck/DynamicEnums</RepositoryUrl>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<VersionPrefix>1.3.0</VersionPrefix>
|
||||
<VersionPrefix>1.4.1</VersionPrefix>
|
||||
</PropertyGroup>
|
||||
|
||||
<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>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type whose enum to get</typeparam>
|
||||
/// <returns>An enumerable of the values of the enum, in declaration order.</returns>
|
||||
/// <returns>An enumerable of the values of the enum, in ascending value order.</returns>
|
||||
public static T[] GetValues<T>() where T : struct, Enum {
|
||||
#if NET6_0_OR_GREATER
|
||||
return Enum.GetValues<T>();
|
||||
|
@ -21,6 +21,22 @@ namespace DynamicEnums {
|
|||
#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>
|
||||
/// 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.
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using DynamicEnums;
|
||||
using NUnit.Framework;
|
||||
|
@ -9,8 +10,18 @@ public class EnumTests {
|
|||
|
||||
[Test]
|
||||
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(
|
||||
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));
|
||||
|
||||
Assert.AreEqual(
|
||||
|
@ -29,15 +40,22 @@ public class EnumTests {
|
|||
|
||||
[Test]
|
||||
public void TestDynamicEnums() {
|
||||
var combined = DynamicEnum.Add<TestDynamicEnum>("Combined", (1 << 7) | (1 << 13));
|
||||
var flags = new TestDynamicEnum[100];
|
||||
for (var i = 0; i < flags.Length; i++)
|
||||
flags[i] = DynamicEnum.AddFlag<TestDynamicEnum>("Flag" + i);
|
||||
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);
|
||||
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.GetEnumValue<TestDynamicEnum>(BigInteger.One << 75), flags[75]);
|
||||
|
||||
|
@ -71,8 +89,11 @@ public class EnumTests {
|
|||
Assert.False(DynamicEnum.IsDefined(DynamicEnum.Or(combined, flags[49])));
|
||||
|
||||
Assert.AreEqual(
|
||||
new[] {flags[0], flags[7], flags[13], combined},
|
||||
new[] {combined, flags[0], flags[7], flags[13]},
|
||||
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(
|
||||
new[] {flags[0], flags[7], flags[13]},
|
||||
|
@ -82,14 +103,15 @@ public class EnumTests {
|
|||
[Flags]
|
||||
private enum TestEnum {
|
||||
|
||||
Zero = 0,
|
||||
One = 1,
|
||||
Two = 2,
|
||||
Eight = 8,
|
||||
Sixteen = 16,
|
||||
EightSixteen = TestEnum.Eight | TestEnum.Sixteen,
|
||||
Eight = 8,
|
||||
OneTwentyEightTwoOne = TestEnum.OneTwentyEight | TestEnum.Two | TestEnum.One,
|
||||
ThirtyTwo = 32,
|
||||
Sixteen = 16,
|
||||
OneTwentyEight = 128,
|
||||
OneTwentyEightTwoOne = TestEnum.OneTwentyEight | TestEnum.Two | TestEnum.One
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue