blob: cd4ec1c3e5f908bc80c45d0890833f9eab909357 [file] [log] [blame]
William A. Kennington IIIeac9d472020-08-03 13:57:14 -07001#include <fmt/format.h>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05002#include <unistd.h>
3
William A. Kennington IIIeac9d472020-08-03 13:57:14 -07004#include <stdplus/fd/dupable.hpp>
5#include <stdplus/fd/managed.hpp>
6#include <stdplus/fd/ops.hpp>
7#include <stdplus/util/cexec.hpp>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05008
William A. Kennington IIIeac9d472020-08-03 13:57:14 -07009#include <utility>
10
11namespace stdplus
12{
13namespace fd
14{
15namespace detail
16{
17
18void drop(int&& fd)
19{
20 CHECK_ERRNO(close(fd), "close");
21}
22
23} // namespace detail
24
Patrick Williamsd1984dd2023-05-10 16:12:44 -050025ManagedFd::ManagedFd() noexcept : handle(std::nullopt) {}
William A. Kennington III73a20c42021-06-29 18:13:12 -070026
William A. Kennington IIIeac9d472020-08-03 13:57:14 -070027ManagedFd::ManagedFd(int&& fd) : handle(std::move(fd))
28{
29 fd::setFdFlags(*this, fd::getFdFlags(*this).set(fd::FdFlag::CloseOnExec));
30}
31
32ManagedFd::ManagedFd(DupableFd&& other) noexcept :
33 handle(static_cast<detail::ManagedFdHandle&&>(other.handle))
Patrick Williamsd1984dd2023-05-10 16:12:44 -050034{}
William A. Kennington IIIeac9d472020-08-03 13:57:14 -070035
Patrick Williamsd1984dd2023-05-10 16:12:44 -050036ManagedFd::ManagedFd(const DupableFd& other) : ManagedFd(DupableFd(other)) {}
William A. Kennington IIIeac9d472020-08-03 13:57:14 -070037
38ManagedFd& ManagedFd::operator=(DupableFd&& other) noexcept
39{
40 handle = static_cast<detail::ManagedFdHandle&&>(other.handle);
41 return *this;
42}
43
44ManagedFd& ManagedFd::operator=(const DupableFd& other)
45{
46 return *this = DupableFd(other);
47}
48
49int ManagedFd::release()
50{
51 return handle.release();
52}
53
54int ManagedFd::get() const
55{
56 return handle.value();
57}
58
59} // namespace fd
60} // namespace stdplus