#ifndef __JLANG__JGROUP__ #define __JLANG__JGROUP__ #include "JLang/JException.hh" /** * \author mdejong */ namespace JLANG {} namespace JPP { using namespace JLANG; } namespace JLANG { /** * Auxiliary class for a fixed group of objects. * * A group should be defined as follows: *
   *   template<>
   *   const XXX JGroup::group[]  =  {
   *     XXX(...),
   *     XXX(...),
   *     ...
   *     XXX(...)
   *   };
   * 
*/ template struct JGroup { private: /** * Default constructor. */ JGroup() {} static const T group[]; //!< actual group public: /** * Number of objects. */ static const int size; /** * Get unique instance. */ static const JGroup& getInstance() { static const JGroup object; return object; } /** * Get object at given index in group. * * \param index index * \return object */ const T& operator()(const int index) const { if (index >= 0 && index < size) return this->group[index]; else THROW(JIndexOutOfRange, "JGroup::operator() " << index); } /** * Get index of given objec in group. * * \param object object * \return index (-1 if object not found) */ int operator()(const T& object) const { for (int i = 0; i != size; ++i) { if (this->group[i] == object) { return i; } } return -1; } }; /** * Number of objects. */ template const int JGroup::size = sizeof(JGroup::group) / sizeof(JGroup::group[0]); } #endif