• 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: kenton@google.com (Kenton Varda)
9 //  Based on original Protocol Buffers design by
10 //  Sanjay Ghemawat, Jeff Dean, and others.
11 
12 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_ENUM_H__
13 #define GOOGLE_PROTOBUF_COMPILER_CPP_ENUM_H__
14 
15 #include <string>
16 
17 #include "google/protobuf/compiler/cpp/options.h"
18 #include "google/protobuf/descriptor.h"
19 #include "google/protobuf/io/printer.h"
20 
21 namespace google {
22 namespace protobuf {
23 namespace compiler {
24 namespace cpp {
25 class EnumGenerator {
26  public:
27   EnumGenerator(const EnumDescriptor* descriptor, const Options& options);
28 
29   EnumGenerator(const EnumGenerator&) = delete;
30   EnumGenerator& operator=(const EnumGenerator&) = delete;
31 
32   ~EnumGenerator() = default;
33 
34   // Generate header code defining the enum.  This code should be placed
35   // within the enum's package namespace, but NOT within any class, even for
36   // nested enums.
37   void GenerateDefinition(io::Printer* p);
38 
39   // Generate specialization of GetEnumDescriptor<MyEnum>().
40   // Precondition: in ::google::protobuf namespace.
41   void GenerateGetEnumDescriptorSpecializations(io::Printer* p);
42 
43 
44   // For enums nested within a message, generate code to import all the enum's
45   // symbols (e.g. the enum type name, all its values, etc.) into the class's
46   // namespace.  This should be placed inside the class definition in the
47   // header.
48   void GenerateSymbolImports(io::Printer* p) const;
49 
50   // Source file stuff.
51 
52   // Generate non-inline methods related to the enum, such as IsValidValue().
53   // Goes in the .cc file. EnumDescriptors are stored in an array, idx is
54   // the index in this array that corresponds with this enum.
55   void GenerateMethods(int idx, io::Printer* p);
56 
57  private:
58   friend class FileGenerator;
59 
60   struct ValueLimits {
61     const EnumValueDescriptor* min;
62     const EnumValueDescriptor* max;
63 
64     static ValueLimits FromEnum(const EnumDescriptor* descriptor);
65   };
66 
67   const EnumDescriptor* enum_;
68   Options options_;
69 
70   bool generate_array_size_;
71   bool should_cache_;
72   bool has_reflection_;
73   ValueLimits limits_;
74 };
75 
76 }  // namespace cpp
77 }  // namespace compiler
78 }  // namespace protobuf
79 }  // namespace google
80 
81 #endif  // GOOGLE_PROTOBUF_COMPILER_CPP_ENUM_H__
82