• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 // Author: jasonh@google.com (Jason Hsueh)
9 //
10 // This header is logically internal, but is made public because it is used
11 // from protocol-compiler-generated code, which may reside in other components.
12 // It provides reflection support for generated enums, and is included in
13 // generated .pb.h files and should have minimal dependencies. The methods are
14 // implemented in generated_message_reflection.cc.
15 
16 #ifndef GOOGLE_PROTOBUF_GENERATED_ENUM_REFLECTION_H__
17 #define GOOGLE_PROTOBUF_GENERATED_ENUM_REFLECTION_H__
18 
19 #include <string>
20 
21 #include "absl/strings/string_view.h"
22 #include "google/protobuf/generated_enum_util.h"
23 #include "google/protobuf/port.h"
24 
25 #ifdef SWIG
26 #error "You cannot SWIG proto headers"
27 #endif
28 
29 // Must be included last.
30 #include "google/protobuf/port_def.inc"
31 
32 namespace google {
33 namespace protobuf {
34 class EnumDescriptor;
35 }  // namespace protobuf
36 }  // namespace google
37 
38 namespace google {
39 namespace protobuf {
40 
41 // Returns the EnumDescriptor for enum type E, which must be a
42 // proto-declared enum type.  Code generated by the protocol compiler
43 // will include specializations of this template for each enum type declared.
44 template <typename E>
45 const EnumDescriptor* GetEnumDescriptor();
46 
47 namespace internal {
48 
49 // Helper for EnumType_Parse functions: try to parse the string 'name' as
50 // an enum name of the given type, returning true and filling in value on
51 // success, or returning false and leaving value unchanged on failure.
52 PROTOBUF_EXPORT bool ParseNamedEnum(const EnumDescriptor* descriptor,
53                                     absl::string_view name, int* value);
54 
55 template <typename EnumType>
ParseNamedEnum(const EnumDescriptor * descriptor,absl::string_view name,EnumType * value)56 bool ParseNamedEnum(const EnumDescriptor* descriptor, absl::string_view name,
57                     EnumType* value) {
58   int tmp;
59   if (!ParseNamedEnum(descriptor, name, &tmp)) return false;
60   *value = static_cast<EnumType>(tmp);
61   return true;
62 }
63 
64 // Just a wrapper around printing the name of a value. The main point of this
65 // function is not to be inlined, so that you can do this without including
66 // descriptor.h.
67 PROTOBUF_EXPORT const std::string& NameOfEnum(const EnumDescriptor* descriptor,
68                                               int value);
69 
70 }  // namespace internal
71 }  // namespace protobuf
72 }  // namespace google
73 
74 #include "google/protobuf/port_undef.inc"
75 
76 #endif  // GOOGLE_PROTOBUF_GENERATED_ENUM_REFLECTION_H__
77