blob: 2837ff8a731343a360cb367e89e7ec7e81b1400d (
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
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#if !defined(BASE_SCOPED_FD_H_) && defined(OS_POSIX)
#define BASE_SCOPED_FD_H_
#include "base/eintr_wrapper.h"
// POSIX only
//
// A wrapper class for file descriptors which automatically closes them when
// they go out of scope:
// ScopedFd fd(open("/tmp/file", O_RDONLY));
// read(fd.get(), ...);
class ScopedFd {
public:
ScopedFd()
: fd_(-1) { }
explicit ScopedFd(int fd)
: fd_(fd) { }
~ScopedFd() {
Close();
}
void Close() {
if (fd_ >= 0) {
HANDLE_EINTR(close(fd_));
fd_ = -1;
}
}
int get() const { return fd_; }
int Take() {
const int temp = fd_;
fd_ = -1;
return temp;
}
void Set(int new_fd) {
Close();
fd_ = new_fd;
}
private:
int fd_;
DISALLOW_EVIL_CONSTRUCTORS(ScopedFd);
};
#endif // BASE_SCOPED_FD_H_
|