type_traits.h revision 198396
1259698Sdim//===- llvm/Support/type_traits.h - Simplfied type traits -------*- C++ -*-===//
2259698Sdim//
3259698Sdim//                     The LLVM Compiler Infrastructure
4259698Sdim//
5259698Sdim// This file is distributed under the University of Illinois Open Source
6259698Sdim// License. See LICENSE.TXT for details.
7259698Sdim//
8259698Sdim//===----------------------------------------------------------------------===//
9259698Sdim//
10259698Sdim// This file provides a template class that determines if a type is a class or
11259698Sdim// not. The basic mechanism, based on using the pointer to member function of
12259698Sdim// a zero argument to a function was "boosted" from the boost type_traits
13259698Sdim// library. See http://www.boost.org/ for all the gory details.
14259698Sdim//
15259698Sdim//===----------------------------------------------------------------------===//
16259698Sdim
17259698Sdim#ifndef LLVM_SUPPORT_TYPE_TRAITS_H
18259698Sdim#define LLVM_SUPPORT_TYPE_TRAITS_H
19259698Sdim
20259698Sdim// This is actually the conforming implementation which works with abstract
21259698Sdim// classes.  However, enough compilers have trouble with it that most will use
22259698Sdim// the one in boost/type_traits/object_traits.hpp. This implementation actually
23259698Sdim// works with VC7.0, but other interactions seem to fail when we use it.
24259698Sdim
25259698Sdimnamespace llvm {
26259698Sdim
27259698Sdimnamespace dont_use
28259698Sdim{
29259698Sdim    // These two functions should never be used. They are helpers to
30259698Sdim    // the is_class template below. They cannot be located inside
31259698Sdim    // is_class because doing so causes at least GCC to think that
32259698Sdim    // the value of the "value" enumerator is not constant. Placing
33259698Sdim    // them out here (for some strange reason) allows the sizeof
34259698Sdim    // operator against them to magically be constant. This is
35259698Sdim    // important to make the is_class<T>::value idiom zero cost. it
36259698Sdim    // evaluates to a constant 1 or 0 depending on whether the
37259698Sdim    // parameter T is a class or not (respectively).
38259698Sdim    template<typename T> char is_class_helper(void(T::*)());
39259698Sdim    template<typename T> double is_class_helper(...);
40259698Sdim}
41259698Sdim
42259698Sdimtemplate <typename T>
43259698Sdimstruct is_class
44259698Sdim{
45259698Sdim  // is_class<> metafunction due to Paul Mensonides (leavings@attbi.com). For
46259698Sdim  // more details:
47259698Sdim  // http://groups.google.com/groups?hl=en&selm=000001c1cc83%24e154d5e0%247772e50c%40c161550a&rnum=1
48259698Sdim public:
49259698Sdim    enum { value = sizeof(char) == sizeof(dont_use::is_class_helper<T>(0)) };
50259698Sdim};
51259698Sdim
52259698Sdim/// \brief Metafunction that determines whether the two given types are
53259698Sdim/// equivalent.
54259698Sdimtemplate<typename T, typename U>
55259698Sdimstruct is_same {
56259698Sdim  static const bool value = false;
57259698Sdim};
58259698Sdim
59259698Sdimtemplate<typename T>
60259698Sdimstruct is_same<T, T> {
61259698Sdim  static const bool value = true;
62259698Sdim};
63259698Sdim
64259698Sdim// enable_if_c - Enable/disable a template based on a metafunction
65259698Sdimtemplate<bool Cond, typename T = void>
66259698Sdimstruct enable_if_c {
67259698Sdim  typedef T type;
68259698Sdim};
69259698Sdim
70259698Sdimtemplate<typename T> struct enable_if_c<false, T> { };
71259698Sdim
72259698Sdim// enable_if - Enable/disable a template based on a metafunction
73259698Sdimtemplate<typename Cond, typename T = void>
74259698Sdimstruct enable_if : public enable_if_c<Cond::value, T> { };
75259698Sdim
76259698Sdimnamespace dont_use {
77259698Sdim  template<typename Base> char base_of_helper(const volatile Base*);
78259698Sdim  template<typename Base> double base_of_helper(...);
79259698Sdim}
80259698Sdim
81259698Sdim/// is_base_of - Metafunction to determine whether one type is a base class of
82259698Sdim/// (or identical to) another type.
83259698Sdimtemplate<typename Base, typename Derived>
84259698Sdimstruct is_base_of {
85259698Sdim  static const bool value
86259698Sdim    = is_class<Base>::value && is_class<Derived>::value &&
87259698Sdim      sizeof(char) == sizeof(dont_use::base_of_helper<Base>((Derived*)0));
88259698Sdim};
89259698Sdim
90259698Sdim// remove_pointer - Metafunction to turn Foo* into Foo.  Defined in
91259698Sdim// C++0x [meta.trans.ptr].
92259698Sdimtemplate <typename T> struct remove_pointer { typedef T type; };
93259698Sdimtemplate <typename T> struct remove_pointer<T*> { typedef T type; };
94259698Sdimtemplate <typename T> struct remove_pointer<T*const> { typedef T type; };
95259698Sdimtemplate <typename T> struct remove_pointer<T*volatile> { typedef T type; };
96259698Sdimtemplate <typename T> struct remove_pointer<T*const volatile> {
97259698Sdim    typedef T type; };
98259698Sdim
99259698Sdim}
100259698Sdim
101259698Sdim#endif
102259698Sdim