Jpp 21.0.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
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
11namespace JLANG {}
12namespace JPP { using namespace JLANG; }
13
14namespace 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 */
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 * Number of objects.
95 */
96 template<class T>
97 const int JGroup<T>::size = sizeof(JGroup<T>::group) / sizeof(JGroup<T>::group[0]);
98}
99
100#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Exception for accessing an index in a collection that is outside of its range.
Definition JException.hh:92
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary class for a fixed group of objects.
Definition JGroup.hh:31
static const int size
Number of objects.
Definition JGroup.hh:45
JGroup()
Default constructor.
Definition JGroup.hh:36
static const JGroup & getInstance()
Get unique instance.
Definition JGroup.hh:51
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
int operator()(const T &object) const
Get index of given objec in group.
Definition JGroup.hh:80