• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDevelop Platform Libraries
  • Sitemap
  • Contact Us
 

util

type_traits.h

00001 // Copyright (c) 2006, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 // ----
00031 // Author: Matt Austern
00032 //
00033 // Define a small subset of tr1 type traits. The traits we define are:
00034 //   is_integral
00035 //   is_floating_point
00036 //   is_pointer
00037 //   is_reference
00038 //   is_pod
00039 //   has_trivial_constructor
00040 //   has_trivial_copy
00041 //   has_trivial_assign
00042 //   has_trivial_destructor
00043 //   remove_const
00044 //   remove_volatile
00045 //   remove_cv
00046 //   remove_reference
00047 //   remove_pointer
00048 //   is_convertible
00049 // We can add more type traits as required.
00050 
00051 #ifndef BASE_TYPE_TRAITS_H_
00052 #define BASE_TYPE_TRAITS_H_
00053 
00054 #if defined(Q_OS_WIN)
00055 #include "sparsehash/sparseconfig_windows.h"
00056 #else
00057 #include "sparsehash/sparseconfig.h"
00058 #endif
00059 #include <utility>                  // For pair
00060 
00061 _START_GOOGLE_NAMESPACE_
00062 
00063 // integral_constant, defined in tr1, is a wrapper for an integer
00064 // value. We don't really need this generality; we could get away
00065 // with hardcoding the integer type to bool. We use the fully
00066 // general integer_constant for compatibility with tr1.
00067 
00068 template<class T, T v>
00069 struct integral_constant {
00070   static const T value = v;
00071   typedef T value_type;
00072   typedef integral_constant<T, v> type;
00073 };
00074 
00075 template <class T, T v> const T integral_constant<T, v>::value;
00076 
00077 // Abbreviations: true_type and false_type are structs that represent
00078 // boolean true and false values.
00079 typedef integral_constant<bool, true>  true_type;
00080 typedef integral_constant<bool, false> false_type;
00081 
00082 // Types small_ and big_ are guaranteed such that sizeof(small_) <
00083 // sizeof(big_)
00084 typedef char small_;
00085 
00086 struct big_ {
00087   char dummy[2];
00088 };
00089 
00090 // is_integral is false except for the built-in integer types.
00091 template <class T> struct is_integral : false_type { };
00092 template<> struct is_integral<bool> : true_type { };
00093 template<> struct is_integral<char> : true_type { };
00094 template<> struct is_integral<unsigned char> : true_type { };
00095 template<> struct is_integral<signed char> : true_type { };
00096 #if defined(_MSC_VER)
00097 // wchar_t is not by default a distinct type from unsigned short in
00098 // Microsoft C.
00099 // See http://msdn2.microsoft.com/en-us/library/dh8che7s(VS.80).aspx
00100 template<> struct is_integral<__wchar_t> : true_type { };
00101 #else
00102 template<> struct is_integral<wchar_t> : true_type { };
00103 #endif
00104 template<> struct is_integral<short> : true_type { };
00105 template<> struct is_integral<unsigned short> : true_type { };
00106 template<> struct is_integral<int> : true_type { };
00107 template<> struct is_integral<unsigned int> : true_type { };
00108 template<> struct is_integral<long> : true_type { };
00109 template<> struct is_integral<unsigned long> : true_type { };
00110 #ifdef HAVE_LONG_LONG
00111 template<> struct is_integral<long long> : true_type { };
00112 template<> struct is_integral<unsigned long long> : true_type { };
00113 #endif
00114 
00115 
00116 // is_floating_point is false except for the built-in floating-point types.
00117 template <class T> struct is_floating_point : false_type { };
00118 template<> struct is_floating_point<float> : true_type { };
00119 template<> struct is_floating_point<double> : true_type { };
00120 template<> struct is_floating_point<long double> : true_type { };
00121 
00122 
00123 // is_pointer is false except for pointer types.
00124 template <class T> struct is_pointer : false_type { };
00125 template <class T> struct is_pointer<T*> : true_type { };
00126 
00127 
00128 // is_reference is false except for reference types.
00129 template<typename T> struct is_reference : false_type {};
00130 template<typename T> struct is_reference<T&> : true_type {};
00131 
00132 
00133 // We can't get is_pod right without compiler help, so fail conservatively.
00134 // We will assume it's false except for arithmetic types and pointers,
00135 // and const versions thereof. Note that std::pair is not a POD.
00136 template <class T> struct is_pod
00137  : integral_constant<bool, (is_integral<T>::value ||
00138                             is_floating_point<T>::value ||
00139                             is_pointer<T>::value)> { };
00140 template <class T> struct is_pod<const T> : is_pod<T> { };
00141 
00142 
00143 // We can't get has_trivial_constructor right without compiler help, so
00144 // fail conservatively. We will assume it's false except for: (1) types
00145 // for which is_pod is true. (2) std::pair of types with trivial
00146 // constructors. (3) array of a type with a trivial constructor.
00147 // (4) const versions thereof.
00148 template <class T> struct has_trivial_constructor : is_pod<T> { };
00149 template <class T, class U> struct has_trivial_constructor<std::pair<T, U> >
00150   : integral_constant<bool,
00151                       (has_trivial_constructor<T>::value &&
00152                        has_trivial_constructor<U>::value)> { };
00153 template <class A, int N> struct has_trivial_constructor<A[N]>
00154   : has_trivial_constructor<A> { };
00155 template <class T> struct has_trivial_constructor<const T>
00156   : has_trivial_constructor<T> { };
00157 
00158 // We can't get has_trivial_copy right without compiler help, so fail
00159 // conservatively. We will assume it's false except for: (1) types
00160 // for which is_pod is true. (2) std::pair of types with trivial copy
00161 // constructors. (3) array of a type with a trivial copy constructor.
00162 // (4) const versions thereof.
00163 template <class T> struct has_trivial_copy : is_pod<T> { };
00164 template <class T, class U> struct has_trivial_copy<std::pair<T, U> >
00165   : integral_constant<bool,
00166                       (has_trivial_copy<T>::value &&
00167                        has_trivial_copy<U>::value)> { };
00168 template <class A, int N> struct has_trivial_copy<A[N]>
00169   : has_trivial_copy<A> { };
00170 template <class T> struct has_trivial_copy<const T> : has_trivial_copy<T> { };
00171 
00172 // We can't get has_trivial_assign right without compiler help, so fail
00173 // conservatively. We will assume it's false except for: (1) types
00174 // for which is_pod is true. (2) std::pair of types with trivial copy
00175 // constructors. (3) array of a type with a trivial assign constructor.
00176 template <class T> struct has_trivial_assign : is_pod<T> { };
00177 template <class T, class U> struct has_trivial_assign<std::pair<T, U> >
00178   : integral_constant<bool,
00179                       (has_trivial_assign<T>::value &&
00180                        has_trivial_assign<U>::value)> { };
00181 template <class A, int N> struct has_trivial_assign<A[N]>
00182   : has_trivial_assign<A> { };
00183 
00184 // We can't get has_trivial_destructor right without compiler help, so
00185 // fail conservatively. We will assume it's false except for: (1) types
00186 // for which is_pod is true. (2) std::pair of types with trivial
00187 // destructors. (3) array of a type with a trivial destructor.
00188 // (4) const versions thereof.
00189 template <class T> struct has_trivial_destructor : is_pod<T> { };
00190 template <class T, class U> struct has_trivial_destructor<std::pair<T, U> >
00191   : integral_constant<bool,
00192                       (has_trivial_destructor<T>::value &&
00193                        has_trivial_destructor<U>::value)> { };
00194 template <class A, int N> struct has_trivial_destructor<A[N]>
00195   : has_trivial_destructor<A> { };
00196 template <class T> struct has_trivial_destructor<const T>
00197   : has_trivial_destructor<T> { };
00198 
00199 // Specified by TR1 [4.7.1]
00200 template<typename T> struct remove_const { typedef T type; };
00201 template<typename T> struct remove_const<T const> { typedef T type; };
00202 template<typename T> struct remove_volatile { typedef T type; };
00203 template<typename T> struct remove_volatile<T volatile> { typedef T type; };
00204 template<typename T> struct remove_cv {
00205   typedef typename remove_const<typename remove_volatile<T>::type>::type type;
00206 };
00207 
00208 
00209 // Specified by TR1 [4.7.2]
00210 template<typename T> struct remove_reference { typedef T type; };
00211 template<typename T> struct remove_reference<T&> { typedef T type; };
00212 
00213 // Specified by TR1 [4.7.4] Pointer modifications.
00214 template<typename T> struct remove_pointer { typedef T type; };
00215 template<typename T> struct remove_pointer<T*> { typedef T type; };
00216 template<typename T> struct remove_pointer<T* const> { typedef T type; };
00217 template<typename T> struct remove_pointer<T* volatile> { typedef T type; };
00218 template<typename T> struct remove_pointer<T* const volatile> {
00219   typedef T type; };
00220 
00221 // Specified by TR1 [4.6] Relationships between types
00222 #ifndef _MSC_VER
00223 namespace internal {
00224 
00225 // This class is an implementation detail for is_convertible, and you
00226 // don't need to know how it works to use is_convertible. For those
00227 // who care: we declare two different functions, one whose argument is
00228 // of type To and one with a variadic argument list. We give them
00229 // return types of different size, so we can use sizeof to trick the
00230 // compiler into telling us which function it would have chosen if we
00231 // had called it with an argument of type From.  See Alexandrescu's
00232 // _Modern C++ Design_ for more details on this sort of trick.
00233 
00234 template <typename From, typename To>
00235 struct ConvertHelper {
00236   static small_ Test(To);
00237   static big_ Test(...);
00238   static From Create();
00239 };
00240 }  // namespace internal
00241 
00242 // Inherits from true_type if From is convertible to To, false_type otherwise.
00243 template <typename From, typename To>
00244 struct is_convertible
00245     : integral_constant<bool,
00246                         sizeof(internal::ConvertHelper<From, To>::Test(
00247                                   internal::ConvertHelper<From, To>::Create()))
00248                         == sizeof(small_)> {
00249 };
00250 #endif
00251 
00252 _END_GOOGLE_NAMESPACE_
00253 
00254 #endif  // BASE_TYPE_TRAITS_H_

util

Skip menu "util"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDevelop Platform Libraries

Skip menu "KDevelop Platform Libraries"
  • interfaces
  • language
  •   codegen
  •   duchain
  •   editor
  • outputview
  • project
  • shell
  • sublime
  • util
  • vcs
Generated for KDevelop Platform Libraries by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal