blob: 1b1928430b88515c27433ff06251997050e09c4d [file] [log] [blame]
William A. Kennington IIIeac9d472020-08-03 13:57:14 -07001#include <fmt/format.h>
2#include <stdplus/fd/dupable.hpp>
3#include <stdplus/fd/managed.hpp>
4#include <stdplus/fd/ops.hpp>
5#include <stdplus/util/cexec.hpp>
6#include <unistd.h>
7#include <utility>
8
9namespace stdplus
10{
11namespace fd
12{
13namespace detail
14{
15
16void drop(int&& fd)
17{
18 CHECK_ERRNO(close(fd), "close");
19}
20
21} // namespace detail
22
William A. Kennington III73a20c42021-06-29 18:13:12 -070023ManagedFd::ManagedFd() noexcept : handle(std::nullopt)
24{
25}
26
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))
34{
35}
36
37ManagedFd::ManagedFd(const DupableFd& other) : ManagedFd(DupableFd(other))
38{
39}
40
41ManagedFd& ManagedFd::operator=(DupableFd&& other) noexcept
42{
43 handle = static_cast<detail::ManagedFdHandle&&>(other.handle);
44 return *this;
45}
46
47ManagedFd& ManagedFd::operator=(const DupableFd& other)
48{
49 return *this = DupableFd(other);
50}
51
52int ManagedFd::release()
53{
54 return handle.release();
55}
56
57int ManagedFd::get() const
58{
59 return handle.value();
60}
61
62} // namespace fd
63} // namespace stdplus