libstdc++
type_utils.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2007, 2009, 2010 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
00026 
00027 // Permission to use, copy, modify, sell, and distribute this software
00028 // is hereby granted without fee, provided that the above copyright
00029 // notice appears in all copies, and that both that copyright notice
00030 // and this permission notice appear in supporting documentation. None
00031 // of the above authors, nor IBM Haifa Research Laboratories, make any
00032 // representation about the suitability of this software for any
00033 // purpose. It is provided "as is" without express or implied
00034 // warranty.
00035 
00036 /**
00037  * @file detail/type_utils.hpp
00038  * Contains utilities for handnling types. All of these classes are based on
00039  * Modern C++ by Andrei Alxandrescu.
00040  */
00041 
00042 #ifndef PB_DS_TYPE_UTILS_HPP
00043 #define PB_DS_TYPE_UTILS_HPP
00044 
00045 #include <cstddef>
00046 #include <utility>
00047 #include <tr1/type_traits>
00048 #include <ext/type_traits.h>
00049 #include <ext/numeric_traits.h>
00050 
00051 namespace __gnu_pbds
00052 {
00053   namespace detail
00054   {
00055     using std::tr1::is_same;
00056     using std::tr1::is_const;
00057     using std::tr1::is_pointer;
00058     using std::tr1::is_reference;
00059     using std::tr1::is_fundamental;
00060     using std::tr1::is_member_object_pointer;
00061     using std::tr1::is_member_pointer;
00062     using std::tr1::is_base_of;
00063     using std::tr1::remove_const;
00064     using std::tr1::remove_reference;
00065 
00066     // Need integral_const<bool, true> <-> integral_const<int, 1>, so
00067     // because of this use the following typedefs instead of importing
00068     // std::tr1's.
00069     using std::tr1::integral_constant;
00070     typedef std::tr1::integral_constant<int, 1> true_type;
00071     typedef std::tr1::integral_constant<int, 0> false_type;
00072 
00073     using __gnu_cxx::__conditional_type;
00074     using __gnu_cxx::__numeric_traits;
00075 
00076     template<typename T>
00077     struct is_const_pointer
00078     {
00079       enum
00080     {
00081       value = is_const<T>::value && is_pointer<T>::value
00082     };
00083     };
00084 
00085     template<typename T>
00086     struct is_const_reference
00087     {
00088       enum
00089     {
00090       value = is_const<T>::value && is_reference<T>::value
00091     };
00092     };
00093 
00094     template<typename T>
00095     struct is_simple
00096     {
00097       enum
00098     {
00099       value = is_fundamental<typename remove_const<T>::type>::value 
00100       || is_pointer<typename remove_const<T>::type>::value 
00101       || is_member_pointer<T>::value 
00102     };
00103     };
00104 
00105     template<typename T>
00106     class is_pair
00107     {
00108     private:
00109       template<typename U>
00110       struct is_pair_imp
00111       {
00112     enum
00113       {
00114         value = 0
00115       };
00116       };
00117 
00118       template<typename U, typename V>
00119       struct is_pair_imp<std::pair<U,V> >
00120       {
00121     enum
00122       {
00123         value = 1
00124       };
00125       };
00126 
00127     public:
00128       enum
00129     {
00130       value = is_pair_imp<T>::value
00131     };
00132     };
00133 
00134     // Use C++0x's static_assert if possible.
00135 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00136 #define PB_DS_STATIC_ASSERT(UNIQUE, E)  static_assert(E, #UNIQUE)
00137 #else
00138     template<bool>
00139     struct __static_assert;
00140 
00141     template<>
00142     struct __static_assert<true>
00143     { };
00144 
00145     template<int>
00146     struct __static_assert_dumclass
00147     {
00148       enum
00149     {
00150       v = 1
00151     };
00152     };
00153 
00154 #define PB_DS_STATIC_ASSERT(UNIQUE, E)  \
00155     typedef __gnu_pbds::detail::__static_assert_dumclass<sizeof(__gnu_pbds::detail::__static_assert<bool(E)>)> UNIQUE##__static_assert_type
00156 
00157 #endif
00158 
00159     template<typename Type>
00160     struct type_to_type
00161     {
00162       typedef Type type;
00163     };
00164   } // namespace detail
00165 } // namespace __gnu_pbds
00166 
00167 #endif