28 lines
555 B
C++
28 lines
555 B
C++
#pragma once
|
|
|
|
#include <filesystem>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
class ScopedFile {
|
|
public:
|
|
explicit ScopedFile(std::string fileName) : fileName_(std::move(fileName)) {}
|
|
|
|
void throwIfExists() const {
|
|
if (std::filesystem::exists(fileName_)) {
|
|
throw std::runtime_error("Test file already exists: " + fileName_);
|
|
}
|
|
}
|
|
|
|
const std::string &getFileName() const { return fileName_; }
|
|
|
|
~ScopedFile() noexcept(false) {
|
|
std::error_code ec;
|
|
|
|
std::filesystem::remove(fileName_, ec);
|
|
}
|
|
|
|
private:
|
|
std::string fileName_;
|
|
};
|