Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTypeList.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JTYPELIST__
2 #define __JLANG__JTYPELIST__
3 
4 #include "JLang/JNullType.hh"
5 #include "JLang/JType.hh"
6 
7 
8 /**
9  * \author mdejong
10  */
11 
12 namespace JLANG {}
13 namespace JPP { using namespace JLANG; }
14 
15 namespace JLANG {
16 
17 
18  /**
19  * Type list.
20  */
21  template<class JHead_t = JNullType, class JTail_t = JNullType>
22  struct JTypeList
23  {
24  typedef JHead_t head;
25  typedef JTail_t tail;
26  };
27 
28 
29  /**
30  * List of identical types.
31  */
32  template<unsigned int N, class T>
33  struct JMultipleType
34  {
35  typedef JTypeList<T, typename JMultipleType<N-1, T>::typelist> typelist;
36  };
37 
38 
39  /**
40  * Terminator class of list of identical types.
41  */
42  template<class T>
43  struct JMultipleType<1, T>
44  {
46  };
47 
48 
49  /**
50  * Append to type list.
51  *
52  * Source code is taken from reference:
53  * A. Alexandrescu, Modern C++ Design, Addison Wesley.
54  */
55 
56 
57  /**
58  * Template specialisation of append to type list.
59  */
60  template<class JHead_t, class JTail_t>
61  struct JAppend
62  {
64  };
65 
66 
67  /**
68  * Template specialisation of append to type list.
69  */
70  template<>
72  {
74  };
75 
76 
77  /**
78  * Template specialisation of append to type list.
79  */
80  template<class T>
81  struct JAppend<JNullType, T>
82  {
84  };
85 
86 
87  /**
88  * Template specialisation of append to type list.
89  */
90  template<class JHead_t, class JTail_t>
91  struct JAppend<JNullType, JTypeList<JHead_t, JTail_t> >
92  {
94  };
95 
96 
97  /**
98  * Template specialisation of append to type list.
99  */
100  template<class JHead_t, class JTail_t, class T>
101  struct JAppend<JTypeList<JHead_t, JTail_t>, T>
102  {
104  };
105 
106 
107  /**
108  * Removal of data type from type list.
109  *
110  * Source code is taken from reference:
111  * A. Alexandrescu, Modern C++ Design, Addison Wesley.
112  */
113  template<class JTypelist_t, class T>
114  struct JRemove;
115 
116 
117  /**
118  * Template specialisation of removal of data type from type list.
119  */
120  template<class T>
121  struct JRemove<JNullType, T>
122  {
124  };
125 
126 
127  /**
128  * Template specialisation of removal of data type from type list.
129  */
130  template<class T, class JTail_t>
131  struct JRemove<JTypeList<T, JTail_t>, T>
132  {
133  typedef JTail_t typelist;
134  };
135 
136 
137  /**
138  * Template specialisation of removal of data type from type list.
139  */
140  template<class JHead_t, class JTail_t, class T>
141  struct JRemove<JTypeList<JHead_t, JTail_t>, T>
142  {
144  };
145 
146 
147  /**
148  * Template specialisation of removal of type list from type list.
149  */
150  template<class JHead_t1, class JTail_t1, class JHead_t2, class JTail_t2>
151  struct JRemove<JTypeList<JHead_t1, JTail_t1>,
152  JTypeList<JHead_t2, JTail_t2> >
153  {
156  };
157 
158 
159  /**
160  * Template specialisation of removal of type list from type list.
161  */
162  template<class JHead_t1, class JTail_t1, class JHead_t2>
163  struct JRemove<JTypeList<JHead_t1, JTail_t1>,
164  JTypeList<JHead_t2, JNullType> >
165  {
167  };
168 
169 
170  /**
171  * Length of type list.
172  *
173  * Source code is taken from reference:
174  * A. Alexandrescu, Modern C++ Design, Addison Wesley.
175  */
176  template<class JTypeList_t> struct JLength {};
177 
178 
179  /**
180  * Recursive length of type list.
181  */
182  template<class JHead_t, class JTail_t>
183  struct JLength< JTypeList<JHead_t, JTail_t> >
184  {
185  enum { value = 1 + JLength<JTail_t>::value };
186  };
187 
188 
189  /**
190  * Terminator class of length of type list.
191  */
192  template<>
194  {
195  enum { value = 0 };
196  };
197 
198 
199  /**
200  * Test presence of data type in type list.
201  */
202  template<class JTypeList_t, class T>
203  struct JHasType;
204 
205 
206  /**
207  * Recursive test of presence data type in type list.
208  */
209  template<class JHead_t, class JTail_t, class T>
210  struct JHasType<JTypeList<JHead_t, JTail_t>, T>
211  {
212  enum { value = JHasType<JTail_t, T>::value };
213  };
214 
215 
216  /**
217  * Identify presence data type in type list.
218  */
219  template<class JTail_t, class T>
220  struct JHasType<JTypeList<T, JTail_t>, T>
221  {
222  enum { value = true };
223  };
224 
225 
226  /**
227  * Termination of recursive test of presence data type in type list.
228  */
229  template<class T>
230  struct JHasType<JNullType, T>
231  {
232  enum { value = false };
233  };
234 
235 
236  /**
237  * Specialisation of JHasType for single class type.
238  */
239  template<class T>
240  struct JHasType<T, T>
241  {
242  enum { value = true };
243  };
244 
245 
246  /**
247  * Resolve template class to JTypeList.
248  */
249  template<class T>
251  {
253  };
254 
255 
256  /**
257  * Resolve JTypeList to JTypeList.
258  */
259  template<class JHead_t, class JTail_t>
260  struct JResolveTypeList< JTypeList<JHead_t, JTail_t> >
261  {
263  };
264 
265 
266  /**
267  * Extraction of data type from type list.
268  *
269  * Source code is taken from reference:
270  * A. Alexandrescu, Modern C++ Design, Addison Wesley.
271  */
272  template<class JTypelist_t, unsigned int index, bool range_check = true>
273  struct JTypeAt;
274 
275 
276  /**
277  * Recursive extraction of data type from type list.
278  */
279  template<class JHead_t, class JTail_t, unsigned int index, bool range_check>
280  struct JTypeAt<JTypeList<JHead_t, JTail_t>, index, range_check>
281  {
282  typedef typename JTypeAt<JTail_t, index - 1, range_check>::value_type value_type;
283  };
284 
285 
286  /**
287  * Termination of recursive extraction of data type from type list.
288  */
289  template<class JHead_t, class JTail_t, bool range_check>
290  struct JTypeAt<JTypeList<JHead_t, JTail_t>, 0, range_check>
291  {
292  typedef JHead_t value_type;
293  };
294 
295 
296  /**
297  * Termination of recursive extraction of data type from type list.
298  */
299  template<unsigned int index>
300  struct JTypeAt<JNullType, index, false>
301  {
303  };
304 
305 
306  /**
307  * Indexing of data type in type list.
308  */
309  template<class JTypeList_t, class T>
310  struct JIndexOf;
311 
312 
313  /**
314  * Recursive indexing of data type in type list.
315  */
316  template<class JHead_t, class JTail_t, class T>
317  struct JIndexOf<JTypeList<JHead_t, JTail_t>, T>
318  {
319  private:
321 
322  public:
323  enum { value = (tmp == -1 ? -1 : tmp + 1) };
324  };
325 
326 
327  /**
328  * Identify indexi of data type in type list.
329  */
330  template<class JTail_t, class T>
331  struct JIndexOf<JTypeList<T, JTail_t>, T>
332  {
333  enum { value = 0 };
334  };
335 
336 
337  /**
338  * Termination of recursive indexing of data type in type list.
339  */
340  template<class T>
341  struct JIndexOf<JNullType, T>
342  {
343  enum { value = -1 };
344  };
345 
346 
347  /**
348  * Auxiliary class for recursive type list generation.
349  * This class accepts up to 26 data types.
350  */
351  template<class A = JNullType,
352  class B = JNullType,
353  class C = JNullType,
354  class D = JNullType,
355  class E = JNullType,
356  class F = JNullType,
357  class G = JNullType,
358  class H = JNullType,
359  class I = JNullType,
360  class J = JNullType,
361  class K = JNullType,
362  class L = JNullType,
363  class M = JNullType,
364  class N = JNullType,
365  class O = JNullType,
366  class P = JNullType,
367  class Q = JNullType,
368  class R = JNullType,
369  class S = JNullType,
370  class T = JNullType,
371  class U = JNullType,
372  class V = JNullType,
373  class W = JNullType,
374  class X = JNullType,
375  class Y = JNullType,
376  class Z = JNullType>
377  struct JTYPELIST
378  {
380  };
381 
382 
383  /**
384  * Template specialisation for expanding type list.
385  */
386  template<class JHead_t,
387  class JTail_t,
388  class B,
389  class C,
390  class D,
391  class E,
392  class F,
393  class G,
394  class H,
395  class I,
396  class J,
397  class K,
398  class L,
399  class M,
400  class N,
401  class O,
402  class P,
403  class Q,
404  class R,
405  class S,
406  class T,
407  class U,
408  class V,
409  class W,
410  class X,
411  class Y,
412  class Z>
413  struct JTYPELIST<JTypeList<JHead_t, JTail_t>,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z>
414  {
416  };
417 
418 
419  /**
420  * Template specialisation to skip JNullType.
421  */
422  template<class B,
423  class C,
424  class D,
425  class E,
426  class F,
427  class G,
428  class H,
429  class I,
430  class J,
431  class K,
432  class L,
433  class M,
434  class N,
435  class O,
436  class P,
437  class Q,
438  class R,
439  class S,
440  class T,
441  class U,
442  class V,
443  class W,
444  class X,
445  class Y,
446  class Z>
447  struct JTYPELIST<JNullType,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z>
448  {
450  };
451 
452 
453  /**
454  * Template specialisation to terminate recursive type list generation.
455  */
456  template<class A>
457  struct JTYPELIST<A,
458  JNullType,
459  JNullType,
460  JNullType,
461  JNullType,
462  JNullType,
463  JNullType,
464  JNullType,
465  JNullType,
466  JNullType,
467  JNullType,
468  JNullType,
469  JNullType,
470  JNullType,
471  JNullType,
472  JNullType,
473  JNullType,
474  JNullType,
475  JNullType,
476  JNullType,
477  JNullType,
478  JNullType,
479  JNullType,
480  JNullType,
481  JNullType,
482  JNullType>
483  {
485  };
486 
487 
488  /**
489  * Template specialisation for empty type list generation.
490  */
491  template<>
493  JNullType,
494  JNullType,
495  JNullType,
496  JNullType,
497  JNullType,
498  JNullType,
499  JNullType,
500  JNullType,
501  JNullType,
502  JNullType,
503  JNullType,
504  JNullType,
505  JNullType,
506  JNullType,
507  JNullType,
508  JNullType,
509  JNullType,
510  JNullType,
511  JNullType,
512  JNullType,
513  JNullType,
514  JNullType,
515  JNullType,
516  JNullType,
517  JNullType>
518  {
520  };
521 
522 
523  /**
524  * Template specialisation to terminate recursive type list generation.
525  */
526  template<class JHead_t, class JTail_t>
527  struct JTYPELIST<JTypeList<JHead_t, JTail_t>,
528  JNullType,
529  JNullType,
530  JNullType,
531  JNullType,
532  JNullType,
533  JNullType,
534  JNullType,
535  JNullType,
536  JNullType,
537  JNullType,
538  JNullType,
539  JNullType,
540  JNullType,
541  JNullType,
542  JNullType,
543  JNullType,
544  JNullType,
545  JNullType,
546  JNullType,
547  JNullType,
548  JNullType,
549  JNullType,
550  JNullType,
551  JNullType,
552  JNullType>
553  {
555  };
556 
557 
558  /**
559  * For each data type method.
560  *
561  * The given object should provide for the function object operator
562  * <pre>
563  * template<class T>
564  * void object()(JType<T> type);
565  * </pre>
566  *
567  * \param object object
568  * \param typelist type list
569  * \return object
570  */
571  template<class JObject_t, class JHead_t, class JTail_t>
572  JObject_t& for_each(JObject_t& object, JType< JTypeList<JHead_t, JTail_t> > typelist)
573  {
574  for_each(object, JType<JHead_t>());
575  for_each(object, JType<JTail_t>());
576 
577  return object;
578  }
579 
580 
581  /**
582  * For each data type method.
583  *
584  * The given object should provide for the function object operator
585  * <pre>
586  * template<class T>
587  * void object()(JType<T> type);
588  * </pre>
589  *
590  * \param object object
591  * \param type type
592  * \return object
593  */
594  template<class JObject_t, class T>
595  JObject_t& for_each(JObject_t& object, JType<T> type)
596  {
597  object(type);
598 
599  return object;
600  }
601 
602 
603  /**
604  * Termination method of for each data type method.
605  *
606  * \param object object
607  * \param type null type
608  * \return object
609  */
610  template<class JObject_t>
611  JObject_t& for_each(JObject_t& object, JType<JNullType> type)
612  {
613  return object;
614  }
615 }
616 
617 #endif
static const double H
Planck constant [eV s].
Definition: JConstants.hh:25
JRemove< typename JRemove< JTypeList< JHead_t1, JTail_t1 >, JHead_t2 >::typelist, JTail_t2 >::typelist typelist
Definition: JTypeList.hh:155
JTypeAt< JTail_t, index-1, range_check >::value_type value_type
Definition: JTypeList.hh:282
Append to type list.
Definition: JTypeList.hh:61
Test presence of data type in type list.
Definition: JTypeList.hh:203
JTypeList< JHead_t, typename JTYPELIST< JTail_t, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z >::typelist > typelist
Definition: JTypeList.hh:415
Removal of data type from type list.
Definition: JTypeList.hh:114
Auxiliary class for a type holder.
Definition: JType.hh:19
JTypeList< T, JNullType > typelist
Definition: JTypeList.hh:45
Length of type list.
Definition: JTypeList.hh:176
Type list.
Definition: JTypeList.hh:22
JTypeList< A, typename JTYPELIST< B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z >::typelist > typelist
Definition: JTypeList.hh:379
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:377
JTypeList< T > typelist
Definition: JTypeList.hh:252
Resolve template class to JTypeList.
Definition: JTypeList.hh:250
Auxiliary class for no type definition.
Definition: JNullType.hh:19
JRemove< JTypeList< JHead_t1, JTail_t1 >, JHead_t2 >::typelist typelist
Definition: JTypeList.hh:166
List of identical types.
Definition: JTypeList.hh:33
JTypeList< JHead_t, typename JRemove< JTail_t, T >::typelist > typelist
Definition: JTypeList.hh:143
JTypeList< JHead_t, typename JAppend< JTail_t, T >::typelist > typelist
Definition: JTypeList.hh:103
JTYPELIST< B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z >::typelist typelist
Definition: JTypeList.hh:449
JObject_t & for_each(JObject_t &object, JType< JTypeList< JHead_t, JTail_t > > typelist)
For each data type method.
Definition: JTypeList.hh:572
JTypeList< JHead_t, JTail_t > typelist
Definition: JTypeList.hh:63
Indexing of data type in type list.
Definition: JTypeList.hh:310
static const double C
Speed of light in vacuum [m/ns].
Definition: JConstants.hh:22
JTypeList< T, typename JMultipleType< N-1, T >::typelist > typelist
Definition: JTypeList.hh:35
Extraction of data type from type list.
Definition: JTypeList.hh:273