Skip to content

Commit

Permalink
Implement copy-and-swap idiom
Browse files Browse the repository at this point in the history
Signed-off-by: dumitru <[email protected]>
  • Loading branch information
x3medima17 committed Feb 2, 2018
1 parent e461c26 commit 41bef10
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
33 changes: 17 additions & 16 deletions irohad/torii/command_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
7 changes: 4 additions & 3 deletions irohad/torii/command_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<iroha::protocol::CommandService::Stub> stub_;
Expand Down
26 changes: 14 additions & 12 deletions irohad/torii/query_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
8 changes: 5 additions & 3 deletions irohad/torii/query_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<iroha::protocol::QueryService::Stub> stub_;
Expand Down

0 comments on commit 41bef10

Please sign in to comment.