Home | History | Annotate | Download | only in Support
      1 //===- llvm/Support/type_traits.h - Simplfied type traits -------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file provides useful additions to the standard type_traits library.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_SUPPORT_TYPE_TRAITS_H
     15 #define LLVM_SUPPORT_TYPE_TRAITS_H
     16 
     17 #include <type_traits>
     18 #include <utility>
     19 
     20 #ifndef __has_feature
     21 #define LLVM_DEFINED_HAS_FEATURE
     22 #define __has_feature(x) 0
     23 #endif
     24 
     25 namespace llvm {
     26 
     27 /// isPodLike - This is a type trait that is used to determine whether a given
     28 /// type can be copied around with memcpy instead of running ctors etc.
     29 template <typename T>
     30 struct isPodLike {
     31 #if __has_feature(is_trivially_copyable)
     32   // If the compiler supports the is_trivially_copyable trait use it, as it
     33   // matches the definition of isPodLike closely.
     34   static const bool value = __is_trivially_copyable(T);
     35 #else
     36   // If we don't know anything else, we can (at least) assume that all non-class
     37   // types are PODs.
     38   static const bool value = !std::is_class<T>::value;
     39 #endif
     40 };
     41 
     42 // std::pair's are pod-like if their elements are.
     43 template<typename T, typename U>
     44 struct isPodLike<std::pair<T, U> > {
     45   static const bool value = isPodLike<T>::value && isPodLike<U>::value;
     46 };
     47 
     48 /// \brief Metafunction that determines whether the given type is either an
     49 /// integral type or an enumeration type.
     50 ///
     51 /// Note that this accepts potentially more integral types than is_integral
     52 /// because it is based on merely being convertible implicitly to an integral
     53 /// type.
     54 template <typename T> class is_integral_or_enum {
     55   typedef typename std::remove_reference<T>::type UnderlyingT;
     56 
     57 public:
     58   static const bool value =
     59       !std::is_class<UnderlyingT>::value && // Filter conversion operators.
     60       !std::is_pointer<UnderlyingT>::value &&
     61       !std::is_floating_point<UnderlyingT>::value &&
     62       std::is_convertible<UnderlyingT, unsigned long long>::value;
     63 };
     64 
     65 /// \brief If T is a pointer, just return it. If it is not, return T&.
     66 template<typename T, typename Enable = void>
     67 struct add_lvalue_reference_if_not_pointer { typedef T &type; };
     68 
     69 template <typename T>
     70 struct add_lvalue_reference_if_not_pointer<
     71     T, typename std::enable_if<std::is_pointer<T>::value>::type> {
     72   typedef T type;
     73 };
     74 
     75 /// \brief If T is a pointer to X, return a pointer to const X. If it is not,
     76 /// return const T.
     77 template<typename T, typename Enable = void>
     78 struct add_const_past_pointer { typedef const T type; };
     79 
     80 template <typename T>
     81 struct add_const_past_pointer<
     82     T, typename std::enable_if<std::is_pointer<T>::value>::type> {
     83   typedef const typename std::remove_pointer<T>::type *type;
     84 };
     85 
     86 }
     87 
     88 #ifdef LLVM_DEFINED_HAS_FEATURE
     89 #undef __has_feature
     90 #endif
     91 
     92 #endif
     93