Jpp
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  /**
33  * Number of objects.
34  */
35  static const int size = sizeof(JGroup::group) / sizeof(JGroup::group[0]);
36 
37 
38  /**
39  * Get unique instance.
40  */
41  static const JGroup& getInstance()
42  {
43  static const JGroup object;
44 
45  return object;
46  }
47 
48 
49  /**
50  * Get object at given index in group.
51  *
52  * \param index index
53  * \return object
54  */
55  const T& operator()(const int index) const
56  {
57  if (index >= 0 && index < size)
58  return this->group[index];
59  else
60  THROW(JIndexOutOfRange, "JGroup::operator() " << index);
61  }
62 
63 
64  /**
65  * Get index of given objec in group.
66  *
67  * \param object object
68  * \return index (-1 if object not found)
69  */
70  int operator()(const T& object) const
71  {
72  for (int i = 0; i != size; ++i) {
73  if (this->group[i] == object) {
74  return i;
75  }
76  }
77 
78  return -1;
79  }
80 
81  private:
82  /**
83  * Default constructor.
84  */
85  JGroup()
86  {}
87 
88  static const T group[]; //!< actual group
89  };
90 }
91 
92 #endif
JException.hh
JLANG::JGroup::operator()
int operator()(const T &object) const
Get index of given objec in group.
Definition: JGroup.hh:70
JLANG::JIndexOutOfRange
Exception for accessing an index in a collection that is outside of its range.
Definition: JException.hh:90
JLANG::JGroup::size
static const int size
Number of objects.
Definition: JGroup.hh:35
JLANG::JGroup::JGroup
JGroup()
Default constructor.
Definition: JGroup.hh:85
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:669
JLANG::JGroup::getInstance
static const JGroup & getInstance()
Get unique instance.
Definition: JGroup.hh:41
JLANG::JGroup::group
static const T group[]
actual group
Definition: JGroup.hh:88
JLANG::JGroup::operator()
const T & operator()(const int index) const
Get object at given index in group.
Definition: JGroup.hh:55
JLANG::JGroup
Auxiliary class for a fixed group of objects.
Definition: JGroup.hh:31
JLANG
Auxiliary classes and methods for language specific functionality.
Definition: JAbstractClass.hh:10