Automatically generate unique id
This commit is contained in:
+31
-2
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <random>
|
||||
|
||||
namespace util {
|
||||
union uuid_t {
|
||||
std::uint8_t b8[16];
|
||||
std::uint16_t b16[8];
|
||||
@@ -17,7 +18,7 @@ union uuid_t {
|
||||
std::uniform_int_distribution<std::uint8_t> dist(0, std::numeric_limits<std::uint8_t>::max());
|
||||
|
||||
uuid_t buf;
|
||||
for(auto &el : buf.b8) {
|
||||
for (auto &el : buf.b8) {
|
||||
el = dist(engine);
|
||||
}
|
||||
|
||||
@@ -30,11 +31,38 @@ union uuid_t {
|
||||
static uuid_t generate() {
|
||||
std::random_device r;
|
||||
|
||||
std::default_random_engine engine { r() };
|
||||
std::default_random_engine engine{r()};
|
||||
|
||||
return generate(engine);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string string() const {
|
||||
std::string result;
|
||||
|
||||
result.reserve(sizeof(uuid_t) * 2 + 4);
|
||||
|
||||
auto hex = util::hex(*this, true);
|
||||
auto hex_view = hex.to_string_view();
|
||||
|
||||
std::string_view slices[] = {
|
||||
hex_view.substr(0, 8),
|
||||
hex_view.substr(8, 4),
|
||||
hex_view.substr(12, 4),
|
||||
hex_view.substr(16, 4)
|
||||
};
|
||||
auto last_slice = hex_view.substr(20, 12);
|
||||
|
||||
for(auto &slice : slices) {
|
||||
std::copy(std::begin(slice), std::end(slice), std::back_inserter(result));
|
||||
|
||||
result.push_back('-');
|
||||
}
|
||||
|
||||
std::copy(std::begin(last_slice), std::end(last_slice), std::back_inserter(result));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
constexpr bool operator==(const uuid_t &other) const {
|
||||
return b64[0] == other.b64[0] && b64[1] == other.b64[1];
|
||||
}
|
||||
@@ -47,4 +75,5 @@ union uuid_t {
|
||||
return (b64[0] > other.b64[0] || (b64[0] == other.b64[0] && b64[1] > other.b64[1]));
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif //T_MAN_UUID_H
|
||||
|
||||
Reference in New Issue
Block a user