LLVM API Documentation

type_traits.h

Go to the documentation of this file.
00001 //===- llvm/Support/type_traits.h - Simplfied type traits -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file provides a template class that determines if a type is a class or
00011 // not. The basic mechanism, based on using the pointer to member function of
00012 // a zero argument to a function was "boosted" from the boost type_traits
00013 // library. See http://www.boost.org/ for all the gory details.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_SUPPORT_TYPE_TRAITS_H
00018 #define LLVM_SUPPORT_TYPE_TRAITS_H
00019 
00020 // This is actually the conforming implementation which works with abstract
00021 // classes.  However, enough compilers have trouble with it that most will use
00022 // the one in boost/type_traits/object_traits.hpp. This implementation actually
00023 // works with VC7.0, but other interactions seem to fail when we use it.
00024 
00025 namespace llvm {
00026 
00027 namespace dont_use
00028 {
00029     // These two functions should never be used. They are helpers to
00030     // the is_class template below. They cannot be located inside
00031     // is_class because doing so causes at least GCC to think that
00032     // the value of the "value" enumerator is not constant. Placing
00033     // them out here (for some strange reason) allows the sizeof
00034     // operator against them to magically be constant. This is
00035     // important to make the is_class<T>::value idiom zero cost. it
00036     // evaluates to a constant 1 or 0 depending on whether the
00037     // parameter T is a class or not (respectively).
00038     template<typename T> char is_class_helper(void(T::*)());
00039     template<typename T> double is_class_helper(...);
00040 }
00041 
00042 template <typename T>
00043 struct is_class
00044 {
00045   // is_class<> metafunction due to Paul Mensonides (leavings@attbi.com). For
00046   // more details:
00047   // http://groups.google.com/groups?hl=en&selm=000001c1cc83%24e154d5e0%247772e50c%40c161550a&rnum=1
00048  public:
00049     enum { value = sizeof(char) == sizeof(dont_use::is_class_helper<T>(0)) };
00050 };
00051 
00052   
00053 // enable_if_c - Enable/disable a template based on a metafunction
00054 template<bool Cond, typename T = void>
00055 struct enable_if_c {
00056   typedef T type;
00057 };
00058 
00059 template<typename T> struct enable_if_c<false, T> { };
00060   
00061 // enable_if - Enable/disable a template based on a metafunction
00062 template<typename Cond, typename T = void>
00063 struct enable_if : public enable_if_c<Cond::value, T> { };
00064 
00065 namespace dont_use {
00066   template<typename Base> char base_of_helper(const volatile Base*);
00067   template<typename Base> double base_of_helper(...);
00068 }
00069 
00070 /// is_base_of - Metafunction to determine whether one type is a base class of
00071 /// (or identical to) another type.
00072 template<typename Base, typename Derived>
00073 struct is_base_of {
00074   static const bool value 
00075     = is_class<Base>::value && is_class<Derived>::value &&
00076       sizeof(char) == sizeof(dont_use::base_of_helper<Base>((Derived*)0));
00077 };
00078 
00079 }
00080 
00081 #endif