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.

macros.h 4.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. namespace google
  33. {
  34. namespace protobuf
  35. {
  36. #undef GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
  37. #define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName) \
  38. TypeName(const TypeName&) = delete; \
  39. void operator=(const TypeName&) = delete
  40. #undef GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS
  41. #define GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
  42. TypeName() = delete; \
  43. TypeName(const TypeName&) = delete; \
  44. void operator=(const TypeName&) = delete
  45. // ===================================================================
  46. // from google3/base/basictypes.h
  47. // The GOOGLE_ARRAYSIZE(arr) macro returns the # of elements in an array arr.
  48. // The expression is a compile-time constant, and therefore can be
  49. // used in defining new arrays, for example.
  50. //
  51. // GOOGLE_ARRAYSIZE catches a few type errors. If you see a compiler error
  52. //
  53. // "warning: division by zero in ..."
  54. //
  55. // when using GOOGLE_ARRAYSIZE, you are (wrongfully) giving it a pointer.
  56. // You should only use GOOGLE_ARRAYSIZE on statically allocated arrays.
  57. //
  58. // The following comments are on the implementation details, and can
  59. // be ignored by the users.
  60. //
  61. // ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in
  62. // the array) and sizeof(*(arr)) (the # of bytes in one array
  63. // element). If the former is divisible by the latter, perhaps arr is
  64. // indeed an array, in which case the division result is the # of
  65. // elements in the array. Otherwise, arr cannot possibly be an array,
  66. // and we generate a compiler error to prevent the code from
  67. // compiling.
  68. //
  69. // Since the size of bool is implementation-defined, we need to cast
  70. // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
  71. // result has type size_t.
  72. //
  73. // This macro is not perfect as it wrongfully accepts certain
  74. // pointers, namely where the pointer size is divisible by the pointee
  75. // size. Since all our code has to go through a 32-bit compiler,
  76. // where a pointer is 4 bytes, this means all pointers to a type whose
  77. // size is 3 or greater than 4 will be (righteously) rejected.
  78. //
  79. // Kudos to Jorg Brown for this simple and elegant implementation.
  80. #undef GOOGLE_ARRAYSIZE
  81. #define GOOGLE_ARRAYSIZE(a) \
  82. ((sizeof(a) / sizeof(*(a))) / \
  83. static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
  84. } // namespace protobuf
  85. } // namespace google
  86. #endif // GOOGLE_PROTOBUF_MACROS_H__