Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
5.4 KiB
145 lines
5.4 KiB
1 year ago
|
// Protocol Buffers - Google's data interchange format
|
||
|
// Copyright 2023 Google LLC. All rights reserved.
|
||
|
// https://developers.google.com/protocol-buffers/
|
||
2 years ago
|
//
|
||
|
// Redistribution and use in source and binary forms, with or without
|
||
1 year ago
|
// modification, are permitted provided that the following conditions are
|
||
|
// met:
|
||
|
//
|
||
2 years ago
|
// * Redistributions of source code must retain the above copyright
|
||
1 year ago
|
// notice, this list of conditions and the following disclaimer.
|
||
|
// * Redistributions in binary form must reproduce the above
|
||
|
// copyright notice, this list of conditions and the following disclaimer
|
||
|
// in the documentation and/or other materials provided with the
|
||
|
// distribution.
|
||
|
// * Neither the name of Google LLC nor the names of its
|
||
|
// contributors may be used to endorse or promote products derived from
|
||
|
// this software without specific prior written permission.
|
||
2 years ago
|
//
|
||
1 year ago
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
2 years ago
|
|
||
|
#include "protos_generator/gen_enums.h"
|
||
|
|
||
2 years ago
|
#include <algorithm>
|
||
|
#include <limits>
|
||
|
#include <string>
|
||
|
#include <vector>
|
||
|
|
||
2 years ago
|
#include "google/protobuf/descriptor.pb.h"
|
||
|
#include "google/protobuf/descriptor.h"
|
||
2 years ago
|
#include "protos_generator/gen_utils.h"
|
||
2 years ago
|
#include "protos_generator/names.h"
|
||
2 years ago
|
|
||
|
namespace protos_generator {
|
||
|
|
||
|
namespace protobuf = ::google::protobuf;
|
||
|
|
||
|
// Convert enum value to C++ literal.
|
||
|
//
|
||
|
// In C++, an value of -2147483648 gets interpreted as the negative of
|
||
|
// 2147483648, and since 2147483648 can't fit in an integer, this produces a
|
||
|
// compiler warning. This works around that issue.
|
||
|
std::string EnumInt32ToString(int number) {
|
||
|
if (number == std::numeric_limits<int32_t>::min()) {
|
||
|
// This needs to be special-cased, see explanation here:
|
||
|
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661
|
||
|
return absl::StrCat(number + 1, " - 1");
|
||
|
} else {
|
||
|
return absl::StrCat(number);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
std::string EnumTypeName(const protobuf::EnumDescriptor* enum_descriptor) {
|
||
|
auto containing_type = enum_descriptor->containing_type();
|
||
|
if (containing_type == nullptr) {
|
||
|
// enums types with no package name are prefixed with protos_ to prevent
|
||
|
// conflicts with generated C headers.
|
||
|
if (enum_descriptor->file()->package().empty()) {
|
||
2 years ago
|
return absl::StrCat(kNoPackageNamePrefix,
|
||
|
ToCIdent(enum_descriptor->name()));
|
||
2 years ago
|
}
|
||
|
return ToCIdent(enum_descriptor->name());
|
||
|
} else {
|
||
2 years ago
|
// Since the enum is in global name space (no package), it will have the
|
||
|
// same classified name as the C header include, to prevent collision
|
||
|
// rename as above.
|
||
|
if (containing_type->file()->package().empty()) {
|
||
|
return ToCIdent(absl::StrCat(containing_type->name(), "_",
|
||
|
kNoPackageNamePrefix,
|
||
|
enum_descriptor->name()));
|
||
|
} else {
|
||
|
return ToCIdent(
|
||
|
absl::StrCat(containing_type->name(), "_", enum_descriptor->name()));
|
||
|
}
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
|
std::string EnumValueSymbolInNameSpace(
|
||
|
const protobuf::EnumDescriptor* desc,
|
||
|
const protobuf::EnumValueDescriptor* value) {
|
||
|
auto containing_type = desc->containing_type();
|
||
|
if (containing_type != nullptr) {
|
||
|
return ToCIdent(absl::StrCat(containing_type->name(), "_", desc->name(),
|
||
|
"_", value->name()));
|
||
|
} else {
|
||
|
// protos enum values with no package name are prefixed with protos_ to
|
||
|
// prevent conflicts with generated C headers.
|
||
|
if (desc->file()->package().empty()) {
|
||
2 years ago
|
return absl::StrCat(kNoPackageNamePrefix, ToCIdent(value->name()));
|
||
2 years ago
|
}
|
||
|
return ToCIdent(value->name());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void WriteEnumValues(const protobuf::EnumDescriptor* desc, Output& output) {
|
||
|
std::vector<const protobuf::EnumValueDescriptor*> values;
|
||
|
auto value_count = desc->value_count();
|
||
|
values.reserve(value_count);
|
||
|
for (int i = 0; i < value_count; i++) {
|
||
|
values.push_back(desc->value(i));
|
||
|
}
|
||
|
std::sort(values.begin(), values.end(),
|
||
|
[](const protobuf::EnumValueDescriptor* a,
|
||
|
const protobuf::EnumValueDescriptor* b) {
|
||
|
return a->number() < b->number();
|
||
|
});
|
||
|
|
||
|
for (size_t i = 0; i < values.size(); i++) {
|
||
|
auto value = values[i];
|
||
|
output(" $0", EnumValueSymbolInNameSpace(desc, value));
|
||
|
output(" = $0", EnumInt32ToString(value->number()));
|
||
|
if (i != values.size() - 1) {
|
||
|
output(",");
|
||
|
}
|
||
|
output("\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void WriteEnumDeclarations(
|
||
|
const std::vector<const protobuf::EnumDescriptor*>& enums, Output& output) {
|
||
|
for (auto enumdesc : enums) {
|
||
|
output("enum $0 : int {\n", EnumTypeName(enumdesc));
|
||
|
WriteEnumValues(enumdesc, output);
|
||
|
output("};\n\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void WriteHeaderEnumForwardDecls(
|
||
|
std::vector<const protobuf::EnumDescriptor*>& enums, Output& output) {
|
||
|
for (const auto* enumdesc : enums) {
|
||
|
output("enum $0 : int;\n", EnumTypeName(enumdesc));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
} // namespace protos_generator
|