#include <Enumeration.h>
It works like the "enum" keyword, but has several advantages:
To create an enumeration, define a subclass of Enumeration which is parameterized by itself. For example:
class Color : public Enumeration<Color> { public: enum Index {RedIndex = 0, GreenIndex = 1, BlueIndex = 2}; static const Color Red; static const Color Green; static const Color Blue; private: Color(); Color(const Color& thisElement, int index, char* name); static void initValues(); friend class Enumeration<Color>; };
const Color Color::Red; const Color Color::Green; const Color Color::Blue;
Color::Color() : Enumeration<Color>() { }
Color::Color(const Color& thisElement, int index, char* name) : Enumeration<Color>(thisElement, index, name) { }
void Color::initValues() { new(&const_cast<Color&>(Red)) Color(Red, RedIndex, "Red"); new(&const_cast<Color&>(Green)) Color(Green, GreenIndex, "Green"); new(&const_cast<Color&>(Blue)) Color(Blue, BlueIndex, "Blue"); }
You can then define variables of the enumerated type and work with them exactly as you would expect:
Color myColor = Color::Blue; if (myColor == Color::Red) {...}
Enumerations can be used in switch statements, but the cases must be value indices rather than the values themselves:
switch (myColor) { case Color::RedIndex: ... }
You can invoke size() to determine the number of allowed values and getValue() to get the value with a particular index. Alternatively, you can iterate over the allowed values with begin() and end(). You also can invoke getIndex() on any value of the enumerated type to find its index in the list:
assert(Color::getValue(Color::Red.getIndex()) == Color::Red);
This class is designed to be used with the EnumerationSet class, which provides a convenient and efficient way to represent a set of values of a particular enumerated type.
Public Member Functions | |
int | getIndex () const |
Get the index of this value in the list returned by getValue(). | |
const std::string | getName () const |
Get the name of this value. | |
Enumeration (const Enumeration< T > ©) | |
Enumeration< T > & | operator= (const Enumeration< T > ©) |
Enumeration< T > * | operator & () |
bool | operator== (const Enumeration< T > &value) const |
bool | operator!= (const Enumeration< T > &value) const |
operator int () const | |
EnumerationSet< T > | operator| (const EnumerationSet< T > &set) const |
EnumerationSet< T > | operator & (const EnumerationSet< T > &set) const |
EnumerationSet< T > | operator^ (const EnumerationSet< T > &set) const |
Enumeration< T > | operator++ () |
Get the next enumerated value in order of their indices. | |
Enumeration< T > | operator++ (int) |
Get the next enumerated value in order of their indices. | |
Enumeration< T > | operator-- () |
Get the previous enumerated value in order of their indices. | |
Enumeration< T > | operator-- (int) |
Get the previous enumerated value in order of their indices. | |
Static Public Member Functions | |
static int | size () |
Get the total number of allowed values for this enumerated type. | |
static const T & | getValue (int index) |
Get the enumerated value with a particular index. | |
static iterator | begin () |
Get an iterator pointing to the start of the set of all possible values. | |
static iterator | end () |
Get an iterator pointing to the end of the set of all possible values. | |
Protected Member Functions | |
Enumeration () | |
Enumeration (const T &thisElement, int index, const char *name) | |
Static Protected Member Functions | |
static std::vector< const T * > & | updAllValues () |
Classes | |
class | iterator |
This class provides an interface for iterating over the set of all possible enumerated values. More... |
Enumeration | ( | const Enumeration< T > & | copy | ) | [inline] |
Enumeration | ( | ) | [inline, protected] |
Enumeration | ( | const T & | thisElement, | |
int | index, | |||
const char * | name | |||
) | [inline, protected] |
int getIndex | ( | ) | const [inline] |
Get the index of this value in the list returned by getValue().
Referenced by Stage::invalidate(), and Enumeration< SimTK::Stage >::operator int().
const std::string getName | ( | ) | const [inline] |
Get the name of this value.
Referenced by SimTK::operator<<(), RealizeCheckFailed::RealizeCheckFailed(), StageIsWrong::StageIsWrong(), StageOutOfRange::StageOutOfRange(), StageTooHigh::StageTooHigh(), and StageTooLow::StageTooLow().
static int size | ( | ) | [inline, static] |
Get the total number of allowed values for this enumerated type.
Referenced by Enumeration< SimTK::Stage >::end(), Enumeration< SimTK::Stage >::Enumeration(), and Enumeration< SimTK::Stage >::operator++().
static const T& getValue | ( | int | index | ) | [inline, static] |
Get the enumerated value with a particular index.
Referenced by Enumeration< SimTK::Stage >::operator++(), and Enumeration< SimTK::Stage >::operator--().
static iterator begin | ( | ) | [inline, static] |
Get an iterator pointing to the start of the set of all possible values.
static iterator end | ( | ) | [inline, static] |
Get an iterator pointing to the end of the set of all possible values.
Enumeration<T>& operator= | ( | const Enumeration< T > & | copy | ) | [inline] |
Enumeration<T>* operator & | ( | ) | [inline] |
bool operator== | ( | const Enumeration< T > & | value | ) | const [inline] |
bool operator!= | ( | const Enumeration< T > & | value | ) | const [inline] |
operator int | ( | ) | const [inline] |
EnumerationSet<T> operator| | ( | const EnumerationSet< T > & | set | ) | const [inline] |
EnumerationSet<T> operator & | ( | const EnumerationSet< T > & | set | ) | const [inline] |
EnumerationSet<T> operator^ | ( | const EnumerationSet< T > & | set | ) | const [inline] |
Enumeration<T> operator++ | ( | ) | [inline] |
Get the next enumerated value in order of their indices.
Enumeration<T> operator++ | ( | int | ) | [inline] |
Get the next enumerated value in order of their indices.
Enumeration<T> operator-- | ( | ) | [inline] |
Get the previous enumerated value in order of their indices.
Enumeration<T> operator-- | ( | int | ) | [inline] |
Get the previous enumerated value in order of their indices.
static std::vector<const T*>& updAllValues | ( | ) | [inline, static, protected] |