libsshpp v0.1.7
Modern C++17 wrapper for libssh
Loading...
Searching...
No Matches
unique_handle.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2#pragma once
3
4#include <utility>
5
6namespace sshpp::detail {
7
9template <class Handle, class Deleter>
11public:
12 UniqueHandle() = default;
13 explicit UniqueHandle(Handle h) noexcept : h_(h) {}
14
15 UniqueHandle(UniqueHandle&& o) noexcept : h_(std::exchange(o.h_, Handle{})) {}
17 if (this != &o) {
18 reset();
19 h_ = std::exchange(o.h_, Handle{});
20 }
21 return *this;
22 }
23
24 UniqueHandle(const UniqueHandle&) = delete;
26
28
29 Handle get() const noexcept { return h_; }
30 explicit operator bool() const noexcept { return h_ != Handle{}; }
31
32 [[nodiscard]] Handle release() noexcept { return std::exchange(h_, Handle{}); }
33
34 void reset(Handle h = Handle{}) noexcept {
35 if (h_ != Handle{}) {
36 Deleter{}(h_);
37 }
38 h_ = h;
39 }
40
41private:
42 Handle h_{};
43};
44
45} // namespace sshpp::detail
Zero-overhead move-only owner for a libssh C handle. See docs/design/02 ยง2.2.
Definition unique_handle.hpp:10
Handle get() const noexcept
Definition unique_handle.hpp:29
UniqueHandle & operator=(UniqueHandle &&o) noexcept
Definition unique_handle.hpp:16
void reset(Handle h=Handle{}) noexcept
Definition unique_handle.hpp:34
Handle release() noexcept
Definition unique_handle.hpp:32
UniqueHandle(Handle h) noexcept
Definition unique_handle.hpp:13
UniqueHandle(UniqueHandle &&o) noexcept
Definition unique_handle.hpp:15
UniqueHandle(const UniqueHandle &)=delete
~UniqueHandle()
Definition unique_handle.hpp:27
UniqueHandle & operator=(const UniqueHandle &)=delete
Definition handler_bridge.hpp:10