blob: 56699032f947af9433c5a4e4b0ce444e14adca55 [file] [log] [blame]
Patrick Williamsd1984dd2023-05-10 16:12:44 -05001#include <unistd.h>
2
William A. Kennington IIIeac9d472020-08-03 13:57:14 -07003#include <stdplus/fd/dupable.hpp>
4#include <stdplus/fd/managed.hpp>
5#include <stdplus/fd/ops.hpp>
6#include <stdplus/util/cexec.hpp>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05007
William A. Kennington IIIeac9d472020-08-03 13:57:14 -07008#include <utility>
9
10namespace stdplus
11{
12namespace fd
13{
14namespace detail
15{
16
17void drop(int&& fd)
18{
19 CHECK_ERRNO(close(fd), "close");
20}
21
22} // namespace detail
23
Patrick Williamsd1984dd2023-05-10 16:12:44 -050024ManagedFd::ManagedFd() noexcept : handle(std::nullopt) {}
William A. Kennington III73a20c42021-06-29 18:13:12 -070025
William A. Kennington IIIeac9d472020-08-03 13:57:14 -070026ManagedFd::ManagedFd(int&& fd) : handle(std::move(fd))
27{
28 fd::setFdFlags(*this, fd::getFdFlags(*this).set(fd::FdFlag::CloseOnExec));
29}
30
31ManagedFd::ManagedFd(DupableFd&& other) noexcept :
32 handle(static_cast<detail::ManagedFdHandle&&>(other.handle))
Patrick Williamsd1984dd2023-05-10 16:12:44 -050033{}
William A. Kennington IIIeac9d472020-08-03 13:57:14 -070034
Patrick Williamsd1984dd2023-05-10 16:12:44 -050035ManagedFd::ManagedFd(const DupableFd& other) : ManagedFd(DupableFd(other)) {}
William A. Kennington IIIeac9d472020-08-03 13:57:14 -070036
37ManagedFd& ManagedFd::operator=(DupableFd&& other) noexcept
38{
39 handle = static_cast<detail::ManagedFdHandle&&>(other.handle);
40 return *this;
41}
42
43ManagedFd& ManagedFd::operator=(const DupableFd& other)
44{
45 return *this = DupableFd(other);
46}
47
48int ManagedFd::release()
49{
50 return handle.release();
51}
52
53int ManagedFd::get() const
54{
55 return handle.value();
56}
57
58} // namespace fd
59} // namespace stdplus