ocean.core.SmartEnum

Smart enum template class, encapsulates an enum with a map of its codes and descriptions.

Contains two main mixin templates:

1. SmartEnum 2. AutoSmartEnum

1. SmartEnum

Mixin template declaring a class containing an enum with code<->description
lookup. All methods are static, so that the class can be used without an
instance.

This mixin creates classes which contain a real (anonymous) enum, and two
associative arrays mapping from the enum values to their string descriptions
and vice versa, allowing two-way lookup between codes <-> descriptions, and
an implementation of the noperator to tell if a code or description is
valid.

This is especially useful for "Const" classes which define a list of command
or status codes where a kind of reverse lookup is needed, going from a code
to its name / description (or vice versa).

The classes also implement a foreach iterator over the values of the enum,
and several other methods (see SmartEunmCore, below).

The mixin template takes as parameters the name of the class to be generated
and a variadic list of SmartEnumValue structs, specifying the names and
values of the enum members.

The SmartEnumValue struct is also a template, which takes as parameter the
base type of the enum, which must be an integral type (see
http://www.digitalmars.com/d/1.0/enum.html).

Note that as the class generated by the mixin template contains only static
members, it is not necessary to actually instantiate it, only to declare the
class (as demonstrated in the example below).

Usage example:

import ocean.core.SmartEnum; import ocean.io.Stdout;

alias SmartEnumValue!(int) Code;

mixin(SmartEnum!("Commands", Code("first", 1), Code("second", 2) ));

// Getting descriptions of codes // (Note that the getter methods work like opIn, returning pointers.) Stdout.formatln("Description for code first = {}", *Commands.description(Commands.first)); Stdout.formatln("Description for code 1 = {}", *Commands.description(1));

// Getting codes by description // (Note that the getter methods work like opIn, returning pointers.) Stdout.formatln("Code for description 'first' = {}", *Commands.code("first"));

// Testing whether codes exist Stdout.formatln("1 in enum? {}", !!(1 in Commands)); Stdout.formatln("first in enum? {}", !!(Commands.first in Commands));

// Testing whether codes exist by description Stdout.formatln("'first' in enum? {}", !!("first" in Commands)); Stdout.formatln("'third' in enum? {}", !!("third" in Commands));

// Iteration over enum foreach ( code, descr; Commands ) { Stdout.formatln("{} -> {}", code, descr); }

2. AutoSmartEnum

Template to automatically create a SmartEnum from a list of strings. The enum's base type is specified, and the enum values are automatically numbered, starting at 0.

Usage example:

import ocean.core.SmartEnum;

mixin(AutoSmartEnum!("Commands", int,
    "first",
    "second"));

Public Imports

ocean.core.Enforce
public import ocean.core.Enforce;
Undocumented in source.

Members

Classes

ISmartEnum
class ISmartEnum

Abstract base class for SmartEnums. Contains no members, just provided as a convenient way of checking that a class is in fact a SmartEnum, using is(T : ISmartEnum).

Structs

SmartEnumValue
struct SmartEnumValue(T)

Struct template representing a single member of an enum -- containing a string for the enum identifier and a code for the corresponding value.

TwoWayMap
struct TwoWayMap(A)

Two way map struct template

Templates

AutoSmartEnum
template AutoSmartEnum(istring Name, BaseType, Strings...)

Template to mixin a SmartEnum class with the codes automatically generated, starting at 0.

SmartEnum
template SmartEnum(istring Name, T...)

Template to mixin a SmartEnum class.

SmartEnumCore
template SmartEnumCore(BaseType)

Members forming the core of each class generated by the SmartEnum mixin. This template is mixed into each class created by the SmartEnum template.

Meta

License

Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. Alternatively, this file may be distributed under the terms of the Tango 3-Clause BSD License (see LICENSE_BSD.txt for details).