Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:20

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef GOOGLE_PROTOBUF_GENERATED_ENUM_UTIL_H__
0009 #define GOOGLE_PROTOBUF_GENERATED_ENUM_UTIL_H__
0010 
0011 #include <cstddef>
0012 #include <cstdint>
0013 #include <string>
0014 #include <type_traits>
0015 #include <vector>
0016 
0017 #include "absl/strings/string_view.h"
0018 #include "absl/types/span.h"
0019 #include "google/protobuf/explicitly_constructed.h"
0020 #include "google/protobuf/message_lite.h"
0021 
0022 // Must be included last.
0023 #include "google/protobuf/port_def.inc"
0024 
0025 #ifdef SWIG
0026 #error "You cannot SWIG proto headers"
0027 #endif
0028 
0029 namespace google {
0030 namespace protobuf {
0031 
0032 // This type trait can be used to cause templates to only match proto2 enum
0033 // types.
0034 template <typename T>
0035 struct is_proto_enum : ::std::false_type {};
0036 
0037 namespace internal {
0038 
0039 // The table entry format for storing enum name-to-value mapping used with lite
0040 // protos. This struct and the following related functions should only be used
0041 // by protobuf generated code.
0042 struct EnumEntry {
0043   absl::string_view name;
0044   int value;
0045 };
0046 
0047 // Looks up a numeric enum value given the string name.
0048 PROTOBUF_EXPORT bool LookUpEnumValue(const EnumEntry* enums, size_t size,
0049                                      absl::string_view name, int* value);
0050 
0051 // Looks up an enum name given the numeric value.
0052 PROTOBUF_EXPORT int LookUpEnumName(const EnumEntry* enums,
0053                                    const int* sorted_indices, size_t size,
0054                                    int value);
0055 
0056 // Initializes the list of enum names in std::string form.
0057 PROTOBUF_EXPORT bool InitializeEnumStrings(
0058     const EnumEntry* enums, const int* sorted_indices, size_t size,
0059     internal::ExplicitlyConstructed<std::string>* enum_strings);
0060 
0061 // The enum validation format is split in 3 parts:
0062 //  - A dense sequence, with start+length
0063 //  - A variable size presence bitmap (in increments of 32 bits)
0064 //  - A variable size sorted int32_t set for everything else.
0065 //
0066 // The values are as follows:
0067 //
0068 // 0 - [ sequence start (int16_t) ] | [ sequence size (uint16_t) ] << 16
0069 // 1 - [ bitmap size in bits (uint16_t) ] | [ ordered size (uint16_t) ] << 16
0070 // x - [ variable length bitmap ]
0071 // y - [ variable length of int32_t values ]
0072 //
0073 // where the bitmap starts right after the end of the sequence.
0074 PROTOBUF_EXPORT bool ValidateEnum(int value, const uint32_t* data);
0075 PROTOBUF_EXPORT std::vector<uint32_t> GenerateEnumData(
0076     absl::Span<const int32_t> values);
0077 
0078 inline PROTOBUF_ALWAYS_INLINE bool ValidateEnumInlined(int value,
0079                                                        const uint32_t* data) {
0080   const int16_t min_seq = static_cast<int16_t>(data[0] & 0xFFFF);
0081   const uint16_t length_seq = static_cast<uint16_t>(data[0] >> 16);
0082   uint64_t adjusted =
0083       static_cast<uint64_t>(static_cast<int64_t>(value)) - min_seq;
0084   // Check if the value is within the sequential part.
0085   if (PROTOBUF_PREDICT_TRUE(adjusted < length_seq)) {
0086     return true;
0087   }
0088 
0089   const uint16_t length_bitmap = static_cast<uint16_t>(data[1] & 0xFFFF);
0090   adjusted -= length_seq;
0091   // Check if the value is within the bitmap.
0092   if (PROTOBUF_PREDICT_TRUE(adjusted < length_bitmap)) {
0093     return ((data[2 + (adjusted / 32)] >> (adjusted % 32)) & 1) == 1;
0094   }
0095 
0096   // Check if the value is on the ordered part.
0097   const uint16_t num_ordered = static_cast<uint16_t>(data[1] >> 16);
0098   data += 2 + length_bitmap / 32;
0099   size_t pos = 0;
0100   while (pos < num_ordered) {
0101     const int32_t sample = static_cast<int32_t>(data[pos]);
0102     if (sample == value) return true;
0103     pos = 2 * pos + (sample > value ? 1 : 2);
0104   }
0105   return false;
0106 }
0107 
0108 }  // namespace internal
0109 }  // namespace protobuf
0110 }  // namespace google
0111 
0112 #include "google/protobuf/port_undef.inc"
0113 
0114 #endif  // GOOGLE_PROTOBUF_GENERATED_ENUM_UTIL_H__