3 // Copyright (C) 2005, 2006 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)
57 template<typename Root>
63 // Forward declarations of functors.
64 template<typename Hd, typename Typelist>
68 typedef Typelist tail;
71 template<typename Fn, class Typelist>
75 template<typename Typelist0, typename Typelist1>
78 template<typename Typelist_Typelist>
79 struct append_typelist;
81 template<typename Typelist, typename T>
84 template<typename Typelist, template<typename T> class Pred>
87 template<typename Typelist, int i>
90 template<typename Typelist, template<typename T> class Transform>
93 template<typename Typelist_Typelist>
96 template<typename Typelist>
102 template<typename T1, typename T2>
105 template<typename T1, typename T2, typename T3>
108 template<typename T1, typename T2, typename T3, typename T4>
111 template<typename T1, typename T2, typename T3, typename T4, typename T5>
114 template<typename T1, typename T2, typename T3,
115 typename T4, typename T5, typename T6>
117 } // namespace typelist
119 _GLIBCXX_END_NAMESPACE
122 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
128 template<typename Fn, typename Typelist_Chain>
131 template<typename Fn, typename Hd, typename Tl>
132 struct apply_<Fn, chain<Hd, Tl> >
143 template<typename Fn>
144 struct apply_<Fn, null_type>
150 template<typename Typelist_Chain0, typename Typelist_Chain1>
153 template<typename Hd, typename Tl, typename Typelist_Chain>
154 struct append_<chain<Hd, Tl>, Typelist_Chain>
157 typedef append_<Tl, Typelist_Chain> append_type;
160 typedef chain<Hd, typename append_type::type> type;
163 template<typename Typelist_Chain>
164 struct append_<null_type, Typelist_Chain>
166 typedef Typelist_Chain type;
169 template<typename Typelist_Chain>
170 struct append_<Typelist_Chain, null_type>
172 typedef Typelist_Chain type;
176 struct append_<null_type, null_type>
178 typedef null_type type;
181 template<typename Typelist_Typelist_Chain>
182 struct append_typelist_;
184 template<typename Hd>
185 struct append_typelist_<chain<Hd, null_type> >
187 typedef chain<Hd, null_type> type;
190 template<typename Hd, typename Tl>
191 struct append_typelist_<chain< Hd, Tl> >
194 typedef typename append_typelist_<Tl>::type rest_type;
197 typedef typename append<Hd, node<rest_type> >::type::root type;
200 template<typename Typelist_Chain, typename T>
204 struct contains_<null_type, T>
212 template<typename Hd, typename Tl, typename T>
213 struct contains_<chain<Hd, Tl>, T>
217 value = contains_<Tl, T>::value
221 template<typename Tl, typename T>
222 struct contains_<chain<T, Tl>, T>
230 template<typename Typelist_Chain, template<typename T> class Pred>
231 struct chain_filter_;
233 template<template<typename T> class Pred>
234 struct chain_filter_<null_type, Pred>
236 typedef null_type type;
239 template<typename Hd, typename Tl, template<typename T> class Pred>
240 struct chain_filter_<chain<Hd, Tl>, Pred>
245 include_hd = Pred<Hd>::value
248 typedef typename chain_filter_<Tl, Pred>::type rest_type;
249 typedef chain<Hd, rest_type> chain_type;
252 typedef typename __conditional_type<include_hd, chain_type, rest_type>::__type type;
255 template<typename Typelist_Chain, int i>
256 struct chain_at_index_;
258 template<typename Hd, typename Tl>
259 struct chain_at_index_<chain<Hd, Tl>, 0>
264 template<typename Hd, typename Tl, int i>
265 struct chain_at_index_<chain<Hd, Tl>, i>
267 typedef typename chain_at_index_<Tl, i - 1>::type type;
270 template<class Typelist_Chain, template<typename T> class Transform>
271 struct chain_transform_;
273 template<template<typename T> class Transform>
274 struct chain_transform_<null_type, Transform>
276 typedef null_type type;
279 template<class Hd, class Tl, template<typename T> class Transform>
280 struct chain_transform_<chain<Hd, Tl>, Transform>
283 typedef typename chain_transform_<Tl, Transform>::type rest_type;
284 typedef typename Transform<Hd>::type transform_type;
287 typedef chain<transform_type, rest_type> type;
290 template<typename Typelist_Typelist_Chain>
291 struct chain_flatten_;
293 template<typename Hd_Tl>
294 struct chain_flatten_<chain<Hd_Tl, null_type> >
296 typedef typename Hd_Tl::root type;
299 template<typename Hd_Typelist, class Tl_Typelist>
300 struct chain_flatten_<chain<Hd_Typelist, Tl_Typelist> >
303 typedef typename chain_flatten_<Tl_Typelist>::type rest_type;
304 typedef append<Hd_Typelist, node<rest_type> > append_type;
306 typedef typename append_type::type::root type;
308 } // namespace detail
309 } // namespace typelist
311 _GLIBCXX_END_NAMESPACE
313 #define _GLIBCXX_TYPELIST_CHAIN1(X0) __gnu_cxx::typelist::chain<X0, __gnu_cxx::typelist::null_type>
314 #define _GLIBCXX_TYPELIST_CHAIN2(X0, X1) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN1(X1) >
315 #define _GLIBCXX_TYPELIST_CHAIN3(X0, X1, X2) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN2(X1, X2) >
316 #define _GLIBCXX_TYPELIST_CHAIN4(X0, X1, X2, X3) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN3(X1, X2, X3) >
317 #define _GLIBCXX_TYPELIST_CHAIN5(X0, X1, X2, X3, X4) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN4(X1, X2, X3, X4) >
318 #define _GLIBCXX_TYPELIST_CHAIN6(X0, X1, X2, X3, X4, X5) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN5(X1, X2, X3, X4, X5) >
319 #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) >
320 #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) >
321 #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) >
322 #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) >
323 #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) >
324 #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) >
325 #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) >
326 #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) >
327 #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) >
329 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
333 template<typename Fn, class Typelist>
335 apply(Fn& fn, Typelist)
337 detail::apply_<Fn, typename Typelist::root> a;
341 template<typename Typelist0, typename Typelist1>
345 typedef typename Typelist0::root root0_type;
346 typedef typename Typelist1::root root1_type;
347 typedef detail::append_<root0_type, root1_type> append_type;
350 typedef node<typename append_type::type> type;
353 template<typename Typelist_Typelist>
354 struct append_typelist
357 typedef typename Typelist_Typelist::root root_type;
358 typedef detail::append_typelist_<root_type> append_type;
361 typedef node<typename append_type::type> type;
364 template<typename Typelist, typename T>
368 typedef typename Typelist::root root_type;
373 value = detail::contains_<root_type, T>::value
377 template<typename Typelist, template<typename T> class Pred>
381 typedef typename Typelist::root root_type;
382 typedef detail::chain_filter_<root_type, Pred> filter_type;
385 typedef node<typename filter_type::type> type;
388 template<typename Typelist, int i>
392 typedef typename Typelist::root root_type;
393 typedef detail::chain_at_index_<root_type, i> index_type;
396 typedef typename index_type::type type;
399 template<typename Typelist, template<typename T> class Transform>
403 typedef typename Typelist::root root_type;
404 typedef detail::chain_transform_<root_type, Transform> transform_type;
407 typedef node<typename transform_type::type> type;
410 template<typename Typelist_Typelist>
414 typedef typename Typelist_Typelist::root root_type;
415 typedef typename detail::chain_flatten_<root_type>::type flatten_type;
418 typedef node<flatten_type> type;
421 template<typename Typelist>
425 typedef typename at_index<Typelist, 0>::type first_type;
428 typedef node<chain<first_type, null_type> > type;
431 template<typename T1>
434 typedef node<_GLIBCXX_TYPELIST_CHAIN1(T1)> type;
437 template<typename T1, typename T2>
440 typedef node<_GLIBCXX_TYPELIST_CHAIN2(T1,T2)> type;
443 template<typename T1, typename T2, typename T3>
446 typedef node<_GLIBCXX_TYPELIST_CHAIN3(T1,T2,T3)> type;
449 template<typename T1, typename T2, typename T3, typename T4>
452 typedef node<_GLIBCXX_TYPELIST_CHAIN4(T1,T2,T3,T4)> type;
455 template<typename T1, typename T2, typename T3,
456 typename T4, typename T5>
459 typedef node<_GLIBCXX_TYPELIST_CHAIN5(T1,T2,T3,T4,T5)> type;
462 template<typename T1, typename T2, typename T3,
463 typename T4, typename T5, typename T6>
466 typedef node<_GLIBCXX_TYPELIST_CHAIN6(T1,T2,T3,T4,T5,T6)> type;
468 } // namespace typelist
469 _GLIBCXX_END_NAMESPACE