ToolOutputFile.h revision 87f34658dec9097d987d254a990ea7f311bfc95f
1//===- ToolOutputFile.h ---------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#ifndef MCLD_SUPPORT_TOOLOUTPUTFILE_H
10#define MCLD_SUPPORT_TOOLOUTPUTFILE_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14
15#include <string>
16#include <mcld/Support/FileHandle.h>
17
18namespace llvm {
19class formatted_raw_ostream;
20} // end of namespace llvm
21
22namespace mcld {
23
24class Path;
25class FileHandle;
26
27/** \class ToolOutputFile
28 *  \brief ToolOutputFile has the following features:
29 *   - The file is automatically deleted if the process is killed.
30 *   - The file is automatically deleted when the TooOutputFile object is
31 *     destoryed unless the client calls keep().
32 */
33class ToolOutputFile
34{
35public:
36  ToolOutputFile(const sys::fs::Path& pPath,
37                 FileHandle::OpenMode pMode,
38                 FileHandle::Permission pPermission);
39
40  ~ToolOutputFile();
41
42  /// fd - Retutn the output file handle
43  FileHandle& fd() { return m_FileHandle; }
44
45  /// os - Return the contained raw_fd_ostream
46  llvm::raw_fd_ostream& os();
47
48  /// formatted_os - Return the contained formatted_raw_ostream
49  llvm::formatted_raw_ostream& formatted_os();
50
51  /// keep - Indicate that the tool's job wrt this output file has been
52  /// successful and the file should not be deleted.
53  void keep();
54
55private:
56  class CleanupInstaller
57  {
58  public:
59    explicit CleanupInstaller(const sys::fs::Path& pPath);
60
61    ~CleanupInstaller();
62
63    /// Keep - The flag which indicates whether we should not delete the file.
64    bool Keep;
65
66  private:
67    sys::fs::Path m_Path;
68  };
69
70private:
71  FileHandle m_FileHandle;
72  CleanupInstaller m_Installer;
73  llvm::raw_fd_ostream* m_pFdOstream;
74  llvm::formatted_raw_ostream* m_pFormattedOstream;
75};
76
77} // namespace of mcld
78
79#endif
80