Skip to content

Commit e938e5f

Browse files
author
Daniel Zahka
committed
lib/ynl: make ynl_socket a move-only type
Copying ynl_socket will result in ynl_sock_destroy() being called twice on the same ynl_sock *. Like other c++ "RAII" types that manage a resource, copying should either be deep or disallowed, and moving should be supported. This commit opts for disallowing copy construction and assignment. Signed-off-by: Daniel Zahka <[email protected]>
1 parent 031732f commit e938e5f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/ynl-cpp.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ ynl_socket::ynl_socket(const ynl_family& family, struct ynl_error* err) {
66
sock_ = ynl_sock_create(&family, err);
77
}
88

9+
ynl_socket::ynl_socket(ynl_socket&& other) noexcept : sock_(other.sock_) {
10+
other.sock_ = nullptr;
11+
}
12+
13+
ynl_socket& ynl_socket::operator=(ynl_socket&& other) noexcept {
14+
if (this == &other)
15+
return *this;
16+
17+
if (sock_)
18+
ynl_sock_destroy(sock_);
19+
20+
sock_ = other.sock_;
21+
other.sock_ = nullptr;
22+
return *this;
23+
}
24+
925
ynl_socket::~ynl_socket() {
1026
if (sock_) {
1127
ynl_sock_destroy(sock_);

lib/ynl.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ namespace ynl_cpp {
1414
class ynl_socket {
1515
public:
1616
explicit ynl_socket(const ynl_family& family, ynl_error* err = nullptr);
17+
18+
ynl_socket(const ynl_socket&) = delete;
19+
ynl_socket& operator=(const ynl_socket&) = delete;
20+
21+
ynl_socket(ynl_socket&& other) noexcept;
22+
ynl_socket& operator=(ynl_socket&& other) noexcept;
23+
1724
~ynl_socket();
1825

1926
operator bool() const {

0 commit comments

Comments
 (0)