• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 namespace llvm {
16 class formatted_raw_ostream;
17 } // end of namespace llvm
18 
19 namespace mcld {
20 
21 class Path;
22 class 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  */
30 class ToolOutputFile
31 {
32 public:
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
fd()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 
52 private:
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 
67 private:
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