diff options
-rw-r--r-- | src/compiler/cpp_generator.cc | 169 | ||||
-rw-r--r-- | test/cpp/end2end/async_end2end_test.cc | 2 | ||||
-rw-r--r-- | test/cpp/end2end/end2end_test.cc | 25 |
3 files changed, 100 insertions, 96 deletions
diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index f10824e6b0..891032343b 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -183,34 +183,40 @@ void PrintHeaderClientMethod(google::protobuf::io::Printer *printer, printer->Print(*vars, "::grpc::Status $Method$(::grpc::ClientContext* context, " "const $Request$& request, $Response$* response);\n"); - printer->Print(*vars, - "::grpc::ClientAsyncResponseReader< $Response$>* " - "$Method$(::grpc::ClientContext* context, " - "const $Request$& request, " - "::grpc::CompletionQueue* cq, void* tag);\n"); + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> " + "$Method$(::grpc::ClientContext* context, " + "const $Request$& request, " + "::grpc::CompletionQueue* cq, void* tag);\n"); } else if (ClientOnlyStreaming(method)) { - printer->Print(*vars, - "::grpc::ClientWriter< $Request$>* $Method$(" - "::grpc::ClientContext* context, $Response$* response);\n"); - printer->Print(*vars, - "::grpc::ClientAsyncWriter< $Request$>* $Method$(" - "::grpc::ClientContext* context, $Response$* response, " - "::grpc::CompletionQueue* cq, void* tag);\n"); + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientWriter< $Request$>> $Method$(" + "::grpc::ClientContext* context, $Response$* response);\n"); + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>> $Method$(" + "::grpc::ClientContext* context, $Response$* response, " + "::grpc::CompletionQueue* cq, void* tag);\n"); } else if (ServerOnlyStreaming(method)) { printer->Print( *vars, - "::grpc::ClientReader< $Response$>* $Method$(" + "std::unique_ptr< ::grpc::ClientReader< $Response$>> $Method$(" "::grpc::ClientContext* context, const $Request$& request);\n"); - printer->Print(*vars, - "::grpc::ClientAsyncReader< $Response$>* $Method$(" - "::grpc::ClientContext* context, const $Request$& request, " - "::grpc::CompletionQueue* cq, void* tag);\n"); + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> $Method$(" + "::grpc::ClientContext* context, const $Request$& request, " + "::grpc::CompletionQueue* cq, void* tag);\n"); } else if (BidiStreaming(method)) { + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientReaderWriter< $Request$, $Response$>> " + "$Method$(::grpc::ClientContext* context);\n"); printer->Print(*vars, - "::grpc::ClientReaderWriter< $Request$, $Response$>* " - "$Method$(::grpc::ClientContext* context);\n"); - printer->Print(*vars, - "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* " + "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " + "$Request$, $Response$>> " "$Method$(::grpc::ClientContext* context, " "::grpc::CompletionQueue* cq, void* tag);\n"); } @@ -309,7 +315,8 @@ void PrintHeaderService(google::protobuf::io::Printer *printer, printer->Outdent(); printer->Print("};\n"); printer->Print( - "static Stub* NewStub(const std::shared_ptr< ::grpc::ChannelInterface>& " + "static std::unique_ptr<Stub> NewStub(const std::shared_ptr< " + "::grpc::ChannelInterface>& " "channel);\n"); printer->Print("\n"); @@ -380,91 +387,101 @@ void PrintSourceClientMethod(google::protobuf::io::Printer *printer, "::grpc::RpcMethod($Service$_method_names[$Idx$]), " "context, request, response);\n" "}\n\n"); + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> " + "$Service$::Stub::$Method$(::grpc::ClientContext* context, " + "const $Request$& request, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); printer->Print(*vars, - "::grpc::ClientAsyncResponseReader< $Response$>* " - "$Service$::Stub::$Method$(::grpc::ClientContext* context, " - "const $Request$& request, " - "::grpc::CompletionQueue* cq, void* tag) {\n"); - printer->Print(*vars, - " return new ::grpc::ClientAsyncResponseReader< $Response$>(" + " return std::unique_ptr< " + "::grpc::ClientAsyncResponseReader< $Response$>>(new " + "::grpc::ClientAsyncResponseReader< $Response$>(" "channel(), cq, " "::grpc::RpcMethod($Service$_method_names[$Idx$]), " - "context, request, tag);\n" + "context, request, tag));\n" "}\n\n"); } else if (ClientOnlyStreaming(method)) { - printer->Print( - *vars, - "::grpc::ClientWriter< $Request$>* $Service$::Stub::$Method$(" - "::grpc::ClientContext* context, $Response$* response) {\n"); printer->Print(*vars, - " return new ::grpc::ClientWriter< $Request$>(" + "std::unique_ptr< ::grpc::ClientWriter< $Request$>> " + "$Service$::Stub::$Method$(" + "::grpc::ClientContext* context, $Response$* response) {\n"); + printer->Print(*vars, + " return std::unique_ptr< ::grpc::ClientWriter< " + "$Request$>>(new ::grpc::ClientWriter< $Request$>(" "channel()," "::grpc::RpcMethod($Service$_method_names[$Idx$], " "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " - "context, response);\n" + "context, response));\n" "}\n\n"); - printer->Print( - *vars, - "::grpc::ClientAsyncWriter< $Request$>* $Service$::Stub::$Method$(" - "::grpc::ClientContext* context, $Response$* response, " - "::grpc::CompletionQueue* cq, void* tag) {\n"); printer->Print(*vars, - " return new ::grpc::ClientAsyncWriter< $Request$>(" + "std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>> " + "$Service$::Stub::$Method$(" + "::grpc::ClientContext* context, $Response$* response, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Print(*vars, + " return std::unique_ptr< ::grpc::ClientAsyncWriter< " + "$Request$>>(new ::grpc::ClientAsyncWriter< $Request$>(" "channel(), cq, " "::grpc::RpcMethod($Service$_method_names[$Idx$], " "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " - "context, response, tag);\n" + "context, response, tag));\n" "}\n\n"); } else if (ServerOnlyStreaming(method)) { printer->Print( *vars, - "::grpc::ClientReader< $Response$>* $Service$::Stub::$Method$(" + "std::unique_ptr< ::grpc::ClientReader< $Response$>> " + "$Service$::Stub::$Method$(" "::grpc::ClientContext* context, const $Request$& request) {\n"); printer->Print(*vars, - " return new ::grpc::ClientReader< $Response$>(" + " return std::unique_ptr< ::grpc::ClientReader< " + "$Response$>>(new ::grpc::ClientReader< $Response$>(" "channel()," "::grpc::RpcMethod($Service$_method_names[$Idx$], " "::grpc::RpcMethod::RpcType::SERVER_STREAMING), " - "context, request);\n" + "context, request));\n" "}\n\n"); - printer->Print( - *vars, - "::grpc::ClientAsyncReader< $Response$>* $Service$::Stub::$Method$(" - "::grpc::ClientContext* context, const $Request$& request, " - "::grpc::CompletionQueue* cq, void* tag) {\n"); printer->Print(*vars, - " return new ::grpc::ClientAsyncReader< $Response$>(" + "std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> " + "$Service$::Stub::$Method$(" + "::grpc::ClientContext* context, const $Request$& request, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Print(*vars, + " return std::unique_ptr< ::grpc::ClientAsyncReader< " + "$Response$>>(new ::grpc::ClientAsyncReader< $Response$>(" "channel(), cq, " "::grpc::RpcMethod($Service$_method_names[$Idx$], " "::grpc::RpcMethod::RpcType::SERVER_STREAMING), " - "context, request, tag);\n" + "context, request, tag));\n" "}\n\n"); } else if (BidiStreaming(method)) { printer->Print( *vars, - "::grpc::ClientReaderWriter< $Request$, $Response$>* " + "std::unique_ptr< ::grpc::ClientReaderWriter< $Request$, $Response$>> " "$Service$::Stub::$Method$(::grpc::ClientContext* context) {\n"); - printer->Print( - *vars, - " return new ::grpc::ClientReaderWriter< $Request$, $Response$>(" - "channel()," - "::grpc::RpcMethod($Service$_method_names[$Idx$], " - "::grpc::RpcMethod::RpcType::BIDI_STREAMING), " - "context);\n" - "}\n\n"); - printer->Print( - *vars, - "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* " - "$Service$::Stub::$Method$(::grpc::ClientContext* context, " - "::grpc::CompletionQueue* cq, void* tag) {\n"); - printer->Print( - *vars, - " return new ::grpc::ClientAsyncReaderWriter< $Request$, $Response$>(" - "channel(), cq, " - "::grpc::RpcMethod($Service$_method_names[$Idx$], " - "::grpc::RpcMethod::RpcType::BIDI_STREAMING), " - "context, tag);\n" - "}\n\n"); + printer->Print(*vars, + " return std::unique_ptr< ::grpc::ClientReaderWriter< " + "$Request$, $Response$>>(new ::grpc::ClientReaderWriter< " + "$Request$, $Response$>(" + "channel()," + "::grpc::RpcMethod($Service$_method_names[$Idx$], " + "::grpc::RpcMethod::RpcType::BIDI_STREAMING), " + "context));\n" + "}\n\n"); + printer->Print(*vars, + "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " + "$Request$, $Response$>> " + "$Service$::Stub::$Method$(::grpc::ClientContext* context, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Print(*vars, + " return std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " + "$Request$, $Response$>>(new " + "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>(" + "channel(), cq, " + "::grpc::RpcMethod($Service$_method_names[$Idx$], " + "::grpc::RpcMethod::RpcType::BIDI_STREAMING), " + "context, tag));\n" + "}\n\n"); } } @@ -587,9 +604,9 @@ void PrintSourceService(google::protobuf::io::Printer *printer, printer->Print( *vars, - "$Service$::Stub* $Service$::NewStub(" + "std::unique_ptr< $Service$::Stub> $Service$::NewStub(" "const std::shared_ptr< ::grpc::ChannelInterface>& channel) {\n" - " $Service$::Stub* stub = new $Service$::Stub();\n" + " std::unique_ptr< $Service$::Stub> stub(new $Service$::Stub());\n" " stub->set_channel(channel);\n" " return stub;\n" "};\n\n"); diff --git a/test/cpp/end2end/async_end2end_test.cc b/test/cpp/end2end/async_end2end_test.cc index 248e054e49..6dca0aa348 100644 --- a/test/cpp/end2end/async_end2end_test.cc +++ b/test/cpp/end2end/async_end2end_test.cc @@ -106,7 +106,7 @@ class AsyncEnd2endTest : public ::testing::Test { void ResetStub() { std::shared_ptr<ChannelInterface> channel = CreateChannel(server_address_.str(), ChannelArguments()); - stub_.reset(grpc::cpp::test::util::TestService::NewStub(channel)); + stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel)); } void server_ok(int i) { diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index d4ca3ef49e..8b3fb4d485 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -162,7 +162,7 @@ class End2endTest : public ::testing::Test { void ResetStub() { std::shared_ptr<ChannelInterface> channel = CreateChannel(server_address_.str(), ChannelArguments()); - stub_.reset(grpc::cpp::test::util::TestService::NewStub(channel)); + stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel)); } std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_; @@ -292,15 +292,13 @@ TEST_F(End2endTest, RequestStreamOneRequest) { EchoResponse response; ClientContext context; - ClientWriter<EchoRequest>* stream = stub_->RequestStream(&context, &response); + auto stream = stub_->RequestStream(&context, &response); request.set_message("hello"); EXPECT_TRUE(stream->Write(request)); stream->WritesDone(); Status s = stream->Finish(); EXPECT_EQ(response.message(), request.message()); EXPECT_TRUE(s.IsOk()); - - delete stream; } TEST_F(End2endTest, RequestStreamTwoRequests) { @@ -309,7 +307,7 @@ TEST_F(End2endTest, RequestStreamTwoRequests) { EchoResponse response; ClientContext context; - ClientWriter<EchoRequest>* stream = stub_->RequestStream(&context, &response); + auto stream = stub_->RequestStream(&context, &response); request.set_message("hello"); EXPECT_TRUE(stream->Write(request)); EXPECT_TRUE(stream->Write(request)); @@ -317,8 +315,6 @@ TEST_F(End2endTest, RequestStreamTwoRequests) { Status s = stream->Finish(); EXPECT_EQ(response.message(), "hellohello"); EXPECT_TRUE(s.IsOk()); - - delete stream; } TEST_F(End2endTest, ResponseStream) { @@ -328,8 +324,7 @@ TEST_F(End2endTest, ResponseStream) { ClientContext context; request.set_message("hello"); - ClientReader<EchoResponse>* stream = - stub_->ResponseStream(&context, request); + auto stream = stub_->ResponseStream(&context, request); EXPECT_TRUE(stream->Read(&response)); EXPECT_EQ(response.message(), request.message() + "0"); EXPECT_TRUE(stream->Read(&response)); @@ -340,8 +335,6 @@ TEST_F(End2endTest, ResponseStream) { Status s = stream->Finish(); EXPECT_TRUE(s.IsOk()); - - delete stream; } TEST_F(End2endTest, BidiStream) { @@ -351,8 +344,7 @@ TEST_F(End2endTest, BidiStream) { ClientContext context; grpc::string msg("hello"); - ClientReaderWriter<EchoRequest, EchoResponse>* stream = - stub_->BidiStream(&context); + auto stream = stub_->BidiStream(&context); request.set_message(msg + "0"); EXPECT_TRUE(stream->Write(request)); @@ -374,8 +366,6 @@ TEST_F(End2endTest, BidiStream) { Status s = stream->Finish(); EXPECT_TRUE(s.IsOk()); - - delete stream; } // Talk to the two services with the same name but different package names. @@ -426,14 +416,11 @@ TEST_F(End2endTest, BadCredentials) { EXPECT_EQ("Rpc sent on a lame channel.", s.details()); ClientContext context2; - ClientReaderWriter<EchoRequest, EchoResponse>* stream = - stub->BidiStream(&context2); + auto stream = stub->BidiStream(&context2); s = stream->Finish(); EXPECT_FALSE(s.IsOk()); EXPECT_EQ(StatusCode::UNKNOWN, s.code()); EXPECT_EQ("Rpc sent on a lame channel.", s.details()); - - delete stream; } } // namespace testing |