macros.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #ifndef GOOGLE_PROTOBUF_MACROS_H__
  31. #define GOOGLE_PROTOBUF_MACROS_H__
  32. #include <google/protobuf/stubs/port.h>
  33. namespace google {
  34. namespace protobuf {
  35. #undef GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
  36. #define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName) \
  37. TypeName(const TypeName&); \
  38. void operator=(const TypeName&)
  39. #undef GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS
  40. #define GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
  41. TypeName(); \
  42. TypeName(const TypeName&); \
  43. void operator=(const TypeName&)
  44. // ===================================================================
  45. // from google3/base/basictypes.h
  46. // The GOOGLE_ARRAYSIZE(arr) macro returns the # of elements in an array arr.
  47. // The expression is a compile-time constant, and therefore can be
  48. // used in defining new arrays, for example.
  49. //
  50. // GOOGLE_ARRAYSIZE catches a few type errors. If you see a compiler error
  51. //
  52. // "warning: division by zero in ..."
  53. //
  54. // when using GOOGLE_ARRAYSIZE, you are (wrongfully) giving it a pointer.
  55. // You should only use GOOGLE_ARRAYSIZE on statically allocated arrays.
  56. //
  57. // The following comments are on the implementation details, and can
  58. // be ignored by the users.
  59. //
  60. // ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in
  61. // the array) and sizeof(*(arr)) (the # of bytes in one array
  62. // element). If the former is divisible by the latter, perhaps arr is
  63. // indeed an array, in which case the division result is the # of
  64. // elements in the array. Otherwise, arr cannot possibly be an array,
  65. // and we generate a compiler error to prevent the code from
  66. // compiling.
  67. //
  68. // Since the size of bool is implementation-defined, we need to cast
  69. // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
  70. // result has type size_t.
  71. //
  72. // This macro is not perfect as it wrongfully accepts certain
  73. // pointers, namely where the pointer size is divisible by the pointee
  74. // size. Since all our code has to go through a 32-bit compiler,
  75. // where a pointer is 4 bytes, this means all pointers to a type whose
  76. // size is 3 or greater than 4 will be (righteously) rejected.
  77. //
  78. // Kudos to Jorg Brown for this simple and elegant implementation.
  79. #undef GOOGLE_ARRAYSIZE
  80. #define GOOGLE_ARRAYSIZE(a) \
  81. ((sizeof(a) / sizeof(*(a))) / \
  82. static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
  83. // The COMPILE_ASSERT macro can be used to verify that a compile time
  84. // expression is true. For example, you could use it to verify the
  85. // size of a static array:
  86. //
  87. // COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
  88. // content_type_names_incorrect_size);
  89. //
  90. // or to make sure a struct is smaller than a certain size:
  91. //
  92. // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
  93. //
  94. // The second argument to the macro is the name of the variable. If
  95. // the expression is false, most compilers will issue a warning/error
  96. // containing the name of the variable.
  97. namespace internal {
  98. template <bool>
  99. struct CompileAssert {
  100. };
  101. } // namespace internal
  102. #undef GOOGLE_COMPILE_ASSERT
  103. #if __cplusplus >= 201103L
  104. #define GOOGLE_COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
  105. #else
  106. #define GOOGLE_COMPILE_ASSERT(expr, msg) \
  107. ::google::protobuf::internal::CompileAssert<(bool(expr))> \
  108. msg[bool(expr) ? 1 : -1]; \
  109. (void)msg
  110. // Implementation details of COMPILE_ASSERT:
  111. //
  112. // - COMPILE_ASSERT works by defining an array type that has -1
  113. // elements (and thus is invalid) when the expression is false.
  114. //
  115. // - The simpler definition
  116. //
  117. // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
  118. //
  119. // does not work, as gcc supports variable-length arrays whose sizes
  120. // are determined at run-time (this is gcc's extension and not part
  121. // of the C++ standard). As a result, gcc fails to reject the
  122. // following code with the simple definition:
  123. //
  124. // int foo;
  125. // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
  126. // // not a compile-time constant.
  127. //
  128. // - By using the type CompileAssert<(bool(expr))>, we ensures that
  129. // expr is a compile-time constant. (Template arguments must be
  130. // determined at compile-time.)
  131. //
  132. // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
  133. // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
  134. //
  135. // CompileAssert<bool(expr)>
  136. //
  137. // instead, these compilers will refuse to compile
  138. //
  139. // COMPILE_ASSERT(5 > 0, some_message);
  140. //
  141. // (They seem to think the ">" in "5 > 0" marks the end of the
  142. // template argument list.)
  143. //
  144. // - The array size is (bool(expr) ? 1 : -1), instead of simply
  145. //
  146. // ((expr) ? 1 : -1).
  147. //
  148. // This is to avoid running into a bug in MS VC 7.1, which
  149. // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
  150. #endif // __cplusplus >= 201103L
  151. } // namespace protobuf
  152. } // namespace google
  153. #endif // GOOGLE_PROTOBUF_MACROS_H__