blob: 56b5aa24fd1a1f427de6ae1e7b6b1001293fab81 [file] [log] [blame]
Reid Spencer55cf0a62004-12-22 10:24:55 +00001//===- lib/Debugger/FDHandle.h - File Descriptor Handle ---------*- C++ -*-===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Reid Spencer55cf0a62004-12-22 10:24:55 +00003// 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.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Reid Spencer55cf0a62004-12-22 10:24:55 +00008//===----------------------------------------------------------------------===//
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
20namespace 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///
28class FDHandle {
29 int FD;
30public:
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