1 #pragma once 2 3 #include "io/Path.h" 4 #include "math/rng/Rng.h" 5 #include "objects/containers/FlatSet.h" 6 7 NAMESPACE_SPH_BEGIN 8 9 /// \brief Object generating unique paths. 10 /// 11 /// Unique paths are generated by appending _001, _002, etc. to the file name. In case more than 999 unique 12 /// paths are requested from given path, it throws an exception. 13 class UniquePathManager : public Noncopyable { 14 private: 15 FlatSet<Path> usedPaths; 16 17 public: 18 /// \brief Generates a unique path, based on given input path. 19 /// 20 /// The generated file may or may not exist on filesystem, the function only ensures it returns a 21 /// different path every time. If more multiple manager objects are used, they may generate equal paths. 22 /// The function is NOT thread safe, it has to be explicitly synchronized. 23 Path getPath(const Path& expected); 24 }; 25 26 /// \brief Object generating unique names. 27 /// 28 /// Similar to \ref UniquePathManager, but does not include file extensions. 29 class UniqueNameManager { 30 private: 31 FlatSet<String> names; 32 33 public: 34 UniqueNameManager() = default; 35 36 explicit UniqueNameManager(ArrayView<const String> initial); 37 38 String getName(const String& name); 39 }; 40 41 /// \brief Generates random file names. 42 /// 43 /// Object generates random paths, given a parent directory. It has no seed, so each constructed object 44 /// generates a different sequence of random paths. This does not create the file, only returns a usable path; 45 /// it is guaranteed that no file with returned path exist, provided no other thread or process is creating 46 /// random file names at the same time. 47 class RandomPathManager : public Noncopyable { 48 private: 49 UniformRng rng; 50 51 static char chars[]; 52 53 public: RandomPathManager()54 RandomPathManager() 55 : rng(std::random_device{}()) {} 56 57 /// \brief Generates a new random path. 58 /// 59 /// \param extension Optional extension of the generated path. If empty, the path has no extension. 60 Path getPath(const String& extension = ""); 61 }; 62 63 64 NAMESPACE_SPH_END 65