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_JAVA_ENUM_H__ 13 #define GOOGLE_PROTOBUF_COMPILER_JAVA_ENUM_H__ 14 15 #include <string> 16 #include <vector> 17 18 #include "google/protobuf/compiler/java/generator_factory.h" 19 #include "google/protobuf/descriptor.h" 20 21 namespace google { 22 namespace protobuf { 23 namespace compiler { 24 namespace java { 25 class Context; // context.h 26 class ClassNameResolver; // name_resolver.h 27 } // namespace java 28 } // namespace compiler 29 namespace io { 30 class Printer; // printer.h 31 } 32 } // namespace protobuf 33 } // namespace google 34 35 namespace google { 36 namespace protobuf { 37 namespace compiler { 38 namespace java { 39 40 // This class is in the immutable/ directory, but since the mutable API for 41 // enums is nearly identical to the immutable one, we also use this for mutable 42 // enums. It is used for all enums except lite enums. 43 class EnumNonLiteGenerator : public EnumGenerator { 44 public: 45 EnumNonLiteGenerator(const EnumDescriptor* descriptor, bool immutable_api, 46 Context* context); 47 EnumNonLiteGenerator(const EnumNonLiteGenerator&) = delete; 48 EnumNonLiteGenerator& operator=(const EnumNonLiteGenerator&) = delete; 49 50 void Generate(io::Printer* printer) override; 51 52 private: 53 const EnumDescriptor* descriptor_; 54 55 // The proto language allows multiple enum constants to have the same 56 // numeric value. Java, however, does not allow multiple enum constants to 57 // be considered equivalent. We treat the first defined constant for any 58 // given numeric value as "canonical" and the rest as aliases of that 59 // canonical value. 60 std::vector<const EnumValueDescriptor*> canonical_values_; 61 62 struct Alias { 63 const EnumValueDescriptor* value; 64 const EnumValueDescriptor* canonical_value; 65 }; 66 std::vector<Alias> aliases_; 67 68 bool immutable_api_; 69 70 Context* context_; 71 ClassNameResolver* name_resolver_; 72 73 bool CanUseEnumValues(); 74 }; 75 76 } // namespace java 77 } // namespace compiler 78 } // namespace protobuf 79 } // namespace google 80 81 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_ENUM_H__ 82