Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 3 | #include <stdplus/fd/dupable.hpp> |
| 4 | #include <stdplus/fd/managed.hpp> |
| 5 | #include <stdplus/fd/ops.hpp> |
| 6 | #include <stdplus/util/cexec.hpp> |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 7 | |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
| 10 | namespace stdplus |
| 11 | { |
| 12 | namespace fd |
| 13 | { |
| 14 | namespace detail |
| 15 | { |
| 16 | |
| 17 | void drop(int&& fd) |
| 18 | { |
| 19 | CHECK_ERRNO(close(fd), "close"); |
| 20 | } |
| 21 | |
| 22 | } // namespace detail |
| 23 | |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 24 | ManagedFd::ManagedFd() noexcept : handle(std::nullopt) {} |
William A. Kennington III | 73a20c4 | 2021-06-29 18:13:12 -0700 | [diff] [blame] | 25 | |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 26 | ManagedFd::ManagedFd(int&& fd) : handle(std::move(fd)) |
| 27 | { |
| 28 | fd::setFdFlags(*this, fd::getFdFlags(*this).set(fd::FdFlag::CloseOnExec)); |
| 29 | } |
| 30 | |
| 31 | ManagedFd::ManagedFd(DupableFd&& other) noexcept : |
| 32 | handle(static_cast<detail::ManagedFdHandle&&>(other.handle)) |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 33 | {} |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 34 | |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 35 | ManagedFd::ManagedFd(const DupableFd& other) : ManagedFd(DupableFd(other)) {} |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 36 | |
| 37 | ManagedFd& ManagedFd::operator=(DupableFd&& other) noexcept |
| 38 | { |
| 39 | handle = static_cast<detail::ManagedFdHandle&&>(other.handle); |
| 40 | return *this; |
| 41 | } |
| 42 | |
| 43 | ManagedFd& ManagedFd::operator=(const DupableFd& other) |
| 44 | { |
| 45 | return *this = DupableFd(other); |
| 46 | } |
| 47 | |
| 48 | int ManagedFd::release() |
| 49 | { |
| 50 | return handle.release(); |
| 51 | } |
| 52 | |
| 53 | int ManagedFd::get() const |
| 54 | { |
| 55 | return handle.value(); |
| 56 | } |
| 57 | |
| 58 | } // namespace fd |
| 59 | } // namespace stdplus |