LLVM  3.7.0
FileOutputBuffer.h
Go to the documentation of this file.
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"
21 
22 namespace llvm {
23 /// FileOutputBuffer - This interface provides simple way to create an in-memory
24 /// buffer which will be written to a file. During the lifetime of these
25 /// objects, the content or existence of the specified file is undefined. That
26 /// is, creating an OutputBuffer for a file may immediately remove the file.
27 /// If the FileOutputBuffer is committed, the target file's content will become
28 /// the buffer content at the time of the commit. If the FileOutputBuffer is
29 /// not committed, the file will be deleted in the FileOutputBuffer destructor.
31 public:
32 
33  enum {
34  F_executable = 1 /// set the 'x' bit on the resulting file
35  };
36 
37  /// Factory method to create an OutputBuffer object which manages a read/write
38  /// buffer of the specified size. When committed, the buffer will be written
39  /// to the file at the specified path.
40  static std::error_code create(StringRef FilePath, size_t Size,
41  std::unique_ptr<FileOutputBuffer> &Result,
42  unsigned Flags = 0);
43 
44  /// Returns a pointer to the start of the buffer.
45  uint8_t *getBufferStart() {
46  return (uint8_t*)Region->data();
47  }
48 
49  /// Returns a pointer to the end of the buffer.
50  uint8_t *getBufferEnd() {
51  return (uint8_t*)Region->data() + Region->size();
52  }
53 
54  /// Returns size of the buffer.
55  size_t getBufferSize() const {
56  return Region->size();
57  }
58 
59  /// Returns path where file will show up if buffer is committed.
60  StringRef getPath() const {
61  return FinalPath;
62  }
63 
64  /// Flushes the content of the buffer to its file and deallocates the
65  /// buffer. If commit() is not called before this object's destructor
66  /// is called, the file is deleted in the destructor. The optional parameter
67  /// is used if it turns out you want the file size to be smaller than
68  /// initially requested.
69  std::error_code commit();
70 
71  /// If this object was previously committed, the destructor just deletes
72  /// this object. If this object was not committed, the destructor
73  /// deallocates the buffer and the target file is never written.
75 
76 private:
77  FileOutputBuffer(const FileOutputBuffer &) = delete;
78  FileOutputBuffer &operator=(const FileOutputBuffer &) = delete;
79 
80  FileOutputBuffer(std::unique_ptr<llvm::sys::fs::mapped_file_region> R,
81  StringRef Path, StringRef TempPath);
82 
83  std::unique_ptr<llvm::sys::fs::mapped_file_region> Region;
84  SmallString<128> FinalPath;
85  SmallString<128> TempPath;
86 };
87 } // end namespace llvm
88 
89 #endif
size_t getBufferSize() const
Returns size of the buffer.
~FileOutputBuffer()
If this object was previously committed, the destructor just deletes this object. ...
FileOutputBuffer - This interface provides simple way to create an in-memory buffer which will be wri...
std::error_code commit()
Flushes the content of the buffer to its file and deallocates the buffer.
static std::error_code create(StringRef FilePath, size_t Size, std::unique_ptr< FileOutputBuffer > &Result, unsigned Flags=0)
Factory method to create an OutputBuffer object which manages a read/write buffer of the specified si...
uint8_t * getBufferEnd()
Returns a pointer to the end of the buffer.
StringRef getPath() const
Returns path where file will show up if buffer is committed.
uint8_t * getBufferStart()
Returns a pointer to the start of the buffer.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40