iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages

Macros

#define NRT_DEFINE_ENUM_CLASS(name, seq)
 

Macro Definition Documentation

#define NRT_DEFINE_ENUM_CLASS (   name,
  seq 
)

Helper macro to define new enum class types. The macro NRT_DEFINE_ENUM_CLASS(name, SEQ) helps you define a new enum class. SEQ should be a BOOST_PP_SEQ. You will get:

  • an enum class declared and defined with the name and values supplied
  • a static const std::vector of your enum values, with all the values that the enum can take, in name_Values
  • a static const std::vector of std::string, with string names of all the values the enum can take, in name_Strings
  • operator<< and operator>> for your enum.

You can in particular use name_Values when declaring a ParameterDef<name> to list all the valid values, and the streaming operators will allow you to set the Parameter value by string, etc.

For example:

NRT_DEFINE_ENUM_CLASS(myEnum, (One) (Two) (Three));

expands to:

  • first, the actual definition of the enum class, with the values you specified in the sequence:
    enum class myEnum { One, Two, Three };
  • second, a static const vector that contains all the enum values, listed in the order you specified:
    static std::vector<myEnum> const myEnum_Values { myEnum::One, myEnum::Two, myEnum::Three };
  • third, a static const vector of strings that contains all the enum values in string form, listed in the order you specified:
    static std::vector<std::string> const myEnum_Strings { "One", "Two", "Three" };
  • finally, two stream operators that allow you to convert between your enum values and string:
    inline std::ostream & operator<<(std::ostream & os, myEnum const & v) {
    std::vector<myEnum>::const_iterator vitr = myEnum_Values.begin(), vend = myEnum_Values.end();
    std::vector<std::string>::const_iterator sitr = myEnum_Strings.begin();
    while (vitr != vend) if (v == *vitr) { os << *sitr; return os; } else { ++vitr; ++sitr; }
    return os;
    }
    inline std::istream & operator>>(std::istream & is, myEnum & v) {
    std::string s; is >> s;
    std::vector<myEnum>::const_iterator vitr = myEnum_Values.begin(), vend = myEnum_Values.end();
    std::vector<std::string>::const_iterator sitr = myEnum_Strings.begin();
    while (vitr != vend) if (s == *sitr) { v = *vitr; return is; } else { ++vitr; ++sitr; }
    throw nrt::exception::BadEnumStringException("myEnum", s.c_str());
    };

Note how the operator>> will throw nrt::exception::BadEnumStringException if the given string does not match the string representation of one of the enum's values.