generated_enum_util.cc
Go to the documentation of this file.
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 
32 
33 #include <algorithm>
34 
36 
37 namespace google {
38 namespace protobuf {
39 namespace internal {
40 namespace {
41 
42 bool EnumCompareByName(const EnumEntry& a, const EnumEntry& b) {
43  return StringPiece(a.name) < StringPiece(b.name);
44 }
45 
46 // Gets the numeric value of the EnumEntry at the given index, but returns a
47 // special value for the index -1. This gives a way to use std::lower_bound on a
48 // sorted array of indices while searching for value that we associate with -1.
49 int GetValue(const EnumEntry* enums, int i, int target) {
50  if (i == -1) {
51  return target;
52  } else {
53  return enums[i].value;
54  }
55 }
56 
57 } // namespace
58 
59 bool LookUpEnumValue(const EnumEntry* enums, size_t size,
60  StringPiece name, int* value) {
61  EnumEntry target{name, 0};
62  auto it = std::lower_bound(enums, enums + size, target, EnumCompareByName);
63  if (it != enums + size && it->name == name) {
64  *value = it->value;
65  return true;
66  }
67  return false;
68 }
69 
70 int LookUpEnumName(const EnumEntry* enums, const int* sorted_indices,
71  size_t size, int value) {
72  auto comparator = [enums, value](int a, int b) {
73  return GetValue(enums, a, value) < GetValue(enums, b, value);
74  };
75  auto it =
76  std::lower_bound(sorted_indices, sorted_indices + size, -1, comparator);
77  if (it != sorted_indices + size && enums[*it].value == value) {
78  return it - sorted_indices;
79  }
80  return -1;
81 }
82 
84  const EnumEntry* enums, const int* sorted_indices, size_t size,
86  for (int i = 0; i < size; ++i) {
87  enum_strings[i].Construct(enums[sorted_indices[i]].name);
88  internal::OnShutdownDestroyString(enum_strings[i].get_mutable());
89  }
90  return true;
91 }
92 
93 } // namespace internal
94 } // namespace protobuf
95 } // namespace google
enums
static const upb_enumdef enums[5]
Definition: ruby/ext/google/protobuf_c/upb.c:7672
google::protobuf.internal::LookUpEnumName
int LookUpEnumName(const EnumEntry *enums, const int *sorted_indices, size_t size, int value)
Definition: generated_enum_util.cc:70
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
google::protobuf::value
const Descriptor::ReservedRange value
Definition: src/google/protobuf/descriptor.h:1954
target
GLenum target
Definition: glcorearb.h:3739
b
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:3228
google::protobuf::StringPiece
Definition: stringpiece.h:180
size
#define size
Definition: glcorearb.h:2944
google::protobuf.internal::ExplicitlyConstructed::Construct
void Construct(Args &&... args)
Definition: message_lite.h:128
google::protobuf.internal::OnShutdownDestroyString
void OnShutdownDestroyString(const std::string *ptr)
Definition: generated_message_util.h:230
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf.internal::InitializeEnumStrings
bool InitializeEnumStrings(const EnumEntry *enums, const int *sorted_indices, size_t size, internal::ExplicitlyConstructed< std::string > *enum_strings)
Definition: generated_enum_util.cc:83
google::protobuf.internal::LookUpEnumValue
bool LookUpEnumValue(const EnumEntry *enums, size_t size, StringPiece name, int *value)
Definition: generated_enum_util.cc:59
size
GLsizeiptr size
Definition: glcorearb.h:2943
generated_message_util.h
generated_enum_util.h
internal
Definition: any.pb.h:40
google::protobuf.internal::ExplicitlyConstructed
Definition: message_lite.h:123
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf.internal::EnumEntry
Definition: generated_enum_util.h:58
a
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:3228
it
MapIter it
Definition: php/ext/google/protobuf/map.c:205
google
Definition: data_proto2_to_proto3_util.h:11


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:51