This class defines an enumerated type. More...
#include <Enumeration.h>
Classes | |
class | iterator |
This class provides an interface for iterating over the set of all possible enumerated values. More... | |
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 > ©) |
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 Array_< const T * > & | updAllValues () |
This class defines an enumerated type.
It works like the "enum" keyword, but has several advantages:
One drawback is that there is no way to provide default construction for objects of Enumeration type, because default construction is necessary internally to get the static values initialized properly. Consequently you must declare the default constructor as private in your derived class.
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.
Enumeration | ( | const Enumeration< T > & | copy | ) | [inline] |
Enumeration | ( | ) | [inline, protected] |
Enumeration | ( | const T & | thisElement, | |
int | index, | |||
const char * | name | |||
) | [inline, protected] |
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.
int getIndex | ( | ) | const [inline] |
Get the index of this value in the list returned by getValue().
Referenced by Stage::invalidate(), and Enumeration< Stage >::operator int().
const std::string getName | ( | ) | const [inline] |
Get the name of this value.
Referenced by CacheEntryOutOfDate::CacheEntryOutOfDate(), Implementation::getValue(), SimTK::operator<<(), RealizeCheckFailed::RealizeCheckFailed(), StageIsWrong::StageIsWrong(), StageOutOfRange::StageOutOfRange(), StageTooHigh::StageTooHigh(), and StageTooLow::StageTooLow().
static const T& getValue | ( | int | index | ) | [inline, static] |
Get the enumerated value with a particular index.
Referenced by iterator::operator*(), Enumeration< Stage >::operator++(), and Enumeration< Stage >::operator--().
operator int | ( | ) | const [inline] |
bool operator!= | ( | const Enumeration< T > & | value | ) | const [inline] |
EnumerationSet<T> operator& | ( | const EnumerationSet< T > & | set | ) | const [inline] |
T* operator& | ( | ) | [inline] |
Enumeration<T> operator++ | ( | int | ) | [inline] |
Get the next enumerated value in order of their indices.
Enumeration<T> operator++ | ( | ) | [inline] |
Get the next enumerated value in order of their indices.
Enumeration<T> operator-- | ( | int | ) | [inline] |
Get the previous enumerated value in order of their indices.
Enumeration<T> operator-- | ( | ) | [inline] |
Get the previous enumerated value in order of their indices.
Enumeration<T>& operator= | ( | const Enumeration< T > & | copy | ) | [inline] |
bool operator== | ( | const Enumeration< T > & | value | ) | const [inline] |
EnumerationSet<T> operator^ | ( | const EnumerationSet< T > & | set | ) | const [inline] |
EnumerationSet<T> operator| | ( | const EnumerationSet< T > & | set | ) | const [inline] |
static int size | ( | ) | [inline, static] |
Get the total number of allowed values for this enumerated type.
Referenced by Enumeration< Stage >::end(), Enumeration< Stage >::Enumeration(), and Enumeration< Stage >::operator++().
static Array_<const T*>& updAllValues | ( | ) | [inline, static, protected] |