Go to the documentation of this file.
35 #include <google/protobuf/compiler/cpp/cpp_enum.h>
41 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
43 #include <google/protobuf/io/printer.h>
44 #include <google/protobuf/stubs/strutil.h>
58 if (
descriptor->value(i)->number() > max_value) {
78 const std::map<std::string, std::string>& vars,
83 generate_array_size_(ShouldGenerateArraySize(
descriptor)),
105 auto format_value =
format;
111 format_value.Set(
"deprecation",
114 if (i > 0) format_value(
",\n");
115 format_value(
"${1$$prefix$$name$$}$ $deprecation$= $number$",
131 "$classname$_$prefix$INT_MIN_SENTINEL_DO_NOT_USE_ = "
132 "std::numeric_limits<$int32$>::min(),\n"
133 "$classname$_$prefix$INT_MAX_SENTINEL_DO_NOT_USE_ = "
134 "std::numeric_limits<$int32$>::max()");
141 "$dllexport_decl $bool $classname$_IsValid(int value);\n"
142 "constexpr $classname$ ${1$$prefix$$short_name$_MIN$}$ = "
144 "constexpr $classname$ ${1$$prefix$$short_name$_MAX$}$ = "
150 "constexpr int ${1$$prefix$$short_name$_ARRAYSIZE$}$ = "
151 "$prefix$$short_name$_MAX + 1;\n\n",
157 "$dllexport_decl $const ::$proto_ns$::EnumDescriptor* "
158 "$classname$_descriptor();\n");
164 format(
"const std::string& $classname$_Name($classname$ value);\n");
169 "template<typename T>\n"
170 "inline const std::string& $classname$_Name(T enum_t_value) {\n"
171 " static_assert(::std::is_same<T, $classname$>::value ||\n"
172 " ::std::is_integral<T>::value,\n"
173 " \"Incorrect type passed to function $classname$_Name.\");\n");
176 " return ::$proto_ns$::internal::NameOfEnum(\n"
177 " $classname$_descriptor(), enum_t_value);\n");
180 " return $classname$_Name(static_cast<$classname$>(enum_t_value));\n");
186 "inline bool $classname$_Parse(\n"
187 " ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, $classname$* "
190 " return ::$proto_ns$::internal::ParseNamedEnum<$classname$>(\n"
191 " $classname$_descriptor(), name, value);\n"
195 "bool $classname$_Parse(\n"
196 " ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, $classname$* "
205 "template <> struct is_proto_enum< $classtype$> : ::std::true_type "
210 "inline const EnumDescriptor* GetEnumDescriptor< $classtype$>() {\n"
211 " return $classtype$_descriptor();\n"
218 format(
"typedef $classname$ $resolved_name$;\n");
224 "$1$static constexpr $resolved_name$ ${2$$3$$}$ =\n"
225 " $classname$_$3$;\n",
231 "static inline bool $nested_name$_IsValid(int value) {\n"
232 " return $classname$_IsValid(value);\n"
234 "static constexpr $resolved_name$ ${1$$nested_name$_MIN$}$ =\n"
235 " $classname$_$nested_name$_MIN;\n"
236 "static constexpr $resolved_name$ ${1$$nested_name$_MAX$}$ =\n"
237 " $classname$_$nested_name$_MAX;\n",
241 "static constexpr int ${1$$nested_name$_ARRAYSIZE$}$ =\n"
242 " $classname$_$nested_name$_ARRAYSIZE;\n",
248 "static inline const ::$proto_ns$::EnumDescriptor*\n"
249 "$nested_name$_descriptor() {\n"
250 " return $classname$_descriptor();\n"
255 "template<typename T>\n"
256 "static inline const std::string& $nested_name$_Name(T enum_t_value) {\n"
257 " static_assert(::std::is_same<T, $resolved_name$>::value ||\n"
258 " ::std::is_integral<T>::value,\n"
259 " \"Incorrect type passed to function $nested_name$_Name.\");\n"
260 " return $classname$_Name(enum_t_value);\n"
263 "static inline bool "
264 "$nested_name$_Parse(::PROTOBUF_NAMESPACE_ID::ConstStringParam name,\n"
265 " $resolved_name$* value) {\n"
266 " return $classname$_Parse(name, value);\n"
274 "const ::$proto_ns$::EnumDescriptor* $classname$_descriptor() {\n"
275 " ::$proto_ns$::internal::AssignDescriptors(&$desc_table$);\n"
276 " return $file_level_enum_descriptors$[$1$];\n"
282 "bool $classname$_IsValid(int value) {\n"
283 " switch (value) {\n");
289 std::set<int> numbers;
292 numbers.insert(
value->number());
322 std::map<std::string, int> name_to_number;
323 std::map<int, std::string> number_to_canonical_name;
326 name_to_number.emplace(
value->name(),
value->number());
329 number_to_canonical_name.emplace(
value->number(),
value->name());
333 "static ::$proto_ns$::internal::ExplicitlyConstructed<std::string> "
334 "$classname$_strings[$1$] = {};\n\n",
341 format(
"static const char $classname$_names[] =");
342 for (
const auto& p : name_to_number) {
348 "static const ::$proto_ns$::internal::EnumEntry $classname$_entries[] "
351 std::map<int, int> number_to_index;
353 for (
const auto& p : name_to_number) {
354 format(
" { {$classname$_names + $1$, $2$}, $3$ },\n", data_index,
355 p.first.size(),
p.second);
356 if (number_to_canonical_name[
p.second] ==
p.first) {
357 number_to_index.emplace(
p.second, i);
360 data_index +=
p.first.size();
366 "static const int $classname$_entries_by_number[] = {\n");
367 for (
const auto& p : number_to_index) {
368 format(
" $1$, // $2$ -> $3$\n",
p.second,
p.first,
369 number_to_canonical_name[
p.first]);
376 "const std::string& $classname$_Name(\n"
377 " $classname$ value) {\n"
378 " static const bool dummy =\n"
379 " ::$proto_ns$::internal::InitializeEnumStrings(\n"
380 " $classname$_entries,\n"
381 " $classname$_entries_by_number,\n"
382 " $1$, $classname$_strings);\n"
384 " int idx = ::$proto_ns$::internal::LookUpEnumName(\n"
385 " $classname$_entries,\n"
386 " $classname$_entries_by_number,\n"
388 " return idx == -1 ? ::$proto_ns$::internal::GetEmptyString() :\n"
389 " $classname$_strings[idx].get();\n"
393 "bool $classname$_Parse(\n"
394 " ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, $classname$* "
398 " bool success = ::$proto_ns$::internal::LookUpEnumValue(\n"
399 " $classname$_entries, $1$, name, &int_value);\n"
401 " *value = static_cast<$classname$>(int_value);\n"
414 "#if (__cplusplus < 201703) && "
415 "(!defined(_MSC_VER) || (_MSC_VER >= 1900 && _MSC_VER < 1912))\n");
418 format(
"constexpr $classname$ $1$::$2$;\n", parent,
422 "constexpr $classname$ $1$::$nested_name$_MIN;\n"
423 "constexpr $classname$ $1$::$nested_name$_MAX;\n",
426 format(
"constexpr int $1$::$nested_name$_ARRAYSIZE;\n", parent);
430 "#endif // (__cplusplus < 201703) && "
431 "(!defined(_MSC_VER) || (_MSC_VER >= 1900 && _MSC_VER < 1912))\n");
const Descriptor::ReservedRange value
std::map< std::string, std::string > variables_
void GenerateMethods(int idx, io::Printer *printer)
GRPC_CUSTOM_PRINTER Printer
std::string QualifiedClassName(const Descriptor *d, const Options &options)
EnumGenerator(const EnumDescriptor *descriptor, const std::map< std::string, std::string > &vars, const Options &options)
void GenerateDefinition(io::Printer *printer)
std::string EnumValueName(const EnumValueDescriptor *enum_value)
const typedef MCPhysReg * iterator
const std::string classname_
const std::string & name() const
void GenerateSymbolImports(io::Printer *printer) const
const FileDescriptor * file() const
std::string ResolveKeyword(const string &name)
const EnumValueDescriptor * value(int index) const
std::string DeprecatedAttribute(const Options &options, bool deprecated)
bool HasDescriptorMethods(const FileDescriptor *file, const Options &options)
void GenerateGetEnumDescriptorSpecializations(io::Printer *printer)
std::string Int32ToString(int number)
DebugStringOptions options_
const Descriptor * containing_type() const
const bool generate_array_size_
const EnumDescriptor * descriptor_
std::array< int64_t, Size > values
std::string ClassName(const Descriptor *descriptor)
static const char descriptor[1336]
std::map< std::string, std::string > variables_
grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:04