From a761f061f4cc0e50324261cc1eb12526ae8783e3 Mon Sep 17 00:00:00 2001 From: Krishna Date: Wed, 10 Dec 2014 20:39:34 +0100 Subject: [PATCH] Source code refactoring. Extracted common functionality in cpp_helpers.cc and python/python_generator.cc --- .../protobuf/compiler/cpp/cpp_helpers.cc | 20 +++++++++---- .../compiler/python/python_generator.cc | 28 ++++++------------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/cpp_helpers.cc b/src/google/protobuf/compiler/cpp/cpp_helpers.cc index 28c4dd5407..e293b41304 100644 --- a/src/google/protobuf/compiler/cpp/cpp_helpers.cc +++ b/src/google/protobuf/compiler/cpp/cpp_helpers.cc @@ -361,18 +361,26 @@ string FilenameIdentifier(const string& filename) { } // Return the name of the AddDescriptors() function for a given file. -string GlobalAddDescriptorsName(const string& filename) { - return "protobuf_AddDesc_" + FilenameIdentifier(filename); +string GlobalAddDescriptorsName(const string& filename) +{ + return GlobalSymbolName(filename,"protobuf_AddDesc_"); } // Return the name of the AssignDescriptors() function for a given file. -string GlobalAssignDescriptorsName(const string& filename) { - return "protobuf_AssignDesc_" + FilenameIdentifier(filename); +string GlobalAssignDescriptorsName(const string& filename) +{ + return GlobalSymbolName(filename,"protobuf_AssignDesc_"); } // Return the name of the ShutdownFile() function for a given file. -string GlobalShutdownFileName(const string& filename) { - return "protobuf_ShutdownFile_" + FilenameIdentifier(filename); +string GlobalShutdownFileName(const string& filename) +{ + return GlobalSymbolName(filename,"protobuf_ShutdownFile_"); +} + +string GlobalSymbolName(const string& filename, string prefix) +{ + return prefix + FilenameIdentifier(filename); } // Return the qualified C++ name for a file level symbol. diff --git a/src/google/protobuf/compiler/python/python_generator.cc b/src/google/protobuf/compiler/python/python_generator.cc index e6faf7ef9f..508cab0dbb 100644 --- a/src/google/protobuf/compiler/python/python_generator.cc +++ b/src/google/protobuf/compiler/python/python_generator.cc @@ -580,11 +580,9 @@ void Generator::PrintServiceDescriptor( printer_->Print("])\n\n"); } -void Generator::PrintServiceClass(const ServiceDescriptor& descriptor) const { - // Print the service. - printer_->Print("$class_name$ = service_reflection.GeneratedServiceType(" - "'$class_name$', (_service.Service,), dict(\n", - "class_name", descriptor.name()); + +void Generator::PrintDescriptorKeyAndModuleName(const ServiceDescriptor& descriptor, string keyandmodule) { + printer_->Print(keyandmodule,"class_name", descriptor.name()); printer_->Indent(); printer_->Print( "$descriptor_key$ = $descriptor_name$,\n", @@ -597,22 +595,12 @@ void Generator::PrintServiceClass(const ServiceDescriptor& descriptor) const { printer_->Outdent(); } +void Generator::PrintServiceClass(const ServiceDescriptor& descriptor) const { + Generator::PrintDescriptorKeyAndModuleName(descriptor, "$class_name$_Stub = service_reflection.GeneratedServiceStubType('$class_name$_Stub', ($class_name$,), dict(\n"); +} + void Generator::PrintServiceStub(const ServiceDescriptor& descriptor) const { - // Print the service stub. - printer_->Print("$class_name$_Stub = " - "service_reflection.GeneratedServiceStubType(" - "'$class_name$_Stub', ($class_name$,), dict(\n", - "class_name", descriptor.name()); - printer_->Indent(); - printer_->Print( - "$descriptor_key$ = $descriptor_name$,\n", - "descriptor_key", kDescriptorKey, - "descriptor_name", ModuleLevelServiceDescriptorName(descriptor)); - printer_->Print( - "__module__ = '$module_name$'\n", - "module_name", ModuleName(file_->name())); - printer_->Print("))\n\n"); - printer_->Outdent(); + Generator::PrintDescriptorKeyAndModuleName(descriptor, "$class_name$ = service_reflection.GeneratedServiceType('$class_name$', (_service.Service,), dict(\n"); } // Prints statement assigning ModuleLevelDescriptorName(message_descriptor)