Jpp 21.0.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JTemplate.hh
Go to the documentation of this file.
1#ifndef __JLANG__JTEMPLATE__
2#define __JLANG__JTEMPLATE__
3
4#include <istream>
5#include <ostream>
6#include <type_traits>
7
8#include "JLang/JTypeList.hh"
10#include "JLang/JConstructor.hh"
11
12
13/**
14 * \file
15 *
16 * Auxiliary class to alleviate the diamond problem due to multiple inheritance.
17 * \author mdejong
18 */
19namespace JLANG {}
20namespace JPP { using namespace JLANG; }
21
22namespace JLANG {
23
24 /**
25 * Auxiliary class for managing multiple objects.
26 *
27 * This class can be used to replace regular base classes by corresponding data members.
28 * The various operators and methods facilitate the access to these data members.
29 *
30 * The template value <tt>use_polymorfism</tt> in methods <tt>get</tt> and <tt>set</tt>
31 * applies to the given template parameter of these methods.
32 * If <tt>false</tt>, only exact type matches are allowed.
33 * If <tt>true</tt>,
34 * method <tt>get</tt> will return a reference to the first data member
35 * of which the type derives from the specified class and
36 * method <tt>set</tt> will assign the given value to the first data member
37 * for which a matching constructor exists.
38 */
39 template<class JType_t>
40 struct JTemplate {
41
43
44
45 /**
46 * Default constructor.
47 */
49 object()
50 {}
51
52
53 /**
54 * Constructor.
55 *
56 * \param value value
57 */
58 JTemplate(const JType_t& value) :
59 object(value)
60 {}
61
62
63 /**
64 * Type conversion.
65 *
66 * \return reference to object
67 */
68 inline operator const JType_t&() const
69 {
70 return object;
71 }
72
73
74 /**
75 * Type conversion.
76 *
77 * \return reference to object
78 */
79 inline operator JType_t&()
80 {
81 return object;
82 }
83
84
85 /**
86 * Smart pointer operator.
87 *
88 * \return pointer to object
89 */
90 const JType_t* operator->() const
91 {
92 return &object;
93 }
94
95
96 /**
97 * Smart pointer operator.
98 *
99 * \return pointer to object
100 */
101 JType_t* operator->()
102 {
103 return &object;
104 }
105
106
107 /**
108 * Get reference to object
109 *
110 * \return reference to object
111 */
112 template<class T, bool use_polymorphism = false>
113 const T& get() const
114 {
115 return this->c_get(std::bool_constant<use_polymorphism ? JConversion<JType_t, T> ::is_derived : JConversion<JType_t, T>::is_same>());
116 }
117
118
119 /**
120 * Get reference to object
121 *
122 * \return reference to object
123 */
124 template<class T, bool use_polymorphism = false>
125 T& get()
126 {
127 return this->c_get(std::bool_constant<use_polymorphism ? JConversion<JType_t, T> ::is_derived : JConversion<JType_t, T>::is_same>());
128 }
129
130
131 /**
132 * Set value.
133 *
134 * \param value value
135 * \return this object
136 */
137 inline template_type& set(const JType_t& value)
138 {
139 return c_set(value, std::true_type());
140 }
141
142
143 /**
144 * Set value.
145 *
146 * \param value value
147 * \return this object
148 */
149 template<bool use_polymorphism, class T>
150 inline template_type& set(const T& value)
151 {
152 return this->c_set(value, std::bool_constant<use_polymorphism ? JConstructor<JType_t, T> ::has_constructor : JConversion<JType_t, T>::is_same>());
153 }
154
155
156 /**
157 * Read object from input.
158 *
159 * \param in input stream
160 * \param object object
161 * \return input stream
162 */
163 friend inline std::istream& operator>>(std::istream& in, JTemplate<JType_t>& object)
164 {
165 return in >> object.object;
166 }
167
168
169 /**
170 * Write object to output.
171 *
172 * \param out output stream
173 * \param object object
174 * \return output stream
175 */
176 friend inline std::ostream& operator<<(std::ostream& out, const JTemplate<JType_t>& object)
177 {
178 return out << object.object;
179 }
180
181 protected:
182 /**
183 * Get reference to object
184 *
185 * \param option true
186 * \return reference to object
187 */
188 const JType_t& c_get(std::true_type option) const
189 {
190 return object;
191 }
192
193
194 /**
195 * Get reference to object
196 *
197 * \param option true
198 * \return reference to object
199 */
200 JType_t& c_get(std::true_type option)
201 {
202 return object;
203 }
204
205
206 /**
207 * Set value.
208 *
209 * \param value value
210 * \param option true
211 * \return this object
212 */
213 template<class T>
214 inline template_type& c_set(const T& value, std::true_type option)
215 {
216 object = value;
217
218 return *this;
219 }
220
221
222 JType_t object; //!< object
223 };
224
225
226 /**
227 * Auxiliary class for managing multiple objects.
228 */
229 template<class JHead_t, class JTail_t>
230 struct JTemplate< JTypeList<JHead_t, JTail_t> > :
231 public JTemplate<JHead_t>,
232 public JTemplate<JTail_t>
233 {
234
236
237
238 /**
239 * Default constructor.
240 */
242 JTemplate<JHead_t>(),
243 JTemplate<JTail_t>()
244 {}
245
246
247 /**
248 * Constructor.
249 *
250 * \param head value
251 * \param tail value
252 */
253 JTemplate(const JHead_t& head,
254 const JTail_t& tail) :
255 JTemplate<JHead_t>(head),
256 JTemplate<JTail_t>(tail)
257 {}
258
259
260 /**
261 * Get reference to object
262 *
263 * \return reference to object
264 */
265 template<class T, bool use_polymorphism = false>
266 const T& get() const
267 {
268 return this->c_get<T, use_polymorphism>(std::bool_constant<use_polymorphism ? JConversion<JHead_t, T> ::is_derived : JConversion<JHead_t, T>::is_same>());
269 }
270
271
272 /**
273 * Get reference to object
274 *
275 * \return reference to object
276 */
277 template<class T, bool use_polymorphism = false>
278 T& get()
279 {
280 return this->c_get<T, use_polymorphism>(std::bool_constant<use_polymorphism ? JConversion<JHead_t, T> ::is_derived : JConversion<JHead_t, T>::is_same>());
281 }
282
283
284 /**
285 * Set value.
286 *
287 * \param value value
288 * \return this object
289 */
290 template<class T>
291 inline template_type& set(const T& value)
292 {
293 return this->c_set<false>(value, std::bool_constant<JConversion<JHead_t, T>::is_same>());
294 }
295
296
297 /**
298 * Set value.
299 *
300 * \param value value
301 * \return this object
302 */
303 template<bool use_polymorphism, class T>
304 inline template_type& set(const T& value)
305 {
306 return this->c_set<use_polymorphism>(value, std::bool_constant<use_polymorphism ? JConstructor<JHead_t, T> ::has_constructor : JConversion<JHead_t, T>::is_same>());
307 }
308
309
310 /**
311 * Read object from input.
312 *
313 * \param in input stream
314 * \param object object
315 * \return input stream
316 */
317 friend inline std::istream& operator>>(std::istream& in, JTemplate< JTypeList<JHead_t, JTail_t> >& object)
318 {
319 return in >> static_cast<JTemplate<JHead_t>&>(object)
320 >> static_cast<JTemplate<JTail_t>&>(object);
321 }
322
323
324 /**
325 * Write object to output.
326 *
327 * \param out output stream
328 * \param object object
329 * \return output stream
330 */
331 friend inline std::ostream& operator<<(std::ostream& out, const JTemplate< JTypeList<JHead_t, JTail_t> >& object)
332 {
333 return out << static_cast<const JTemplate<JHead_t>&>(object) << ' '
334 << static_cast<const JTemplate<JTail_t>&>(object);
335 }
336
337 protected:
338 /**
339 * Get reference to object
340 *
341 * \param option true
342 * \return reference to object
343 */
344 template<class T, bool use_polymorphism>
345 const T& c_get(std::true_type option) const
346 {
347 return static_cast<JTemplate<JHead_t>&>(*this).template get<T, use_polymorphism>();
348 }
349
350
351 /**
352 * Get reference to object
353 *
354 * \param option false
355 * \return reference to object
356 */
357 template<class T, bool use_polymorphism>
358 const T& c_get(std::false_type option) const
359 {
360 return static_cast<JTemplate<JTail_t>&>(*this).template get<T, use_polymorphism>();
361 }
362
363
364 /**
365 * Get reference to object
366 *
367 * \param option true
368 * \return reference to object
369 */
370 template<class T, bool use_polymorphism>
371 T& c_get(std::true_type option)
372 {
373 return static_cast<JTemplate<JHead_t>&>(*this).template get<T, use_polymorphism>();
374 }
375
376
377 /**
378 * Get reference to object
379 *
380 * \param option false
381 * \return reference to object
382 */
383 template<class T, bool use_polymorphism>
384 T& c_get(std::false_type option)
385 {
386 return static_cast<JTemplate<JTail_t>&>(*this).template get<T, use_polymorphism>();
387 }
388
389
390 /**
391 * Set value.
392 *
393 * \param value value
394 * \param option true
395 * \return this object
396 */
397 template<bool use_polymorphism, class T>
398 inline template_type& c_set(const T& value, std::true_type option)
399 {
400 static_cast<JTemplate<JHead_t>&>(*this).template set<use_polymorphism>(value);
401
402 return *this;
403 }
404
405
406 /**
407 * Set value.
408 *
409 * \param value value
410 * \param option false
411 * \return this object
412 */
413 template<bool use_polymorphism, class T>
414 inline template_type& c_set(const T& value, std::false_type option)
415 {
416 static_cast<JTemplate<JTail_t>&>(*this).template set<use_polymorphism>(value);
417
418 return *this;
419 }
420 };
421
422
423 /**
424 * Auxiliary class for managing multiple objects.
425 */
426 template<class JHead_t>
427 struct JTemplate< JTypeList<JHead_t> > :
428 public JTemplate<JHead_t>
429 {
430 /**
431 * Default constructor.
432 */
434 JTemplate<JHead_t>()
435 {}
436
437
438 /**
439 * Constructor.
440 *
441 * \param value value
442 */
443 JTemplate(const JHead_t& value) :
444 JTemplate<JHead_t>(value)
445 {}
446 };
447}
448
449#endif
Template class test for availability of a suitable constructor JType_t(const JArgument_t).
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Template class test for polymorphism.
Auxiliary class for managing multiple objects.
Definition JTemplate.hh:233
friend std::istream & operator>>(std::istream &in, JTemplate< JTypeList< JHead_t, JTail_t > > &object)
Read object from input.
Definition JTemplate.hh:317
template_type & c_set(const T &value, std::false_type option)
Set value.
Definition JTemplate.hh:414
const T & c_get(std::false_type option) const
Get reference to object.
Definition JTemplate.hh:358
const T & c_get(std::true_type option) const
Get reference to object.
Definition JTemplate.hh:345
T & c_get(std::false_type option)
Get reference to object.
Definition JTemplate.hh:384
template_type & c_set(const T &value, std::true_type option)
Set value.
Definition JTemplate.hh:398
JTemplate< JTypeList< JHead_t, JTail_t > > template_type
Definition JTemplate.hh:235
JTemplate(const JHead_t &head, const JTail_t &tail)
Constructor.
Definition JTemplate.hh:253
template_type & set(const T &value)
Set value.
Definition JTemplate.hh:291
T & c_get(std::true_type option)
Get reference to object.
Definition JTemplate.hh:371
const T & get() const
Get reference to object.
Definition JTemplate.hh:266
template_type & set(const T &value)
Set value.
Definition JTemplate.hh:304
friend std::ostream & operator<<(std::ostream &out, const JTemplate< JTypeList< JHead_t, JTail_t > > &object)
Write object to output.
Definition JTemplate.hh:331
JTemplate(const JHead_t &value)
Constructor.
Definition JTemplate.hh:443
Auxiliary class for managing multiple objects.
Definition JTemplate.hh:40
template_type & set(const T &value)
Set value.
Definition JTemplate.hh:150
T & get()
Get reference to object.
Definition JTemplate.hh:125
JTemplate(const JType_t &value)
Constructor.
Definition JTemplate.hh:58
template_type & set(const JType_t &value)
Set value.
Definition JTemplate.hh:137
JTemplate< JType_t > template_type
Definition JTemplate.hh:42
JTemplate()
Default constructor.
Definition JTemplate.hh:48
template_type & c_set(const T &value, std::true_type option)
Set value.
Definition JTemplate.hh:214
const T & get() const
Get reference to object.
Definition JTemplate.hh:113
const JType_t & c_get(std::true_type option) const
Get reference to object.
Definition JTemplate.hh:188
JType_t * operator->()
Smart pointer operator.
Definition JTemplate.hh:101
friend std::istream & operator>>(std::istream &in, JTemplate< JType_t > &object)
Read object from input.
Definition JTemplate.hh:163
const JType_t * operator->() const
Smart pointer operator.
Definition JTemplate.hh:90
JType_t object
object
Definition JTemplate.hh:222
JType_t & c_get(std::true_type option)
Get reference to object.
Definition JTemplate.hh:200
friend std::ostream & operator<<(std::ostream &out, const JTemplate< JType_t > &object)
Write object to output.
Definition JTemplate.hh:176
Type list.
Definition JTypeList.hh:23