# typelist.h

Go to the documentation of this file.

    00001 // -*- C++ -*-
    00002
    00003 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
    00031
    00032 // Permission to use, copy, modify, sell, and distribute this software
    00033 // is hereby granted without fee, provided that the above copyright
    00034 // notice appears in all copies, and that both that copyright notice and
    00035 // this permission notice appear in supporting documentation. None of
    00036 // the above authors, nor IBM Haifa Research Laboratories, make any
    00037 // representation about the suitability of this software for any
    00038 // purpose. It is provided "as is" without express or implied warranty.
    00039
    00040 /**
    00041  * @file typelist.h
    00042  * Contains typelist_chain definitions.
    00043  * Typelists are an idea by Andrei Alexandrescu.
    00044  */
    00045
    00046 #ifndef _TYPELIST_H
    00047 #define _TYPELIST_H 1
    00048
    00049 #include <ext/type_traits.h>
    00050
    00051 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
    00052
    00053 /** @namespace __gnu_cxx::typelist
    00054  *  @brief GNU typelist extensions for public compile-time use.
    00055 */
    00056 namespace typelist
    00057 {
    00058   struct null_type { };
    00059
    00060   template<typename Root>
    00061     struct node
    00062     {
    00063       typedef Root  root;
    00064     };
    00065
    00066   // Forward declarations of functors.
    00067   template<typename Hd, typename Typelist>
    00068     struct chain
    00069     {
    00070       typedef Hd    head;
    00071       typedef Typelist  tail;
    00072     };
    00073
    00074   template<typename Fn, class Typelist>
    00075     void
    00076     apply(Fn&, Typelist);
    00077
    00078   template<typename Typelist0, typename Typelist1>
    00079     struct append;
    00080
    00081   template<typename Typelist_Typelist>
    00082     struct append_typelist;
    00083
    00084   template<typename Typelist, typename T>
    00085     struct contains;
    00086
    00087   template<typename Typelist, template<typename T> class Pred>
    00088     struct filter;
    00089
    00090   template<typename Typelist, int i>
    00091     struct at_index;
    00092
    00093   template<typename Typelist, template<typename T> class Transform>
    00094     struct transform;
    00095
    00096   template<typename Typelist_Typelist>
    00097     struct flatten;
    00098
    00099   template<typename Typelist>
    00100     struct from_first;
    00101
    00102   template<typename T1>
    00103     struct create1;
    00104
    00105   template<typename T1, typename T2>
    00106     struct create2;
    00107
    00108   template<typename T1, typename T2, typename T3>
    00109     struct create3;
    00110
    00111   template<typename T1, typename T2, typename T3, typename T4>
    00112     struct create4;
    00113
    00114   template<typename T1, typename T2, typename T3, typename T4, typename T5>
    00115     struct create5;
    00116
    00117   template<typename T1, typename T2, typename T3,
    00118        typename T4, typename T5, typename T6>
    00119     struct create6;
    00120 } // namespace typelist
    00121
    00122 _GLIBCXX_END_NAMESPACE
    00123
    00124
    00125 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
    00126
    00127 namespace typelist
    00128 {
    00129 namespace detail
    00130 {
    00131   template<typename Fn, typename Typelist_Chain>
    00132     struct apply_;
    00133
    00134   template<typename Fn, typename Hd, typename Tl>
    00135     struct apply_<Fn, chain<Hd, Tl> >
    00136     {
    00137       void
    00138       operator() (Fn& f)
    00139       {
    00140     f.operator()(Hd());
    00141     apply_<Fn, Tl> next;
    00142     next(f);
    00143       }
    00144   };
    00145
    00146   template<typename Fn>
    00147     struct apply_<Fn, null_type>
    00148     {
    00149       void
    00150       operator()(Fn&) { }
    00151   };
    00152
    00153   template<typename Typelist_Chain0, typename Typelist_Chain1>
    00154     struct append_;
    00155
    00156   template<typename Hd, typename Tl, typename Typelist_Chain>
    00157     struct append_<chain<Hd, Tl>, Typelist_Chain>
    00158     {
    00159     private:
    00160       typedef append_<Tl, Typelist_Chain>           append_type;
    00161
    00162     public:
    00163       typedef chain<Hd, typename append_type::type>         type;
    00164     };
    00165
    00166   template<typename Typelist_Chain>
    00167     struct append_<null_type, Typelist_Chain>
    00168     {
    00169       typedef Typelist_Chain                        type;
    00170     };
    00171
    00172   template<typename Typelist_Chain>
    00173     struct append_<Typelist_Chain, null_type>
    00174     {
    00175       typedef Typelist_Chain                    type;
    00176     };
    00177
    00178   template<>
    00179     struct append_<null_type, null_type>
    00180     {
    00181       typedef null_type                     type;
    00182     };
    00183
    00184   template<typename Typelist_Typelist_Chain>
    00185     struct append_typelist_;
    00186
    00187   template<typename Hd>
    00188     struct append_typelist_<chain<Hd, null_type> >
    00189     {
    00190       typedef chain<Hd, null_type>              type;
    00191     };
    00192
    00193   template<typename Hd, typename Tl>
    00194     struct append_typelist_<chain< Hd, Tl> >
    00195     {
    00196     private:
    00197       typedef typename append_typelist_<Tl>::type       rest_type;
    00198
    00199     public:
    00200       typedef typename append<Hd, node<rest_type> >::type::root type;
    00201     };
    00202
    00203   template<typename Typelist_Chain, typename T>
    00204     struct contains_;
    00205
    00206   template<typename T>
    00207     struct contains_<null_type, T>
    00208     {
    00209       enum
    00210     {
    00211       value = false
    00212     };
    00213     };
    00214
    00215   template<typename Hd, typename Tl, typename T>
    00216     struct contains_<chain<Hd, Tl>, T>
    00217     {
    00218       enum
    00219     {
    00220       value = contains_<Tl, T>::value
    00221     };
    00222     };
    00223
    00224   template<typename Tl, typename T>
    00225     struct contains_<chain<T, Tl>, T>
    00226     {
    00227       enum
    00228     {
    00229       value = true
    00230     };
    00231     };
    00232
    00233   template<typename Typelist_Chain, template<typename T> class Pred>
    00234     struct chain_filter_;
    00235
    00236   template<template<typename T> class Pred>
    00237     struct chain_filter_<null_type, Pred>
    00238     {
    00239       typedef null_type                     type;
    00240   };
    00241
    00242   template<typename Hd, typename Tl, template<typename T> class Pred>
    00243     struct chain_filter_<chain<Hd, Tl>, Pred>
    00244     {
    00245     private:
    00246       enum
    00247     {
    00248       include_hd = Pred<Hd>::value
    00249     };
    00250
    00251       typedef typename chain_filter_<Tl, Pred>::type        rest_type;
    00252       typedef chain<Hd, rest_type>              chain_type;
    00253
    00254     public:
    00255       typedef typename __conditional_type<include_hd, chain_type, rest_type>::__type type;
    00256   };
    00257
    00258   template<typename Typelist_Chain, int i>
    00259     struct chain_at_index_;
    00260
    00261   template<typename Hd, typename Tl>
    00262     struct chain_at_index_<chain<Hd, Tl>, 0>
    00263     {
    00264       typedef Hd                        type;
    00265     };
    00266
    00267   template<typename Hd, typename Tl, int i>
    00268     struct chain_at_index_<chain<Hd, Tl>, i>
    00269     {
    00270       typedef typename chain_at_index_<Tl, i - 1>::type     type;
    00271     };
    00272
    00273   template<class Typelist_Chain, template<typename T> class Transform>
    00274     struct chain_transform_;
    00275
    00276   template<template<typename T> class Transform>
    00277     struct chain_transform_<null_type, Transform>
    00278     {
    00279       typedef null_type                     type;
    00280     };
    00281
    00282   template<class Hd, class Tl, template<typename T> class Transform>
    00283     struct chain_transform_<chain<Hd, Tl>, Transform>
    00284     {
    00285     private:
    00286       typedef typename chain_transform_<Tl, Transform>::type    rest_type;
    00287       typedef typename Transform<Hd>::type          transform_type;
    00288
    00289     public:
    00290       typedef chain<transform_type, rest_type>          type;
    00291     };
    00292
    00293   template<typename Typelist_Typelist_Chain>
    00294     struct chain_flatten_;
    00295
    00296   template<typename Hd_Tl>
    00297   struct chain_flatten_<chain<Hd_Tl, null_type> >
    00298   {
    00299     typedef typename Hd_Tl::root                type;
    00300   };
    00301
    00302   template<typename Hd_Typelist, class Tl_Typelist>
    00303   struct chain_flatten_<chain<Hd_Typelist, Tl_Typelist> >
    00304   {
    00305   private:
    00306     typedef typename chain_flatten_<Tl_Typelist>::type      rest_type;
    00307     typedef append<Hd_Typelist, node<rest_type> >       append_type;
    00308   public:
    00309     typedef typename append_type::type::root            type;
    00310   };
    00311 } // namespace detail
    00312 } // namespace typelist
    00313
    00314 _GLIBCXX_END_NAMESPACE
    00315
    00316 #define _GLIBCXX_TYPELIST_CHAIN1(X0) __gnu_cxx::typelist::chain<X0, __gnu_cxx::typelist::null_type>
    00317 #define _GLIBCXX_TYPELIST_CHAIN2(X0, X1) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN1(X1) >
    00318 #define _GLIBCXX_TYPELIST_CHAIN3(X0, X1, X2) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN2(X1, X2) >
    00319 #define _GLIBCXX_TYPELIST_CHAIN4(X0, X1, X2, X3) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN3(X1, X2, X3) >
    00320 #define _GLIBCXX_TYPELIST_CHAIN5(X0, X1, X2, X3, X4) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN4(X1, X2, X3, X4) >
    00321 #define _GLIBCXX_TYPELIST_CHAIN6(X0, X1, X2, X3, X4, X5) __gnu_cxx::typelist::chain<X0, _GLIBCXX_TYPELIST_CHAIN5(X1, X2, X3, X4, X5) >
    00322 #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) >
    00323 #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) >
    00324 #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) >
    00325 #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) >
    00326 #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) >
    00327 #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) >
    00328 #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) >
    00329 #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) >
    00330 #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) >
    00331
    00332 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
    00333
    00334 namespace typelist
    00335 {
    00336   template<typename Fn, class Typelist>
    00337     void
    00338     apply(Fn& fn, Typelist)
    00339     {
    00340       detail::apply_<Fn, typename Typelist::root> a;
    00341       a(fn);
    00342     }
    00343
    00344   template<typename Typelist0, typename Typelist1>
    00345     struct append
    00346     {
    00347     private:
    00348       typedef typename Typelist0::root              root0_type;
    00349       typedef typename Typelist1::root              root1_type;
    00350       typedef detail::append_<root0_type, root1_type>       append_type;
    00351
    00352     public:
    00353       typedef node<typename append_type::type>          type;
    00354     };
    00355
    00356   template<typename Typelist_Typelist>
    00357     struct append_typelist
    00358     {
    00359     private:
    00360       typedef typename Typelist_Typelist::root              root_type;
    00361       typedef detail::append_typelist_<root_type>       append_type;
    00362
    00363     public:
    00364       typedef node<typename append_type::type>          type;
    00365     };
    00366
    00367   template<typename Typelist, typename T>
    00368     struct contains
    00369     {
    00370     private:
    00371       typedef typename Typelist::root               root_type;
    00372
    00373     public:
    00374       enum
    00375     {
    00376       value = detail::contains_<root_type, T>::value
    00377     };
    00378     };
    00379
    00380   template<typename Typelist, template<typename T> class Pred>
    00381     struct filter
    00382     {
    00383     private:
    00384       typedef typename Typelist::root               root_type;
    00385       typedef detail::chain_filter_<root_type, Pred>        filter_type;
    00386
    00387     public:
    00388       typedef node<typename filter_type::type>              type;
    00389     };
    00390
    00391   template<typename Typelist, int i>
    00392     struct at_index
    00393     {
    00394     private:
    00395       typedef typename Typelist::root               root_type;
    00396       typedef detail::chain_at_index_<root_type, i>         index_type;
    00397
    00398     public:
    00399       typedef typename index_type::type             type;
    00400     };
    00401
    00402   template<typename Typelist, template<typename T> class Transform>
    00403     struct transform
    00404     {
    00405     private:
    00406       typedef typename Typelist::root               root_type;
    00407       typedef detail::chain_transform_<root_type, Transform>    transform_type;
    00408
    00409     public:
    00410       typedef node<typename transform_type::type>       type;
    00411     };
    00412
    00413   template<typename Typelist_Typelist>
    00414     struct flatten
    00415     {
    00416     private:
    00417       typedef typename Typelist_Typelist::root              root_type;
    00418       typedef typename detail::chain_flatten_<root_type>::type  flatten_type;
    00419
    00420     public:
    00421       typedef node<flatten_type>                type;
    00422     };
    00423
    00424   template<typename Typelist>
    00425     struct from_first
    00426     {
    00427     private:
    00428       typedef typename at_index<Typelist, 0>::type      first_type;
    00429
    00430     public:
    00431       typedef node<chain<first_type, null_type> >       type;
    00432     };
    00433
    00434   template<typename T1>
    00435     struct create1
    00436     {
    00437       typedef node<_GLIBCXX_TYPELIST_CHAIN1(T1)>        type;
    00438     };
    00439
    00440   template<typename T1, typename T2>
    00441     struct create2
    00442     {
    00443       typedef node<_GLIBCXX_TYPELIST_CHAIN2(T1,T2)>         type;
    00444     };
    00445
    00446   template<typename T1, typename T2, typename T3>
    00447     struct create3
    00448     {
    00449       typedef node<_GLIBCXX_TYPELIST_CHAIN3(T1,T2,T3)>      type;
    00450     };
    00451
    00452   template<typename T1, typename T2, typename T3, typename T4>
    00453     struct create4
    00454     {
    00455       typedef node<_GLIBCXX_TYPELIST_CHAIN4(T1,T2,T3,T4)>   type;
    00456     };
    00457
    00458   template<typename T1, typename T2, typename T3,
    00459        typename T4, typename T5>
    00460     struct create5
    00461     {
    00462       typedef node<_GLIBCXX_TYPELIST_CHAIN5(T1,T2,T3,T4,T5)>    type;
    00463     };
    00464
    00465   template<typename T1, typename T2, typename T3,
    00466        typename T4, typename T5, typename T6>
    00467     struct create6
    00468     {
    00469       typedef node<_GLIBCXX_TYPELIST_CHAIN6(T1,T2,T3,T4,T5,T6)> type;
    00470     };
    00471 } // namespace typelist
    00472 _GLIBCXX_END_NAMESPACE
    00473
    00474
    00475 #endif
    00476