xref: /freebsd/lib/libutil++/freebsd__fd_up.3 (revision 159503125826bc2d3b988921e7e85735ee09ad46)
1*15950312SJohn Baldwin.\"
2*15950312SJohn Baldwin.\" SPDX-License-Identifier: BSD-2-Clause
3*15950312SJohn Baldwin.\"
4*15950312SJohn Baldwin.\" Copyright (c) 2025 Chelsio Communications, Inc.
5*15950312SJohn Baldwin.\" Written by: John Baldwin <jhb@FreeBSD.org>
6*15950312SJohn Baldwin.\"
7*15950312SJohn Baldwin.Dd July 31, 2025
8*15950312SJohn Baldwin.Dt FREEBSD::STRINGF 3
9*15950312SJohn Baldwin.Os
10*15950312SJohn Baldwin.Sh NAME
11*15950312SJohn Baldwin.Nm freebsd::fd_up
12*15950312SJohn Baldwin.Nd own a file descriptor
13*15950312SJohn Baldwin.Sh LIBRARY
14*15950312SJohn Baldwin.Lb libutil++
15*15950312SJohn Baldwin.Sh SYNOPSIS
16*15950312SJohn Baldwin.In libutil++.hh
17*15950312SJohn Baldwin.Pp
18*15950312SJohn Baldwin.Vt class freebsd::fd_up
19*15950312SJohn Baldwin{
20*15950312SJohn Baldwin.Bd -ragged -offset indent
21*15950312SJohn Baldwin.Fn fd_up
22*15950312SJohn Baldwin.Fn fd_up "int fd"
23*15950312SJohn Baldwin.Fn fd_up "fd_up &&other"
24*15950312SJohn Baldwin.Fn ~fd_up
25*15950312SJohn Baldwin.Ft int
26*15950312SJohn Baldwin.Fn get
27*15950312SJohn Baldwin.Ft int
28*15950312SJohn Baldwin.Fn release
29*15950312SJohn Baldwin.Ft void
30*15950312SJohn Baldwin.Fn reset "int newfd = -1"
31*15950312SJohn Baldwin.Ft "fd_up &"
32*15950312SJohn Baldwin.Fn operator= "fd_up &&other"
33*15950312SJohn Baldwin.Ft "fd_up &"
34*15950312SJohn Baldwin.Fn operator= "int fd"
35*15950312SJohn Baldwin.Fn "explicit operator bool"
36*15950312SJohn Baldwin.Fn "operator int"
37*15950312SJohn Baldwin.Ed
38*15950312SJohn Baldwin};
39*15950312SJohn Baldwin.Sh DESCRIPTION
40*15950312SJohn BaldwinEach instance of this class owns a file descriptor.
41*15950312SJohn BaldwinThis class is patterned on std::unique_ptr,
42*15950312SJohn Baldwinbut instead of owning a pointer to an object,
43*15950312SJohn Baldwinthis class owns a file descriptor.
44*15950312SJohn BaldwinThe currently-owned file descriptor is disposed by invoking
45*15950312SJohn Baldwin.Xr close 2
46*15950312SJohn Baldwinwhen an instance of this class is destroyed.
47*15950312SJohn BaldwinThe currently-owned file descriptor is also disposed if it is replaced by the
48*15950312SJohn Baldwin.Fn reset
49*15950312SJohn Baldwinmethod or assignment operators.
50*15950312SJohn Baldwin.Pp
51*15950312SJohn BaldwinThe
52*15950312SJohn Baldwin.Fn get
53*15950312SJohn Baldwinmethod returns the current file descriptor value while retaining ownership.
54*15950312SJohn Baldwin.Pp
55*15950312SJohn BaldwinThe
56*15950312SJohn Baldwin.Fn release
57*15950312SJohn Baldwinmethod relinquishes ownership of the current file descriptor and returns the
58*15950312SJohn Baldwinvalue of the previously-owned file descriptor.
59*15950312SJohn Baldwin.Pp
60*15950312SJohn BaldwinThe explicit
61*15950312SJohn Baldwin.Vt bool
62*15950312SJohn Baldwinconversion operator permits testing the validity of an object.
63*15950312SJohn BaldwinThe operator returns true if the instance owns a valid file descriptor.
64*15950312SJohn Baldwin.Pp
65*15950312SJohn BaldwinThe implicit
66*15950312SJohn Baldwin.Vt int
67*15950312SJohn Baldwinconversion operator permits passing an instance of this class directly as
68*15950312SJohn Baldwinan argument to existing functions which expect a file descriptor.
69*15950312SJohn Baldwin.Sh EXAMPLES
70*15950312SJohn Baldwin.Bd -literal -offset indent
71*15950312SJohn Baldwinfreebsd::fd_up fd(open("/dev/null", O_RDWR));
72*15950312SJohn Baldwinif (!fd)
73*15950312SJohn Baldwin	err(1, "open");
74*15950312SJohn Baldwinwrite(fd, "test", 4);
75*15950312SJohn Baldwin// `fd' is implicitly closed on destruction
76*15950312SJohn Baldwin.Ed
77*15950312SJohn Baldwin.Sh SEE ALSO
78*15950312SJohn Baldwin.Xr close 2
79