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. 4 5// Template metaprogramming utility functions. 6// 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. 10// 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. 20// 21#ifndef BASE_TEMPLATE_UTIL_H_ 22#define BASE_TEMPLATE_UTIL_H_ 23 24namespace base { 25 26// Types small_ and big_ are guaranteed such that sizeof(small_) < 27// sizeof(big_) 28typedef char small_; 29 30struct big_ { 31 char dummy[2]; 32}; 33 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. 38 39template<class T, T v> 40struct integral_constant { 41 static const T value = v; 42 typedef T value_type; 43 typedef integral_constant<T, v> type; 44}; 45 46template <class T, T v> const T integral_constant<T, v>::value; 47 48 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, 51// true_ and false_. 52typedef integral_constant<bool, true> true_type; 53typedef integral_constant<bool, false> false_type; 54typedef true_type true_; 55typedef false_type false_; 56 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. 60template<bool cond, typename A, typename B> 61struct if_{ 62 typedef A type; 63}; 64 65template<typename A, typename B> 66struct if_<false, A, B> { 67 typedef B type; 68}; 69 70 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". 73template<typename A, typename B> 74struct type_equals_ : public false_ { 75}; 76 77template<typename A> 78struct type_equals_<A, A> : public true_ { 79}; 80 81// and_ is a template && operator. 82// and_<A, B>::value evaluates "A::value && B::value". 83template<typename A, typename B> 84struct and_ : public integral_constant<bool, (A::value && B::value)> { 85}; 86 87// or_ is a template || operator. 88// or_<A, B>::value evaluates "A::value || B::value". 89template<typename A, typename B> 90struct or_ : public integral_constant<bool, (A::value || B::value)> { 91}; 92 93 94} // Close namespace base 95 96#endif // BASE_TEMPLATE_UTIL_H_ 97