ToolOutputFile.h revision 533eae20118036f425f27bf0536ef0ccbb090b65
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
12#include <string>
13#include <mcld/Support/FileHandle.h>
14
15namespace llvm {
16class formatted_raw_ostream;
17} // end of namespace llvm
18
19namespace mcld {
20
21class Path;
22class FileHandle;
23
24/** \class ToolOutputFile
25 *  \brief ToolOutputFile has the following features:
26 *   - The file is automatically deleted if the process is killed.
27 *   - The file is automatically deleted when the TooOutputFile object is
28 *     destoryed unless the client calls keep().
29 */
30class ToolOutputFile
31{
32public:
33  ToolOutputFile(const sys::fs::Path& pPath,
34                 FileHandle::OpenMode pMode,
35                 FileHandle::Permission pPermission);
36
37  ~ToolOutputFile();
38
39  /// fd - Retutn the output file handle
40  FileHandle& fd() { return m_FileHandle; }
41
42  /// os - Return the contained raw_fd_ostream
43  llvm::raw_fd_ostream& os();
44
45  /// formatted_os - Return the contained formatted_raw_ostream
46  llvm::formatted_raw_ostream& formatted_os();
47
48  /// keep - Indicate that the tool's job wrt this output file has been
49  /// successful and the file should not be deleted.
50  void keep();
51
52private:
53  class CleanupInstaller
54  {
55  public:
56    explicit CleanupInstaller(const sys::fs::Path& pPath);
57
58    ~CleanupInstaller();
59
60    /// Keep - The flag which indicates whether we should not delete the file.
61    bool Keep;
62
63  private:
64    sys::fs::Path m_Path;
65  };
66
67private:
68  FileHandle m_FileHandle;
69  CleanupInstaller m_Installer;
70  llvm::raw_fd_ostream* m_pFdOstream;
71  llvm::formatted_raw_ostream* m_pFormattedOstream;
72};
73
74} // namespace of mcld
75
76#endif
77