From e938e5f78521505657dc4bf3fcaea9fb4fe655a7 Mon Sep 17 00:00:00 2001 From: Daniel Zahka Date: Mon, 27 Oct 2025 12:03:56 -0700 Subject: [PATCH] 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 --- lib/ynl-cpp.cpp | 16 ++++++++++++++++ lib/ynl.hpp | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/lib/ynl-cpp.cpp b/lib/ynl-cpp.cpp index 2a72b8d..dbbf834 100644 --- a/lib/ynl-cpp.cpp +++ b/lib/ynl-cpp.cpp @@ -6,6 +6,22 @@ ynl_socket::ynl_socket(const ynl_family& family, struct ynl_error* err) { sock_ = ynl_sock_create(&family, err); } +ynl_socket::ynl_socket(ynl_socket&& other) noexcept : sock_(other.sock_) { + other.sock_ = nullptr; +} + +ynl_socket& ynl_socket::operator=(ynl_socket&& other) noexcept { + if (this == &other) + return *this; + + if (sock_) + ynl_sock_destroy(sock_); + + sock_ = other.sock_; + other.sock_ = nullptr; + return *this; +} + ynl_socket::~ynl_socket() { if (sock_) { ynl_sock_destroy(sock_); diff --git a/lib/ynl.hpp b/lib/ynl.hpp index 87d851b..65d9177 100644 --- a/lib/ynl.hpp +++ b/lib/ynl.hpp @@ -14,6 +14,13 @@ namespace ynl_cpp { class ynl_socket { public: explicit ynl_socket(const ynl_family& family, ynl_error* err = nullptr); + + ynl_socket(const ynl_socket&) = delete; + ynl_socket& operator=(const ynl_socket&) = delete; + + ynl_socket(ynl_socket&& other) noexcept; + ynl_socket& operator=(ynl_socket&& other) noexcept; + ~ynl_socket(); operator bool() const {