Added Rule of five to UUID
This commit is contained in:
@@ -12,15 +12,24 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
namespace Tourmaline::Type {
|
namespace Tourmaline::Type {
|
||||||
struct UUID {
|
struct UUID {
|
||||||
|
constexpr static uint8_t BitLength = 128;
|
||||||
|
constexpr static uint8_t QWORDLength = BitLength / 64;
|
||||||
|
constexpr static uint8_t ByteLength = BitLength / 8;
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
std::string asString() const;
|
std::string asString() const;
|
||||||
|
|
||||||
UUID(uint64_t firstHalf, uint64_t secondHalf);
|
UUID(uint64_t firstHalf, uint64_t secondHalf);
|
||||||
UUID(const std::string &uuid);
|
UUID(const std::string &uuid);
|
||||||
|
|
||||||
|
UUID(const UUID &uuid);
|
||||||
UUID(UUID &&uuid) noexcept;
|
UUID(UUID &&uuid) noexcept;
|
||||||
|
UUID &operator=(const UUID &uuid);
|
||||||
|
UUID &operator=(UUID &&uuid);
|
||||||
|
~UUID() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<uint64_t[]> data = std::make_unique<uint64_t[]>(2);
|
std::unique_ptr<uint64_t[]> data = std::make_unique<uint64_t[]>(QWORDLength);
|
||||||
};
|
};
|
||||||
} // namespace Tourmaline::Type
|
} // namespace Tourmaline::Type
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -8,7 +8,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "../Types.hpp"
|
#include "../Types.hpp"
|
||||||
#include <algorithm>
|
#include <charconv>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -18,6 +20,26 @@ std::string UUID::asString() const {
|
|||||||
return std::format("{:016X}{:016X}", data[0], data[1]);
|
return std::format("{:016X}{:016X}", data[0], data[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UUID::UUID(const UUID &uuid) {
|
||||||
|
std::memcpy(data.get(), uuid.data.get(), UUID::ByteLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
UUID &UUID::operator=(const UUID &uuid) {
|
||||||
|
if (this != &uuid) {
|
||||||
|
std::memcpy(data.get(), uuid.data.get(), UUID::ByteLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
UUID &UUID::operator=(UUID &&uuid) {
|
||||||
|
if (this != &uuid) {
|
||||||
|
data.swap(uuid.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
UUID::UUID(UUID &&uuid) noexcept { data.swap(uuid.data); }
|
UUID::UUID(UUID &&uuid) noexcept { data.swap(uuid.data); }
|
||||||
|
|
||||||
UUID::UUID(uint64_t firstHalf, uint64_t secondHalf) {
|
UUID::UUID(uint64_t firstHalf, uint64_t secondHalf) {
|
||||||
@@ -28,5 +50,9 @@ UUID::UUID(uint64_t firstHalf, uint64_t secondHalf) {
|
|||||||
UUID::UUID(const std::string &uuid) {
|
UUID::UUID(const std::string &uuid) {
|
||||||
// We are assuming that it is a valid UUID, if not then somewhere else this
|
// We are assuming that it is a valid UUID, if not then somewhere else this
|
||||||
// UUID should cause an error
|
// UUID should cause an error
|
||||||
std::copy(uuid.begin(), uuid.begin() + 32, data.get());
|
auto start = uuid.c_str(), half = start + ByteLength,
|
||||||
|
tail = half + ByteLength; // Each UUID element is 16 characters padded
|
||||||
|
|
||||||
|
std::from_chars(start, half, data[0], 16);
|
||||||
|
std::from_chars(half, tail, data[1], 16);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user