Jpp  16.0.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JGroup.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JGROUP__
2 #define __JLANG__JGROUP__
3 
4 #include "JLang/JException.hh"
5 
6 
7 /**
8  * \author mdejong
9  */
10 
11 namespace JLANG {}
12 namespace JPP { using namespace JLANG; }
13 
14 namespace JLANG {
15 
16  /**
17  * Auxiliary class for a fixed group of objects.
18  *
19  * A group should be defined as follows:
20  * <pre>
21  * template<>
22  * const XXX JGroup<XXX>::group[] = {
23  * XXX(...),
24  * XXX(...),
25  * ...
26  * XXX(...)
27  * };
28  * </pre>
29  */
30  template<class T>
31  struct JGroup {
32  private:
33  /**
34  * Default constructor.
35  */
36  JGroup()
37  {}
38 
39  static const T group[]; //!< actual group
40 
41  public:
42  /**
43  * Number of objects.
44  */
45  static const int size;
46 
47 
48  /**
49  * Get unique instance.
50  */
51  static const JGroup& getInstance()
52  {
53  static const JGroup object;
54 
55  return object;
56  }
57 
58 
59  /**
60  * Get object at given index in group.
61  *
62  * \param index index
63  * \return object
64  */
65  const T& operator()(const int index) const
66  {
67  if (index >= 0 && index < size)
68  return this->group[index];
69  else
70  THROW(JIndexOutOfRange, "JGroup::operator() " << index);
71  }
72 
73 
74  /**
75  * Get index of given objec in group.
76  *
77  * \param object object
78  * \return index (-1 if object not found)
79  */
80  int operator()(const T& object) const
81  {
82  for (int i = 0; i != size; ++i) {
83  if (this->group[i] == object) {
84  return i;
85  }
86  }
87 
88  return -1;
89  }
90  };
91 
92 
93  /**
94  * Actual group.
95  */
96  template<class T>
97  const T JGroup<T>::group[];
98 
99 
100  /**
101  * Number of objects.
102  */
103  template<class T>
104  const int JGroup<T>::size = sizeof(JGroup<T>::group) / sizeof(JGroup<T>::group[0]);
105 }
106 
107 #endif
Exceptions.
JGroup()
Default constructor.
Definition: JGroup.hh:36
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
static const int size
Number of objects.
Definition: JGroup.hh:45
do set_variable OUTPUT_DIRECTORY $WORKDIR T
static const JGroup & getInstance()
Get unique instance.
Definition: JGroup.hh:51
Auxiliary class for a fixed group of objects.
Definition: JGroup.hh:31
static const T group[]
actual group
Definition: JGroup.hh:39
const T & operator()(const int index) const
Get object at given index in group.
Definition: JGroup.hh:65
Exception for accessing an index in a collection that is outside of its range.
Definition: JException.hh:90
int operator()(const T &object) const
Get index of given objec in group.
Definition: JGroup.hh:80