Make Comms movable

PiperOrigin-RevId: 558110484
Change-Id: I87fec43c0157e16ba683c498d8b50b3655efac17
This commit is contained in:
Wiktor Garbacz 2023-08-18 04:40:43 -07:00 committed by Copybara-Service
parent 08b81b52e0
commit 1e9b686c4f

View File

@ -30,6 +30,7 @@
#include <limits>
#include <memory>
#include <string>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/status/status.h"
@ -86,6 +87,16 @@ class Comms {
// When not specified the constructor uses abstract unix domain sockets.
explicit Comms(const std::string& socket_name, bool abstract_uds = true);
Comms(Comms&& other) { *this = std::move(other); }
Comms& operator=(Comms&& other) {
if (this != &other) {
using std::swap;
swap(*this, other);
other.Terminate();
}
return *this;
}
Comms(const Comms&) = delete;
Comms& operator=(const Comms&) = delete;
@ -175,6 +186,20 @@ class Comms {
bool RecvStatus(absl::Status* status);
bool SendStatus(const absl::Status& status);
void Swap(Comms& other) {
if (this == &other) {
return;
}
using std::swap;
swap(socket_name_, other.socket_name_);
swap(abstract_uds_, other.abstract_uds_);
swap(connection_fd_, other.connection_fd_);
swap(bind_fd_, other.bind_fd_);
swap(state_, other.state_);
}
friend void swap(Comms& x, Comms& y) { return x.Swap(y); }
private:
friend class Client;