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: liujisi@google.com (Pherl Liu) 9 10 #include "google/protobuf/compiler/java/generator_factory.h" 11 12 #include <memory> 13 14 #include "google/protobuf/compiler/java/context.h" 15 #include "google/protobuf/compiler/java/full/enum.h" 16 #include "google/protobuf/compiler/java/full/extension.h" 17 #include "google/protobuf/compiler/java/full/message.h" 18 #include "google/protobuf/compiler/java/full/service.h" 19 #include "google/protobuf/descriptor.h" 20 21 namespace google { 22 namespace protobuf { 23 namespace compiler { 24 namespace java { 25 26 // Factory that creates generators for immutable-default messages. 27 class ImmutableGeneratorFactory : public GeneratorFactory { 28 public: ImmutableGeneratorFactory(Context * context)29 explicit ImmutableGeneratorFactory(Context* context) : context_(context) {} 30 ImmutableGeneratorFactory(const ImmutableGeneratorFactory&) = delete; 31 ImmutableGeneratorFactory& operator=(const ImmutableGeneratorFactory&) = 32 delete; 33 ~ImmutableGeneratorFactory() override = default; 34 NewMessageGenerator(const Descriptor * descriptor) const35 std::unique_ptr<MessageGenerator> NewMessageGenerator( 36 const Descriptor* descriptor) const override { 37 return std::make_unique<ImmutableMessageGenerator>(descriptor, context_); 38 } 39 NewEnumGenerator(const EnumDescriptor * descriptor) const40 std::unique_ptr<EnumGenerator> NewEnumGenerator( 41 const EnumDescriptor* descriptor) const override { 42 return std::make_unique<EnumNonLiteGenerator>(descriptor, true, context_); 43 } 44 NewExtensionGenerator(const FieldDescriptor * descriptor) const45 std::unique_ptr<ExtensionGenerator> NewExtensionGenerator( 46 const FieldDescriptor* descriptor) const override { 47 return std::make_unique<ImmutableExtensionGenerator>(descriptor, context_); 48 } 49 NewServiceGenerator(const ServiceDescriptor * descriptor) const50 std::unique_ptr<ServiceGenerator> NewServiceGenerator( 51 const ServiceDescriptor* descriptor) const override { 52 return std::make_unique<ImmutableServiceGenerator>(descriptor, context_); 53 } 54 55 private: 56 Context* context_; 57 }; 58 MakeImmutableGeneratorFactory(Context * context)59std::unique_ptr<GeneratorFactory> MakeImmutableGeneratorFactory( 60 Context* context) { 61 return std::make_unique<ImmutableGeneratorFactory>(context); 62 } 63 64 } // namespace java 65 } // namespace compiler 66 } // namespace protobuf 67 } // namespace google 68