type_utils.hpp

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005 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 
00046 #ifndef TYPE_UTILS_HPP
00047 #define TYPE_UTILS_HPP
00048 
00049 #include <cstddef>
00050 #include <utility>
00051 
00052 namespace pb_assoc
00053 {
00054 
00055   namespace detail
00056   {
00057 
00058     template<bool>
00059     struct static_assert;
00060 
00061     template<>
00062     struct static_assert<true>
00063     { };
00064 
00065     template<int>
00066     struct static_assert_dummy_class
00067     {
00068       enum
00069     {
00070       v = 1
00071     };
00072     };
00073 
00074     template<class T, class U>
00075     class is_same_type
00076     {
00077     public:
00078       enum
00079     {
00080       value = false
00081     };
00082     };
00083 
00084     template<class T>
00085     class is_same_type<
00086       T,
00087       T>
00088     {
00089     public:
00090       enum
00091     {
00092       value = true
00093     };
00094     };
00095 
00096     template<int n>
00097     struct int_to_type
00098     {
00099       enum
00100     {
00101       value = n
00102     };
00103     };
00104 
00105     template<typename Type>
00106     struct type_to_type
00107     {
00108       typedef Type type;
00109     };
00110 
00111     template<typename T>
00112     class unconst
00113     {
00114     private:
00115       template<class U>
00116       struct unconst_imp
00117       {
00118     typedef U type;
00119       };
00120 
00121       template<class U>
00122       struct unconst_imp<
00123     const U>
00124       {
00125     typedef U type;
00126       };
00127     public:
00128       typedef typename unconst_imp<T>::type type;
00129     };
00130 
00131     template<typename T>
00132     class unreference
00133     {
00134     private:
00135       template<class U>
00136       struct unreference_imp
00137       {
00138     typedef U type;
00139       };
00140 
00141       template<class U>
00142       struct unreference_imp<U&>
00143       {
00144     typedef U type;
00145       };
00146     public:
00147       typedef typename unreference_imp<T>::type type;
00148     };
00149 
00150     /* is_const_type
00151      * Idea by Andrei Alecsandrescu
00152      *  (Modern C++ Design: Generic Programming and Design Patterns Applied)
00153      **/
00154     template<typename T>
00155     class is_const_type
00156     {
00157     private:
00158       template<class U>
00159       struct is_const_type_imp
00160       {
00161     enum
00162       {
00163         value = 0
00164       };
00165       };
00166 
00167       template<class U>
00168       struct is_const_type_imp<const U>
00169       {
00170     enum
00171       {
00172         value = 1
00173       };
00174       };
00175 
00176     public:
00177       enum
00178     {
00179       value = is_const_type_imp<T>::value
00180     };
00181     };
00182 
00183     /* is_pointer_type
00184     **/
00185     template<typename T>
00186     class is_pointer_type
00187     {
00188     private:
00189       template<class U>
00190       struct is_pointer_type_imp
00191       {
00192     enum
00193       {
00194         value = 0
00195       };
00196       };
00197 
00198       template<class U>
00199       struct is_pointer_type_imp
00200       <U* >
00201       {
00202     enum
00203       {
00204         value = 1
00205       };
00206       };
00207 
00208     public:
00209       enum
00210     {
00211       value = is_pointer_type_imp<T>::value
00212     };
00213     };
00214 
00215     /* is_pointer_type
00216     **/
00217     template<typename T>
00218     class is_const_pointer_type
00219     {
00220     private:
00221       template<class U>
00222       struct is_const_pointer_type_imp
00223       {
00224     enum
00225       {
00226         value = 0
00227       };
00228       };
00229 
00230       template<class U>
00231       struct is_const_pointer_type_imp
00232       <const U* >
00233       {
00234     enum
00235       {
00236         value = 1
00237       };
00238       };
00239 
00240     public:
00241       enum
00242     {
00243       value = is_const_pointer_type_imp<T>::value
00244     };
00245     };
00246 
00247     template<typename T>
00248     class is_reference_type
00249     {
00250     private:
00251       template<class U>
00252       struct is_reference_type_imp
00253       {
00254     enum
00255       {
00256         value = 0
00257       };
00258       };
00259 
00260       template<class U>
00261       struct is_reference_type_imp<U& >
00262       {
00263     enum
00264       {
00265         value = 1
00266       };
00267       };
00268 
00269     public:
00270       enum
00271     {
00272       value = is_reference_type_imp<T>::value
00273     };
00274     };
00275 
00276     template<typename T>
00277     class is_const_reference_type
00278     {
00279     private:
00280       template<class U>
00281       struct is_const_reference_type_imp
00282       {
00283     enum
00284       {
00285         value = 0
00286       };
00287       };
00288 
00289       template<class U>
00290       struct is_const_reference_type_imp<U& >
00291       {
00292     enum
00293       {
00294         value = 1
00295       };
00296       };
00297 
00298     public:
00299       enum
00300     {
00301       value = is_const_reference_type_imp<T>::value
00302     };
00303     };
00304 
00305     template<typename T>
00306     class is_member_pointer_type
00307     {
00308     private:
00309       template<typename U>
00310       struct is_member_pointer_type_imp
00311       {
00312     enum
00313       {
00314         value = 0
00315       };
00316       };
00317 
00318       template<typename U, typename V>
00319       struct is_member_pointer_type_imp<
00320     U V::*>
00321       {
00322     enum
00323       {
00324         value = 1
00325       };
00326       };
00327 
00328     public:
00329       enum
00330     {
00331       value = is_member_pointer_type_imp<T>::value
00332     };
00333     };
00334 
00335 #define PB_ASSOC_IS_SAME_TYPE(TYPE) is_same_type<T, TYPE>::value
00336 
00337     template<class T>
00338     class is_simple_type
00339     {
00340       template<class U>
00341       struct is_simple_type_imp
00342       {
00343     enum
00344       {
00345         value = 0
00346       };
00347       };
00348 
00349       template<class U, size_t M>
00350       struct is_simple_type_imp<
00351     U[M]>
00352       {
00353     enum
00354       {
00355         value = is_simple_type<U>::value
00356       };
00357       };
00358 
00359       template<class U>
00360       struct is_simple_type_imp<
00361     U[]>
00362       {
00363     enum
00364       {
00365         value = is_simple_type<U>::value
00366       };
00367       };
00368 
00369       template<typename T0, typename T1>
00370       struct is_simple_type_imp<
00371     std::pair<
00372     T0,
00373     T1> >
00374       {
00375     enum
00376       {
00377         value = is_simple_type<T0>::value&& 
00378         is_simple_type<T1>::value
00379       };
00380       };
00381 
00382     public:
00383       enum
00384     {
00385       value =
00386       PB_ASSOC_IS_SAME_TYPE(void) ||
00387       PB_ASSOC_IS_SAME_TYPE(size_t) ||
00388       PB_ASSOC_IS_SAME_TYPE(const void) ||
00389       PB_ASSOC_IS_SAME_TYPE(unsigned char) ||
00390       PB_ASSOC_IS_SAME_TYPE(unsigned short int) ||
00391       PB_ASSOC_IS_SAME_TYPE(unsigned int) ||
00392       PB_ASSOC_IS_SAME_TYPE(unsigned long int) ||
00393       PB_ASSOC_IS_SAME_TYPE(signed char) ||
00394       PB_ASSOC_IS_SAME_TYPE(signed short int) ||
00395       PB_ASSOC_IS_SAME_TYPE(int) ||
00396       PB_ASSOC_IS_SAME_TYPE(long int) ||
00397       PB_ASSOC_IS_SAME_TYPE(bool) ||
00398       PB_ASSOC_IS_SAME_TYPE(char) ||
00399       PB_ASSOC_IS_SAME_TYPE(float) ||
00400       PB_ASSOC_IS_SAME_TYPE(double) ||
00401       PB_ASSOC_IS_SAME_TYPE(long double) ||
00402       PB_ASSOC_IS_SAME_TYPE(const unsigned char) ||
00403       PB_ASSOC_IS_SAME_TYPE(const unsigned short int) ||
00404       PB_ASSOC_IS_SAME_TYPE(const unsigned int) ||
00405       PB_ASSOC_IS_SAME_TYPE(const unsigned long int) ||
00406       PB_ASSOC_IS_SAME_TYPE(const signed char) ||
00407       PB_ASSOC_IS_SAME_TYPE(const signed short int) ||
00408       PB_ASSOC_IS_SAME_TYPE(const int) ||
00409       PB_ASSOC_IS_SAME_TYPE(const long int) ||
00410       PB_ASSOC_IS_SAME_TYPE(const bool) ||
00411       PB_ASSOC_IS_SAME_TYPE(const char) ||
00412       PB_ASSOC_IS_SAME_TYPE(const float) ||
00413       PB_ASSOC_IS_SAME_TYPE(const double) ||
00414       PB_ASSOC_IS_SAME_TYPE(const long double) ||
00415       is_pointer_type<T>::value ||
00416       is_const_pointer_type<T>::value ||
00417       is_member_pointer_type<T>::value ||
00418       is_simple_type_imp<T>::value
00419     };
00420     };
00421 
00422 #undef PB_ASSOC_IS_SAME_TYPE
00423 
00424     template<bool Cond, class A, class B>
00425     struct cond_type;
00426 
00427     template<class A, class B>
00428     struct cond_type<
00429       true,
00430       A,
00431       B>
00432     {
00433       typedef A type;
00434     };
00435 
00436     template<class A, class B>
00437     struct cond_type<
00438       false,
00439       A,
00440       B>
00441     {
00442       typedef B type;
00443     };
00444 
00445   } // namespace detail
00446 
00447 } // namespace pb_assoc
00448 
00449 #endif // #ifndef TYPE_UTILS_HPP

Generated on Tue Feb 2 16:56:51 2010 for GNU C++ STL by  doxygen 1.4.7