1 | // Copyright 2005 Google Inc. All Rights Reserved. |
2 | // |
3 | // Template metaprogramming utility functions. |
4 | // |
5 | // This code is compiled directly on many platforms, including client |
6 | // platforms like Windows, Mac, and embedded systems. Before making |
7 | // any changes here, make sure that you're not breaking any platforms. |
8 | // |
9 | // |
10 | // The names choosen here reflect those used in tr1 and the boost::mpl |
11 | // library, there are similar operations used in the Loki library as |
12 | // well. I prefer the boost names for 2 reasons: |
13 | // 1. I think that portions of the Boost libraries are more likely to |
14 | // be included in the c++ standard. |
15 | // 2. It is not impossible that some of the boost libraries will be |
16 | // included in our own build in the future. |
17 | // Both of these outcomes means that we may be able to directly replace |
18 | // some of these with boost equivalents. |
19 | // |
20 | #ifndef BASE_TEMPLATE_UTIL_H_ |
21 | #define BASE_TEMPLATE_UTIL_H_ |
22 | |
23 | namespace base { |
24 | |
25 | // Types small_ and big_ are guaranteed such that sizeof(small_) < |
26 | // sizeof(big_) |
27 | typedef char small_; |
28 | |
29 | struct big_ { |
30 | char dummy[2]; |
31 | }; |
32 | |
33 | // integral_constant, defined in tr1, is a wrapper for an integer |
34 | // value. We don't really need this generality; we could get away |
35 | // with hardcoding the integer type to bool. We use the fully |
36 | // general integer_constant for compatibility with tr1. |
37 | |
38 | template<class T, T v> |
39 | struct integral_constant { |
40 | static const T value = v; |
41 | typedef T value_type; |
42 | typedef integral_constant<T, v> type; |
43 | }; |
44 | |
45 | template <class T, T v> const T integral_constant<T, v>::value; |
46 | |
47 | |
48 | // Abbreviations: true_type and false_type are structs that represent boolean |
49 | // true and false values. Also define the boost::mpl versions of those names, |
50 | // true_ and false_. |
51 | typedef integral_constant<bool, true> true_type; |
52 | typedef integral_constant<bool, false> false_type; |
53 | typedef true_type true_; |
54 | typedef false_type false_; |
55 | |
56 | // if_ is a templatized conditional statement. |
57 | // if_<cond, A, B> is a compile time evaluation of cond. |
58 | // if_<>::type contains A if cond is true, B otherwise. |
59 | template<bool cond, typename A, typename B> |
60 | struct if_{ |
61 | typedef A type; |
62 | }; |
63 | |
64 | template<typename A, typename B> |
65 | struct if_<false, A, B> { |
66 | typedef B type; |
67 | }; |
68 | |
69 | |
70 | // type_equals_ is a template type comparator, similar to Loki IsSameType. |
71 | // type_equals_<A, B>::value is true iff "A" is the same type as "B". |
72 | // |
73 | // New code should prefer base::is_same, defined in base/type_traits.h. |
74 | // It is functionally identical, but is_same is the standard spelling. |
75 | template<typename A, typename B> |
76 | struct type_equals_ : public false_ { |
77 | }; |
78 | |
79 | template<typename A> |
80 | struct type_equals_<A, A> : public true_ { |
81 | }; |
82 | |
83 | // and_ is a template && operator. |
84 | // and_<A, B>::value evaluates "A::value && B::value". |
85 | template<typename A, typename B> |
86 | struct and_ : public integral_constant<bool, (A::value && B::value)> { |
87 | }; |
88 | |
89 | // or_ is a template || operator. |
90 | // or_<A, B>::value evaluates "A::value || B::value". |
91 | template<typename A, typename B> |
92 | struct or_ : public integral_constant<bool, (A::value || B::value)> { |
93 | }; |
94 | |
95 | |
96 | } // Close namespace base |
97 | |
98 | #endif // BASE_TEMPLATE_UTIL_H_ |
99 | |