Home | History | Annotate | Download | only in base
      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 
     24 namespace base {
     25 
     26 // Types small_ and big_ are guaranteed such that sizeof(small_) <
     27 // sizeof(big_)
     28 typedef char small_;
     29 
     30 struct 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 
     39 template<class T, T v>
     40 struct integral_constant {
     41   static const T value = v;
     42   typedef T value_type;
     43   typedef integral_constant<T, v> type;
     44 };
     45 
     46 template <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_.
     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_;
     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.
     60 template<bool cond, typename A, typename B>
     61 struct if_{
     62   typedef A type;
     63 };
     64 
     65 template<typename A, typename B>
     66 struct 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".
     73 template<typename A, typename B>
     74 struct type_equals_ : public false_ {
     75 };
     76 
     77 template<typename A>
     78 struct type_equals_<A, A> : public true_ {
     79 };
     80 
     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)> {
     85 };
     86 
     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)> {
     91 };
     92 
     93 
     94 } // Close namespace base
     95 
     96 #endif  // BASE_TEMPLATE_UTIL_H_
     97