3 // Copyright (C) 2005, 2006, 2008, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
28 // Permission to use, copy, modify, sell, and distribute this software
29 // is hereby granted without fee, provided that the above copyright
30 // notice appears in all copies, and that both that copyright notice and
31 // this permission notice appear in supporting documentation. None of
32 // the above authors, nor IBM Haifa Research Laboratories, make any
33 // representation about the suitability of this software for any
34 // purpose. It is provided "as is" without express or implied warranty.
37 * @file ext/typelist.h
38 * This file is a GNU extension to the Standard C++ Library.
40 * Contains typelist_chain definitions.
41 * Typelists are an idea by Andrei Alexandrescu.
47 #include <ext/type_traits.h>
49 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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_VERSION
137 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
139 _GLIBCXX_BEGIN_NAMESPACE_VERSION
145 template<typename Fn, typename Typelist_Chain>
148 template<typename Fn, typename Hd, typename Tl>
149 struct apply_<Fn, chain<Hd, Tl> >
160 template<typename Fn>
161 struct apply_<Fn, null_type>
167 template<typename Gn, typename Typelist_Chain>
168 struct apply_generator1_;
170 template<typename Gn, typename Hd, typename Tl>
171 struct apply_generator1_<Gn, chain<Hd, Tl> >
176 g.template operator()<Hd>();
177 apply_generator1_<Gn, Tl> next;
182 template<typename Gn>
183 struct apply_generator1_<Gn, null_type>
189 template<typename Gn, typename TypelistT_Chain, typename TypelistV_Chain>
190 struct apply_generator2_;
192 template<typename Gn, typename Hd1, typename TlT, typename Hd2, typename TlV>
193 struct apply_generator2_<Gn, chain<Hd1, TlT>, chain<Hd2, TlV> >
198 g.template operator()<Hd1, Hd2>();
199 apply_generator2_<Gn, TlT, TlV> next;
204 template<typename Gn>
205 struct apply_generator2_<Gn, null_type, null_type>
211 template<typename Typelist_Chain0, typename Typelist_Chain1>
214 template<typename Hd, typename Tl, typename Typelist_Chain>
215 struct append_<chain<Hd, Tl>, Typelist_Chain>
218 typedef append_<Tl, Typelist_Chain> append_type;
221 typedef chain<Hd, typename append_type::type> type;
224 template<typename Typelist_Chain>
225 struct append_<null_type, Typelist_Chain>
227 typedef Typelist_Chain type;
230 template<typename Typelist_Chain>
231 struct append_<Typelist_Chain, null_type>
233 typedef Typelist_Chain type;
237 struct append_<null_type, null_type>
239 typedef null_type type;
242 template<typename Typelist_Typelist_Chain>
243 struct append_typelist_;
245 template<typename Hd>
246 struct append_typelist_<chain<Hd, null_type> >
248 typedef chain<Hd, null_type> type;
251 template<typename Hd, typename Tl>
252 struct append_typelist_<chain< Hd, Tl> >
255 typedef typename append_typelist_<Tl>::type rest_type;
258 typedef typename append<Hd, node<rest_type> >::type::root type;
261 template<typename Typelist_Chain, typename T>
265 struct contains_<null_type, T>
273 template<typename Hd, typename Tl, typename T>
274 struct contains_<chain<Hd, Tl>, T>
278 value = contains_<Tl, T>::value
282 template<typename Tl, typename T>
283 struct contains_<chain<T, Tl>, T>
291 template<typename Typelist_Chain, template<typename T> class Pred>
292 struct chain_filter_;
294 template<template<typename T> class Pred>
295 struct chain_filter_<null_type, Pred>
297 typedef null_type type;
300 template<typename Hd, typename Tl, template<typename T> class Pred>
301 struct chain_filter_<chain<Hd, Tl>, Pred>
306 include_hd = Pred<Hd>::value
309 typedef typename chain_filter_<Tl, Pred>::type rest_type;
310 typedef chain<Hd, rest_type> chain_type;
313 typedef typename __conditional_type<include_hd, chain_type, rest_type>::__type type;
316 template<typename Typelist_Chain, int i>
317 struct chain_at_index_;
319 template<typename Hd, typename Tl>
320 struct chain_at_index_<chain<Hd, Tl>, 0>
325 template<typename Hd, typename Tl, int i>
326 struct chain_at_index_<chain<Hd, Tl>, i>
328 typedef typename chain_at_index_<Tl, i - 1>::type type;
331 template<class Typelist_Chain, template<typename T> class Transform>
332 struct chain_transform_;
334 template<template<typename T> class Transform>
335 struct chain_transform_<null_type, Transform>
337 typedef null_type type;
340 template<class Hd, class Tl, template<typename T> class Transform>
341 struct chain_transform_<chain<Hd, Tl>, Transform>
344 typedef typename chain_transform_<Tl, Transform>::type rest_type;
345 typedef typename Transform<Hd>::type transform_type;
348 typedef chain<transform_type, rest_type> type;
351 template<typename Typelist_Typelist_Chain>
352 struct chain_flatten_;
354 template<typename Hd_Tl>
355 struct chain_flatten_<chain<Hd_Tl, null_type> >
357 typedef typename Hd_Tl::root type;
360 template<typename Hd_Typelist, class Tl_Typelist>
361 struct chain_flatten_<chain<Hd_Typelist, Tl_Typelist> >
364 typedef typename chain_flatten_<Tl_Typelist>::type rest_type;
365 typedef append<Hd_Typelist, node<rest_type> > append_type;
367 typedef typename append_type::type::root type;
369 } // namespace detail
370 } // namespace typelist
372 _GLIBCXX_END_NAMESPACE_VERSION
375 #define _GLIBCXX_TYPELIST_CHAIN1(X0) __gnu_cxx::typelist::chain<X0, __gnu_cxx::typelist::null_type>
376 #define _GLIBCXX_TYPELIST_CHAIN2(X0, X1) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN1(X1) >
377 #define _GLIBCXX_TYPELIST_CHAIN3(X0, X1, X2) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN2(X1, X2) >
378 #define _GLIBCXX_TYPELIST_CHAIN4(X0, X1, X2, X3) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN3(X1, X2, X3) >
379 #define _GLIBCXX_TYPELIST_CHAIN5(X0, X1, X2, X3, X4) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN4(X1, X2, X3, X4) >
380 #define _GLIBCXX_TYPELIST_CHAIN6(X0, X1, X2, X3, X4, X5) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN5(X1, X2, X3, X4, X5) >
381 #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) >
382 #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) >
383 #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) >
384 #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) >
385 #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) >
386 #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) >
387 #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) >
388 #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) >
389 #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) >
390 #define _GLIBCXX_TYPELIST_CHAIN16(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN15(X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15) >
391 #define _GLIBCXX_TYPELIST_CHAIN17(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN16(X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16) >
392 #define _GLIBCXX_TYPELIST_CHAIN18(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN17(X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17) >
393 #define _GLIBCXX_TYPELIST_CHAIN19(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X18) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN18(X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X18) >
394 #define _GLIBCXX_TYPELIST_CHAIN20(X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X18, X19) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN19(X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X18, X19) >
396 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
398 _GLIBCXX_BEGIN_NAMESPACE_VERSION
402 template<typename Fn, typename Typelist>
404 apply(Fn& fn, Typelist)
406 detail::apply_<Fn, typename Typelist::root> a;
410 template<typename Fn, typename Typelist>
412 apply_generator(Fn& fn, Typelist)
414 detail::apply_generator1_<Fn, typename Typelist::root> a;
418 template<typename Fn, typename TypelistT, typename TypelistV>
420 apply_generator(Fn& fn, TypelistT, TypelistV)
422 typedef typename TypelistT::root rootT;
423 typedef typename TypelistV::root rootV;
424 detail::apply_generator2_<Fn, rootT, rootV> a;
428 template<typename Typelist0, typename Typelist1>
432 typedef typename Typelist0::root root0_type;
433 typedef typename Typelist1::root root1_type;
434 typedef detail::append_<root0_type, root1_type> append_type;
437 typedef node<typename append_type::type> type;
440 template<typename Typelist_Typelist>
441 struct append_typelist
444 typedef typename Typelist_Typelist::root root_type;
445 typedef detail::append_typelist_<root_type> append_type;
448 typedef node<typename append_type::type> type;
451 template<typename Typelist, typename T>
455 typedef typename Typelist::root root_type;
460 value = detail::contains_<root_type, T>::value
464 template<typename Typelist, template<typename T> class Pred>
468 typedef typename Typelist::root root_type;
469 typedef detail::chain_filter_<root_type, Pred> filter_type;
472 typedef node<typename filter_type::type> type;
475 template<typename Typelist, int i>
479 typedef typename Typelist::root root_type;
480 typedef detail::chain_at_index_<root_type, i> index_type;
483 typedef typename index_type::type type;
486 template<typename Typelist, template<typename T> class Transform>
490 typedef typename Typelist::root root_type;
491 typedef detail::chain_transform_<root_type, Transform> transform_type;
494 typedef node<typename transform_type::type> type;
497 template<typename Typelist_Typelist>
501 typedef typename Typelist_Typelist::root root_type;
502 typedef typename detail::chain_flatten_<root_type>::type flatten_type;
505 typedef node<flatten_type> type;
508 template<typename Typelist>
512 typedef typename at_index<Typelist, 0>::type first_type;
515 typedef node<chain<first_type, null_type> > type;
518 template<typename T1>
521 typedef node<_GLIBCXX_TYPELIST_CHAIN1(T1)> type;
524 template<typename T1, typename T2>
527 typedef node<_GLIBCXX_TYPELIST_CHAIN2(T1,T2)> type;
530 template<typename T1, typename T2, typename T3>
533 typedef node<_GLIBCXX_TYPELIST_CHAIN3(T1,T2,T3)> type;
536 template<typename T1, typename T2, typename T3, typename T4>
539 typedef node<_GLIBCXX_TYPELIST_CHAIN4(T1,T2,T3,T4)> type;
542 template<typename T1, typename T2, typename T3,
543 typename T4, typename T5>
546 typedef node<_GLIBCXX_TYPELIST_CHAIN5(T1,T2,T3,T4,T5)> type;
549 template<typename T1, typename T2, typename T3,
550 typename T4, typename T5, typename T6>
553 typedef node<_GLIBCXX_TYPELIST_CHAIN6(T1,T2,T3,T4,T5,T6)> type;
555 } // namespace typelist
556 _GLIBCXX_END_NAMESPACE_VERSION