has_bits.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_HAS_BITS_H__
  31. #define GOOGLE_PROTOBUF_HAS_BITS_H__
  32. #include <google/protobuf/stubs/common.h>
  33. #include <google/protobuf/port.h>
  34. #include <google/protobuf/port_def.inc>
  35. #ifdef SWIG
  36. #error "You cannot SWIG proto headers"
  37. #endif
  38. namespace google {
  39. namespace protobuf {
  40. namespace internal {
  41. template <size_t doublewords>
  42. class HasBits {
  43. public:
  44. PROTOBUF_NDEBUG_INLINE constexpr HasBits() : has_bits_{} {}
  45. PROTOBUF_NDEBUG_INLINE void Clear() {
  46. memset(has_bits_, 0, sizeof(has_bits_));
  47. }
  48. PROTOBUF_NDEBUG_INLINE uint32& operator[](int index) {
  49. return has_bits_[index];
  50. }
  51. PROTOBUF_NDEBUG_INLINE const uint32& operator[](int index) const {
  52. return has_bits_[index];
  53. }
  54. bool operator==(const HasBits<doublewords>& rhs) const {
  55. return memcmp(has_bits_, rhs.has_bits_, sizeof(has_bits_)) == 0;
  56. }
  57. bool operator!=(const HasBits<doublewords>& rhs) const {
  58. return !(*this == rhs);
  59. }
  60. void Or(const HasBits<doublewords>& rhs) {
  61. for (size_t i = 0; i < doublewords; i++) has_bits_[i] |= rhs[i];
  62. }
  63. bool empty() const;
  64. private:
  65. uint32 has_bits_[doublewords];
  66. };
  67. template <>
  68. inline bool HasBits<1>::empty() const {
  69. return !has_bits_[0];
  70. }
  71. template <>
  72. inline bool HasBits<2>::empty() const {
  73. return !(has_bits_[0] | has_bits_[1]);
  74. }
  75. template <>
  76. inline bool HasBits<3>::empty() const {
  77. return !(has_bits_[0] | has_bits_[1] | has_bits_[2]);
  78. }
  79. template <>
  80. inline bool HasBits<4>::empty() const {
  81. return !(has_bits_[0] | has_bits_[1] | has_bits_[2] | has_bits_[3]);
  82. }
  83. template <size_t doublewords>
  84. inline bool HasBits<doublewords>::empty() const {
  85. for (size_t i = 0; i < doublewords; ++i) {
  86. if (has_bits_[i]) return false;
  87. }
  88. return true;
  89. }
  90. } // namespace internal
  91. } // namespace protobuf
  92. } // namespace google
  93. #include <google/protobuf/port_undef.inc>
  94. #endif // GOOGLE_PROTOBUF_HAS_BITS_H__