You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

template_util.h 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. {
  53. namespace protobuf
  54. {
  55. namespace internal
  56. {
  57. // Types small_ and big_ are guaranteed such that sizeof(small_) <
  58. // sizeof(big_)
  59. typedef char small_;
  60. struct big_
  61. {
  62. char dummy[2];
  63. };
  64. // Identity metafunction.
  65. template<class T>
  66. struct identity_
  67. {
  68. typedef T type;
  69. };
  70. // integral_constant, defined in tr1, is a wrapper for an integer
  71. // value. We don't really need this generality; we could get away
  72. // with hardcoding the integer type to bool. We use the fully
  73. // general integer_constant for compatibility with tr1.
  74. template<class T, T v>
  75. struct integral_constant
  76. {
  77. static const T value = v;
  78. typedef T value_type;
  79. typedef integral_constant<T, v> type;
  80. };
  81. template<class T, T v>
  82. const T integral_constant<T, v>::value;
  83. // Abbreviations: true_type and false_type are structs that represent boolean
  84. // true and false values. Also define the boost::mpl versions of those names,
  85. // true_ and false_.
  86. typedef integral_constant<bool, true> true_type;
  87. typedef integral_constant<bool, false> false_type;
  88. typedef true_type true_;
  89. typedef false_type false_;
  90. // if_ is a templatized conditional statement.
  91. // if_<cond, A, B> is a compile time evaluation of cond.
  92. // if_<>::type contains A if cond is true, B otherwise.
  93. template<bool cond, typename A, typename B>
  94. struct if_
  95. {
  96. typedef A type;
  97. };
  98. template<typename A, typename B>
  99. struct if_<false, A, B>
  100. {
  101. typedef B type;
  102. };
  103. // type_equals_ is a template type comparator, similar to Loki IsSameType.
  104. // type_equals_<A, B>::value is true iff "A" is the same type as "B".
  105. //
  106. // New code should prefer base::is_same, defined in base/type_traits.h.
  107. // It is functionally identical, but is_same is the standard spelling.
  108. template<typename A, typename B>
  109. struct type_equals_ : public false_
  110. {
  111. };
  112. template<typename A>
  113. struct type_equals_<A, A> : public true_
  114. {
  115. };
  116. // and_ is a template && operator.
  117. // and_<A, B>::value evaluates "A::value && B::value".
  118. template<typename A, typename B>
  119. struct and_ : public integral_constant<bool, (A::value && B::value)>
  120. {
  121. };
  122. // or_ is a template || operator.
  123. // or_<A, B>::value evaluates "A::value || B::value".
  124. template<typename A, typename B>
  125. struct or_ : public integral_constant<bool, (A::value || B::value)>
  126. {
  127. };
  128. } // namespace internal
  129. } // namespace protobuf
  130. } // namespace google
  131. #endif // GOOGLE_PROTOBUF_TEMPLATE_UTIL_H_