template_util.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2005 Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // ----
  30. //
  31. // Template metaprogramming utility functions.
  32. //
  33. // This code is compiled directly on many platforms, including client
  34. // platforms like Windows, Mac, and embedded systems. Before making
  35. // any changes here, make sure that you're not breaking any platforms.
  36. //
  37. //
  38. // The names choosen here reflect those used in tr1 and the boost::mpl
  39. // library, there are similar operations used in the Loki library as
  40. // well. I prefer the boost names for 2 reasons:
  41. // 1. I think that portions of the Boost libraries are more likely to
  42. // be included in the c++ standard.
  43. // 2. It is not impossible that some of the boost libraries will be
  44. // included in our own build in the future.
  45. // Both of these outcomes means that we may be able to directly replace
  46. // some of these with boost equivalents.
  47. //
  48. #ifndef BASE_TEMPLATE_UTIL_H_
  49. #define BASE_TEMPLATE_UTIL_H_
  50. #include "internal/sparseconfig.h"
  51. _START_GOOGLE_NAMESPACE_
  52. // Types small_ and big_ are guaranteed such that sizeof(small_) <
  53. // sizeof(big_)
  54. typedef char small_;
  55. struct big_ {
  56. char dummy[2];
  57. };
  58. // Identity metafunction.
  59. template <class T>
  60. struct identity_ {
  61. typedef T type;
  62. };
  63. // integral_constant, defined in tr1, is a wrapper for an integer
  64. // value. We don't really need this generality; we could get away
  65. // with hardcoding the integer type to bool. We use the fully
  66. // general integer_constant for compatibility with tr1.
  67. template<class T, T v>
  68. struct integral_constant {
  69. static const T value = v;
  70. typedef T value_type;
  71. typedef integral_constant<T, v> type;
  72. };
  73. template <class T, T v> const T integral_constant<T, v>::value;
  74. // Abbreviations: true_type and false_type are structs that represent boolean
  75. // true and false values. Also define the boost::mpl versions of those names,
  76. // true_ and false_.
  77. typedef integral_constant<bool, true> true_type;
  78. typedef integral_constant<bool, false> false_type;
  79. typedef true_type true_;
  80. typedef false_type false_;
  81. // if_ is a templatized conditional statement.
  82. // if_<cond, A, B> is a compile time evaluation of cond.
  83. // if_<>::type contains A if cond is true, B otherwise.
  84. template<bool cond, typename A, typename B>
  85. struct if_{
  86. typedef A type;
  87. };
  88. template<typename A, typename B>
  89. struct if_<false, A, B> {
  90. typedef B type;
  91. };
  92. // type_equals_ is a template type comparator, similar to Loki IsSameType.
  93. // type_equals_<A, B>::value is true iff "A" is the same type as "B".
  94. //
  95. // New code should prefer base::is_same, defined in base/type_traits.h.
  96. // It is functionally identical, but is_same is the standard spelling.
  97. template<typename A, typename B>
  98. struct type_equals_ : public false_ {
  99. };
  100. template<typename A>
  101. struct type_equals_<A, A> : public true_ {
  102. };
  103. // and_ is a template && operator.
  104. // and_<A, B>::value evaluates "A::value && B::value".
  105. template<typename A, typename B>
  106. struct and_ : public integral_constant<bool, (A::value && B::value)> {
  107. };
  108. // or_ is a template || operator.
  109. // or_<A, B>::value evaluates "A::value || B::value".
  110. template<typename A, typename B>
  111. struct or_ : public integral_constant<bool, (A::value || B::value)> {
  112. };
  113. _END_GOOGLE_NAMESPACE_
  114. #endif // BASE_TEMPLATE_UTIL_H_