aboutsummaryrefslogtreecommitdiff
path: root/utils/UniqueFd.h
blob: 1a390ba188e85b689fa1f0f7508e3ad34772acde (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (C) 2015, 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef UNIQUEFD_H_
#define UNIQUEFD_H_

#include <unistd.h>

#include <memory>

namespace android {

/*
 * Using UniqueFd:
 * 1. Create UniqueFd object:
 *    auto fd_obj = UniqueFd(open("SomeFile", xxx));
 *
 * 2. Check whether the fd_obj is empty:
 *    if (!fd_obj) { return -errno; }
 *
 * 3. Accessing the file descriptor:
 *    int ret = read(fd_obj.Get(), buf, buf_size);
 *
 * 4. Closing the file:
 *    FD will be closed once execution leaves fd_obj scope (on any return,
 *    exception, destruction of class/struct where object is member, etc.).
 *    User can also force closing the fd_obj by calling:
 *      fd_obj = UniqueFd();
 *      // fd is closed and fd_obj is empty now.
 *
 * 5. File descriptor may be transferred to the code, which will close it after
 *    using. This can be done in 2 ways:
 *    a. Duplicate the fd, in this case both fds should be closed separately:
 *      int out_fd = dup(fd_obj.Get();
 *      ...
 *      close(out_fd);
 *    b. Transfer ownership, use this method if you do not need the fd anymore.
 *      int out_fd = fd_obj.Release();
 *      // fd_obj is empty now.
 *      ...
 *      close(out_fd);
 *
 * 6. Transferring fd into another UniqueFD object:
 *    UniqueFd fd_obj_2 = std::move(fd_obj);
 *    // fd_obj empty now
 */

constexpr int kEmptyFd = -1;

class UniqueFd {
 public:
  UniqueFd() = default;
  explicit UniqueFd(int fd) : fd_(fd){};

  auto Release [[nodiscard]] () -> int {
    return std::exchange(fd_, kEmptyFd);
  }

  auto Get [[nodiscard]] () const -> int {
    return fd_;
  }

  explicit operator bool() const {
    return fd_ != kEmptyFd;
  }

  ~UniqueFd() {
    Set(kEmptyFd);
  }

  /* Allow move semantics */
  UniqueFd(UniqueFd &&rhs) noexcept {
    Set(rhs.Release());
  }

  auto operator=(UniqueFd &&rhs) noexcept -> UniqueFd & {
    Set(rhs.Release());
    return *this;
  }

  /* Disable copy semantics */
  UniqueFd(const UniqueFd &) = delete;
  auto operator=(const UniqueFd &) = delete;

 private:
  void Set(int new_fd) {
    if (fd_ != kEmptyFd) {
      close(fd_);
    }
    fd_ = new_fd;
  }

  int fd_ = kEmptyFd;
};

}  // namespace android

#endif