File indexing completed on 2025-01-31 10:12:21
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef GOOGLE_PROTOBUF_HAS_BITS_H__
0009 #define GOOGLE_PROTOBUF_HAS_BITS_H__
0010
0011 #include <cassert>
0012 #include <cstddef>
0013 #include <cstdint>
0014 #include <cstring>
0015 #include <initializer_list>
0016
0017
0018 #include "google/protobuf/port_def.inc"
0019
0020 #ifdef SWIG
0021 #error "You cannot SWIG proto headers"
0022 #endif
0023
0024 namespace google {
0025 namespace protobuf {
0026 namespace internal {
0027
0028 template <int doublewords>
0029 class HasBits {
0030 public:
0031 PROTOBUF_NDEBUG_INLINE constexpr HasBits() : has_bits_{} {}
0032
0033 constexpr HasBits(std::initializer_list<uint32_t> has_bits) : has_bits_{} {
0034 Copy(has_bits_, &*has_bits.begin(), has_bits.size());
0035 }
0036
0037 PROTOBUF_NDEBUG_INLINE void Clear() {
0038 memset(has_bits_, 0, sizeof(has_bits_));
0039 }
0040
0041 PROTOBUF_NDEBUG_INLINE uint32_t& operator[](int index) {
0042 return has_bits_[index];
0043 }
0044
0045 PROTOBUF_NDEBUG_INLINE const uint32_t& operator[](int index) const {
0046 return has_bits_[index];
0047 }
0048
0049 bool operator==(const HasBits<doublewords>& rhs) const {
0050 return memcmp(has_bits_, rhs.has_bits_, sizeof(has_bits_)) == 0;
0051 }
0052
0053 bool operator!=(const HasBits<doublewords>& rhs) const {
0054 return !(*this == rhs);
0055 }
0056
0057 void Or(const HasBits<doublewords>& rhs) {
0058 for (int i = 0; (i + 1) < doublewords; i += 2) {
0059 Write64B(Read64B(i) | rhs.Read64B(i), i);
0060 }
0061 if ((doublewords % 2) != 0) {
0062 has_bits_[doublewords - 1] |= rhs.has_bits_[doublewords - 1];
0063 }
0064 }
0065
0066 bool empty() const;
0067
0068 private:
0069
0070
0071
0072
0073 static inline constexpr void Copy(uint32_t* dst, const uint32_t* src,
0074 size_t n) {
0075 assert(n <= doublewords);
0076 for (size_t ix = 0; ix < n; ++ix) {
0077 dst[ix] = src[ix];
0078 }
0079 for (size_t ix = n; ix < doublewords; ++ix) {
0080 dst[ix] = 0;
0081 }
0082 }
0083
0084 uint64_t Read64B(int index) const {
0085 uint64_t v;
0086 memcpy(&v, has_bits_ + index, sizeof(v));
0087 return v;
0088 }
0089
0090 void Write64B(uint64_t v, int index) {
0091 memcpy(has_bits_ + index, &v, sizeof(v));
0092 }
0093
0094 uint32_t has_bits_[doublewords];
0095 };
0096
0097 template <>
0098 inline bool HasBits<1>::empty() const {
0099 return !has_bits_[0];
0100 }
0101
0102 template <>
0103 inline bool HasBits<2>::empty() const {
0104 return !(has_bits_[0] | has_bits_[1]);
0105 }
0106
0107 template <>
0108 inline bool HasBits<3>::empty() const {
0109 return !(has_bits_[0] | has_bits_[1] | has_bits_[2]);
0110 }
0111
0112 template <>
0113 inline bool HasBits<4>::empty() const {
0114 return !(has_bits_[0] | has_bits_[1] | has_bits_[2] | has_bits_[3]);
0115 }
0116
0117 template <int doublewords>
0118 inline bool HasBits<doublewords>::empty() const {
0119 for (uint32_t bits : has_bits_) {
0120 if (bits) return false;
0121 }
0122 return true;
0123 }
0124
0125 }
0126 }
0127 }
0128
0129 #include "google/protobuf/port_undef.inc"
0130
0131 #endif