diff options
author | nechda <[email protected]> | 2024-08-29 23:50:27 +0300 |
---|---|---|
committer | nechda <[email protected]> | 2024-08-30 00:05:25 +0300 |
commit | e10d6638f07a82edae3ea8197b9f5c0affcc07ea (patch) | |
tree | 571c38cec05813766a1ad290c9d51ce7ace52919 /contrib/libs/protoc/src/google/protobuf/compiler/cpp/service.cc | |
parent | e79b38f2bbbf78d295d1901d2a79f898022d5224 (diff) |
Update cpp-protobuf to 22.5
Привет!\
Этот PR переключат cpp & python библиотеки protobuf на версию 22.5
Если у вас возникли проблемы после влития этого PR:
1. Если начали падать канон тесты, то проведите их переканонизацию
2. Прочитайте <https://wiki.yandex-team.ru/users/nechda/obnovlenie-cpp-protobuf-22.5/> страничку с основными изменениями
3. Если страничка в вики не помогла, то пишите в [DEVTOOLSSUPPORT](https://st.yandex-team.ru/DEVTOOLSSUPPORT)
7fecade616c20a841b9e9af7b7998bdfc8d2807d
Diffstat (limited to 'contrib/libs/protoc/src/google/protobuf/compiler/cpp/service.cc')
-rw-r--r-- | contrib/libs/protoc/src/google/protobuf/compiler/cpp/service.cc | 488 |
1 files changed, 237 insertions, 251 deletions
diff --git a/contrib/libs/protoc/src/google/protobuf/compiler/cpp/service.cc b/contrib/libs/protoc/src/google/protobuf/compiler/cpp/service.cc index fae7128a097..ae1d2b8a2da 100644 --- a/contrib/libs/protoc/src/google/protobuf/compiler/cpp/service.cc +++ b/contrib/libs/protoc/src/google/protobuf/compiler/cpp/service.cc @@ -32,293 +32,279 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include <google/protobuf/compiler/cpp/service.h> +#include "google/protobuf/compiler/cpp/service.h" -#include <google/protobuf/io/printer.h> -#include <google/protobuf/stubs/strutil.h> -#include <google/protobuf/compiler/cpp/helpers.h> +#include <string> + +#include "y_absl/strings/str_cat.h" +#include "google/protobuf/compiler/cpp/helpers.h" +#include "google/protobuf/io/printer.h" namespace google { namespace protobuf { namespace compiler { namespace cpp { - -namespace { - -void InitMethodVariables(const MethodDescriptor* method, const Options& options, - Formatter* format) { - format->Set("name", method->name()); - format->Set("input_type", QualifiedClassName(method->input_type(), options)); - format->Set("output_type", - QualifiedClassName(method->output_type(), options)); -} - -} // namespace - -ServiceGenerator::ServiceGenerator( - const ServiceDescriptor* descriptor, - const std::map<TProtoStringType, TProtoStringType>& vars, const Options& options) - : descriptor_(descriptor), vars_(vars), options_(options) { - vars_["classname"] = descriptor_->name(); - vars_["full_name"] = descriptor_->full_name(); -} - -ServiceGenerator::~ServiceGenerator() {} - void ServiceGenerator::GenerateDeclarations(io::Printer* printer) { - Formatter format(printer, vars_); - // Forward-declare the stub type. - format( - "class $classname$_Stub;\n" - "\n"); - - GenerateInterface(printer); - GenerateStubDefinition(printer); -} - -void ServiceGenerator::GenerateInterface(io::Printer* printer) { - Formatter format(printer, vars_); - format( - "class $dllexport_decl $$classname$ : public ::$proto_ns$::Service {\n" - " protected:\n" - " // This class should be treated as an abstract interface.\n" - " inline $classname$() {};\n" - " public:\n" - " virtual ~$classname$();\n"); - printer->Indent(); - - format( - "\n" - "typedef $classname$_Stub Stub;\n" - "\n" - "static const ::$proto_ns$::ServiceDescriptor* descriptor();\n" - "\n"); - - GenerateMethodSignatures(VIRTUAL, printer); - - format( - "\n" - "// implements Service ----------------------------------------------\n" - "\n" - "const ::$proto_ns$::ServiceDescriptor* GetDescriptor();\n" - "void CallMethod(const ::$proto_ns$::MethodDescriptor* method,\n" - " ::$proto_ns$::RpcController* controller,\n" - " const ::$proto_ns$::Message* request,\n" - " ::$proto_ns$::Message* response,\n" - " ::google::protobuf::Closure* done);\n" - "const ::$proto_ns$::Message& GetRequestPrototype(\n" - " const ::$proto_ns$::MethodDescriptor* method) const;\n" - "const ::$proto_ns$::Message& GetResponsePrototype(\n" - " const ::$proto_ns$::MethodDescriptor* method) const;\n"); - - printer->Outdent(); - format( - "\n" - " private:\n" - " GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$);\n" - "};\n" - "\n"); -} - -void ServiceGenerator::GenerateStubDefinition(io::Printer* printer) { - Formatter format(printer, vars_); - format( - "class $dllexport_decl $$classname$_Stub : public $classname$ {\n" - " public:\n"); - - printer->Indent(); - - format( - "$classname$_Stub(::$proto_ns$::RpcChannel* channel);\n" - "$classname$_Stub(::$proto_ns$::RpcChannel* channel,\n" - " ::$proto_ns$::Service::ChannelOwnership ownership);\n" - "~$classname$_Stub();\n" - "\n" - "inline ::$proto_ns$::RpcChannel* channel() { return channel_; }\n" - "\n" - "// implements $classname$ ------------------------------------------\n" - "\n"); - - GenerateMethodSignatures(NON_VIRTUAL, printer); - - printer->Outdent(); - format( - " private:\n" - " ::$proto_ns$::RpcChannel* channel_;\n" - " bool owns_channel_;\n" - " GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$_Stub);\n" - "};\n" - "\n"); + auto vars = printer->WithVars(&vars_); + printer->Emit( + { + {"virts", [&] { GenerateMethodSignatures(kVirtual, printer); }}, + {"impls", [&] { GenerateMethodSignatures(kNonVirtual, printer); }}, + }, + R"cc( + class $classname$_Stub; + class $dllexport_decl $$classname$ : public ::$proto_ns$::Service { + protected: + $classname$() = default; + + public: + using Stub = $classname$_Stub; + + $classname$(const $classname$&) = delete; + $classname$& operator=(const $classname$&) = delete; + virtual ~$classname$() = default; + + static const ::$proto_ns$::ServiceDescriptor* descriptor(); + + $virts$; + + // implements Service ---------------------------------------------- + const ::$proto_ns$::ServiceDescriptor* GetDescriptor() override; + + void CallMethod(const ::$proto_ns$::MethodDescriptor* method, + ::$proto_ns$::RpcController* controller, + const ::$proto_ns$::Message* request, + ::$proto_ns$::Message* response, + ::google::protobuf::Closure* done) override; + + const ::$proto_ns$::Message& GetRequestPrototype( + const ::$proto_ns$::MethodDescriptor* method) const override; + + const ::$proto_ns$::Message& GetResponsePrototype( + const ::$proto_ns$::MethodDescriptor* method) const override; + }; + + class $dllexport_decl $$classname$_Stub final : public $classname$ { + public: + $classname$_Stub(::$proto_ns$::RpcChannel* channel); + $classname$_Stub(::$proto_ns$::RpcChannel* channel, + ::$proto_ns$::Service::ChannelOwnership ownership); + + $classname$_Stub(const $classname$_Stub&) = delete; + $classname$_Stub& operator=(const $classname$_Stub&) = delete; + + ~$classname$_Stub() override; + + inline ::$proto_ns$::RpcChannel* channel() { return channel_; } + + // implements $classname$ ------------------------------------------ + $impls$; + + private: + ::$proto_ns$::RpcChannel* channel_; + bool owns_channel_; + }; + )cc"); } -void ServiceGenerator::GenerateMethodSignatures(VirtualOrNon virtual_or_non, +void ServiceGenerator::GenerateMethodSignatures(VirtualOrNot virtual_or_not, io::Printer* printer) { - for (int i = 0; i < descriptor_->method_count(); i++) { + for (int i = 0; i < descriptor_->method_count(); ++i) { const MethodDescriptor* method = descriptor_->method(i); - Formatter format(printer, vars_); - InitMethodVariables(method, options_, &format); - format.Set("virtual", virtual_or_non == VIRTUAL ? "virtual " : ""); - format( - "$virtual$void $name$(::$proto_ns$::RpcController* controller,\n" - " const $input_type$* request,\n" - " $output_type$* response,\n" - " ::google::protobuf::Closure* done);\n"); + + printer->Emit( + { + {"name", method->name()}, + {"input", QualifiedClassName(method->input_type(), *options_)}, + {"output", QualifiedClassName(method->output_type(), *options_)}, + {"virtual", virtual_or_not == kVirtual ? "virtual" : ""}, + {"override", virtual_or_not != kVirtual ? "override" : ""}, + }, + // No cc, clang-format does not format this string well due to the + // $ override$ substitution. + R"( + $virtual $void $name$(::$proto_ns$::RpcController* controller, + const $input$* request, + $output$* response, + ::google::protobuf::Closure* done)$ override$; + )"); } } // =================================================================== void ServiceGenerator::GenerateImplementation(io::Printer* printer) { - Formatter format(printer, vars_); - format( - "$classname$::~$classname$() {}\n" - "\n" - "const ::$proto_ns$::ServiceDescriptor* $classname$::descriptor() {\n" - " " - "::$proto_ns$::internal::AssignDescriptors(&$desc_table$);\n" - " return $file_level_service_descriptors$[$1$];\n" - "}\n" - "\n" - "const ::$proto_ns$::ServiceDescriptor* $classname$::GetDescriptor() {\n" - " return descriptor();\n" - "}\n" - "\n", - index_in_metadata_); - - // Generate methods of the interface. - GenerateNotImplementedMethods(printer); - GenerateCallMethod(printer); - GenerateGetPrototype(REQUEST, printer); - GenerateGetPrototype(RESPONSE, printer); - - // Generate stub implementation. - format( - "$classname$_Stub::$classname$_Stub(::$proto_ns$::RpcChannel* channel)\n" - " : channel_(channel), owns_channel_(false) {}\n" - "$classname$_Stub::$classname$_Stub(\n" - " ::$proto_ns$::RpcChannel* channel,\n" - " ::$proto_ns$::Service::ChannelOwnership ownership)\n" - " : channel_(channel),\n" - " owns_channel_(ownership == " - "::$proto_ns$::Service::STUB_OWNS_CHANNEL) " - "{}\n" - "$classname$_Stub::~$classname$_Stub() {\n" - " if (owns_channel_) delete channel_;\n" - "}\n" - "\n"); - - GenerateStubMethods(printer); + auto vars = printer->WithVars(&vars_); + printer->Emit( + { + {"index", index_in_metadata_}, + {"no_impl_methods", [&] { GenerateNotImplementedMethods(printer); }}, + {"call_method", [&] { GenerateCallMethod(printer); }}, + {"get_request", [&] { GenerateGetPrototype(kRequest, printer); }}, + {"get_response", [&] { GenerateGetPrototype(kResponse, printer); }}, + {"stub_methods", [&] { GenerateStubMethods(printer); }}, + }, + R"cc( + const ::$proto_ns$::ServiceDescriptor* $classname$::descriptor() { + ::$proto_ns$::internal::AssignDescriptors(&$desc_table$); + return $file_level_service_descriptors$[$index$]; + } + + const ::$proto_ns$::ServiceDescriptor* $classname$::GetDescriptor() { + return descriptor(); + } + + $no_impl_methods$; + + $call_method$; + + $get_request$; + + $get_response$; + + $classname$_Stub::$classname$_Stub(::$proto_ns$::RpcChannel* channel) + : channel_(channel), owns_channel_(false) {} + + $classname$_Stub::$classname$_Stub( + ::$proto_ns$::RpcChannel* channel, + ::$proto_ns$::Service::ChannelOwnership ownership) + : channel_(channel), + owns_channel_(ownership == + ::$proto_ns$::Service::STUB_OWNS_CHANNEL) {} + + $classname$_Stub::~$classname$_Stub() { + if (owns_channel_) delete channel_; + } + + $stub_methods$; + )cc"); } void ServiceGenerator::GenerateNotImplementedMethods(io::Printer* printer) { - for (int i = 0; i < descriptor_->method_count(); i++) { + for (int i = 0; i < descriptor_->method_count(); ++i) { const MethodDescriptor* method = descriptor_->method(i); - Formatter format(printer, vars_); - InitMethodVariables(method, options_, &format); - format( - "void $classname$::$name$(::$proto_ns$::RpcController* controller,\n" - " const $input_type$*,\n" - " $output_type$*,\n" - " ::google::protobuf::Closure* done) {\n" - " controller->SetFailed(\"Method $name$() not implemented.\");\n" - " done->Run();\n" - "}\n" - "\n"); + + printer->Emit( + { + {"name", method->name()}, + {"input", QualifiedClassName(method->input_type(), *options_)}, + {"output", QualifiedClassName(method->output_type(), *options_)}, + }, + R"cc( + void $classname$::$name$(::$proto_ns$::RpcController* controller, + const $input$*, $output$*, ::google::protobuf::Closure* done) { + controller->SetFailed("Method $name$() not implemented."); + done->Run(); + } + )cc"); } } void ServiceGenerator::GenerateCallMethod(io::Printer* printer) { - Formatter format(printer, vars_); - format( - "void $classname$::CallMethod(const ::$proto_ns$::MethodDescriptor* " - "method,\n" - " ::$proto_ns$::RpcController* controller,\n" - " const ::$proto_ns$::Message* request,\n" - " ::$proto_ns$::Message* response,\n" - " ::google::protobuf::Closure* done) {\n" - " GOOGLE_DCHECK_EQ(method->service(), $file_level_service_descriptors$[$1$]);\n" - " switch(method->index()) {\n", - index_in_metadata_); - - for (int i = 0; i < descriptor_->method_count(); i++) { - const MethodDescriptor* method = descriptor_->method(i); - Formatter format_method(printer, vars_); - InitMethodVariables(method, options_, &format_method); - - // Note: down_cast does not work here because it only works on pointers, - // not references. - format_method( - " case $1$:\n" - " $name$(controller,\n" - " ::$proto_ns$::internal::DownCast<const $input_type$*>(\n" - " request),\n" - " ::$proto_ns$::internal::DownCast<$output_type$*>(\n" - " response),\n" - " done);\n" - " break;\n", - i); - } - - format( - " default:\n" - " GOOGLE_LOG(FATAL) << \"Bad method index; this should never happen.\";\n" - " break;\n" - " }\n" - "}\n" - "\n"); + printer->Emit( + { + {"index", y_absl::StrCat(index_in_metadata_)}, + {"cases", [&] { GenerateCallMethodCases(printer); }}, + }, + R"cc( + void $classname$::CallMethod( + const ::$proto_ns$::MethodDescriptor* method, + ::$proto_ns$::RpcController* controller, + const ::$proto_ns$::Message* request, + ::$proto_ns$::Message* response, ::google::protobuf::Closure* done) { + Y_ABSL_DCHECK_EQ(method->service(), $file_level_service_descriptors$[$index$]); + switch (method->index()) { + $cases$; + + default: + Y_ABSL_LOG(FATAL) << "Bad method index; this should never happen."; + break; + } + } + )cc"); } void ServiceGenerator::GenerateGetPrototype(RequestOrResponse which, io::Printer* printer) { - Formatter format(printer, vars_); - if (which == REQUEST) { - format("const ::$proto_ns$::Message& $classname$::GetRequestPrototype(\n"); - } else { - format("const ::$proto_ns$::Message& $classname$::GetResponsePrototype(\n"); - } - - format( - " const ::$proto_ns$::MethodDescriptor* method) const {\n" - " GOOGLE_DCHECK_EQ(method->service(), descriptor());\n" - " switch(method->index()) {\n"); + printer->Emit( + { + {"which", which == kRequest ? "Request" : "Response"}, + {"which_type", which == kRequest ? "input" : "output"}, + {"cases", + [&] { + for (int i = 0; i < descriptor_->method_count(); ++i) { + const MethodDescriptor* method = descriptor_->method(i); + const Descriptor* type = which == kRequest + ? method->input_type() + : method->output_type(); + + printer->Emit( + { + {"index", y_absl::StrCat(i)}, + {"type", QualifiedClassName(type, *options_)}, + }, + R"cc( + case $index$: + return $type$::default_instance(); + )cc"); + } + }}, + }, + R"cc( + const ::$proto_ns$::Message& $classname$::Get$which$Prototype( + const ::$proto_ns$::MethodDescriptor* method) const { + Y_ABSL_DCHECK_EQ(method->service(), descriptor()); + switch (method->index()) { + $cases$; + + default: + Y_ABSL_LOG(FATAL) << "Bad method index; this should never happen."; + return *::$proto_ns$::MessageFactory::generated_factory() + ->GetPrototype(method->$which_type$_type()); + } + } + )cc"); +} - for (int i = 0; i < descriptor_->method_count(); i++) { +void ServiceGenerator::GenerateCallMethodCases(io::Printer* printer) { + for (int i = 0; i < descriptor_->method_count(); ++i) { const MethodDescriptor* method = descriptor_->method(i); - const Descriptor* type = - (which == REQUEST) ? method->input_type() : method->output_type(); - - format( - " case $1$:\n" - " return $2$::default_instance();\n", - i, QualifiedClassName(type, options_)); + printer->Emit( + { + {"name", method->name()}, + {"input", QualifiedClassName(method->input_type(), *options_)}, + {"output", QualifiedClassName(method->output_type(), *options_)}, + {"index", y_absl::StrCat(i)}, + }, + R"cc( + case $index$: + $name$(controller, + ::$proto_ns$::internal::DownCast<const $input$*>(request), + ::$proto_ns$::internal::DownCast<$output$*>(response), done); + break; + )cc"); } - - format( - " default:\n" - " GOOGLE_LOG(FATAL) << \"Bad method index; this should never happen.\";\n" - " return *::$proto_ns$::MessageFactory::generated_factory()\n" - " ->GetPrototype(method->$1$_type());\n" - " }\n" - "}\n" - "\n", - which == REQUEST ? "input" : "output"); } void ServiceGenerator::GenerateStubMethods(io::Printer* printer) { - for (int i = 0; i < descriptor_->method_count(); i++) { + for (int i = 0; i < descriptor_->method_count(); ++i) { const MethodDescriptor* method = descriptor_->method(i); - Formatter format(printer, vars_); - InitMethodVariables(method, options_, &format); - format( - "void $classname$_Stub::$name$(::$proto_ns$::RpcController* " - "controller,\n" - " const $input_type$* request,\n" - " $output_type$* response,\n" - " ::google::protobuf::Closure* done) {\n" - " channel_->CallMethod(descriptor()->method($1$),\n" - " controller, request, response, done);\n" - "}\n", - i); + + printer->Emit( + { + {"name", method->name()}, + {"input", QualifiedClassName(method->input_type(), *options_)}, + {"output", QualifiedClassName(method->output_type(), *options_)}, + {"index", y_absl::StrCat(i)}, + }, + R"cc( + void $classname$_Stub::$name$(::$proto_ns$::RpcController* controller, + const $input$* request, + $output$* response, ::google::protobuf::Closure* done) { + channel_->CallMethod(descriptor()->method($index$), controller, + request, response, done); + } + )cc"); } } |