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