Simbody
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions

SimTK::Enumeration< T > Class Template Reference

This class defines an enumerated type. More...

#include <Enumeration.h>

List of all members.

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 > &copy)
Enumeration< T > & operator= (const Enumeration< T > &copy)
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 ()

Detailed Description

template<class T>
class SimTK::Enumeration< T >

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.


Constructor & Destructor Documentation

template<class T>
SimTK::Enumeration< T >::Enumeration ( const Enumeration< T > &  copy) [inline]
template<class T>
SimTK::Enumeration< T >::Enumeration ( ) [inline, protected]
template<class T>
SimTK::Enumeration< T >::Enumeration ( const T &  thisElement,
int  index,
const char *  name 
) [inline, protected]

Member Function Documentation

template<class T>
int SimTK::Enumeration< T >::getIndex ( ) const [inline]

Get the index of this value in the list returned by getValue().

template<class T>
const std::string SimTK::Enumeration< T >::getName ( ) const [inline]

Get the name of this value.

template<class T>
static int SimTK::Enumeration< T >::size ( ) [inline, static]

Get the total number of allowed values for this enumerated type.

template<class T>
static const T& SimTK::Enumeration< T >::getValue ( int  index) [inline, static]

Get the enumerated value with a particular index.

template<class T>
static iterator SimTK::Enumeration< T >::begin ( ) [inline, static]

Get an iterator pointing to the start of the set of all possible values.

template<class T>
static iterator SimTK::Enumeration< T >::end ( ) [inline, static]

Get an iterator pointing to the end of the set of all possible values.

template<class T>
Enumeration<T>& SimTK::Enumeration< T >::operator= ( const Enumeration< T > &  copy) [inline]
template<class T>
T* SimTK::Enumeration< T >::operator& ( ) [inline]
template<class T>
bool SimTK::Enumeration< T >::operator== ( const Enumeration< T > &  value) const [inline]
template<class T>
bool SimTK::Enumeration< T >::operator!= ( const Enumeration< T > &  value) const [inline]
template<class T>
SimTK::Enumeration< T >::operator int ( ) const [inline]
template<class T>
EnumerationSet<T> SimTK::Enumeration< T >::operator| ( const EnumerationSet< T > &  set) const [inline]
template<class T>
EnumerationSet<T> SimTK::Enumeration< T >::operator& ( const EnumerationSet< T > &  set) const [inline]
template<class T>
EnumerationSet<T> SimTK::Enumeration< T >::operator^ ( const EnumerationSet< T > &  set) const [inline]
template<class T>
Enumeration<T> SimTK::Enumeration< T >::operator++ ( ) [inline]

Get the next enumerated value in order of their indices.

template<class T>
Enumeration<T> SimTK::Enumeration< T >::operator++ ( int  ) [inline]

Get the next enumerated value in order of their indices.

template<class T>
Enumeration<T> SimTK::Enumeration< T >::operator-- ( ) [inline]

Get the previous enumerated value in order of their indices.

template<class T>
Enumeration<T> SimTK::Enumeration< T >::operator-- ( int  ) [inline]

Get the previous enumerated value in order of their indices.

template<class T>
static Array_<const T*>& SimTK::Enumeration< T >::updAllValues ( ) [inline, static, protected]

The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines