# type_utils.hpp

Go to the documentation of this file.

    00001 // -*- C++ -*-
    00002
    00003 // Copyright (C) 2005, 2006, 2007 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 terms
    00007 // of the GNU General Public License as published by the Free Software
    00008 // Foundation; either version 2, or (at your option) any later
    00009 // version.
    00010
    00011 // This library is distributed in the hope that it will be useful, but
    00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014 // General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License
    00017 // along with this library; see the file COPYING.  If not, write to
    00018 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
    00019 // MA 02111-1307, USA.
    00020
    00021 // As a special exception, you may use this file as part of a free
    00022 // software library without restriction.  Specifically, if other files
    00023 // instantiate templates or use macros or inline functions from this
    00024 // file, or you compile this file and link it with other files to
    00025 // produce an executable, this file does not by itself cause the
    00026 // resulting executable to be covered by the GNU General Public
    00027 // License.  This exception does not however invalidate any other
    00028 // reasons why the executable file might be covered by the GNU General
    00029 // Public License.
    00030
    00031 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
    00032
    00033 // Permission to use, copy, modify, sell, and distribute this software
    00034 // is hereby granted without fee, provided that the above copyright
    00035 // notice appears in all copies, and that both that copyright notice
    00036 // and this permission notice appear in supporting documentation. None
    00037 // of the above authors, nor IBM Haifa Research Laboratories, make any
    00038 // representation about the suitability of this software for any
    00039 // purpose. It is provided "as is" without express or implied
    00040 // warranty.
    00041
    00042 /**
    00043  * @file type_utils.hpp
    00044  * Contains utilities for handnling types. All of these classes are based on
    00045  *    "Modern C++" by Andrei Alxandrescu.
    00046  */
    00047
    00048 #ifndef PB_DS_TYPE_UTILS_HPP
    00049 #define PB_DS_TYPE_UTILS_HPP
    00050
    00051 #include <cstddef>
    00052 #include <utility>
    00053 #include <tr1/type_traits>
    00054 #include <ext/type_traits.h>
    00055 #include <ext/numeric_traits.h>
    00056
    00057 namespace __gnu_pbds
    00058 {
    00059   namespace detail
    00060   {
    00061     using std::tr1::is_same;
    00062     using std::tr1::is_const;
    00063     using std::tr1::is_pointer;
    00064     using std::tr1::is_reference;
    00065     using std::tr1::is_fundamental;
    00066     using std::tr1::is_member_object_pointer;
    00067     using std::tr1::is_member_pointer;
    00068     using std::tr1::is_base_of;
    00069     using std::tr1::remove_const;
    00070     using std::tr1::remove_reference;
    00071
    00072     // Need integral_const<bool, true> <-> integral_const<int, 1>, so
    00073     // because of this use the following typedefs instead of importing
    00074     // std::tr1's.
    00075     using std::tr1::integral_constant;
    00076     typedef std::tr1::integral_constant<int, 1> true_type;
    00077     typedef std::tr1::integral_constant<int, 0> false_type;
    00078
    00079     using __gnu_cxx::__conditional_type;
    00080     using __gnu_cxx::__numeric_traits;
    00081
    00082     template<typename T>
    00083     struct is_const_pointer
    00084     {
    00085       enum
    00086     {
    00087       value = is_const<T>::value && is_pointer<T>::value
    00088     };
    00089     };
    00090
    00091     template<typename T>
    00092     struct is_const_reference
    00093     {
    00094       enum
    00095     {
    00096       value = is_const<T>::value && is_reference<T>::value
    00097     };
    00098     };
    00099
    00100     template<typename T>
    00101     struct is_simple
    00102     {
    00103       enum
    00104     {
    00105       value = is_fundamental<typename remove_const<T>::type>::value
    00106       || is_pointer<typename remove_const<T>::type>::value
    00107       || is_member_pointer<T>::value
    00108     };
    00109     };
    00110
    00111     template<typename T>
    00112     class is_pair
    00113     {
    00114     private:
    00115       template<typename U>
    00116       struct is_pair_imp
    00117       {
    00118     enum
    00119       {
    00120         value = 0
    00121       };
    00122       };
    00123
    00124       template<typename U, typename V>
    00125       struct is_pair_imp<std::pair<U,V> >
    00126       {
    00127     enum
    00128       {
    00129         value = 1
    00130       };
    00131       };
    00132
    00133     public:
    00134       enum
    00135     {
    00136       value = is_pair_imp<T>::value
    00137     };
    00138     };
    00139
    00140     // Use C++0x's static_assert if possible.
    00141 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00142 #define PB_DS_STATIC_ASSERT(UNIQUE, E)  static_assert(E, #UNIQUE)
    00143 #else
    00144     template<bool>
    00145     struct __static_assert;
    00146
    00147     template<>
    00148     struct __static_assert<true>
    00149     { };
    00150
    00151     template<int>
    00152     struct __static_assert_dumclass
    00153     {
    00154       enum
    00155     {
    00156       v = 1
    00157     };
    00158     };
    00159
    00160 #define PB_DS_STATIC_ASSERT(UNIQUE, E)  \
    00161     typedef __gnu_pbds::detail::__static_assert_dumclass<sizeof(__gnu_pbds::detail::__static_assert<bool(E)>)> UNIQUE##__static_assert_type
    00162
    00163 #endif
    00164
    00165     template<typename Type>
    00166     struct type_to_type
    00167     {
    00168       typedef Type type;
    00169     };
    00170   } // namespace detail
    00171 } // namespace __gnu_pbds
    00172
    00173 #endif