Enum<T>

Edit on GitHub
#include <Enum.h>
SReturnNameParameters
uintptr Size (void)
const ValueMap& GetMap (void)
Type Parse (const string& key, Type default = -1)
string Parse (Type value, const char* default = "")

Description

Represents a helper class for modeling and analyzing C++ enumerations. Traditionally, C++ provides little to no runtime reflection capabilities, especially when it comes to enumerations. The Enum class changes this by providing the ability to model existing enumerations and analyze them at runtime. The underlying type remains unchanged, and all functionality is accessed directly through the Enum class.

To begin analyzing enumerations, they must first be modeled through the use of macros. Afterwards, static functions in the class can be used for a variety of tasks. For example, to get the size of the enumeration, use the Size function. To retrieve the raw string-constant pairs, use the GetMap function. To convert constants into strings and vice-versa, use the Parse functions.

Macros

Defines two macros for modeling enumerations. The first macro, ROBOT_ENUM, expands into a function signature defining the constructor of this class. Since templates expand into unique types, a constructor must be defined for each supported enumeration. Place this macro inside a source file, with the desired enumeration name followed by an opening and closing brace.

Within the declaration itself, use the second macro, ROBOT_ENUM_MAP, which expands into a hash map assignment. The first parameter represents the enumeration constant while the second represents the mapped string. If the second parameter is omitted, a stringified version of the first parameter will be used instead. A single constant may map to multiple strings.

Linux: To maintain compatibility with GCC, define models within the Robot namespace.

#define ROBOT_ENUM     (type     )
#define ROBOT_ENUM_MAP (cnst     )
#define ROBOT_ENUM_MAP (cnst, str)

Types

typedef unordered_map<string, Type> Enum::ValueMap;

Functions

    
static uintptr Size (void)

Returns the size of the enumeration, as specified by the model. The size does not reflect the number of entries in the enumeration but rather, the total number of entries in the model.

    
static const ValueMap& GetMap (void)

Returns the enumeration model as a hash map of string-constant pairs.

    
static Type Parse (const string& key, Type default = -1)
static string Parse (Type value, const char* default = "")

Converts between enumeration constants and their equivalent string values, as specified by the model. If key or value is not found, default is instead returned.

Examples

// C++
#include <Robot.h>
ROBOT_NS_USE_ALL;

enum Example
{
    Value1,
    Value2,
    Value3,
    Value4,
};

// Models must be placed within source files and wrapped
// inside the Robot namespace, as needed by GCC on Linux

ROBOT_NS_BEGIN
    ROBOT_ENUM (Example)
    {
        ROBOT_ENUM_MAP (Value1);
        ROBOT_ENUM_MAP (Value2);
        ROBOT_ENUM_MAP (Value3, "Custom1");
        ROBOT_ENUM_MAP (Value4, "Custom2");
    }
ROBOT_NS_END

int main (void)
{
    Enum<Example>::Size(); // 4

    Enum<Example>::Parse (  Value1 ); //  "Value1"
    Enum<Example>::Parse (  Value3 ); // "Custom1"
    Enum<Example>::Parse ( "Value2"); //   Value2
    Enum<Example>::Parse ("Custom2"); //   Value4

    Enum<Example>::Parse ((Example) 4); // Empty
    Enum<Example>::Parse ("Invalid"  ); // -1

    // Retrieve map for manual parsing
    auto map = Enum<Example>::GetMap();

    // Map is an unordered_map
    for (const auto& i : map)
    {
        i.first;  // String
        i.second; // Constant
    }

    return 0;
}