forked from krockot/ipcz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_descriptor.h
More file actions
44 lines (29 loc) · 1.14 KB
/
file_descriptor.h
File metadata and controls
44 lines (29 loc) · 1.14 KB
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IPCZ_SRC_REFERENCE_DRIVERS_FILE_DESCRIPTOR_H_
#define IPCZ_SRC_REFERENCE_DRIVERS_FILE_DESCRIPTOR_H_
#include <utility>
namespace ipcz::reference_drivers {
// Implements unique ownership of a single POSIX file descriptor.
class FileDescriptor {
public:
FileDescriptor();
explicit FileDescriptor(int fd);
FileDescriptor(const FileDescriptor&) = delete;
FileDescriptor& operator=(const FileDescriptor&) = delete;
FileDescriptor(FileDescriptor&& other);
FileDescriptor& operator=(FileDescriptor&& other);
~FileDescriptor();
void reset();
[[nodiscard]] int release() { return std::exchange(fd_, -1); }
// Duplicates the underlying descriptor, returning a new FileDescriptor object
// to wrap it. This object must be valid before calling Clone().
FileDescriptor Clone() const;
bool is_valid() const { return fd_ != -1; }
int get() const { return fd_; }
private:
int fd_ = -1;
};
} // namespace ipcz::reference_drivers
#endif // IPCZ_SRC_REFERENCE_DRIVERS_FILE_DESCRIPTOR_H_