source: soft/giet_vm/mover/include/libelfpp/dpp/template_switch @ 160

Last change on this file since 160 was 160, checked in by karaoui, 12 years ago

giet-vm new version

File size: 13.1 KB
Line 
1/* -*- c++ -*-
2
3   This file is part of the dpp library of C++ template classes
4
5   doc: http://diaxen.ssji.net/dpp/index.html
6   repo: https://www.ssji.net/svn/projets/trunk/libdpp
7
8   This program is free software: you can redistribute it and/or
9   modify it under the terms of the GNU Lesser General Public License
10   as published by the Free Software Foundation, either version 3 of
11   the License, or (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public
19   License along with this program.  If not, see
20   <http://www.gnu.org/licenses/>.
21
22   (c) 2012 Alexandre Becoulet <alexandre.becoulet@free.fr>
23
24   Some parts of this file were generated using tools/template_switch.pl
25
26*/
27
28#ifndef DPP_TEMPLATE_SWITCH_HH_
29#define DPP_TEMPLATE_SWITCH_HH_
30
31/** @file @module {Template switch} */
32
33namespace dpp {
34
35  /**
36      @module {Template switch}
37      @header dpp/template_switch
38
39      @This declares a class designed to hold an array of function
40      pointers for multiple instances of the same template function
41      but with different values of its enum template parameter.
42
43      The class constructor fill the array with function pointers for
44      all enum values between 0 and @tt C0. The function pointer can
45      later be retrieved using the @tt {()} operator on the template
46      switch object.
47
48      Other classes exists which hold multidimensional array to deal
49      with multiple enum template parameters.
50
51      @section {Examples}
52      Use of template switch with global function and 1 enum parameter:
53      @example test/test_template_switch.cc:example|example1
54 
55      Use of template switch with member function and 2 enum parameters:
56      @example test/test_template_switch.cc:example|example2
57      @end section
58  */
59#define DPP_TEMPLATE_SWITCH_1(clname, func_type, func, C0)      \
60  class clname                                                  \
61  {                                                             \
62    typedef typeof(C0) e0;                                      \
63                                                                \
64    typedef func_type table_type[C0+1];                         \
65    table_type _t;                                              \
66                                                                \
67    template <int I0, bool done>                                \
68    struct iter;                                                \
69                                                                \
70    template <int I0>                                           \
71    struct iter<I0, true>                                       \
72    {                                                           \
73      static void s(table_type &t)                              \
74      {                                                         \
75      }                                                         \
76    };                                                          \
77                                                                \
78    template <int I0, bool done>                                \
79    struct iter                                                 \
80    {                                                           \
81      static void s(table_type &t)                              \
82      {                                                         \
83        t[I0] = &func<(e0)I0>;                                  \
84        iter<2*I0+1, (2*I0+1 >= C0+1)>::s(t);                   \
85        iter<2*I0+2, (2*I0+2 >= C0+1)>::s(t);                   \
86      }                                                         \
87    };                                                          \
88                                                                \
89  public:                                                       \
90                                                                \
91    clname()                                                    \
92    {                                                           \
93      iter<0, false>::s(_t);                                    \
94    }                                                           \
95                                                                \
96    const func_type operator() (e0 p0) const                    \
97    {                                                           \
98      return _t[p0];                                            \
99    }                                                           \
100  }
101
102  /**
103      @module {Template switch}
104      @header dpp/template_switch
105      @see #DPP_TEMPLATE_SWITCH_1
106  */
107#define DPP_TEMPLATE_SWITCH_2(clname, func_type, func, C0, C1)  \
108  class clname                                                  \
109  {                                                             \
110    typedef typeof(C0) e0;                                      \
111    typedef typeof(C1) e1;                                      \
112                                                                \
113    typedef func_type table_type[C0+1][C1+1];                   \
114    table_type _t;                                              \
115                                                                \
116    template <int I0, int I1, bool done>                        \
117    struct iter;                                                \
118                                                                \
119    template <int I0, int I1>                                   \
120    struct iter<I0, I1, true>                                   \
121    {                                                           \
122      static void s(table_type &t)                              \
123      {                                                         \
124      }                                                         \
125    };                                                          \
126                                                                \
127    template <int I0, int I1, bool done>                        \
128    struct iter                                                 \
129    {                                                           \
130      static void s(table_type &t)                              \
131      {                                                         \
132        t[I0][I1] = &func<(e0)I0, (e1)I1>;                      \
133        iter<2*I0+1, I1, (2*I0+1 >= C0+1)>::s(t);               \
134        iter<2*I0+2, I1, (2*I0+2 >= C0+1)>::s(t);               \
135        if (2*I0+1 == C0+1 || 2*I0+2 == C0+1) {                 \
136          iter<0, 2*I1+1, (2*I1+1 >= C1+1)>::s(t);              \
137          iter<0, 2*I1+2, (2*I1+2 >= C1+1)>::s(t);              \
138        }                                                       \
139      }                                                         \
140    };                                                          \
141                                                                \
142  public:                                                       \
143                                                                \
144    clname()                                                    \
145    {                                                           \
146      iter<0, 0, false>::s(_t);                                 \
147    }                                                           \
148                                                                \
149    const func_type operator() (e0 p0, e1 p1) const             \
150    {                                                           \
151      return _t[p0][p1];                                        \
152    }                                                           \
153  }
154
155
156  /**
157      @module {Template switch}
158      @header dpp/template_switch
159      @see #DPP_TEMPLATE_SWITCH_1
160  */
161#define DPP_TEMPLATE_SWITCH_3(clname, func_type, func,          \
162                              C0, C1, C2)                       \
163  class clname                                                  \
164  {                                                             \
165    typedef typeof(C0) e0;                                      \
166    typedef typeof(C1) e1;                                      \
167    typedef typeof(C2) e2;                                      \
168                                                                \
169    typedef func_type table_type[C0+1][C1+1][C2+1];             \
170    table_type _t;                                              \
171                                                                \
172    template <int I0, int I1, int I2, bool done>                \
173    struct iter;                                                \
174                                                                \
175    template <int I0, int I1, int I2>                           \
176    struct iter<I0, I1, I2, true>                               \
177    {                                                           \
178      static void s(table_type &t)                              \
179      {                                                         \
180      }                                                         \
181    };                                                          \
182                                                                \
183    template <int I0, int I1, int I2, bool done>                \
184    struct iter                                                 \
185    {                                                           \
186      static void s(table_type &t)                              \
187      {                                                         \
188        t[I0][I1][I2] = &func<(e0)I0, (e1)I1, (e2)I2>;          \
189        iter<2*I0+1, I1, I2, (2*I0+1 >= C0+1)>::s(t);           \
190        iter<2*I0+2, I1, I2, (2*I0+2 >= C0+1)>::s(t);           \
191        if (2*I0+1 == C0+1 || 2*I0+2 == C0+1) {                 \
192          iter<0, 2*I1+1, I2, (2*I1+1 >= C1+1)>::s(t);          \
193          iter<0, 2*I1+2, I2, (2*I1+2 >= C1+1)>::s(t);          \
194          if (2*I1+1 == C1+1 || 2*I1+2 == C1+1) {               \
195            iter<0, 0, 2*I2+1, (2*I2+1 >= C2+1)>::s(t);         \
196            iter<0, 0, 2*I2+2, (2*I2+2 >= C2+1)>::s(t);         \
197          }                                                     \
198        }                                                       \
199      }                                                         \
200    };                                                          \
201                                                                \
202  public:                                                       \
203                                                                \
204    clname()                                                    \
205    {                                                           \
206      iter<0, 0, 0, false>::s(_t);                              \
207    }                                                           \
208                                                                \
209    const func_type operator() (e0 p0, e1 p1, e2 p2) const      \
210    {                                                           \
211      return _t[p0][p1][p2];                                    \
212    }                                                           \
213  }
214
215
216  /**
217      @module {Template switch}
218      @header dpp/template_switch
219      @see #DPP_TEMPLATE_SWITCH_1
220  */
221#define DPP_TEMPLATE_SWITCH_4(clname, func_type, func,                  \
222                              C0, C1, C2, C3)                           \
223  class clname                                                          \
224  {                                                                     \
225    typedef typeof(C0) e0;                                              \
226    typedef typeof(C1) e1;                                              \
227    typedef typeof(C2) e2;                                              \
228    typedef typeof(C3) e3;                                              \
229                                                                        \
230    typedef func_type table_type[C0+1][C1+1][C2+1][C3+1];               \
231    table_type _t;                                                      \
232                                                                        \
233    template <int I0, int I1, int I2, int I3, bool done>                \
234    struct iter;                                                        \
235                                                                        \
236    template <int I0, int I1, int I2, int I3>                           \
237    struct iter<I0, I1, I2, I3, true>                                   \
238    {                                                                   \
239      static void s(table_type &t)                                      \
240      {                                                                 \
241      }                                                                 \
242    };                                                                  \
243                                                                        \
244    template <int I0, int I1, int I2, int I3, bool done>                \
245    struct iter                                                         \
246    {                                                                   \
247      static void s(table_type &t)                                      \
248      {                                                                 \
249        t[I0][I1][I2][I3] = &func<(e0)I0, (e1)I1, (e2)I2, (e3)I3>;      \
250        iter<2*I0+1, I1, I2, I3, (2*I0+1 >= C0+1)>::s(t);               \
251        iter<2*I0+2, I1, I2, I3, (2*I0+2 >= C0+1)>::s(t);               \
252        if (2*I0+1 == C0+1 || 2*I0+2 == C0+1) {                         \
253          iter<0, 2*I1+1, I2, I3, (2*I1+1 >= C1+1)>::s(t);              \
254          iter<0, 2*I1+2, I2, I3, (2*I1+2 >= C1+1)>::s(t);              \
255          if (2*I1+1 == C1+1 || 2*I1+2 == C1+1) {                       \
256            iter<0, 0, 2*I2+1, I3, (2*I2+1 >= C2+1)>::s(t);             \
257            iter<0, 0, 2*I2+2, I3, (2*I2+2 >= C2+1)>::s(t);             \
258            if (2*I2+1 == C2+1 || 2*I2+2 == C2+1) {                     \
259              iter<0, 0, 0, 2*I3+1, (2*I3+1 >= C3+1)>::s(t);            \
260              iter<0, 0, 0, 2*I3+2, (2*I3+2 >= C3+1)>::s(t);            \
261            }                                                           \
262          }                                                             \
263        }                                                               \
264      }                                                                 \
265    };                                                                  \
266                                                                        \
267  public:                                                               \
268                                                                        \
269    clname()                                                            \
270    {                                                                   \
271      iter<0, 0, 0, 0, false>::s(_t);                                   \
272    }                                                                   \
273                                                                        \
274    const func_type operator() (e0 p0, e1 p1, e2 p2, e3 p3) const       \
275    {                                                                   \
276      return _t[p0][p1][p2][p3];                                        \
277    }                                                                   \
278  }
279
280
281  /**
282      @module {Template switch}
283      @header dpp/template_switch
284      @see #DPP_TEMPLATE_SWITCH_1
285  */
286#define DPP_TEMPLATE_SWITCH_5(clname, func_type, func,                  \
287                              C0, C1, C2, C3, C4)                       \
288  class clname                                                          \
289  {                                                                     \
290    typedef typeof(C0) e0;                                              \
291    typedef typeof(C1) e1;                                              \
292    typedef typeof(C2) e2;                                              \
293    typedef typeof(C3) e3;                                              \
294    typedef typeof(C4) e4;                                              \
295                                                                        \
296    typedef func_type table_type[C0+1][C1+1][C2+1][C3+1][C4+1];         \
297    table_type _t;                                                      \
298                                                                        \
299    template <int I0, int I1, int I2, int I3, int I4, bool done>        \
300    struct iter;                                                        \
301                                                                        \
302    template <int I0, int I1, int I2, int I3, int I4>                   \
303    struct iter<I0, I1, I2, I3, I4, true>                               \
304    {                                                                   \
305      static void s(table_type &t)                                      \
306      {                                                                 \
307      }                                                                 \
308    };                                                                  \
309                                                                        \
310    template <int I0, int I1, int I2, int I3, int I4, bool done>        \
311    struct iter                                                         \
312    {                                                                   \
313      static void s(table_type &t)                                      \
314      {                                                                 \
315        t[I0][I1][I2][I3][I4] = &func<(e0)I0, (e1)I1, (e2)I2, (e3)I3, (e4)I4>; \
316        iter<2*I0+1, I1, I2, I3, I4, (2*I0+1 >= C0+1)>::s(t);           \
317        iter<2*I0+2, I1, I2, I3, I4, (2*I0+2 >= C0+1)>::s(t);           \
318        if (2*I0+1 == C0+1 || 2*I0+2 == C0+1) {                         \
319          iter<0, 2*I1+1, I2, I3, I4, (2*I1+1 >= C1+1)>::s(t);          \
320          iter<0, 2*I1+2, I2, I3, I4, (2*I1+2 >= C1+1)>::s(t);          \
321          if (2*I1+1 == C1+1 || 2*I1+2 == C1+1) {                       \
322            iter<0, 0, 2*I2+1, I3, I4, (2*I2+1 >= C2+1)>::s(t);         \
323            iter<0, 0, 2*I2+2, I3, I4, (2*I2+2 >= C2+1)>::s(t);         \
324            if (2*I2+1 == C2+1 || 2*I2+2 == C2+1) {                     \
325              iter<0, 0, 0, 2*I3+1, I4, (2*I3+1 >= C3+1)>::s(t);        \
326              iter<0, 0, 0, 2*I3+2, I4, (2*I3+2 >= C3+1)>::s(t);        \
327              if (2*I3+1 == C3+1 || 2*I3+2 == C3+1) {                   \
328                iter<0, 0, 0, 0, 2*I4+1, (2*I4+1 >= C4+1)>::s(t);       \
329                iter<0, 0, 0, 0, 2*I4+2, (2*I4+2 >= C4+1)>::s(t);       \
330              }                                                         \
331            }                                                           \
332          }                                                             \
333        }                                                               \
334      }                                                                 \
335    };                                                                  \
336                                                                        \
337  public:                                                               \
338                                                                        \
339    clname()                                                            \
340    {                                                                   \
341      iter<0, 0, 0, 0, 0, false>::s(_t);                                \
342    }                                                                   \
343                                                                        \
344    const func_type operator() (e0 p0, e1 p1, e2 p2, e3 p3, e4 p4) const \
345    {                                                                   \
346      return _t[p0][p1][p2][p3][p4];                                    \
347    }                                                                   \
348  }
349
350
351  /**
352      @module {Template switch}
353      @header dpp/template_switch
354      @see #DPP_TEMPLATE_SWITCH_1
355  */
356#define DPP_TEMPLATE_SWITCH_6(clname, func_type, func,                  \
357                              C0, C1, C2, C3, C4, C5)                   \
358  class clname                                                          \
359  {                                                                     \
360    typedef typeof(C0) e0;                                              \
361    typedef typeof(C1) e1;                                              \
362    typedef typeof(C2) e2;                                              \
363    typedef typeof(C3) e3;                                              \
364    typedef typeof(C4) e4;                                              \
365    typedef typeof(C5) e5;                                              \
366                                                                        \
367    typedef func_type table_type[C0+1][C1+1][C2+1][C3+1][C4+1][C5+1];   \
368    table_type _t;                                                      \
369                                                                        \
370    template <int I0, int I1, int I2, int I3, int I4, int I5, bool done> \
371    struct iter;                                                        \
372                                                                        \
373    template <int I0, int I1, int I2, int I3, int I4, int I5>           \
374    struct iter<I0, I1, I2, I3, I4, I5, true>                           \
375    {                                                                   \
376      static void s(table_type &t)                                      \
377      {                                                                 \
378      }                                                                 \
379    };                                                                  \
380                                                                        \
381    template <int I0, int I1, int I2, int I3, int I4, int I5, bool done> \
382    struct iter                                                         \
383    {                                                                   \
384      static void s(table_type &t)                                      \
385      {                                                                 \
386        t[I0][I1][I2][I3][I4][I5] = &func<(e0)I0, (e1)I1, (e2)I2, (e3)I3, (e4)I4, (e5)I5>; \
387        iter<2*I0+1, I1, I2, I3, I4, I5, (2*I0+1 >= C0+1)>::s(t);       \
388        iter<2*I0+2, I1, I2, I3, I4, I5, (2*I0+2 >= C0+1)>::s(t);       \
389        if (2*I0+1 == C0+1 || 2*I0+2 == C0+1) {                         \
390          iter<0, 2*I1+1, I2, I3, I4, I5, (2*I1+1 >= C1+1)>::s(t);      \
391          iter<0, 2*I1+2, I2, I3, I4, I5, (2*I1+2 >= C1+1)>::s(t);      \
392          if (2*I1+1 == C1+1 || 2*I1+2 == C1+1) {                       \
393            iter<0, 0, 2*I2+1, I3, I4, I5, (2*I2+1 >= C2+1)>::s(t);     \
394            iter<0, 0, 2*I2+2, I3, I4, I5, (2*I2+2 >= C2+1)>::s(t);     \
395            if (2*I2+1 == C2+1 || 2*I2+2 == C2+1) {                     \
396              iter<0, 0, 0, 2*I3+1, I4, I5, (2*I3+1 >= C3+1)>::s(t);    \
397              iter<0, 0, 0, 2*I3+2, I4, I5, (2*I3+2 >= C3+1)>::s(t);    \
398              if (2*I3+1 == C3+1 || 2*I3+2 == C3+1) {                   \
399                iter<0, 0, 0, 0, 2*I4+1, I5, (2*I4+1 >= C4+1)>::s(t);   \
400                iter<0, 0, 0, 0, 2*I4+2, I5, (2*I4+2 >= C4+1)>::s(t);   \
401                if (2*I4+1 == C4+1 || 2*I4+2 == C4+1) {                 \
402                  iter<0, 0, 0, 0, 0, 2*I5+1, (2*I5+1 >= C5+1)>::s(t);  \
403                  iter<0, 0, 0, 0, 0, 2*I5+2, (2*I5+2 >= C5+1)>::s(t);  \
404                }                                                       \
405              }                                                         \
406            }                                                           \
407          }                                                             \
408        }                                                               \
409      }                                                                 \
410    };                                                                  \
411                                                                        \
412  public:                                                               \
413                                                                        \
414    clname()                                                            \
415    {                                                                   \
416      iter<0, 0, 0, 0, 0, 0, false>::s(_t);                             \
417    }                                                                   \
418                                                                        \
419    const func_type operator() (e0 p0, e1 p1, e2 p2, e3 p3, e4 p4, e5 p5) const \
420    {                                                                   \
421      return _t[p0][p1][p2][p3][p4][p5];                                \
422    }                                                                   \
423  }
424
425}
426
427#endif
428
Note: See TracBrowser for help on using the repository browser.