xref: /aosp_15_r20/external/libbrillo/brillo/dbus/file_descriptor.h (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2018 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_DBUS_FILE_DESCRIPTOR_H_
6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_DBUS_FILE_DESCRIPTOR_H_
7*1a96fba6SXin Li 
8*1a96fba6SXin Li #include <utility>
9*1a96fba6SXin Li 
10*1a96fba6SXin Li #include <base/files/scoped_file.h>
11*1a96fba6SXin Li #include <base/macros.h>
12*1a96fba6SXin Li 
13*1a96fba6SXin Li namespace brillo {
14*1a96fba6SXin Li namespace dbus_utils {
15*1a96fba6SXin Li 
16*1a96fba6SXin Li // This struct wraps file descriptors to give them a type other than int.
17*1a96fba6SXin Li // Implicit conversions are provided because this should be as transparent
18*1a96fba6SXin Li // a wrapper as possible to match the libchrome bindings below when this
19*1a96fba6SXin Li // class is used by chromeos-dbus-bindings.
20*1a96fba6SXin Li //
21*1a96fba6SXin Li // Because we might pass these around and the calling code neither passes
22*1a96fba6SXin Li // ownership nor knows when this will be destroyed, it actually dups the FD
23*1a96fba6SXin Li // so that the calling code and binding code both have a clear handle on the
24*1a96fba6SXin Li // lifetimes of their respective copies of the FD.
25*1a96fba6SXin Li struct FileDescriptor {
26*1a96fba6SXin Li   FileDescriptor() = default;
FileDescriptorFileDescriptor27*1a96fba6SXin Li   FileDescriptor(int fd) : fd(dup(fd)) {}
FileDescriptorFileDescriptor28*1a96fba6SXin Li   FileDescriptor(FileDescriptor&& other) : fd(std::move(other.fd)) {}
FileDescriptorFileDescriptor29*1a96fba6SXin Li   FileDescriptor(base::ScopedFD&& other) : fd(std::move(other)) {}
30*1a96fba6SXin Li 
31*1a96fba6SXin Li   inline FileDescriptor& operator=(int new_fd) {
32*1a96fba6SXin Li     fd.reset(dup(new_fd));
33*1a96fba6SXin Li     return *this;
34*1a96fba6SXin Li   }
35*1a96fba6SXin Li 
36*1a96fba6SXin Li   FileDescriptor& operator=(FileDescriptor&& other) {
37*1a96fba6SXin Li     fd = std::move(other.fd);
38*1a96fba6SXin Li     return *this;
39*1a96fba6SXin Li   }
40*1a96fba6SXin Li 
41*1a96fba6SXin Li   FileDescriptor& operator=(base::ScopedFD&& other) {
42*1a96fba6SXin Li     fd = std::move(other);
43*1a96fba6SXin Li     return *this;
44*1a96fba6SXin Li   }
45*1a96fba6SXin Li 
releaseFileDescriptor46*1a96fba6SXin Li   int release() { return fd.release(); }
47*1a96fba6SXin Li 
getFileDescriptor48*1a96fba6SXin Li   int get() const { return fd.get(); }
49*1a96fba6SXin Li 
50*1a96fba6SXin Li  private:
51*1a96fba6SXin Li   DISALLOW_COPY_AND_ASSIGN(FileDescriptor);
52*1a96fba6SXin Li 
53*1a96fba6SXin Li   base::ScopedFD fd;
54*1a96fba6SXin Li };
55*1a96fba6SXin Li 
56*1a96fba6SXin Li }  // namespace dbus_utils
57*1a96fba6SXin Li }  // namespace brillo
58*1a96fba6SXin Li 
59*1a96fba6SXin Li #endif  // LIBBRILLO_BRILLO_DBUS_FILE_DESCRIPTOR_H_
60