Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
a local clone.
mir.enums
Enum utilities.
License:
Authors:
Ilia Ki
- pure nothrow @nogc @safe bool
getEnumIndex
(T)(const Tvalue
, ref uintindex
)
if (is(T == enum)); - Enum index that corresponds of the list returned by std.traits.EnumMembers.Returns:enum member position index in the enum definition that corresponds the
value
.Examples:import std.meta: AliasSeq; enum Common { a, b, c } enum Reversed { a = 1, b = 0, c = -1 } enum Shifted { a = -4, b, c } enum Small { a = -4, b, c = 10 } enum Big { a = -4, b, c = 1000 } enum InverseBool { True = true, False = false } enum FP : float { a = -4, b, c } enum S : string { a = "а", b = "б", c = "ц" } uint index = -1; foreach (E; AliasSeq!(Common, Reversed, Shifted, Small, Big, FP, S)) { assert(getEnumIndex(E.a, index) && index == 0); assert(getEnumIndex(E.b, index) && index == 1); assert(getEnumIndex(E.c, index) && index == 2); } assert(getEnumIndex(InverseBool.True, index) && index == 0); assert(getEnumIndex(InverseBool.False, index) && index == 1);
- template
enumMembers
(T) if (is(T == enum)) - Static immutable instance of [std.traits.EnumMembers!T].Examples:
enum E {a = 1, b = -1, c} static assert(enumMembers!E == [E.a, E.b, E.c]);
- static immutable T[EnumMembers!T.length]
enumMembers
;
- template
enumIdentifiers
(T) if (is(T == enum)) - Static immutable instance of Enum Identifiers.Examples:
enum E {z = 1, b = -1, c} static assert(enumIdentifiers!E == ["z", "b", "c"]);
- template
enumStrings
(T) if (is(T == enum)) - Aliases itself to enumMembers for string enums and enumIdentifiers for integral and floating point enums.Examples:
enum E {z = 1, b = -1, c} static assert(enumStrings!E == ["z", "b", "c"]); enum S {a = "A", b = "B", c = ""} static assert(enumStrings!S == [S.a, S.b, S.c]);
- pure nothrow @nogc @trusted T
unsafeEnumFromIndex
(T)(size_tindex
)
if (is(T == enum)); - Parameters:
size_t index
enum index std.traits.EnumMembers!T Returns:A enum value that corresponds to the index.Note The function doesn't check that index is less then EnumMembers!T.length.
Examples:enum Linear { one = 1, two = 2 } static assert(is(typeof(unsafeEnumFromIndex!Linear(0)) == Linear)); assert(unsafeEnumFromIndex!Linear(0) == Linear.one); assert(unsafeEnumFromIndex!Linear(1) == Linear.two); enum Mixed { one = 1, oneAgain = 1, two = 2 } assert(unsafeEnumFromIndex!Mixed(0) == Mixed.one); assert(unsafeEnumFromIndex!Mixed(1) == Mixed.one); assert(unsafeEnumFromIndex!Mixed(2) == Mixed.two);
- template
getEnumIndexFromKey
(T, bool caseInsensitive = true, getKeysTemplate...) if (is(T == enum) && (getKeysTemplate.length <= 1)) - Parameters:
T enum type to introspect const(C)[] key some string that corresponds to some key name of the given enum uint index resulting enum index if this method returns true. Returns:boolean whether the key was found in the enum keys and if so, index is set.Examples:enum Short { hello, world } enum Long { This, Is, An, Enum, With, Lots, Of, Very, Long, EntriesThatArePartiallyAlsoVeryLongInStringLengthAsWeNeedToTestALotOfDifferentCasesThatCouldHappenInRealWorldCode_tm } uint i; assert(getEnumIndexFromKey!Short("hello", i)); assert(i == 0); assert(getEnumIndexFromKey!Short("world", i)); assert(i == 1); assert(!getEnumIndexFromKey!Short("foo", i)); assert(getEnumIndexFromKey!Short("HeLlO", i)); assert(i == 0); assert(getEnumIndexFromKey!Short("WoRLd", i)); assert(i == 1); assert(!getEnumIndexFromKey!(Short, false)("HeLlO", i)); assert(!getEnumIndexFromKey!(Short, false)("WoRLd", i)); assert(getEnumIndexFromKey!Long("Is", i)); assert(i == 1); assert(getEnumIndexFromKey!Long("Long", i)); assert(i == 8); assert(getEnumIndexFromKey!Long("EntriesThatArePartiallyAlsoVeryLongInStringLengthAsWeNeedToTestALotOfDifferentCasesThatCouldHappenInRealWorldCode_tm", i)); assert(i == 9); assert(!getEnumIndexFromKey!Long("EntriesThatArePartiallyAlsoVeryLongInStringLengthAsWeNeedToTestALotOfDifferentCasesThatCouldHappenInRealWorldCodeatm", i)); assert(!getEnumIndexFromKey!(Long, false)("EntriesThatArePartiallyAlsoVeryLongInStringLengthAsWeNeedToTestALotOfDifferentCasesThatCouldHappenInRealWorldCode_tM", i)); assert(!getEnumIndexFromKey!(Long, false)("entriesThatArePartiallyAlsoVeryLongInStringLengthAsWeNeedToTestALotOfDifferentCasesThatCouldHappenInRealWorldCode_tm", i));
- pure nothrow @nogc @safe bool
getEnumIndexFromKey
(C)(scope const(C)[]key
, ref uintindex
)
if (is(C == char) || is(C == wchar) || is(C == dchar));
Copyright © 2016-2023 by Ilya Yaroshenko | Page generated by
Ddoc on Mon Nov 6 15:24:34 2023