3 // Copyright (C) 2005, 2006, 2008 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
42 * Contains typelist_chain definitions.
43 * Typelists are an idea by Andrei Alexandrescu.
49 #include <ext/type_traits.h>
51 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
53 /** @namespace __gnu_cxx::typelist
54 * @brief GNU typelist extensions for public compile-time use.
60 template<typename Root
>
66 // Forward declarations of functors.
67 template<typename Hd
, typename Typelist
>
71 typedef Typelist tail
;
74 // Apply all typelist types to unary functor.
75 template<typename Fn
, typename Typelist
>
79 /// Apply all typelist types to generator functor.
80 template<typename Gn
, typename Typelist
>
82 apply_generator(Gn
&, Typelist
);
84 // Apply all typelist types and values to generator functor.
85 template<typename Gn
, typename TypelistT
, typename TypelistV
>
87 apply_generator(Gn
&, TypelistT
, TypelistV
);
89 template<typename Typelist0
, typename Typelist1
>
92 template<typename Typelist_Typelist
>
93 struct append_typelist
;
95 template<typename Typelist
, typename T
>
98 template<typename Typelist
, template<typename T
> class Pred
>
101 template<typename Typelist
, int i
>
104 template<typename Typelist
, template<typename T
> class Transform
>
107 template<typename Typelist_Typelist
>
110 template<typename Typelist
>
113 template<typename T1
>
116 template<typename T1
, typename T2
>
119 template<typename T1
, typename T2
, typename T3
>
122 template<typename T1
, typename T2
, typename T3
, typename T4
>
125 template<typename T1
, typename T2
, typename T3
, typename T4
, typename T5
>
128 template<typename T1
, typename T2
, typename T3
,
129 typename T4
, typename T5
, typename T6
>
131 } // namespace typelist
133 _GLIBCXX_END_NAMESPACE
136 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
142 template<typename Fn
, typename Typelist_Chain
>
145 template<typename Fn
, typename Hd
, typename Tl
>
146 struct apply_
<Fn
, chain
<Hd
, Tl
> >
157 template<typename Fn
>
158 struct apply_
<Fn
, null_type
>
164 template<typename Gn
, typename Typelist_Chain
>
165 struct apply_generator1_
;
167 template<typename Gn
, typename Hd
, typename Tl
>
168 struct apply_generator1_
<Gn
, chain
<Hd
, Tl
> >
173 g
.template operator()<Hd
>();
174 apply_generator1_
<Gn
, Tl
> next
;
179 template<typename Gn
>
180 struct apply_generator1_
<Gn
, null_type
>
186 template<typename Gn
, typename TypelistT_Chain
, typename TypelistV_Chain
>
187 struct apply_generator2_
;
189 template<typename Gn
, typename Hd1
, typename TlT
, typename Hd2
, typename TlV
>
190 struct apply_generator2_
<Gn
, chain
<Hd1
, TlT
>, chain
<Hd2
, TlV
> >
195 g
.template operator()<Hd1
, Hd2
>();
196 apply_generator2_
<Gn
, TlT
, TlV
> next
;
201 template<typename Gn
>
202 struct apply_generator2_
<Gn
, null_type
, null_type
>
208 template<typename Typelist_Chain0
, typename Typelist_Chain1
>
211 template<typename Hd
, typename Tl
, typename Typelist_Chain
>
212 struct append_
<chain
<Hd
, Tl
>, Typelist_Chain
>
215 typedef append_
<Tl
, Typelist_Chain
> append_type
;
218 typedef chain
<Hd
, typename
append_type::type
> type
;
221 template<typename Typelist_Chain
>
222 struct append_
<null_type
, Typelist_Chain
>
224 typedef Typelist_Chain type
;
227 template<typename Typelist_Chain
>
228 struct append_
<Typelist_Chain
, null_type
>
230 typedef Typelist_Chain type
;
234 struct append_
<null_type
, null_type
>
236 typedef null_type type
;
239 template<typename Typelist_Typelist_Chain
>
240 struct append_typelist_
;
242 template<typename Hd
>
243 struct append_typelist_
<chain
<Hd
, null_type
> >
245 typedef chain
<Hd
, null_type
> type
;
248 template<typename Hd
, typename Tl
>
249 struct append_typelist_
<chain
< Hd
, Tl
> >
252 typedef typename append_typelist_
<Tl
>::type rest_type
;
255 typedef typename append
<Hd
, node
<rest_type
> >::type::root type
;
258 template<typename Typelist_Chain
, typename T
>
262 struct contains_
<null_type
, T
>
270 template<typename Hd
, typename Tl
, typename T
>
271 struct contains_
<chain
<Hd
, Tl
>, T
>
275 value
= contains_
<Tl
, T
>::value
279 template<typename Tl
, typename T
>
280 struct contains_
<chain
<T
, Tl
>, T
>
288 template<typename Typelist_Chain
, template<typename T
> class Pred
>
289 struct chain_filter_
;
291 template<template<typename T
> class Pred
>
292 struct chain_filter_
<null_type
, Pred
>
294 typedef null_type type
;
297 template<typename Hd
, typename Tl
, template<typename T
> class Pred
>
298 struct chain_filter_
<chain
<Hd
, Tl
>, Pred
>
303 include_hd
= Pred
<Hd
>::value
306 typedef typename chain_filter_
<Tl
, Pred
>::type rest_type
;
307 typedef chain
<Hd
, rest_type
> chain_type
;
310 typedef typename __conditional_type
<include_hd
, chain_type
, rest_type
>::__type type
;
313 template<typename Typelist_Chain
, int i
>
314 struct chain_at_index_
;
316 template<typename Hd
, typename Tl
>
317 struct chain_at_index_
<chain
<Hd
, Tl
>, 0>
322 template<typename Hd
, typename Tl
, int i
>
323 struct chain_at_index_
<chain
<Hd
, Tl
>, i
>
325 typedef typename chain_at_index_
<Tl
, i
- 1>::type type
;
328 template<class Typelist_Chain
, template<typename T
> class Transform
>
329 struct chain_transform_
;
331 template<template<typename T
> class Transform
>
332 struct chain_transform_
<null_type
, Transform
>
334 typedef null_type type
;
337 template<class Hd
, class Tl
, template<typename T
> class Transform
>
338 struct chain_transform_
<chain
<Hd
, Tl
>, Transform
>
341 typedef typename chain_transform_
<Tl
, Transform
>::type rest_type
;
342 typedef typename Transform
<Hd
>::type transform_type
;
345 typedef chain
<transform_type
, rest_type
> type
;
348 template<typename Typelist_Typelist_Chain
>
349 struct chain_flatten_
;
351 template<typename Hd_Tl
>
352 struct chain_flatten_
<chain
<Hd_Tl
, null_type
> >
354 typedef typename
Hd_Tl::root type
;
357 template<typename Hd_Typelist
, class Tl_Typelist
>
358 struct chain_flatten_
<chain
<Hd_Typelist
, Tl_Typelist
> >
361 typedef typename chain_flatten_
<Tl_Typelist
>::type rest_type
;
362 typedef append
<Hd_Typelist
, node
<rest_type
> > append_type
;
364 typedef typename
append_type::type::root type
;
366 } // namespace detail
367 } // namespace typelist
369 _GLIBCXX_END_NAMESPACE
371 #define _GLIBCXX_TYPELIST_CHAIN1(X0) __gnu_cxx::typelist::chain<X0, __gnu_cxx::typelist::null_type>
372 #define _GLIBCXX_TYPELIST_CHAIN2(X0, X1) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN1(X1) >
373 #define _GLIBCXX_TYPELIST_CHAIN3(X0, X1, X2) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN2(X1, X2) >
374 #define _GLIBCXX_TYPELIST_CHAIN4(X0, X1, X2, X3) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN3(X1, X2, X3) >
375 #define _GLIBCXX_TYPELIST_CHAIN5(X0, X1, X2, X3, X4) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN4(X1, X2, X3, X4) >
376 #define _GLIBCXX_TYPELIST_CHAIN6(X0, X1, X2, X3, X4, X5) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN5(X1, X2, X3, X4, X5) >
377 #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) >
378 #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) >
379 #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) >
380 #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) >
381 #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) >
382 #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) >
383 #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) >
384 #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) >
385 #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) >
387 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
391 template<typename Fn
, typename Typelist
>
393 apply(Fn
& fn
, Typelist
)
395 detail::apply_
<Fn
, typename
Typelist::root
> a
;
399 template<typename Fn
, typename Typelist
>
401 apply_generator(Fn
& fn
, Typelist
)
403 detail::apply_generator1_
<Fn
, typename
Typelist::root
> a
;
407 template<typename Fn
, typename TypelistT
, typename TypelistV
>
409 apply_generator(Fn
& fn
, TypelistT
, TypelistV
)
411 typedef typename
TypelistT::root rootT
;
412 typedef typename
TypelistV::root rootV
;
413 detail::apply_generator2_
<Fn
, rootT
, rootV
> a
;
417 template<typename Typelist0
, typename Typelist1
>
421 typedef typename
Typelist0::root root0_type
;
422 typedef typename
Typelist1::root root1_type
;
423 typedef detail::append_
<root0_type
, root1_type
> append_type
;
426 typedef node
<typename
append_type::type
> type
;
429 template<typename Typelist_Typelist
>
430 struct append_typelist
433 typedef typename
Typelist_Typelist::root root_type
;
434 typedef detail::append_typelist_
<root_type
> append_type
;
437 typedef node
<typename
append_type::type
> type
;
440 template<typename Typelist
, typename T
>
444 typedef typename
Typelist::root root_type
;
449 value
= detail::contains_
<root_type
, T
>::value
453 template<typename Typelist
, template<typename T
> class Pred
>
457 typedef typename
Typelist::root root_type
;
458 typedef detail::chain_filter_
<root_type
, Pred
> filter_type
;
461 typedef node
<typename
filter_type::type
> type
;
464 template<typename Typelist
, int i
>
468 typedef typename
Typelist::root root_type
;
469 typedef detail::chain_at_index_
<root_type
, i
> index_type
;
472 typedef typename
index_type::type type
;
475 template<typename Typelist
, template<typename T
> class Transform
>
479 typedef typename
Typelist::root root_type
;
480 typedef detail::chain_transform_
<root_type
, Transform
> transform_type
;
483 typedef node
<typename
transform_type::type
> type
;
486 template<typename Typelist_Typelist
>
490 typedef typename
Typelist_Typelist::root root_type
;
491 typedef typename
detail::chain_flatten_
<root_type
>::type flatten_type
;
494 typedef node
<flatten_type
> type
;
497 template<typename Typelist
>
501 typedef typename at_index
<Typelist
, 0>::type first_type
;
504 typedef node
<chain
<first_type
, null_type
> > type
;
507 template<typename T1
>
510 typedef node
<_GLIBCXX_TYPELIST_CHAIN1(T1
)> type
;
513 template<typename T1
, typename T2
>
516 typedef node
<_GLIBCXX_TYPELIST_CHAIN2(T1
,T2
)> type
;
519 template<typename T1
, typename T2
, typename T3
>
522 typedef node
<_GLIBCXX_TYPELIST_CHAIN3(T1
,T2
,T3
)> type
;
525 template<typename T1
, typename T2
, typename T3
, typename T4
>
528 typedef node
<_GLIBCXX_TYPELIST_CHAIN4(T1
,T2
,T3
,T4
)> type
;
531 template<typename T1
, typename T2
, typename T3
,
532 typename T4
, typename T5
>
535 typedef node
<_GLIBCXX_TYPELIST_CHAIN5(T1
,T2
,T3
,T4
,T5
)> type
;
538 template<typename T1
, typename T2
, typename T3
,
539 typename T4
, typename T5
, typename T6
>
542 typedef node
<_GLIBCXX_TYPELIST_CHAIN6(T1
,T2
,T3
,T4
,T5
,T6
)> type
;
544 } // namespace typelist
545 _GLIBCXX_END_NAMESPACE