1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Template metaprogramming utility functions.
7 // This code is compiled directly on many platforms, including client
8 // platforms like Windows, Mac, and embedded systems. Before making
9 // any changes here, make sure that you're not breaking any platforms.
11 // The names choosen here reflect those used in tr1 and the boost::mpl
12 // library, there are similar operations used in the Loki library as
13 // well. I prefer the boost names for 2 reasons:
14 // 1. I think that portions of the Boost libraries are more likely to
15 // be included in the c++ standard.
16 // 2. It is not impossible that some of the boost libraries will be
17 // included in our own build in the future.
18 // Both of these outcomes means that we may be able to directly replace
19 // some of these with boost equivalents.
21 #ifndef BASE_TEMPLATE_UTIL_H_
22 #define BASE_TEMPLATE_UTIL_H_
26 // Types small_ and big_ are guaranteed such that sizeof(small_) <
34 // integral_constant, defined in tr1, is a wrapper for an integer
35 // value. We don't really need this generality; we could get away
36 // with hardcoding the integer type to bool. We use the fully
37 // general integer_constant for compatibility with tr1.
39 template<class T
, T v
>
40 struct integral_constant
{
41 static const T value
= v
;
43 typedef integral_constant
<T
, v
> type
;
46 template <class T
, T v
> const T integral_constant
<T
, v
>::value
;
49 // Abbreviations: true_type and false_type are structs that represent boolean
50 // true and false values. Also define the boost::mpl versions of those names,
52 typedef integral_constant
<bool, true> true_type
;
53 typedef integral_constant
<bool, false> false_type
;
54 typedef true_type true_
;
55 typedef false_type false_
;
57 // if_ is a templatized conditional statement.
58 // if_<cond, A, B> is a compile time evaluation of cond.
59 // if_<>::type contains A if cond is true, B otherwise.
60 template<bool cond
, typename A
, typename B
>
65 template<typename A
, typename B
>
66 struct if_
<false, A
, B
> {
71 // type_equals_ is a template type comparator, similar to Loki IsSameType.
72 // type_equals_<A, B>::value is true iff "A" is the same type as "B".
73 template<typename A
, typename B
>
74 struct type_equals_
: public false_
{
78 struct type_equals_
<A
, A
> : public true_
{
81 // and_ is a template && operator.
82 // and_<A, B>::value evaluates "A::value && B::value".
83 template<typename A
, typename B
>
84 struct and_
: public integral_constant
<bool, (A::value
&& B::value
)> {
87 // or_ is a template || operator.
88 // or_<A, B>::value evaluates "A::value || B::value".
89 template<typename A
, typename B
>
90 struct or_
: public integral_constant
<bool, (A::value
|| B::value
)> {
94 } // Close namespace base
96 #endif // BASE_TEMPLATE_UTIL_H_