Embedded Template Library  1.0
type_select_generator.h
1 /******************************************************************************
2 The MIT License(MIT)
3 
4 Embedded Template Library.
5 https://github.com/ETLCPP/etl
6 https://www.etlcpp.com
7 
8 Copyright(c) 2018 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef ETL_TYPE_SELECT_INCLUDED
30 #define ETL_TYPE_SELECT_INCLUDED
31 
32 #include "platform.h"
33 #include "static_assert.h"
34 #include "type_traits.h"
35 #include "null_type.h"
36 
37 #undef ETL_FILE
38 #define ETL_FILE "49"
39 
40 /*[[[cog
41 import cog
42 cog.outl("#if 0")
43 ]]]*/
44 /*[[[end]]]*/
45 #error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
46 /*[[[cog
47 import cog
48 cog.outl("#endif")
49 ]]]*/
50 /*[[[end]]]*/
51 
52 /*[[[cog
53 import cog
54 cog.outl("//***************************************************************************")
55 cog.outl("// THIS FILE HAS BEEN AUTO GENERATED. DO NOT EDIT THIS FILE.")
56 cog.outl("//***************************************************************************")
57 ]]]*/
58 /*[[[end]]]*/
59 
60 namespace etl
61 {
62 #if ETL_CPP11_SUPPORTED && !defined(ETL_TYPE_SELECT_FORCE_CPP03)
63  //***************************************************************************
64  // Variadic version.
65  //***************************************************************************
66  template <typename... TTypes>
67  struct type_select
68  {
69  private:
70 
71  //***********************************
72  template <size_t ID, size_t N, typename T1, typename... TRest>
73  struct type_select_helper
74  {
75  using type = typename etl::conditional<ID == N,
76  T1,
77  typename type_select_helper<ID, N + 1, TRest...>::type>::type;
78  };
79 
80  //***********************************
81  template <size_t ID, size_t N, typename T1>
82  struct type_select_helper<ID, N, T1>
83  {
84  using type = T1;
85  };
86 
87  public:
88 
89  template <size_t ID>
90  struct select
91  {
92  static_assert(ID < sizeof...(TTypes), "Illegal type_select::select index");
93 
94  using type = typename type_select_helper<ID, 0, TTypes...>::type;
95  };
96 
97 #if ETL_CPP14_SUPPORTED
98  template <size_t ID>
99  using select_t = typename select<ID>::type;
100 #endif
101  };
102 
103 #else
104 
105  /*[[[cog
106  import cog
107  cog.outl("//***************************************************************************")
108  cog.outl("// For %s types." % int(NTypes))
109  cog.outl("//***************************************************************************")
110  cog.outl("template <typename T0,")
111  for n in range(1, int(NTypes) - 1):
112  cog.outl(" typename T%s = void," % n)
113  cog.outl(" typename T%s = void>" %(int(NTypes) - 1))
114  cog.outl("struct type_select")
115  cog.outl("{")
116  cog.outl("public:")
117  cog.outl("")
118  cog.outl(" template <const size_t ID>")
119  cog.outl(" struct select")
120  cog.outl(" {")
121  cog.outl(" typedef typename etl::conditional<ID == 0, T0,")
122  for n in range(1, int(NTypes)) :
123  cog.outl(" typename etl::conditional<ID == %s, T%s," % (n, n))
124  cog.outl(" etl::null_type<0> >")
125  cog.out(" ")
126  for n in range(1, int(NTypes)) :
127  cog.out("::type>")
128  if n % 8 == 0:
129  cog.outl("")
130  cog.out(" ")
131  cog.outl("::type type;")
132  cog.outl("");
133  cog.outl(" ETL_STATIC_ASSERT(ID < %s, \"Invalid ID\");" % int(NTypes));
134  cog.outl(" };")
135  cog.outl("};")
136 
137  for s in range(int(NTypes) - 1, 0, -1):
138  cog.outl("")
139  cog.outl("//***************************************************************************")
140  cog.outl("// For %s types." % int(s))
141  cog.outl("//***************************************************************************")
142  cog.out("template <")
143  for n in range(0, s - 1):
144  cog.outl("typename T%s, " % n)
145  cog.out(" ")
146  cog.outl("typename T%s>" % (s - 1))
147  cog.out("struct type_select<")
148  for n in range(0, s - 1):
149  cog.out("T%s, " % n)
150  cog.outl("T%s>" % (s - 1))
151  cog.outl("{")
152  cog.outl("public:")
153  cog.outl(" template <const size_t ID>")
154  cog.outl(" struct select")
155  cog.outl(" {")
156  cog.outl(" typedef typename etl::conditional<ID == 0, T0,")
157  for n in range(1, s) :
158  cog.outl(" typename etl::conditional<ID == %s, T%s," % (n, n))
159  cog.outl(" etl::null_type<0> >")
160  cog.out(" ")
161  for n in range(1, s) :
162  cog.out("::type>")
163  if n % 8 == 0:
164  cog.outl("")
165  cog.out(" ")
166  cog.outl("::type type;")
167  cog.outl("");
168  cog.outl(" ETL_STATIC_ASSERT(ID < %s, \"Invalid ID\");" % s);
169  cog.outl(" };")
170  cog.outl("};")
171  ]]]*/
172  /*[[[end]]]*/
173 #endif
174 }
175 
176 #undef ETL_FILE
177 
178 #endif
conditional
Definition: type_traits_generator.h:1202
Definition: absolute.h:37