opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
temporary_file.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <filesystem>
6#include <fstream>
7
8namespace osc
9{
10 // `TemporaryFile` securely creates and manages a temporary file.
11 //
12 // The implementation guarantees that:
13 //
14 // - The file is created in the operating system's temporary directory, throwing otherwise.
15 // - The name of the file begins with `prefix`, ends with `suffix`, and the characters
16 // between the prefix and suffix are chosen to result in a new, unique, filename, throwing
17 // otherwise.
18 // - The file will be deleted from the filesystem upon destruction of the `TemporaryFile` object.
20 public:
21 // Constructs a `TemporaryFile` with the given parameters.
22 //
23 // The file is created on-disk and opened by the constructor.
25 TemporaryFile(const TemporaryFile&) = delete;
30
31 // Returns the name of the temporary file.
32 std::filesystem::path filename() const { return absolute_path_.filename(); }
33
34 // Returns the absolute path to the temporary file.
35 const std::filesystem::path& absolute_path() const { return absolute_path_; }
36
37 // Returns the underlying stream that is connected to the temporary file.
38 std::fstream& stream() { return handle_; }
39
40 // Closes the handle that this `TemporaryFile` has to the underlying file, but does not delete
41 // the underlying file (the destructor still deletes it, though)
42 void close() { handle_.close(); }
43
44 private:
45 std::filesystem::path absolute_path_;
46 std::fstream handle_;
47 bool should_delete_ = true;
48 };
49}
Definition temporary_file.h:19
std::fstream & stream()
Definition temporary_file.h:38
void close()
Definition temporary_file.h:42
TemporaryFile(const TemporaryFile &)=delete
const std::filesystem::path & absolute_path() const
Definition temporary_file.h:35
TemporaryFile(const TemporaryFileParameters &={})
std::filesystem::path filename() const
Definition temporary_file.h:32
TemporaryFile(TemporaryFile &&) noexcept
Definition custom_decoration_generator.h:5
constexpr U to(T &&value)
Definition conversion.h:56
Definition temporary_file_parameters.h:12