For PR495:
[oota-llvm.git] / lib / Debugger / FDHandle.h
1 //===- lib/Debugger/FDHandle.h - File Descriptor Handle ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a family of utility functions which are useful for doing
11 // various things with files.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LIB_DEBUGGER_FDHANDLE_H
16 #define LIB_DEBUGGER_FDHANDLE_H
17
18 #include "llvm/System/Path.h"
19
20 namespace llvm {
21
22 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
23 /// when the object is destroyed.  This handle acts similarly to an
24 /// std::auto_ptr, in that the copy constructor and assignment operators
25 /// transfer ownership of the handle.  This means that FDHandle's do not have
26 /// value semantics.
27 ///
28 class FDHandle {
29   int FD;
30 public:
31   FDHandle() : FD(-1) {}
32   FDHandle(int fd) : FD(fd) {}
33   FDHandle(FDHandle &RHS) : FD(RHS.FD) {
34     RHS.FD = -1;       // Transfer ownership
35   }
36
37   ~FDHandle() throw();
38
39   /// get - Get the current file descriptor, without releasing ownership of it.
40   int get() const { return FD; }
41   operator int() const { return FD; }
42
43   FDHandle &operator=(int fd) throw();
44
45   FDHandle &operator=(FDHandle &RHS) {
46     int fd = RHS.FD;
47     RHS.FD = -1;       // Transfer ownership
48     return operator=(fd);
49   }
50
51   /// release - Take ownership of the file descriptor away from the FDHandle
52   /// object, so that the file is not closed when the FDHandle is destroyed.
53   int release() {
54     int Ret = FD;
55     FD = -1;
56     return Ret;
57   }
58 };
59
60 } // End llvm namespace
61
62 #endif