| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_IO_FILE_UNIQUE_UNIQUE_BASENAME_HPP |
| 4 |
#define MPT_IO_FILE_UNIQUE_UNIQUE_BASENAME_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/path/os_path.hpp" |
| 11 |
#include "mpt/random/default_engines.hpp" |
| 12 |
#include "mpt/random/device.hpp" |
| 13 |
#include "mpt/random/seed.hpp" |
| 14 |
#include "mpt/string_transcode/transcode.hpp" |
| 15 |
#include "mpt/uuid/uuid.hpp" |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
namespace mpt { |
| 20 |
inline namespace MPT_INLINE_NS { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace IO { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class unique_basename { |
| 29 |
|
| 30 |
private: |
| 31 |
mpt::os_path m_Basename; |
| 32 |
|
| 33 |
private: |
| 34 |
static mpt::good_engine make_prng() { |
| 35 |
mpt::sane_random_device rd; |
| 36 |
return mpt::make_prng<mpt::good_engine>(rd); |
| 37 |
} |
| 38 |
|
| 39 |
static mpt::UUID make_uuid() { |
| 40 |
mpt::good_engine rng = make_prng(); |
| 41 |
return mpt::UUID::Generate(rng); |
| 42 |
} |
| 43 |
|
| 44 |
public: |
| 45 |
explicit unique_basename(mpt::UUID uuid) |
| 46 |
: m_Basename(mpt::transcode<mpt::os_path>(uuid.ToUString())) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
explicit unique_basename(const mpt::os_path & prefix, mpt::UUID uuid) |
| 51 |
: m_Basename((prefix.empty() ? prefix : prefix + MPT_OS_PATH("-")) + mpt::transcode<mpt::os_path>(uuid.ToUString())) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
template <typename Trng> |
| 56 |
explicit unique_basename(Trng & rng) |
| 57 |
: m_Basename(mpt::transcode<mpt::os_path>(mpt::UUID::Generate(rng).ToUString())) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
template <typename Trng> |
| 62 |
explicit unique_basename(const mpt::os_path & prefix, Trng & rng) |
| 63 |
: m_Basename((prefix.empty() ? prefix : prefix + MPT_OS_PATH("-")) + mpt::transcode<mpt::os_path>(mpt::UUID::Generate(rng).ToUString())) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
explicit unique_basename() |
| 68 |
: m_Basename(mpt::transcode<mpt::os_path>(make_uuid().ToUString())) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
public: |
| 73 |
operator mpt::os_path() const { |
| 74 |
return m_Basename; |
| 75 |
} |
| 76 |
|
| 77 |
}; // class unique_basename |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
} // namespace IO |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
} // namespace MPT_INLINE_NS |
| 87 |
} // namespace mpt |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
#endif // MPT_IO_FILE_UNIQUE_UNIQUE_BASENAME_HPP |