Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JTypeID.hh
Go to the documentation of this file.
1#ifndef __JLANG__JTYPEID__
2#define __JLANG__JTYPEID__
3
4#include <ostream>
5
6#include "JLang/JNullType.hh"
7#include "JLang/JTest.hh"
8
9
10/**
11 * \author mdejong
12 */
13
14namespace JLANG {}
15namespace JPP { using namespace JLANG; }
16
17namespace JLANG {
18
19 /**
20 * Auxiliary data structure to label data types within a type list.
21 */
22 struct JLabel_t {
23 /**
24 * Constructor.
25 *
26 * \param ID identifier
27 * \param name name
28 * \param version version
29 */
30 JLabel_t(const int ID,
31 const char* name,
32 const int version) :
33 __ID (ID),
34 __name (name),
35 __version(version)
36 {}
37
38
39 /**
40 * Get data type identifier.
41 *
42 * \return identifier
43 */
44 inline int getID() const
45 {
46 return __ID;
47 }
48
49
50 /**
51 * Get name of data type.
52 *
53 * \return name
54 */
55 inline const char* getName() const
56 {
57 return __name;
58 }
59
60
61 /**
62 * Get version of data type.
63 *
64 * \return version
65 */
66 inline int getVersion() const
67 {
68 return __version;
69 }
70
71
72 /**
73 * Write label to output.
74 *
75 * \param out output stream
76 * \param object label
77 * \return output stream
78 */
79 friend inline std::ostream& operator<<(std::ostream& out, const JLabel_t& object)
80 {
81 return out << object.getID() << '/'
82 << object.getName() << ':'
83 << object.getVersion();
84 }
85
86 protected:
87 int __ID;
88 const char* __name;
90 };
91
92
93 /**
94 * Get default name of data type.
95 *
96 * \return name
97 */
98 inline const char* getDefaultName()
99 {
100 return "?";
101 }
102
103
104 /**
105 * Get default version of data type.
106 *
107 * \return version
108 */
109 inline int getDefaultVersion()
110 {
111 return -1;
112 }
113
114
115 /**
116 * Test availability of static member methods for labelling data types in a type list.
117 */
118 template <class T>
119 class JTypeID_t :
120 public JTest
121 {
122 template<class U> static JTrue test1(JTypecheck<const char* (*)(), &U::getName>*);
123 template<class U> static JFalse test1(...);
124
125 template<class U> static JTrue test2(JTypecheck<int (*)(), &U::getVersion>*);
126 template<class U> static JFalse test2(...);
127
128 public:
129 static const bool has_name = sizeof(test1<T>(NULL)) == sizeof(JTrue);
130 static const bool has_version = sizeof(test2<T>(NULL)) == sizeof(JTrue);
131 };
132}
133
134
135/**
136 * Auxiliary data structure to label data types within a type list.
137 */
138template<int __ID,
139 const char* (*__getName) () = JLANG::getDefaultName,
140 int (*__getVersion)() = JLANG::getDefaultVersion>
141struct JLabel {
142
143 /**
144 * Data type identifier.
145 */
146 static const int ID = __ID;
147
148
149 /**
150 * Get name of data type.
151 *
152 * \return name
153 */
154 static inline const char* getName()
155 {
156 return (*__getName)();
157 }
158
159
160 /**
161 * Get version of data type.
162 *
163 * \return version
164 */
165 static inline int getVersion()
166 {
167 return (*__getVersion)();
168 }
169};
170
171
172/**
173 * Template definition of policy class to label data types in a type list.
174 *
175 * A default implementation of this class is provided for data types which already have a unique identifier.
176 * This identifier should be implemented as follows:
177 * <pre>
178 * static const int ID = <value>;
179 * </pre>
180 *
181 * The optional name and/or a version is then maintained, provided their implementations
182 * follow the corresponding declarations in JLANG::JTypeID_t, i.e:
183 * <pre>
184 * static const char* getName();
185 * static int getVersion();
186 * </pre>
187 *
188 * A specialisation of this class should otherwise be provided, e.g.\ by deriving it from class JLabel.
189 * The specialisation should provide the static data member <tt>ID</tt> and the static member methods listed above.
190 */
191template<class T,
194struct JTypeID {};
195
196
197/**
198 * Template specialisation of class without name and version.
199 */
200template<class T>
201struct JTypeID<T, false, false> :
202 public JLabel<T::ID>
203{};
204
205
206/**
207 * Template specialisation of class with name and without version.
208 */
209template<class T>
210struct JTypeID<T, true, false> :
211 public JLabel<T::ID, &T::getName>
212{};
213
214
215/**
216 * Template specialisation of class without name and with version.
217 */
218template<class T>
219struct JTypeID<T, false, true> :
220 public JLabel<T::ID, &JLANG::getDefaultName, &T::getVersion>
221{};
222
223
224/**
225 * Template specialisation of class with name and version.
226 */
227template<class T>
228struct JTypeID<T, true, true> :
229 public JLabel<T::ID, &T::getName, &T::getVersion>
230{};
231
232
233/**
234 * Template specialisation of class JTypeID for class JLANG::JNullType.
235 */
236template<>
238 public JLabel<0>
239{};
240
241#endif
Test availability of static member methods for labelling data types in a type list.
Definition JTypeID.hh:121
static const bool has_version
Definition JTypeID.hh:130
static JFalse test1(...)
static JTrue test2(JTypecheck< int(*)(), &U::getVersion > *)
static JFalse test2(...)
static JTrue test1(JTypecheck< const char *(*)(), &U::getName > *)
static const bool has_name
Definition JTypeID.hh:129
Auxiliary classes and methods for language specific functionality.
int getDefaultVersion()
Get default version of data type.
Definition JTypeID.hh:109
const char * getDefaultName()
Get default name of data type.
Definition JTypeID.hh:98
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure to label data types within a type list.
Definition JTypeID.hh:22
JLabel_t(const int ID, const char *name, const int version)
Constructor.
Definition JTypeID.hh:30
int getVersion() const
Get version of data type.
Definition JTypeID.hh:66
int getID() const
Get data type identifier.
Definition JTypeID.hh:44
const char * getName() const
Get name of data type.
Definition JTypeID.hh:55
const char * __name
Definition JTypeID.hh:88
friend std::ostream & operator<<(std::ostream &out, const JLabel_t &object)
Write label to output.
Definition JTypeID.hh:79
Auxiliary class for no type definition.
Definition JNullType.hh:19
definition of false
Definition JTest.hh:25
definition of true
Definition JTest.hh:24
Auxiliary class for type checking.
Definition JTest.hh:36
Auxiliary base class for compile time evaluation of test.
Definition JTest.hh:21
Auxiliary data structure to label data types within a type list.
Definition JTypeID.hh:141
static int getVersion()
Get version of data type.
Definition JTypeID.hh:165
static const char * getName()
Get name of data type.
Definition JTypeID.hh:154
Template definition of policy class to label data types in a type list.
Definition JTypeID.hh:194