|
|
@ -31,45 +31,42 @@ |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
#include <string> |
|
|
|
|
|
|
|
#include <map> |
|
|
|
#include <map> |
|
|
|
|
|
|
|
|
|
|
|
#include "src/compiler/cpp_generator.h" |
|
|
|
#include "src/compiler/cpp_generator.h" |
|
|
|
|
|
|
|
|
|
|
|
#include "src/compiler/cpp_generator_helpers.h" |
|
|
|
#include "src/compiler/cpp_generator_helpers.h" |
|
|
|
#include <google/protobuf/descriptor.h> |
|
|
|
|
|
|
|
#include <google/protobuf/descriptor.pb.h> |
|
|
|
#include "src/compiler/config.h" |
|
|
|
#include <google/protobuf/io/printer.h> |
|
|
|
|
|
|
|
#include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
|
|
|
|
|
|
|
#include <sstream> |
|
|
|
#include <sstream> |
|
|
|
|
|
|
|
|
|
|
|
namespace grpc_cpp_generator { |
|
|
|
namespace grpc_cpp_generator { |
|
|
|
namespace { |
|
|
|
namespace { |
|
|
|
|
|
|
|
|
|
|
|
template <class T> |
|
|
|
template <class T> |
|
|
|
std::string as_string(T x) { |
|
|
|
grpc::string as_string(T x) { |
|
|
|
std::ostringstream out; |
|
|
|
std::ostringstream out; |
|
|
|
out << x; |
|
|
|
out << x; |
|
|
|
return out.str(); |
|
|
|
return out.str(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool NoStreaming(const google::protobuf::MethodDescriptor *method) { |
|
|
|
bool NoStreaming(const grpc::protobuf::MethodDescriptor *method) { |
|
|
|
return !method->client_streaming() && !method->server_streaming(); |
|
|
|
return !method->client_streaming() && !method->server_streaming(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool ClientOnlyStreaming(const google::protobuf::MethodDescriptor *method) { |
|
|
|
bool ClientOnlyStreaming(const grpc::protobuf::MethodDescriptor *method) { |
|
|
|
return method->client_streaming() && !method->server_streaming(); |
|
|
|
return method->client_streaming() && !method->server_streaming(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool ServerOnlyStreaming(const google::protobuf::MethodDescriptor *method) { |
|
|
|
bool ServerOnlyStreaming(const grpc::protobuf::MethodDescriptor *method) { |
|
|
|
return !method->client_streaming() && method->server_streaming(); |
|
|
|
return !method->client_streaming() && method->server_streaming(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool BidiStreaming(const google::protobuf::MethodDescriptor *method) { |
|
|
|
bool BidiStreaming(const grpc::protobuf::MethodDescriptor *method) { |
|
|
|
return method->client_streaming() && method->server_streaming(); |
|
|
|
return method->client_streaming() && method->server_streaming(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool HasUnaryCalls(const google::protobuf::FileDescriptor *file) { |
|
|
|
bool HasUnaryCalls(const grpc::protobuf::FileDescriptor *file) { |
|
|
|
for (int i = 0; i < file->service_count(); i++) { |
|
|
|
for (int i = 0; i < file->service_count(); i++) { |
|
|
|
for (int j = 0; j < file->service(i)->method_count(); j++) { |
|
|
|
for (int j = 0; j < file->service(i)->method_count(); j++) { |
|
|
|
if (NoStreaming(file->service(i)->method(j))) { |
|
|
|
if (NoStreaming(file->service(i)->method(j))) { |
|
|
@ -80,7 +77,7 @@ bool HasUnaryCalls(const google::protobuf::FileDescriptor *file) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool HasClientOnlyStreaming(const google::protobuf::FileDescriptor *file) { |
|
|
|
bool HasClientOnlyStreaming(const grpc::protobuf::FileDescriptor *file) { |
|
|
|
for (int i = 0; i < file->service_count(); i++) { |
|
|
|
for (int i = 0; i < file->service_count(); i++) { |
|
|
|
for (int j = 0; j < file->service(i)->method_count(); j++) { |
|
|
|
for (int j = 0; j < file->service(i)->method_count(); j++) { |
|
|
|
if (ClientOnlyStreaming(file->service(i)->method(j))) { |
|
|
|
if (ClientOnlyStreaming(file->service(i)->method(j))) { |
|
|
@ -91,7 +88,7 @@ bool HasClientOnlyStreaming(const google::protobuf::FileDescriptor *file) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool HasServerOnlyStreaming(const google::protobuf::FileDescriptor *file) { |
|
|
|
bool HasServerOnlyStreaming(const grpc::protobuf::FileDescriptor *file) { |
|
|
|
for (int i = 0; i < file->service_count(); i++) { |
|
|
|
for (int i = 0; i < file->service_count(); i++) { |
|
|
|
for (int j = 0; j < file->service(i)->method_count(); j++) { |
|
|
|
for (int j = 0; j < file->service(i)->method_count(); j++) { |
|
|
|
if (ServerOnlyStreaming(file->service(i)->method(j))) { |
|
|
|
if (ServerOnlyStreaming(file->service(i)->method(j))) { |
|
|
@ -102,7 +99,7 @@ bool HasServerOnlyStreaming(const google::protobuf::FileDescriptor *file) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool HasBidiStreaming(const google::protobuf::FileDescriptor *file) { |
|
|
|
bool HasBidiStreaming(const grpc::protobuf::FileDescriptor *file) { |
|
|
|
for (int i = 0; i < file->service_count(); i++) { |
|
|
|
for (int i = 0; i < file->service_count(); i++) { |
|
|
|
for (int j = 0; j < file->service(i)->method_count(); j++) { |
|
|
|
for (int j = 0; j < file->service(i)->method_count(); j++) { |
|
|
|
if (BidiStreaming(file->service(i)->method(j))) { |
|
|
|
if (BidiStreaming(file->service(i)->method(j))) { |
|
|
@ -114,8 +111,8 @@ bool HasBidiStreaming(const google::protobuf::FileDescriptor *file) { |
|
|
|
} |
|
|
|
} |
|
|
|
} // namespace
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
std::string GetHeaderIncludes(const google::protobuf::FileDescriptor *file) { |
|
|
|
grpc::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor *file) { |
|
|
|
std::string temp = |
|
|
|
grpc::string temp = |
|
|
|
"#include <grpc++/impl/internal_stub.h>\n" |
|
|
|
"#include <grpc++/impl/internal_stub.h>\n" |
|
|
|
"#include <grpc++/impl/service_type.h>\n" |
|
|
|
"#include <grpc++/impl/service_type.h>\n" |
|
|
|
"#include <grpc++/status.h>\n" |
|
|
|
"#include <grpc++/status.h>\n" |
|
|
@ -161,7 +158,7 @@ std::string GetHeaderIncludes(const google::protobuf::FileDescriptor *file) { |
|
|
|
return temp; |
|
|
|
return temp; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string GetSourceIncludes() { |
|
|
|
grpc::string GetSourceIncludes() { |
|
|
|
return "#include <grpc++/async_unary_call.h>\n" |
|
|
|
return "#include <grpc++/async_unary_call.h>\n" |
|
|
|
"#include <grpc++/channel_interface.h>\n" |
|
|
|
"#include <grpc++/channel_interface.h>\n" |
|
|
|
"#include <grpc++/impl/client_unary_call.h>\n" |
|
|
|
"#include <grpc++/impl/client_unary_call.h>\n" |
|
|
@ -171,9 +168,9 @@ std::string GetSourceIncludes() { |
|
|
|
"#include <grpc++/stream.h>\n"; |
|
|
|
"#include <grpc++/stream.h>\n"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PrintHeaderClientMethod(google::protobuf::io::Printer *printer, |
|
|
|
void PrintHeaderClientMethod(grpc::protobuf::io::Printer *printer, |
|
|
|
const google::protobuf::MethodDescriptor *method, |
|
|
|
const grpc::protobuf::MethodDescriptor *method, |
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
std::map<grpc::string, grpc::string> *vars) { |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Request"] = |
|
|
|
(*vars)["Request"] = |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
@ -223,9 +220,9 @@ void PrintHeaderClientMethod(google::protobuf::io::Printer *printer, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PrintHeaderServerMethodSync( |
|
|
|
void PrintHeaderServerMethodSync( |
|
|
|
google::protobuf::io::Printer *printer, |
|
|
|
grpc::protobuf::io::Printer *printer, |
|
|
|
const google::protobuf::MethodDescriptor *method, |
|
|
|
const grpc::protobuf::MethodDescriptor *method, |
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
std::map<grpc::string, grpc::string> *vars) { |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Request"] = |
|
|
|
(*vars)["Request"] = |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
@ -258,9 +255,9 @@ void PrintHeaderServerMethodSync( |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PrintHeaderServerMethodAsync( |
|
|
|
void PrintHeaderServerMethodAsync( |
|
|
|
google::protobuf::io::Printer *printer, |
|
|
|
grpc::protobuf::io::Printer *printer, |
|
|
|
const google::protobuf::MethodDescriptor *method, |
|
|
|
const grpc::protobuf::MethodDescriptor *method, |
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
std::map<grpc::string, grpc::string> *vars) { |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Request"] = |
|
|
|
(*vars)["Request"] = |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
@ -294,9 +291,9 @@ void PrintHeaderServerMethodAsync( |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PrintHeaderService(google::protobuf::io::Printer *printer, |
|
|
|
void PrintHeaderService(grpc::protobuf::io::Printer *printer, |
|
|
|
const google::protobuf::ServiceDescriptor *service, |
|
|
|
const grpc::protobuf::ServiceDescriptor *service, |
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
std::map<grpc::string, grpc::string> *vars) { |
|
|
|
(*vars)["Service"] = service->name(); |
|
|
|
(*vars)["Service"] = service->name(); |
|
|
|
|
|
|
|
|
|
|
|
printer->Print(*vars, |
|
|
|
printer->Print(*vars, |
|
|
@ -356,11 +353,11 @@ void PrintHeaderService(google::protobuf::io::Printer *printer, |
|
|
|
printer->Print("};\n"); |
|
|
|
printer->Print("};\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string GetHeaderServices(const google::protobuf::FileDescriptor *file) { |
|
|
|
grpc::string GetHeaderServices(const grpc::protobuf::FileDescriptor *file) { |
|
|
|
std::string output; |
|
|
|
grpc::string output; |
|
|
|
google::protobuf::io::StringOutputStream output_stream(&output); |
|
|
|
grpc::protobuf::io::StringOutputStream output_stream(&output); |
|
|
|
google::protobuf::io::Printer printer(&output_stream, '$'); |
|
|
|
grpc::protobuf::io::Printer printer(&output_stream, '$'); |
|
|
|
std::map<std::string, std::string> vars; |
|
|
|
std::map<grpc::string, grpc::string> vars; |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < file->service_count(); ++i) { |
|
|
|
for (int i = 0; i < file->service_count(); ++i) { |
|
|
|
PrintHeaderService(&printer, file->service(i), &vars); |
|
|
|
PrintHeaderService(&printer, file->service(i), &vars); |
|
|
@ -369,9 +366,9 @@ std::string GetHeaderServices(const google::protobuf::FileDescriptor *file) { |
|
|
|
return output; |
|
|
|
return output; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PrintSourceClientMethod(google::protobuf::io::Printer *printer, |
|
|
|
void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, |
|
|
|
const google::protobuf::MethodDescriptor *method, |
|
|
|
const grpc::protobuf::MethodDescriptor *method, |
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
std::map<grpc::string, grpc::string> *vars) { |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Request"] = |
|
|
|
(*vars)["Request"] = |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
@ -485,9 +482,9 @@ void PrintSourceClientMethod(google::protobuf::io::Printer *printer, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PrintSourceServerMethod(google::protobuf::io::Printer *printer, |
|
|
|
void PrintSourceServerMethod(grpc::protobuf::io::Printer *printer, |
|
|
|
const google::protobuf::MethodDescriptor *method, |
|
|
|
const grpc::protobuf::MethodDescriptor *method, |
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
std::map<grpc::string, grpc::string> *vars) { |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Request"] = |
|
|
|
(*vars)["Request"] = |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
@ -536,9 +533,9 @@ void PrintSourceServerMethod(google::protobuf::io::Printer *printer, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PrintSourceServerAsyncMethod( |
|
|
|
void PrintSourceServerAsyncMethod( |
|
|
|
google::protobuf::io::Printer *printer, |
|
|
|
grpc::protobuf::io::Printer *printer, |
|
|
|
const google::protobuf::MethodDescriptor *method, |
|
|
|
const grpc::protobuf::MethodDescriptor *method, |
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
std::map<grpc::string, grpc::string> *vars) { |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Request"] = |
|
|
|
(*vars)["Request"] = |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
|
grpc_cpp_generator::ClassName(method->input_type(), true); |
|
|
@ -590,9 +587,9 @@ void PrintSourceServerAsyncMethod( |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PrintSourceService(google::protobuf::io::Printer *printer, |
|
|
|
void PrintSourceService(grpc::protobuf::io::Printer *printer, |
|
|
|
const google::protobuf::ServiceDescriptor *service, |
|
|
|
const grpc::protobuf::ServiceDescriptor *service, |
|
|
|
std::map<std::string, std::string> *vars) { |
|
|
|
std::map<grpc::string, grpc::string> *vars) { |
|
|
|
(*vars)["Service"] = service->name(); |
|
|
|
(*vars)["Service"] = service->name(); |
|
|
|
|
|
|
|
|
|
|
|
printer->Print(*vars, "static const char* $Service$_method_names[] = {\n"); |
|
|
|
printer->Print(*vars, "static const char* $Service$_method_names[] = {\n"); |
|
|
@ -640,7 +637,7 @@ void PrintSourceService(google::protobuf::io::Printer *printer, |
|
|
|
"}\n"); |
|
|
|
"}\n"); |
|
|
|
printer->Print("service_ = new ::grpc::RpcService();\n"); |
|
|
|
printer->Print("service_ = new ::grpc::RpcService();\n"); |
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
|
for (int i = 0; i < service->method_count(); ++i) { |
|
|
|
const google::protobuf::MethodDescriptor *method = service->method(i); |
|
|
|
const grpc::protobuf::MethodDescriptor *method = service->method(i); |
|
|
|
(*vars)["Idx"] = as_string(i); |
|
|
|
(*vars)["Idx"] = as_string(i); |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Method"] = method->name(); |
|
|
|
(*vars)["Request"] = |
|
|
|
(*vars)["Request"] = |
|
|
@ -705,11 +702,11 @@ void PrintSourceService(google::protobuf::io::Printer *printer, |
|
|
|
printer->Print("}\n\n"); |
|
|
|
printer->Print("}\n\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string GetSourceServices(const google::protobuf::FileDescriptor *file) { |
|
|
|
grpc::string GetSourceServices(const grpc::protobuf::FileDescriptor *file) { |
|
|
|
std::string output; |
|
|
|
grpc::string output; |
|
|
|
google::protobuf::io::StringOutputStream output_stream(&output); |
|
|
|
grpc::protobuf::io::StringOutputStream output_stream(&output); |
|
|
|
google::protobuf::io::Printer printer(&output_stream, '$'); |
|
|
|
grpc::protobuf::io::Printer printer(&output_stream, '$'); |
|
|
|
std::map<std::string, std::string> vars; |
|
|
|
std::map<grpc::string, grpc::string> vars; |
|
|
|
// Package string is empty or ends with a dot. It is used to fully qualify
|
|
|
|
// Package string is empty or ends with a dot. It is used to fully qualify
|
|
|
|
// method names.
|
|
|
|
// method names.
|
|
|
|
vars["Package"] = file->package(); |
|
|
|
vars["Package"] = file->package(); |
|
|
|