Skip to content

Commit

Permalink
Add EnumHelper.IsDefined<T>()
Browse files Browse the repository at this point in the history
  • Loading branch information
y-iihoshi committed Jan 11, 2025
1 parent 0906a1b commit 0f7b1a1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ThScoreFileConverter.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
using BenchmarkDotNet.Running;

var config = DefaultConfig.Instance.AddDiagnoser(MemoryDiagnoser.Default);
_ = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).RunAll(config, args);
_ = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).RunAllJoined(config, args);
28 changes: 28 additions & 0 deletions ThScoreFileConverter.Benchmarks/Scenarios/IsDefined.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using BenchmarkDotNet.Attributes;
using EnumHelper_ = ThScoreFileConverter.Core.Helpers.EnumHelper;

namespace ThScoreFileConverter.Benchmarks.Scenarios;

#pragma warning disable CA1822 // Mark members as static

public class IsDefined
{
[GlobalSetup]
public void Setup()
{
_ = Enum.IsDefined(DayOfWeek.Sunday);
_ = EnumHelper_.IsDefined(DayOfWeek.Sunday);
}

[Benchmark]
public bool NetCore()
{
return Enum.IsDefined(DayOfWeek.Sunday);
}

[Benchmark]
public bool EnumHelper()
{
return EnumHelper_.IsDefined(DayOfWeek.Sunday);
}
}
7 changes: 7 additions & 0 deletions ThScoreFileConverter.Core.Tests/Helpers/EnumHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ public void CartesianTest()

CollectionAssert.That.AreEqual(expected, EnumHelper.Cartesian<Protagonist, UnnamedCharacter>());
}

[TestMethod]
public void IsDefinedTest()
{
Assert.IsTrue(EnumHelper.IsDefined(DayOfWeek.Sunday));
Assert.IsFalse(EnumHelper.IsDefined((DayOfWeek)7));
}
}
21 changes: 21 additions & 0 deletions ThScoreFileConverter.Core/Helpers/EnumHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,25 @@ public static TEnum To<TEnum>(object value)
{
return EnumHelper<T1>.Enumerable.Cartesian(EnumHelper<T2>.Enumerable);
}

/// <summary>
/// Returns a boolean telling whether a given integral value exists in the <typeparamref name="TEnum"/> type.
/// </summary>
/// <typeparam name="TEnum">The enumeration type.</typeparam>
/// <param name="value">The value of a constant in <typeparamref name="TEnum"/>.</param>
/// <returns>
/// <see langword="true"/> if a given integral value exists in <typeparamref name="TEnum"/>;
/// <see langword="false"/> otherwise.
/// </returns>
/// <remarks>
/// To avoid circular references, do not call it from the following attributes
/// (use <see cref="Enum.IsDefined{TEnum}(TEnum)"/> instead):
/// <see cref="Models.PatternAttribute"/>, <see cref="Models.EnumDisplayAttribute"/>,
/// <see cref="Models.CharacterAttribute"/>, <see cref="Models.ShotTypeAttribute{T}"/>.
/// </remarks>
public static bool IsDefined<TEnum>(TEnum value)
where TEnum : struct, Enum
{
return EnumHelper<TEnum>.Members.ContainsKey(value);
}
}

0 comments on commit 0f7b1a1

Please sign in to comment.