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.
Authors:
Ilya Yaroshenko
pure nothrow @nogc @safe bool getEnumIndex(T)(const T value, ref uint index)
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_t index)
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.

template getEnumIndexFromKey(T, bool caseInsesetive = true, getKeysTemplate...) if (is(T == enum) && (getKeysTemplate.length <= 1))
pure nothrow @nogc @safe bool getEnumIndexFromKey(C)(scope const(C)[] key, ref uint index)
if (is(C == char) || is(C == wchar) || is(C == dchar));