Home | History | Annotate | Download | only in Support
      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_TOOL_OUTPUT_FILE_H
     10 #define MCLD_SUPPORT_TOOL_OUTPUT_FILE_H
     11 #ifdef ENABLE_UNITTEST
     12 #include <gtest.h>
     13 #endif
     14 
     15 #include <string>
     16 #include <mcld/Support/FileHandle.h>
     17 
     18 namespace llvm {
     19 class formatted_raw_ostream;
     20 } // end of namespace llvm
     21 
     22 namespace mcld {
     23 
     24 class Path;
     25 class FileHandle;
     26 class MemoryArea;
     27 class raw_mem_ostream;
     28 
     29 /** \class ToolOutputFile
     30  *  \brief ToolOutputFile contains a raw_mem_ostream and adds extra new
     31  *  features:
     32  *   - The file is automatically deleted if the process is killed.
     33  *   - The file is automatically deleted when the TooOutputFile object is
     34  *     destoryed unless the client calls keep().
     35  */
     36 class ToolOutputFile
     37 {
     38 public:
     39   ToolOutputFile(const sys::fs::Path& pPath,
     40                  FileHandle::OpenMode pMode,
     41                  FileHandle::Permission pPermission);
     42 
     43   ~ToolOutputFile();
     44 
     45   /// mem_os - Return the contained raw_mem_ostream.
     46   raw_mem_ostream &mem_os();
     47 
     48   /// os - Return the contained formatted_raw_ostream
     49   llvm::formatted_raw_ostream& formatted_os();
     50 
     51   /// memory - Return the contained MemoryArea.
     52   MemoryArea& memory();
     53 
     54   /// keep - Indicate that the tool's job wrt this output file has been
     55   /// successful and the file should not be deleted.
     56   void keep();
     57 
     58 private:
     59   class CleanupInstaller
     60   {
     61   public:
     62     explicit CleanupInstaller(const std::string& pFilename);
     63 
     64     ~CleanupInstaller();
     65 
     66     /// Keep - The flag which indicates whether we should not delete the file.
     67     bool Keep;
     68 
     69   private:
     70     std::string m_Filename;
     71   };
     72 
     73 private:
     74   FileHandle m_FileHandle;
     75   CleanupInstaller m_Installer;
     76   MemoryArea* m_pMemoryArea;
     77   raw_mem_ostream* m_pOStream;
     78   llvm::formatted_raw_ostream* m_pFOStream;
     79 
     80 };
     81 
     82 } // namespace of mcld
     83 
     84 #endif
     85 
     86