aboutsummaryrefslogtreecommitdiffstats
path: root/brillo/dbus/file_descriptor.h
blob: f7be44fb6648a1ec38beefce80bd03f5e9a13aef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIBBRILLO_BRILLO_DBUS_FILE_DESCRIPTOR_H_
#define LIBBRILLO_BRILLO_DBUS_FILE_DESCRIPTOR_H_

#include <base/files/scoped_file.h>
#include <base/macros.h>

namespace brillo {
namespace dbus_utils {

// This struct wraps file descriptors to give them a type other than int.
// Implicit conversions are provided because this should be as transparent
// a wrapper as possible to match the libchrome bindings below when this
// class is used by chromeos-dbus-bindings.
//
// Because we might pass these around and the calling code neither passes
// ownership nor knows when this will be destroyed, it actually dups the FD
// so that the calling code and binding code both have a clear handle on the
// lifetimes of their respective copies of the FD.
struct FileDescriptor {
  FileDescriptor() = default;
  FileDescriptor(int fd) : fd(dup(fd)) {}
  FileDescriptor(FileDescriptor&& other) : fd(std::move(other.fd)) {}
  FileDescriptor(base::ScopedFD&& other) : fd(std::move(other)) {}

  inline FileDescriptor& operator=(int new_fd) {
    fd.reset(dup(new_fd));
    return *this;
  }

  FileDescriptor& operator=(FileDescriptor&& other) {
    fd = std::move(other.fd);
    return *this;
  }

  FileDescriptor& operator=(base::ScopedFD&& other) {
    fd = std::move(other);
    return *this;
  }

  int release() { return fd.release(); }

  int get() const { return fd.get(); }

 private:
  DISALLOW_COPY_AND_ASSIGN(FileDescriptor);

  base::ScopedFD fd;
};

}  // namespace dbus_utils
}  // namespace brillo

#endif  // LIBBRILLO_BRILLO_DBUS_FILE_DESCRIPTOR_H_