Remove last uses of OwningPtr from llvm. As far as I can tell these method versions...
[oota-llvm.git] / include / llvm / Support / FileOutputBuffer.h
1 //=== FileOutputBuffer.h - File Output Buffer -------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Utility for creating a in-memory buffer that will be written to a file.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
15 #define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
16
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/FileSystem.h"
21
22 namespace llvm {
23 class error_code;
24
25 /// FileOutputBuffer - This interface provides simple way to create an in-memory
26 /// buffer which will be written to a file. During the lifetime of these
27 /// objects, the content or existence of the specified file is undefined. That
28 /// is, creating an OutputBuffer for a file may immediately remove the file.
29 /// If the FileOutputBuffer is committed, the target file's content will become
30 /// the buffer content at the time of the commit.  If the FileOutputBuffer is
31 /// not committed, the file will be deleted in the FileOutputBuffer destructor.
32 class FileOutputBuffer {
33 public:
34
35   enum  {
36     F_executable = 1  /// set the 'x' bit on the resulting file
37   };
38
39   /// Factory method to create an OutputBuffer object which manages a read/write
40   /// buffer of the specified size. When committed, the buffer will be written
41   /// to the file at the specified path.
42   static error_code create(StringRef FilePath, size_t Size,
43                            std::unique_ptr<FileOutputBuffer> &Result,
44                            unsigned Flags = 0);
45
46   /// Returns a pointer to the start of the buffer.
47   uint8_t *getBufferStart() {
48     return (uint8_t*)Region->data();
49   }
50
51   /// Returns a pointer to the end of the buffer.
52   uint8_t *getBufferEnd() {
53     return (uint8_t*)Region->data() + Region->size();
54   }
55
56   /// Returns size of the buffer.
57   size_t getBufferSize() const {
58     return Region->size();
59   }
60
61   /// Returns path where file will show up if buffer is committed.
62   StringRef getPath() const {
63     return FinalPath;
64   }
65
66   /// Flushes the content of the buffer to its file and deallocates the
67   /// buffer.  If commit() is not called before this object's destructor
68   /// is called, the file is deleted in the destructor. The optional parameter
69   /// is used if it turns out you want the file size to be smaller than
70   /// initially requested.
71   error_code commit(int64_t NewSmallerSize = -1);
72
73   /// If this object was previously committed, the destructor just deletes
74   /// this object.  If this object was not committed, the destructor
75   /// deallocates the buffer and the target file is never written.
76   ~FileOutputBuffer();
77
78 private:
79   FileOutputBuffer(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
80   FileOutputBuffer &operator=(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
81
82   FileOutputBuffer(llvm::sys::fs::mapped_file_region *R,
83                    StringRef Path, StringRef TempPath);
84
85   std::unique_ptr<llvm::sys::fs::mapped_file_region> Region;
86   SmallString<128>    FinalPath;
87   SmallString<128>    TempPath;
88 };
89 } // end namespace llvm
90
91 #endif