osrm-backend/unit_tests/common/temporary_file.hpp

40 lines
973 B
C++
Raw Permalink Normal View History

2017-06-25 16:13:52 -04:00
#ifndef UNIT_TESTS_TEMPORARY_FILE_HPP
#define UNIT_TESTS_TEMPORARY_FILE_HPP
#include <cstdlib>
#include <filesystem>
#include <random>
#include <string>
inline std::string random_string(std::string::size_type length)
{
static auto &chrs = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
thread_local static std::mt19937 rg{std::random_device{}()};
thread_local static std::uniform_int_distribution<std::string::size_type> pick(
0, sizeof(chrs) - 2);
std::string s;
s.reserve(length);
while (length--)
{
s += chrs[pick(rg)];
}
return s;
}
2017-06-25 16:13:52 -04:00
struct TemporaryFile
{
TemporaryFile() : path(std::filesystem::temp_directory_path() / random_string(8)) {}
2018-03-22 14:26:40 -04:00
TemporaryFile(const std::string &path) : path(path) {}
2017-06-25 16:13:52 -04:00
~TemporaryFile() { std::filesystem::remove(path); }
2017-06-25 16:13:52 -04:00
std::filesystem::path path;
2017-06-25 16:13:52 -04:00
};
#endif // UNIT_TESTS_TEMPORARY_FILE_HPP