diff --git a/irohad/torii/command_client.cpp b/irohad/torii/command_client.cpp index 16224c352c..2a616c17fc 100644 --- a/irohad/torii/command_client.cpp +++ b/irohad/torii/command_client.cpp @@ -34,13 +34,17 @@ namespace torii { CommandSyncClient::CommandSyncClient(const CommandSyncClient &rhs) : CommandSyncClient(rhs.ip_, rhs.port_) {} - CommandSyncClient &CommandSyncClient::operator=( - const CommandSyncClient &rhs) { - this->ip_ = rhs.ip_; - this->port_ = rhs.port_; - this->stub_ = iroha::protocol::CommandService::NewStub( - grpc::CreateChannel(rhs.ip_ + ":" + std::to_string(rhs.port_), - grpc::InsecureChannelCredentials())); + CommandSyncClient &CommandSyncClient::operator=(CommandSyncClient rhs) { + swap(*this, rhs); + return *this; + } + + CommandSyncClient::CommandSyncClient(CommandSyncClient &&rhs) noexcept { + swap(*this, rhs); + } + + CommandSyncClient& CommandSyncClient::operator=(CommandSyncClient &&rhs) noexcept { + swap(*this, rhs); return *this; } @@ -57,16 +61,13 @@ namespace torii { return stub_->Status(&context, request, &response); } - CommandSyncClient::CommandSyncClient(CommandSyncClient &&rhs) - : ip_(std::move(rhs.ip_)), - port_(rhs.port_), - stub_(std::move(rhs.stub_)) {} - CommandSyncClient& CommandSyncClient::operator=(CommandSyncClient &&rhs) { - this->ip_ = std::move(rhs.ip_); - this->port_ = rhs.port_; - this->stub_ = std::move(rhs.stub_); - return *this; + + void CommandSyncClient::swap(CommandSyncClient& lhs, CommandSyncClient& rhs) { + using std::swap; + swap(lhs.ip_, rhs.ip_); + swap(lhs.port_, rhs.port_); + swap(lhs.stub_, rhs.stub_); } } // namespace torii diff --git a/irohad/torii/command_client.hpp b/irohad/torii/command_client.hpp index 6586c3212f..6272b0ab79 100644 --- a/irohad/torii/command_client.hpp +++ b/irohad/torii/command_client.hpp @@ -32,10 +32,10 @@ namespace torii { CommandSyncClient(const std::string& ip, size_t port); CommandSyncClient(const CommandSyncClient&); - CommandSyncClient& operator=(const CommandSyncClient&); + CommandSyncClient& operator=(CommandSyncClient); - CommandSyncClient(CommandSyncClient&&); - CommandSyncClient&operator=(CommandSyncClient&&); + CommandSyncClient(CommandSyncClient&&) noexcept ; + CommandSyncClient&operator=(CommandSyncClient&&) noexcept ; /** * requests tx to a torii server and returns response (blocking, sync) @@ -53,6 +53,7 @@ namespace torii { iroha::protocol::ToriiResponse &response) const; private: + void swap(CommandSyncClient& lhs, CommandSyncClient& rhs); std::string ip_; size_t port_; std::unique_ptr stub_; diff --git a/irohad/torii/query_client.cpp b/irohad/torii/query_client.cpp index fd0197ccd2..6cce6e74f8 100644 --- a/irohad/torii/query_client.cpp +++ b/irohad/torii/query_client.cpp @@ -33,22 +33,17 @@ namespace torii_utils { QuerySyncClient::QuerySyncClient(const QuerySyncClient &rhs) : QuerySyncClient(rhs.ip_, rhs.port_) {} - QuerySyncClient &QuerySyncClient::operator=(const QuerySyncClient &rhs) { - this->ip_ = rhs.ip_; - this->port_ = rhs.port_; - this->stub_ = iroha::protocol::QueryService::NewStub( - grpc::CreateChannel(rhs.ip_ + ":" + std::to_string(rhs.port_), - grpc::InsecureChannelCredentials())); + QuerySyncClient &QuerySyncClient::operator=(QuerySyncClient rhs) { + swap(*this, rhs); return *this; } - QuerySyncClient::QuerySyncClient(QuerySyncClient &&rhs) - : ip_(std::move(rhs.ip_)), port_(rhs.port_), stub_(std::move(rhs.stub_)) {} + QuerySyncClient::QuerySyncClient(QuerySyncClient &&rhs) noexcept { + swap(*this, rhs); + } - QuerySyncClient& QuerySyncClient::operator=(QuerySyncClient &&rhs) { - this->ip_ = std::move(rhs.ip_); - this->port_ = rhs.port_; - this->stub_ = std::move(rhs.stub_); + QuerySyncClient &QuerySyncClient::operator=(QuerySyncClient &&rhs) noexcept { + swap(*this, rhs); return *this; } @@ -64,4 +59,11 @@ namespace torii_utils { return stub_->Find(&context, query, &response); } + void QuerySyncClient::swap(QuerySyncClient &lhs, QuerySyncClient &rhs) { + using std::swap; + swap(lhs.ip_, rhs.ip_); + swap(lhs.port_, rhs.port_); + swap(lhs.stub_, rhs.stub_); + } + } // namespace torii_utils diff --git a/irohad/torii/query_client.hpp b/irohad/torii/query_client.hpp index b8479e3849..698d013138 100644 --- a/irohad/torii/query_client.hpp +++ b/irohad/torii/query_client.hpp @@ -34,10 +34,10 @@ namespace torii_utils { QuerySyncClient(const std::string &ip, size_t port); QuerySyncClient(const QuerySyncClient&); - QuerySyncClient& operator=(const QuerySyncClient&); + QuerySyncClient& operator=(QuerySyncClient); - QuerySyncClient(QuerySyncClient&&); - QuerySyncClient&operator=(QuerySyncClient&&); + QuerySyncClient(QuerySyncClient&&) noexcept ; + QuerySyncClient&operator=(QuerySyncClient&&) noexcept ; /** * requests query to a torii server and returns response (blocking, sync) @@ -49,6 +49,8 @@ namespace torii_utils { iroha::protocol::QueryResponse &response) const; private: + void swap(QuerySyncClient& lhs, QuerySyncClient& rhs); + std::string ip_; size_t port_; std::unique_ptr stub_;