1
// Copyright 2005 Google Inc. All Rights Reserved.
3
// Template metaprogramming utility functions.
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.
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.
20
#ifndef BASE_TEMPLATE_UTIL_H_
21
#define BASE_TEMPLATE_UTIL_H_
25
// Types small_ and big_ are guaranteed such that sizeof(small_) <
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.
38
template<class T, T v>
39
struct integral_constant {
40
static const T value = v;
42
typedef integral_constant<T, v> type;
45
template <class T, T v> const T integral_constant<T, v>::value;
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,
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_;
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>
64
template<typename A, typename B>
65
struct if_<false, A, B> {
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".
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_ {
80
struct type_equals_<A, A> : public true_ {
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)> {
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)> {
96
} // Close namespace base
98
#endif // BASE_TEMPLATE_UTIL_H_