fd_guard.h 680 Bytes
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2011 Baidu, Inc.
gejun's avatar
gejun committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//
// RAII file descriptor.
//
// Example:
//    fd_guard fd1(open(...));
//    if (fd1 < 0) {
//        printf("Fail to open\n");
//        return -1;
//    }
//    if (another-error-happened) {
//        printf("Fail to do sth\n");
//        return -1;   // *** closing fd1 automatically ***
//    }
//
// Author: Ge,Jun (gejun@baidu.com)
// Date: Mon. Nov 7 14:47:36 CST 2011

19 20
#ifndef BUTIL_FD_GUARD_H
#define BUTIL_FD_GUARD_H
gejun's avatar
gejun committed
21

22
#include "butil/files/scoped_file.h"
gejun's avatar
gejun committed
23

24
namespace butil {
gejun's avatar
gejun committed
25 26 27 28 29 30 31 32
namespace files {
class fd_guard : public ScopedFD {
public:
    operator int() const { return get(); }
};
}  // files
}  // base

33
#endif  // BUTIL_FD_GUARD_H