template_util.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // Author: lar@google.com (Laramie Leavitt)
  31. //
  32. // Template metaprogramming utility functions.
  33. //
  34. // This code is compiled directly on many platforms, including client
  35. // platforms like Windows, Mac, and embedded systems. Before making
  36. // any changes here, make sure that you're not breaking any platforms.
  37. //
  38. //
  39. // The names chosen here reflect those used in tr1 and the boost::mpl
  40. // library, there are similar operations used in the Loki library as
  41. // well. I prefer the boost names for 2 reasons:
  42. // 1. I think that portions of the Boost libraries are more likely to
  43. // be included in the c++ standard.
  44. // 2. It is not impossible that some of the boost libraries will be
  45. // included in our own build in the future.
  46. // Both of these outcomes means that we may be able to directly replace
  47. // some of these with boost equivalents.
  48. //
  49. #ifndef GOOGLE_PROTOBUF_TEMPLATE_UTIL_H_
  50. #define GOOGLE_PROTOBUF_TEMPLATE_UTIL_H_
  51. namespace google {
  52. namespace protobuf {
  53. namespace internal {
  54. // Types small_ and big_ are guaranteed such that sizeof(small_) <
  55. // sizeof(big_)
  56. typedef char small_;
  57. struct big_ {
  58. char dummy[2];
  59. };
  60. // Identity metafunction.
  61. template <class T>
  62. struct identity_ {
  63. typedef T type;
  64. };
  65. // integral_constant, defined in tr1, is a wrapper for an integer
  66. // value. We don't really need this generality; we could get away
  67. // with hardcoding the integer type to bool. We use the fully
  68. // general integer_constant for compatibility with tr1.
  69. template<class T, T v>
  70. struct integral_constant {
  71. static const T value = v;
  72. typedef T value_type;
  73. typedef integral_constant<T, v> type;
  74. };
  75. template <class T, T v> const T integral_constant<T, v>::value;
  76. // Abbreviations: true_type and false_type are structs that represent boolean
  77. // true and false values. Also define the boost::mpl versions of those names,
  78. // true_ and false_.
  79. typedef integral_constant<bool, true> true_type;
  80. typedef integral_constant<bool, false> false_type;
  81. typedef true_type true_;
  82. typedef false_type false_;
  83. // if_ is a templatized conditional statement.
  84. // if_<cond, A, B> is a compile time evaluation of cond.
  85. // if_<>::type contains A if cond is true, B otherwise.
  86. template<bool cond, typename A, typename B>
  87. struct if_{
  88. typedef A type;
  89. };
  90. template<typename A, typename B>
  91. struct if_<false, A, B> {
  92. typedef B type;
  93. };
  94. // type_equals_ is a template type comparator, similar to Loki IsSameType.
  95. // type_equals_<A, B>::value is true iff "A" is the same type as "B".
  96. //
  97. // New code should prefer base::is_same, defined in base/type_traits.h.
  98. // It is functionally identical, but is_same is the standard spelling.
  99. template<typename A, typename B>
  100. struct type_equals_ : public false_ {
  101. };
  102. template<typename A>
  103. struct type_equals_<A, A> : public true_ {
  104. };
  105. // and_ is a template && operator.
  106. // and_<A, B>::value evaluates "A::value && B::value".
  107. template<typename A, typename B>
  108. struct and_ : public integral_constant<bool, (A::value && B::value)> {
  109. };
  110. // or_ is a template || operator.
  111. // or_<A, B>::value evaluates "A::value || B::value".
  112. template<typename A, typename B>
  113. struct or_ : public integral_constant<bool, (A::value || B::value)> {
  114. };
  115. } // namespace internal
  116. } // namespace protobuf
  117. } // namespace google
  118. #endif // GOOGLE_PROTOBUF_TEMPLATE_UTIL_H_