3 // Copyright (C) 2005, 2006, 2008, 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
27 // Permission to use, copy, modify, sell, and distribute this software
28 // is hereby granted without fee, provided that the above copyright
29 // notice appears in all copies, and that both that copyright notice and
30 // this permission notice appear in supporting documentation. None of
31 // the above authors, nor IBM Haifa Research Laboratories, make any
32 // representation about the suitability of this software for any
33 // purpose. It is provided "as is" without express or implied warranty.
37 * Contains typelist_chain definitions.
38 * Typelists are an idea by Andrei Alexandrescu.
44 #include <ext/type_traits.h>
46 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
48 /** @namespace __gnu_cxx::typelist
49 * @brief GNU typelist extensions for public compile-time use.
55 template<typename Root
>
61 // Forward declarations of functors.
62 template<typename Hd
, typename Typelist
>
66 typedef Typelist tail
;
69 // Apply all typelist types to unary functor.
70 template<typename Fn
, typename Typelist
>
74 /// Apply all typelist types to generator functor.
75 template<typename Gn
, typename Typelist
>
77 apply_generator(Gn
&, Typelist
);
79 // Apply all typelist types and values to generator functor.
80 template<typename Gn
, typename TypelistT
, typename TypelistV
>
82 apply_generator(Gn
&, TypelistT
, TypelistV
);
84 template<typename Typelist0
, typename Typelist1
>
87 template<typename Typelist_Typelist
>
88 struct append_typelist
;
90 template<typename Typelist
, typename T
>
93 template<typename Typelist
, template<typename T
> class Pred
>
96 template<typename Typelist
, int i
>
99 template<typename Typelist
, template<typename T
> class Transform
>
102 template<typename Typelist_Typelist
>
105 template<typename Typelist
>
108 template<typename T1
>
111 template<typename T1
, typename T2
>
114 template<typename T1
, typename T2
, typename T3
>
117 template<typename T1
, typename T2
, typename T3
, typename T4
>
120 template<typename T1
, typename T2
, typename T3
, typename T4
, typename T5
>
123 template<typename T1
, typename T2
, typename T3
,
124 typename T4
, typename T5
, typename T6
>
126 } // namespace typelist
128 _GLIBCXX_END_NAMESPACE
131 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
137 template<typename Fn
, typename Typelist_Chain
>
140 template<typename Fn
, typename Hd
, typename Tl
>
141 struct apply_
<Fn
, chain
<Hd
, Tl
> >
152 template<typename Fn
>
153 struct apply_
<Fn
, null_type
>
159 template<typename Gn
, typename Typelist_Chain
>
160 struct apply_generator1_
;
162 template<typename Gn
, typename Hd
, typename Tl
>
163 struct apply_generator1_
<Gn
, chain
<Hd
, Tl
> >
168 g
.template operator()<Hd
>();
169 apply_generator1_
<Gn
, Tl
> next
;
174 template<typename Gn
>
175 struct apply_generator1_
<Gn
, null_type
>
181 template<typename Gn
, typename TypelistT_Chain
, typename TypelistV_Chain
>
182 struct apply_generator2_
;
184 template<typename Gn
, typename Hd1
, typename TlT
, typename Hd2
, typename TlV
>
185 struct apply_generator2_
<Gn
, chain
<Hd1
, TlT
>, chain
<Hd2
, TlV
> >
190 g
.template operator()<Hd1
, Hd2
>();
191 apply_generator2_
<Gn
, TlT
, TlV
> next
;
196 template<typename Gn
>
197 struct apply_generator2_
<Gn
, null_type
, null_type
>
203 template<typename Typelist_Chain0
, typename Typelist_Chain1
>
206 template<typename Hd
, typename Tl
, typename Typelist_Chain
>
207 struct append_
<chain
<Hd
, Tl
>, Typelist_Chain
>
210 typedef append_
<Tl
, Typelist_Chain
> append_type
;
213 typedef chain
<Hd
, typename
append_type::type
> type
;
216 template<typename Typelist_Chain
>
217 struct append_
<null_type
, Typelist_Chain
>
219 typedef Typelist_Chain type
;
222 template<typename Typelist_Chain
>
223 struct append_
<Typelist_Chain
, null_type
>
225 typedef Typelist_Chain type
;
229 struct append_
<null_type
, null_type
>
231 typedef null_type type
;
234 template<typename Typelist_Typelist_Chain
>
235 struct append_typelist_
;
237 template<typename Hd
>
238 struct append_typelist_
<chain
<Hd
, null_type
> >
240 typedef chain
<Hd
, null_type
> type
;
243 template<typename Hd
, typename Tl
>
244 struct append_typelist_
<chain
< Hd
, Tl
> >
247 typedef typename append_typelist_
<Tl
>::type rest_type
;
250 typedef typename append
<Hd
, node
<rest_type
> >::type::root type
;
253 template<typename Typelist_Chain
, typename T
>
257 struct contains_
<null_type
, T
>
265 template<typename Hd
, typename Tl
, typename T
>
266 struct contains_
<chain
<Hd
, Tl
>, T
>
270 value
= contains_
<Tl
, T
>::value
274 template<typename Tl
, typename T
>
275 struct contains_
<chain
<T
, Tl
>, T
>
283 template<typename Typelist_Chain
, template<typename T
> class Pred
>
284 struct chain_filter_
;
286 template<template<typename T
> class Pred
>
287 struct chain_filter_
<null_type
, Pred
>
289 typedef null_type type
;
292 template<typename Hd
, typename Tl
, template<typename T
> class Pred
>
293 struct chain_filter_
<chain
<Hd
, Tl
>, Pred
>
298 include_hd
= Pred
<Hd
>::value
301 typedef typename chain_filter_
<Tl
, Pred
>::type rest_type
;
302 typedef chain
<Hd
, rest_type
> chain_type
;
305 typedef typename __conditional_type
<include_hd
, chain_type
, rest_type
>::__type type
;
308 template<typename Typelist_Chain
, int i
>
309 struct chain_at_index_
;
311 template<typename Hd
, typename Tl
>
312 struct chain_at_index_
<chain
<Hd
, Tl
>, 0>
317 template<typename Hd
, typename Tl
, int i
>
318 struct chain_at_index_
<chain
<Hd
, Tl
>, i
>
320 typedef typename chain_at_index_
<Tl
, i
- 1>::type type
;
323 template<class Typelist_Chain
, template<typename T
> class Transform
>
324 struct chain_transform_
;
326 template<template<typename T
> class Transform
>
327 struct chain_transform_
<null_type
, Transform
>
329 typedef null_type type
;
332 template<class Hd
, class Tl
, template<typename T
> class Transform
>
333 struct chain_transform_
<chain
<Hd
, Tl
>, Transform
>
336 typedef typename chain_transform_
<Tl
, Transform
>::type rest_type
;
337 typedef typename Transform
<Hd
>::type transform_type
;
340 typedef chain
<transform_type
, rest_type
> type
;
343 template<typename Typelist_Typelist_Chain
>
344 struct chain_flatten_
;
346 template<typename Hd_Tl
>
347 struct chain_flatten_
<chain
<Hd_Tl
, null_type
> >
349 typedef typename
Hd_Tl::root type
;
352 template<typename Hd_Typelist
, class Tl_Typelist
>
353 struct chain_flatten_
<chain
<Hd_Typelist
, Tl_Typelist
> >
356 typedef typename chain_flatten_
<Tl_Typelist
>::type rest_type
;
357 typedef append
<Hd_Typelist
, node
<rest_type
> > append_type
;
359 typedef typename
append_type::type::root type
;
361 } // namespace detail
362 } // namespace typelist
364 _GLIBCXX_END_NAMESPACE
366 #define _GLIBCXX_TYPELIST_CHAIN1(X0) __gnu_cxx::typelist::chain<X0, __gnu_cxx::typelist::null_type>
367 #define _GLIBCXX_TYPELIST_CHAIN2(X0, X1) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN1(X1) >
368 #define _GLIBCXX_TYPELIST_CHAIN3(X0, X1, X2) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN2(X1, X2) >
369 #define _GLIBCXX_TYPELIST_CHAIN4(X0, X1, X2, X3) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN3(X1, X2, X3) >
370 #define _GLIBCXX_TYPELIST_CHAIN5(X0, X1, X2, X3, X4) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN4(X1, X2, X3, X4) >
371 #define _GLIBCXX_TYPELIST_CHAIN6(X0, X1, X2, X3, X4, X5) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN5(X1, X2, X3, X4, X5) >
372 #define _GLIBCXX_TYPELIST_CHAIN7(X0, X1, X2, X3, X4, X5, X6) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN6(X1, X2, X3, X4, X5, X6) >
373 #define _GLIBCXX_TYPELIST_CHAIN8(X0, X1, X2, X3, X4, X5, X6, X7) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN7(X1, X2, X3, X4, X5, X6, X7) >
374 #define _GLIBCXX_TYPELIST_CHAIN9(X0, X1, X2, X3, X4, X5, X6, X7, X8) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN8(X1, X2, X3, X4, X5, X6, X7, X8) >
375 #define _GLIBCXX_TYPELIST_CHAIN10(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN9(X1, X2, X3, X4, X5, X6, X7, X8, X9) >
376 #define _GLIBCXX_TYPELIST_CHAIN11(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN10(X1, X2, X3, X4, X5, X6, X7, X8, X9, X10) >
377 #define _GLIBCXX_TYPELIST_CHAIN12(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN11(X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11) >
378 #define _GLIBCXX_TYPELIST_CHAIN13(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN12(X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12) >
379 #define _GLIBCXX_TYPELIST_CHAIN14(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN13(X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13) >
380 #define _GLIBCXX_TYPELIST_CHAIN15(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN14(X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14) >
382 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
386 template<typename Fn
, typename Typelist
>
388 apply(Fn
& fn
, Typelist
)
390 detail::apply_
<Fn
, typename
Typelist::root
> a
;
394 template<typename Fn
, typename Typelist
>
396 apply_generator(Fn
& fn
, Typelist
)
398 detail::apply_generator1_
<Fn
, typename
Typelist::root
> a
;
402 template<typename Fn
, typename TypelistT
, typename TypelistV
>
404 apply_generator(Fn
& fn
, TypelistT
, TypelistV
)
406 typedef typename
TypelistT::root rootT
;
407 typedef typename
TypelistV::root rootV
;
408 detail::apply_generator2_
<Fn
, rootT
, rootV
> a
;
412 template<typename Typelist0
, typename Typelist1
>
416 typedef typename
Typelist0::root root0_type
;
417 typedef typename
Typelist1::root root1_type
;
418 typedef detail::append_
<root0_type
, root1_type
> append_type
;
421 typedef node
<typename
append_type::type
> type
;
424 template<typename Typelist_Typelist
>
425 struct append_typelist
428 typedef typename
Typelist_Typelist::root root_type
;
429 typedef detail::append_typelist_
<root_type
> append_type
;
432 typedef node
<typename
append_type::type
> type
;
435 template<typename Typelist
, typename T
>
439 typedef typename
Typelist::root root_type
;
444 value
= detail::contains_
<root_type
, T
>::value
448 template<typename Typelist
, template<typename T
> class Pred
>
452 typedef typename
Typelist::root root_type
;
453 typedef detail::chain_filter_
<root_type
, Pred
> filter_type
;
456 typedef node
<typename
filter_type::type
> type
;
459 template<typename Typelist
, int i
>
463 typedef typename
Typelist::root root_type
;
464 typedef detail::chain_at_index_
<root_type
, i
> index_type
;
467 typedef typename
index_type::type type
;
470 template<typename Typelist
, template<typename T
> class Transform
>
474 typedef typename
Typelist::root root_type
;
475 typedef detail::chain_transform_
<root_type
, Transform
> transform_type
;
478 typedef node
<typename
transform_type::type
> type
;
481 template<typename Typelist_Typelist
>
485 typedef typename
Typelist_Typelist::root root_type
;
486 typedef typename
detail::chain_flatten_
<root_type
>::type flatten_type
;
489 typedef node
<flatten_type
> type
;
492 template<typename Typelist
>
496 typedef typename at_index
<Typelist
, 0>::type first_type
;
499 typedef node
<chain
<first_type
, null_type
> > type
;
502 template<typename T1
>
505 typedef node
<_GLIBCXX_TYPELIST_CHAIN1(T1
)> type
;
508 template<typename T1
, typename T2
>
511 typedef node
<_GLIBCXX_TYPELIST_CHAIN2(T1
,T2
)> type
;
514 template<typename T1
, typename T2
, typename T3
>
517 typedef node
<_GLIBCXX_TYPELIST_CHAIN3(T1
,T2
,T3
)> type
;
520 template<typename T1
, typename T2
, typename T3
, typename T4
>
523 typedef node
<_GLIBCXX_TYPELIST_CHAIN4(T1
,T2
,T3
,T4
)> type
;
526 template<typename T1
, typename T2
, typename T3
,
527 typename T4
, typename T5
>
530 typedef node
<_GLIBCXX_TYPELIST_CHAIN5(T1
,T2
,T3
,T4
,T5
)> type
;
533 template<typename T1
, typename T2
, typename T3
,
534 typename T4
, typename T5
, typename T6
>
537 typedef node
<_GLIBCXX_TYPELIST_CHAIN6(T1
,T2
,T3
,T4
,T5
,T6
)> type
;
539 } // namespace typelist
540 _GLIBCXX_END_NAMESPACE